自己写的程序的收获:
#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include<stack>
#include<iostream>
#include<string>
using namespace std;
bool judgeOperator(char c);
string Operate(char c, string s1, string s2);
/*疑惑问题:怎么区分+-*和数字呢*/
int main()
{
stack<string>s;
string str;
int num = 0;
while (cin.peek() != -1)/*输入没结束*/
{
if (cin.peek() != ' ')
{
str += cin.get();
}
else
{
cin.get();
s.push(str);
str = "";/*清零str*/
while (s.size() >= 3)
{
string s1 = s.top();/*第二个运算数*/
s.pop();
string s2 = s.top();/*第一个运算数*/
s.pop();
string s3 = s.top();
s.pop();
if (judgeOperator(s3[0]) && (!judgeOperator(s2[0]) || s2.length() >= 2) && (!judgeOperator(s1[0]) || s1.length() >= 2))
{
string result = Operate(s3[0], s1, s2);
if (result == "")
{
printf("ERROR");
return 0;
}
s.push(result);
}
else
{
s.push(s3);
s.push(s2);
s.push(s1);
break;
}
}
}
}
s.push(str);
while (s.size() >= 3)
{
string s1 = s.top();/*第二个运算数*/
s.pop();
string s2 = s.top();/*第一个运算数*/
s.pop();
string s3 = s.top();
s.pop();
if (judgeOperator(s3[0]) && (!judgeOperator(s2[0]) || s2.length() >= 2) && (!judgeOperator(s1[0]) || s1.length() >= 2))
{
string result = Operate(s3[0], s1, s2);
if (result == "")
{
printf("ERROR");
return 0;
}
s.push(result);
}
else
{
s.push(s3);
s.push(s2);
s.push(s1);
break;
}
}
if (s.size() == 1)
{
printf("%.1f", stof(s.top()));
}
else {
printf("ERROR");
}
}
bool judgeOperator(char c)
{
if (c == '+' || c == '-' || c == '*' || c == '/')
{
return true;
}
else
{
return false;
}
}
string Operate(char c, string s1, string s2)
{
string result;
char buf[30];
if (c == '+')
{
sprintf(buf, "%f", stof(s2) + stof(s1));
result = buf;
}
else if (c == '-')
{
sprintf(buf, "%f", stof(s2) - stof(s1));
result = buf;
}
else if (c == '*')
{
sprintf(buf, "%f", stof(s2) * stof(s1));
result = buf;
}
else
{
if (stof(s1))
{
sprintf(buf, "%f", stof(s2) / stof(s1));
result = buf;
}
else
{
result = "";
}
}
return result;
}
stof 把string转化为float
把数字转化为字符串:sprintf(buf,"%f",num);
除法除以0判断一下
cin.peek()看看输入输出是啥,到结尾返回EOF
cin.get()取出字符
看别人的收获:
#include<stdio.h>
#include<stack>
#include<iostream>
#include<string>
#include<stdlib.h>
using namespace std;
float operate(float num1, float num2, char c);
int main()
{
string expression;
getline(cin, expression);
stack<float>s;
for (int i = expression.length() - 1; i >= 0; i--)
{
if (expression[i] == '+' || expression[i] == '-'||expression[i] =='*'||expression[i] =='/')
{
if (s.size() >= 2)
{
float num1 = s.top();/*前面的操作数*/
s.pop();
float num2 = s.top();/*后面的操作数*/
s.pop();
if (expression[i] == '/' && num2 == 0)
{
printf("ERROR");
return 0;
}
else
{
s.push(operate(num2, num1, expression[i]));
}
}
else
{
printf("ERROR");
return 0;
}
}
else
{
if (expression[i] != ' ')//为数字
{
string str;/*数字*/
while (expression[i] != ' '&&i>=0)/*多减了*/
{
str = expression[i] + str;
i--;
}
s.push(stof(str));
}
}
}
printf("%.1f", s.top());
}
float operate(float num1,float num2,char c)
{
if (c == '+')
{
return num2 + num1;
}
else if (c == '-')
{
return num2 - num1;
}
else if (c == '*')
{
return num2 * num1;
}
else
{
return num2 / num1;
}
}
getline()读取一行,我浪费时间的一个重要原因是错以为里面会有表达式错误
,对前缀表达式如何计算搞不清楚