#include<stdio.h>
void calculator();
int str_cmp(char *str1, char *str2);
int main()
{
calculator();
}
void calculator()
{
char str[255], operation[2];
int num1, num2, result = 0, exit = 1, count, index;
char op[6][3] = {{"+"}, {"-"}, {"*"}, {"/"}, {"**"}, {"%"}};
printf("\n****CALCULATOR****\n"
"Rules (1) You put a space each expression\n"
"Rules (2) If you want to exit the calculator, enter '.'\n"
"(+) Addition\n"
"(-) Substraction\n"
"(*) Multiplication\n"
"(/) Division\n"
"(**) Power\n"
"(%%) Modulo\n");
while (exit)
{
scanf("%[^\n]s", str);
count = sscanf(str, "%s %d %d", operation, &num1, &num2);
if (str_cmp(operation, ".") == 1)
exit = 0;
for (int i = 0; i < 6; i++)
if (str_cmp(operation, op[i]) == 0)
index = i;
printf("index: %d", index);
}
}
int str_cmp(char *str1, char *str2)
{
while (*str1 && *str2 && *str1 == *str2)
++str1, ++str2;
return *str1 - *str2;
}
I don't understand this section :
scanf("%[^\n]s", str);
==> infinite loop
scanf(" %[^\n]s", str);
==> not infinite loop
What is the differences "%[^\n]s"
and " %[^\n]s"
?
The other parts are working. But this part confuses me.