Skip to content

Instantly share code, notes, and snippets.

@fredgido
Created July 1, 2019 15:47
Show Gist options
  • Save fredgido/aed7499ba8ccf35d555fdcec4d5b36da to your computer and use it in GitHub Desktop.
Save fredgido/aed7499ba8ccf35d555fdcec4d5b36da to your computer and use it in GitHub Desktop.
C
#include <stdio.h>
#include <istream>
void psting(char * s) {
printf("%s", s);
}
void pint(long int s) {
printf("%li\n", s);
}
long int getint() {
int a = 0;
char buf[1024]; // use 1KiB just to be sure
do {
//printf("enter a number: ");
if (!fgets(buf, 1024, stdin)) {
// reading input failed, give up:
return 1;
}
// have some input, convert it to integer:
sscanf(buf, "%li", & a);
} while (a == 0); // repeat until we got a valid number
//printf("You entered %d.\n", a);
return a;
}
/*
void *s[3] = {&&s0, &&s1, &&s2};
if (n >= 0 && n <=2)
goto *s[n];
s0:
...
s1:
...
s2:
...
*/
int main() {
char * menu1 = "1. Operacoes aritmeticas. \n2. Factorial de um número. \n3. Serie de Fibonacci. \n4. Terminar. \nEscolha >>";
char * menu2 = "\n1. Adicao. \n2. Subtracao. \n3. Multiplicacao. \n4. Divisao. \n5. Resto Divisao. \n6. Retornar. \nEscolha >>";
char * askinput = "\nInsira o par de numeros\nEscolha >>";
char * operadores[] = { "=\n", "+\n", "-\n", "*\n", "/\n", "%\n" };
char * newline = " \n";
char * spacer = ", ";
short int opt;
unsigned long int input1, input2, reg, res[10000];
menu:
psting(menu1);
opt = getint();
//if (opt <1) goto menu;
//if (opt >3) goto exit;
if (opt == 1) goto arit;
if (opt == 2) goto facto;
if (opt == 3) goto fib;
if (opt == 4) goto exit;
goto menu;
arit:
aritmenu:
psting(menu2);
opt = getint();
if (opt < 1) goto menu;
if (opt > 5) goto menu;
input1 = (unsigned long int) getint();
psting(operadores[opt]);
psting(newline);
input2 = (unsigned long int) getint();
psting(operadores[0]);
// jump n lines
if (opt == 1) goto soma;
if (opt == 2) goto sub;
if (opt == 3) goto mult;
if (opt == 4) goto div;
if (opt == 5) goto module;
if (opt == 6) goto menu;
soma:
input1 = input1 + input2;
goto resultlabel;
sub:
input1 = input1 - input2;
goto resultlabel;
mult:
input1 = input1 * input2;
goto resultlabel;
div:
input1 = input1 / input2;
goto resultlabel;
module:
input1 = input1 % input2;
goto resultlabel;
resultlabel:
pint(input1);
getchar();
goto aritmenu;
facto:
input1 = (unsigned long int) getint(); // validate
for (input2 = 1, reg = 1; input2 < 1 + input1; input2++) reg *= input2;
pint(reg);
goto menu;
fib:
input1 = (unsigned long int) getint(); // validate
res[0] = 1;
res[1] = 1;
for (input2 = 2; input2 < input1; input2++)
res[input2] = res[input2 - 2] + res[input2 - 1];
for (input2 = 0; input2 < input1; input2++)
pint(res[input2]);
goto menu;
exit:
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment