写计算机
假期得空想练练手写计算机,我的思路是用户写一个算式,如“2-3-4+45-34+23”,然后这程序就帮忙算出来。请问大佬,以下的代码要如何改进才能让计算机懂得先加减后乘除和能先做()里面的内容。如果以下方法实现不了,也请大佬提供下思路。void main() {
char input[100];
int count = 0;
int y,total;
char operation;
FILE *fptr;
printf("Enter a equation without \"=\" >");
scanf("%s", &input);
rewind(stdin);
fptr = fopen("a.txt", "w");
if (fptr != 0) {
printf("\n\nopen a.txt sucess\n\n");
}
else {
printf("\n\n open a.txt error\n\n");
}
fprintf(fptr,"%s",input);
printf("prntf to a.txt success\n\n");
fclose(fptr);
fptr = fopen("a.txt", "r");
if (fptr != 0) {
printf("open a.txt for read success\n\n");
}
else {
printf("Error to read a.txt\n\n");
}
while (!feof(fptr)) {
if (count == 0) {
fscanf(fptr,"%d", &total );
}
else {
fscanf(fptr,"%c%d", &operation, &y);
if (operation == '+') {
total = total + y;
}
else if (operation == '-') {
total = total - y;
}
else if (operation == '*') {
total = total * y;
}
else if (operation == '/') {
total = total / y;
}
}
count++;
}
fclose(fptr);
fptr = fopen("history.txt", "a");
fprintf(fptr,"%s=%d", input, total);
printf("\n\nAns:%s=%d\n\n", input, total);
system("pause");
}