c++ - 是什么导致字符串输出中的笑脸?

标签 c++

这个程序基本上将中缀转换为后缀。问题是,-/ 变成了这样的笑脸:)

问题是,它显示不同的字符,而不是预期的减号或除号。

// the program is used to convert a infix expression to a postfix expression



#include<iostream>
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
#include<string>

using namespace std;


const int size =50;
char infix[size],postfix[size],stack[size];
int top=-1;

int precedence(char ch);   // function to get the precedence of the operator
char pop();  //function to pop an element from the stack
char topelement();  // returns the top element of the stack
void push(char ch);  // pushes an element into the stack



int main()
{
     char ele,elem,st[2];
     int prep,pre,popped,j=0,chk=0;
     char strr[50];
     cout<<"Enter infix notation : "<<endl;
     cin>>infix;
     strcpy(postfix," ");

     //gets(infix);

     for(int i=0;infix[i]!=0;i++)
          {
                  if(infix[i]!='('&&infix[i]!=')'&&infix[i]!='^'&&infix[i]!='*'&&infix[i]!='/'&&infix[i]!='+'&&infix[i]!='-')     
                       postfix[j++]=infix[i];
                  else if(infix[i]=='(')
                      {
                         elem=infix[i];
                         push(elem);
                      }
                  else if(infix[i]==')')
                      {
                         while(popped=pop() != '(')
                             postfix[j++]=popped;
                      }
                  else
                      {
                         elem=infix[i];
                         pre=precedence(elem);//stores the precedence of operator coming frm infix
                         ele=topelement();
                         prep=precedence(ele);//stores the precedence of operator at the top of the stack

                         if(pre > prep)
                           push(elem);                                         

                         else
                           {
                                while(prep >= pre)
                                  {
                                     if(ele=='#')
                                       break;
                                     popped=pop();
                                     ele=topelement();
                                     postfix[j++]=popped;
                                     prep=precedence(ele);
                                   }
                                   push(elem);
                            }
                         }
             } 

          while((popped=pop())!='#')
              postfix[j++]=popped;
          postfix[j]='\0';

          cout<<"\n post fix :"<<postfix<<endl;

           system("pause");
           return 0;
}

int precedence(char ch)
{
       switch(ch)
          {
               case '^' : return 5;
               case '/' : return 4;
               case '*' : return 4;                                            
               case '+' : return 3;
               case '-' : return 3;
               default  : return 0;
          }
}

char pop()                  //function to pop the element from the stack
{
     char ret;
     if(top!=-1)
       {  ret =stack[top];
          top--;
          return ret;
       }
     else
        return '#';
}

char topelement()          // function to return top element from the stack without popping
{     
      char ch;
      if(top!=-1)
        ch=stack[top];
      else
         ch='#';
       return ch;
}

void push(char ch)          // function to push an element in the stack
{
     if(top!=size-1)
         {
            top++;
            stack[top]= ch;
         }
}         

enter image description here

所有 -/ 在代码中看起来都不错

最佳答案

您正在打印 \x01 字符(而不是预期的 -/); 是怎样的 the OEM code page显示它。

关于c++ - 是什么导致字符串输出中的笑脸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10542362/

相关文章:

c++ - 为什么我在 Visual Studio 中收到堆栈溢出错误

c++ - 为什么 "non-standard syntax; use ' &' to create a pointer to member"在 CTOR 中使用线程?

c++ - 如何在不打开串口的情况下使用boost创建串口

c++ - 另一个链接器问题

c++ - Windows Mobile 中 atof 的 Unicode 版本在哪里

c++ - Typedef 指向 C++ 中同名指针的结构

c++ - 使用平台工具集 v100 的 Visual Studio 2012。无法打开源文件 "atlbase.h"

c++ - 在 qt 中解析嵌套 json 的意外结果(数组不存在)

c++ - 使用比较运算符比较 time_t 值

c++ - OpenGL 多个 VBO 只渲染一个