这个程序只需要能够识别无效的参数,比如在命令行中有一个字母或者一个double运算符,除了5--5,它会给你10.操作顺序无关紧要只是向左读对。如果第一个参数是5,下面的代码只给出一个0005,我无法弄清楚如何让代码进行操作。感谢您的任何意见,因为这肯定会对我有所帮助。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int op = 0;
int num1 = 0;
int num2 = 0;
double result = 0;
for (int x = 1; x < argc; x++) {
int num = atof(argv[x]);
if (num ==! argv[x]) {
printf("Invalid Input");
}
switch (atoi(argv[x])) {
case'+':
op = 43; /* ASCII value of '+' */
break;
case'-':
op = 45; /* ASCII value of '-' */
break;
case'*':
op = 42; /* ASCII value of '*' */
break;
case'/':
op = 47; /* ASCII value of '/' */
break;
case'^':
op = 94; /* ASCII value of '^' */
break;
default:
if (op == 0) {
num1 = atof(argv[x]);
} else {
switch(op){
case 43:
printf("%.1d + %.1d = %.1d",num1, num2, num1+num2);
break;
case 45:
printf("%.1d - %.1d = %.1d",num1, num2, num1-num2);
break;
case 42:
printf("%.1d * %.1d = %.1d",num1, num2, num1*num2);
break;
case 47:
printf("%.1d / %.1d = %.1d",num1, num2, num1/num2);
break;
case 94:
printf("%.4d ^ %.4d = %.4d",num1, num2, num1/num2);
break;
default:
break;
}
}
}
}
return 0;
}