您好,我正在尝试创建一个计算反向波兰表达式的程序。一切正常,直到我尝试进行乘法。例如,它适用于以下参数: Program_name 2 3 + 或 2 3 - 4/
但如果尝试 3 4 *
是不再起作用了。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char getcharacter(const char *c){
return c[0];
}
int main(int argc, char *argv[])
{
int a[10],i=0;
char c;
if(argc>3)
{
while(--argc>0){
c=getcharacter(*++argv);
printf("%c ",c);// for debug
if(c>47&&c<58)
{
a[i]=c-'0';
i++;
}
else if(c=='-')
{
a[i-2]=a[i-2]-a[i-1];
a[i-1]=0;
i--;
}
else if(c=='+')
{
a[i-2]=a[i-2]+a[i-1];
a[i-1]=0;
i--;
}
else if(c=='*')
{
a[i-2]=a[i-2]*a[i-1];
a[i-1]=0;
i--;
}
else if(c=='/')
{
if(a[i-2]==0)
printf("Error\n");
else
{
a[i-2]=a[i-2]/a[i-1];
a[i-1]=0;
i--;
}
}
else printf("U have entered something wrong.");
}
}
else
{
printf("Too few arguments");
}
printf("Rezultat=%d\n",a[0]);
return 0;
}
最佳答案
为了避免shell通配符,参数需要加引号。例如。使用 '*' 代替 *
关于c - 使用 '*' 作为反向抛光表达式的参数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35578962/