#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char * argv[]) {
// 从终端输入三个参数第一个和第三个是用于计算的整数第二个是运算符
printf("argc = %d\n",argc);
for (int i = 0;i < 4;i++)
printf("%s\n",argv[i]);
// 从终端输入的算术运算符:
char ch = argv[2][0];
// 从终端输入的两个整数:
int num1 = 0;
int num2 = 0;
// 把从终端输入的字符串转化为整数:
num1 = atoi (argv[1]);
num2 = atoi(argv[3]);
// 实现算术运算
switch (ch) {
case '+':
printf("%d+%d = %d\n",num1,num2,num1+num2);
break;
case '-':
printf("%d-%d = %d\n",num1,num2,num1-num2);
break;
case '*':
printf("%d*%d = %d\n",num1,num2,num1*num2);
break;
case '/':
switch (num2) {
case 0:
printf("error\n");
break;
default:
printf("%d / %d = %d\n",num1,num2,num1 / num2);
break;
}
default:
printf("输入错误\n");
break;
}
return 0;
}
|
|