题目:
用带参数的main函数实现一个完成整数运算的计算器。
例如,输入calc 5 * 3,可得到执行结果为15。
代码:
#include <iostream>
using namespace std;
int main(int argc, char *argv[])
{
int num1 = 0, num2 = 0, i;
for (i = 0; argv[1][i] != '\0'; ++i) num1 = num1 * 10 + argv[1][i];
for (i = 0; argv[3][i] != '\0'; ++i) num2 = num2 * 10 + argv[3][i];
switch (argv[2][0])
{
case '+': cout << (num1 - 48) + (num2 - 48) << endl; break;
case '-': cout << (num1 - 48) - (num2 - 48) << endl; break;
case '*': cout << (num1 - 48) * (num2 - 48) << endl; break;
case '/': cout << (num1 - 48) / (num2 - 48) << endl; break;
default: cout << "error" << endl;
}
system("pause");
return 0;
}