#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;
}
我不理解本节内容:
scanf("%[^\n]s", str);
==>无限循环
scanf(" %[^\n]s", str);
==>不是无限循环
"%[^\n]s"
和" %[^\n]s"
有什么区别?
其他部分正在工作。但是这部分使我感到困惑。