带括号、可去除汉字浮点数四则计算器

参考:https://www.cnblogs.com/GC-hahaha/p/9457720.html

本应用功能:
对一行表达式(可包含汉字等其他字符)进行运算,若该表达式被划分为几个部分,则对这几个部分进行求和

使用:
1、先将表达式粘贴在in文本中
2、运行dtest1.exe
3、结果会显示在黑窗中,也可以去result文本查看结果

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 stack<double> nums;
  4 stack<char> op;
  5 void change()//初始字符串去汉字处理
  6 {
  7     fstream in("in.txt");
  8     string str;
  9     fstream file("out.txt",ios::out);//清空文档
 10     while (getline(in,str))
 11     {
 12         string::iterator it=str.begin();
 13         for (int i=0;i<str.size();)
 14         {
 15             if (str[i]==9)//读入制表符
 16             {
 17                 str[i]='+';
 18                 i++;
 19             }
 20             else if ((str[i]>='0'&&str[i]<='9')||str[i]=='+'||str[i]=='-'||str[i]=='*'||str[i]=='/'||
 21                      str[i]=='('||str[i]==')'||str[i]=='.')
 22             {
 23                 i++;
 24                 continue;
 25             }
 26             else if (str[i]==-93&&str[i+1]==-88)//读入中文左括号
 27             {
 28                 str[i]='(';
 29                 i++;
 30                 str.erase(it+i);
 31             }
 32             else if (str[i]==-93&&str[i+1]==-87)//读入中文右括号
 33             {
 34                 str[i]=')';
 35                 i++;
 36                 str.erase(it+i);
 37             }
 38             else//删除其他字符
 39             {
 40                 str.erase(it+i);
 41             }
 42         }
 43         ofstream oswrite("out.txt",ofstream::app);
 44         oswrite<<str<<endl;//把转化后的串写入out
 45         oswrite.close();
 46     }
 47 }
 48 void math(char c)//进行运算
 49 {
 50     double a,b,temp;
 51     b=nums.top();//注意除法和减法的先后顺序,所以是b在前,a在后!
 52     nums.pop();
 53     a=nums.top();
 54     nums.pop();
 55     switch (c)
 56     {
 57         case '+':
 58             temp=a+b;
 59             break;
 60         case '-':
 61             temp=a-b;
 62             break;
 63         case '*':
 64             temp=a*b;
 65             break;
 66         case '/':
 67             if (b==0)
 68             {
 69                 printf("the divisor cann't be 0
");
 70             }
 71             else
 72             {
 73                 temp=a/b;
 74             }
 75             break;
 76     }
 77     nums.push(temp);//运算后放回数字栈
 78 }
 79 void test()
 80 {
 81     if (!nums.empty())
 82             {
 83                 printf("[%d]%lf ",nums.size(),nums.top());
 84             }
 85             else
 86             {
 87                 printf("null ");
 88             }
 89             if (!op.empty())
 90             {
 91                 printf("%c[%d]
",op.top(),op.size());
 92             }
 93             else
 94             {
 95                 printf("null
");
 96             }
 97 }
 98 void instack()//中缀转后缀进行运算
 99 {
100     string str;
101     freopen("out.txt","r",stdin);
102     fstream file("result.txt",ios::out);//以输出的方式创建文件类型file 输出流,清空文本
103     while (cin>>str)
104     {
105         while (!nums.empty()) nums.pop();
106         while (!op.empty()) op.pop();
107         for (int i=0;i<str.length();i++)
108         {
109            if (str[i]=='(')
110             {
111                 op.push(str[i]);
112             }
113             if(str[i]==')')
114             {
115                 while (op.top()!='(')
116                 {
117                     math(op.top());
118                     op.pop();
119                 }
120                 op.pop();//要记得把左括号也弹出!
121             }
122             int jump=0;
123             if(str[i]>='0'&&str[i]<='9')//把小数字符串转换为浮点数
124             {
125                 double temp=str[i]-'0';
126                 i++;
127                 int flag=0;//标记是小数点左边还是右边
128                 double rate=10;//小数倍率,要放在下边的循环前面!
129                 for (;i<str.length();i++)
130                 {
131                     if (str[i]>='0'&&str[i]<='9'&&flag==0)//整数部分
132                     {
133                         temp=temp*10+str[i]-'0';
134                     }
135                     else if (str[i]=='.')//小数点
136                     {
137                         flag=1;
138                     }
139                     else if (str[i]>='0'&&str[i]<='9'&&flag==1)//小数部分
140                     {
141                         temp+=(str[i]-'0')/rate;
142                         rate*=10;
143                     }
144                     else
145                     {
146                         i--;
147                         jump=1;
148                         break;
149                     }
150 
151                 }
152                 nums.push(temp);//转换后进入数字栈
153             }
154             if (jump)
155             {
156                 continue;
157             }
158             if(str[i]=='/'||str[i]=='*')
159             {
160                 op.push(str[i]);
161             }
162             if (str[i]=='+'||str[i]=='-')
163             {
164                     while (!op.empty()&&(op.top()=='*'||op.top()=='/'||op.top()=='+'||op.top()=='-'))//注意只要是同级的+,-都要先弹出进行运算!
165                     {
166                         math(op.top());
167                         op.pop();
168                     }
169                 op.push(str[i]);
170             }
171         }
172         while (nums.size()!=1)//把栈中剩余元素进行运算
173         {
174             math(op.top());
175             op.pop();
176         }
177         FILE *fp;
178         if ((fp=fopen("result.txt","a"))==NULL)
179         {
180             printf("result.txt open error!
");
181             exit(0);
182         }
183         fprintf(fp,"%g
",nums.top());//输出到文档
184         printf("%g
",nums.top());//输出到屏幕
185         if (fclose(fp))
186         {
187             printf("can not close result.txt
");
188         }
189     }
190 }
191 int main()
192 {
193     change();
194     instack();
195     printf("finish!
");
196 
197     return 0;
198 }
原文地址:https://www.cnblogs.com/hemeiwolong/p/10097333.html