//我的程序假设从命令提示符中进行操作并拖出数字并应用//对两个数字的操作但结果总是错误的为什么?
#include "stdafx.h"
#include<stdio.h>
#include<tchar.h>
#include<stdlib.h>
int main(int argc, char*argv[])
{
if(argc !=4)
{
printf("number of CLP is incorrect\n");
return 0;
}
int num1 = atoi(argv[2]);
int num2 = atoi(argv[3]);
int res ;
if(argv[1] == "+")
res = (num1 + num2);
else if(argv[1]=="-")
res = (num1-num2);
else if(argv[1]=="*")
res = (num1*num2);
else if(argv[1]=="/")
res = (num1/num2);
printf("You enterd Operation %s and the Resualt = %d\n" , argv[1] , res);
return 0;
}
这是我的代码,如果有更好的方法,请告诉我。
最佳答案
您不能将字符串与 ==
进行比较 使用 if(strcmp(argv[1], "+") == 0)
并且在其余的代码。
关于c - 我在使用命令行参数时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9955230/