码迷,mamicode.com
首页 > 其他好文 > 详细

3-6 中缀表达式转换成后缀表达式

时间:2018-10-12 14:00:09      阅读:310      评论:0      收藏:0      [点我收藏+]

标签:visual   标识符   ceil   math.h   studio   erro   中缀   pop   names   

1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\xtgmath.h(214): warning C4602: #pragma pop_macro:“new”该标识符前面没有 #pragma push_macro
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\xtgmath.h(215): warning C4193: #pragma warning(pop) : 没有匹配的“#pragma warning(push)”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\xtgmath.h(216): warning C4161: #pragma pack(pop...) : 出栈的比入栈的多
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(23): error C2061: 语法错误: 标识符“abs”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(23): error C2059: 语法错误:“;”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(23): error C2061: 语法错误: 标识符“acos”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(23): error C2061: 语法错误: 标识符“asin”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(24): error C2061: 语法错误: 标识符“atan”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(24): error C2059: 语法错误:“;”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(24): error C2061: 语法错误: 标识符“atan2”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(24): error C2061: 语法错误: 标识符“ceil”
1>e:\极速考拉下载目录\vs2013(visual studio 2013旗舰版)\vc\include\cmath(25): error C2061: 语法错误: 标识符“cos”

若是switch单词写错了,则可能会出现上述错误。

输入1:

 2+3*(7-4)+8/4#
输入2:
 ((2+3)*4-(8+2))/5#
 1 #include <iostream>
 2 #include <cctype>
 3 #include <stack>
 4 using namespace std;
 5 
 6 int getPriority(char ch)
 7 {
 8     int priority = 0;
 9     switch(ch)
10     {
11         case +:
12             priority = 1;
13             break;
14         case -:
15             priority = 1;
16             break;
17         case *:
18             priority = 2;
19             break;
20         case /:
21             priority = 2;
22             break;
23         case (:
24             priority = 3;
25             break;
26         default:
27             priority = 0;
28             break;
29     }
30     return priority;
31 }
32 
33 
34 int main()
35 {
36     std::stack<char> S;
37     char ch;
38     cin >> ch;
39     while (ch != #)
40     {
41         while (isdigit(ch))
42         {
43             cout << ch;
44             cin >> ch;
45             if(!isdigit(ch))        //ch 不是数字且不是‘#‘, 表示完成了一个整型数据的输入,此时打印一个空格
46                 cout << " ";
47             
48 
49         }
50         if (ch == #)
51             break;
52         if (ch == ))                    //如果读入的字符是‘)‘,那么当栈顶元素不是‘(’时,一直输出栈顶元素
53         {
54             while (S.top() != ()
55             {
56                 cout  << S.top()<< " ";
57                 S.pop();
58             }
59             S.pop();
60         }
61         else     //正常情况,将ch和栈顶字符比较优先级
62         {
63             while (!S.empty() && S.top() != ( && getPriority(S.top()) >= getPriority(ch))
64             {
65                 cout << S.top()<< " " ;
66                 S.pop();
67             }
68             S.push(ch);
69 
70         }cin >> ch;
71     }
72 
73     while (!S.empty())
74     {
75         cout << S.top();
76         S.pop();
77         if (!S.empty())
78             cout << " ";
79     }
80 
81     return 0;
82 }

 

3-6 中缀表达式转换成后缀表达式

标签:visual   标识符   ceil   math.h   studio   erro   中缀   pop   names   

原文地址:https://www.cnblogs.com/hi3254014978/p/9777420.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!