Chinaunix首页 | 论坛 | 博客
  • 博客访问: 686245
  • 博文数量: 111
  • 博客积分: 2109
  • 博客等级: 上尉
  • 技术积分: 1124
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-25 12:11
个人简介

通信码农,Emacs爱好者,业余IOS程序员,更业余的PM

文章分类

全部博文(111)

文章存档

2018年(2)

2016年(2)

2015年(2)

2014年(13)

2013年(21)

2012年(71)

分类: LINUX

2012-09-01 15:10:04


点击(此处)折叠或打开

  1. //coded by zzy for Compilation principle
  2. //
  3. #include <iostream.h>
  4. #include <string>
  5. #include <cctype>
  6. #include <vector>
  7. #include <stack>
  8. using namespace::std;

  9. /******************************************************************/
  10. // global value
  11. // 操作符优先等级表用一结构体数组表示

  12. struct S_Oprsymbol
  13. {
  14.     char OprSymbol;
  15.     int level;
  16. }SoperSym[7] = {
  17.     {'#',-1},
  18.     {'(',0},
  19.     {'+',1},
  20.     {'-',1},
  21.     {'*',2},
  22.     {'/',2},
  23.     {')',0},

  24. };

  25. stack<char> S_OprS;

  26. stack<string> S_result;

  27. //stack<> S_Num;


  28. /******************************************************************/
  29. //判断字符是否为运算符
  30. bool isOperator(char ch)
  31. {
  32.     switch(ch)
  33.     {
  34.     case '(' : return true;break;
  35.     case ')' : return true;break;
  36.     case '+' : return true;break;
  37.     case '-' : return true;break;
  38.     case '*': return true;break;
  39.     case '/' : return true;break;
  40.     case '#' : return true;break;
  41.         //case '**' : return true;break;
  42.     default: return false;break;
  43.     }
  44. }
  45. /******************************************************************/
  46. int GetOprlevel(char OpS)
  47. {
  48.     for (int index = 0; index < 7; index++)
  49.     {
  50.         if (OpS == SoperSym[index].OprSymbol)
  51.         {
  52.             return SoperSym[index].level;
  53.         }
  54.     }
  55.     cout << "can't find this char" << endl;
  56.     return 999;
  57. }

  58. /******************************************************************/
  59. bool precede(char pop, char ch){
  60.     int popI;
  61.     int chI;
  62.     switch(ch){
  63.         case '(' : chI = 0;break;
  64.         case ')' : chI = 0;break;
  65.         case '+' : chI = 1;break;
  66.         case '-' : chI = 1;break;
  67.         case '*' : chI = 2;break;
  68.         case '/' : chI = 2;break;
  69.         case '#' : chI = -1;break;
  70.             //case '**' : return true;break;
  71.         default: break;
  72.     }
  73.     switch(pop){
  74.         case '(' : popI = 0;break;
  75.         case ')' : popI = 0;break;
  76.         case '+' : popI = 1;break;
  77.         case '-' : popI = 1;break;
  78.         case '*' : popI = 2;break;
  79.         case '/' : popI = 2;break;
  80.         case '#' : popI = -1;break;
  81.             //case '**' : return true;break;
  82.         default: break;
  83.     }

  84.     return popI >= chI ? true : false;
  85. }

  86. /******************************************************************/
  87. //change the expression to RPN

  88. void Translate(string str)
  89. {
  90.     S_OprS.push('#');
  91.     string P_str = NULL;
  92.     string s;
  93.     for (int i = 0; i < str.length(); i++)
  94.     {
  95.         if(isOperator(str[i]) == false)
  96.         {
  97.             P_str += str[i];
  98.         }
  99.         
  100.         else
  101.         {
  102.         //    s = str[i];
  103.             S_result.push(P_str);
  104.             
  105.             if (GetOprlevel(str[i]) > GetOprlevel(S_OprS.top()) )
  106.             {
  107.                 s = str[i];
  108.                 S_OprS.push(str[i]);    
  109.             }
  110.             else
  111.             {
  112.                 while (GetOprlevel(S_OprS.top()) > GetOprlevel(str[i]))
  113.                 {
  114.                     s = S_OprS.top();
  115.                     S_result.push(s);
  116.                     S_OprS.pop();
  117.                 }
  118.                 if (GetOprlevel(S_OprS.top()) == GetOprlevel(str[i]))
  119.                 {
  120.                     if (GetOprlevel(S_OprS.top()) == '(' && GetOprlevel(str[i]) == ')')
  121.                     {
  122.                         S_OprS.pop();
  123.                     }
  124.                 }
  125.                 
  126.             }
  127.         
  128.         }
  129.         
  130.     }
  131.     while (S_result.empty() != false)
  132.     {
  133.         cout << S_result.top();
  134.         S_result.pop();
  135.     }
  136. //    return 0 ;    
  137. }



  138. /******************************************************************/
  139. // get the result

  140. int Calculate(string P_str)
  141. {
  142.     return 0;
  143. }
  144. /******************************************************************/
  145. int main(void)
  146. {
  147.     string str,P_str;
  148.     int result;
  149.     cout << "/***********************************/" << endl;
  150.     cout << "please input your expressions" << endl;
  151.     cout << ":";
  152.     cin >> str;
  153.     Translate(str);
  154. //    P_str = Translate(str);
  155.        cout << "the RPN expression is : " << P_str << endl;
  156. //    result = Calculate(P_str);
  157. //    cout << "the result is : " << result << endl;
  158.     return 0;
  159. }

阅读(886) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~