c - 使用 '*' 作为反向抛光表达式的参数不起作用

标签 c

您好,我正在尝试创建一个计算反向波兰表达式的程序。一切正常,直到我尝试进行乘法。例如,它适用于以下参数: 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/

相关文章:

c - 使用 Visual Studio 2010 的 pdcurses 链接

c++ - openSSL:如何使用解码 key 初始化 EVP_PKEY 对象?

c - 缺陷查找器 - 2 个错误溢出缓冲区(char、strlen)

c - 如何同步文件移动到存储?

c - 在 C 中,这个基本的堆栈实现会出现段错误。如何修复它?

c - MIPS:将 C 代码转换为 MIPS 函数调用和返回中的问题

c - 错误 : expected '=' , ','、 ';'、 'asm' 或 '__attribute__' 之前的 'stackEmpty'

c - 如何在 Perl 中通过 Inline::C 使用 VTD-XML?

c - 所有用户和组的列表

c - 使用结构和数组的 Strcmp 的 If 语句不起作用