支持+-*/()int 型数据的计算机c++实现

时间:2023-03-09 15:34:12
支持+-*/()int 型数据的计算机c++实现
 #include <iostream>
#include<sstream>
using namespace std;
template<typename T>
class stack
{
T p[];
int toop;
public:
stack() { toop = -; }
void push(T t) { toop++; p[toop] = t; }
T top() { return p[toop]; }
bool empty() { if (toop == -)return true; return false; }
void pop() { toop--; }
};
class caculator
{
string s;//原波兰式的容器
stack<char>op;
stack<float>num;
stringstream ss;//用于转换的流
stringstream sb;//插入逆波兰式的流
string str;//存放数字的容器,每次更新
string strs;//存放逆波兰式的容器
float x, y;
public:
caculator(char *p) { s = p; }
float trans(const char *p);
float antipoland();
void show() { cout << strs; }
void readnum();
void caucEveTime();
void shownum() { while (!num.empty()) { cout << num.top() << endl; num.pop(); } }
void showop() { while (!op.empty()) { cout << op.top() << endl; op.pop(); } }
};
float caculator::trans(const char *p)//底层const,对象为常量
{
float n;
n = *p - '\0' - ;//确保转化成int后数值不变
int i = strlen(p);
while (--i)
{
*p++;
n = n * + (*p - '\0' - );
}
return n;
}
void caculator::readnum()
{
str = ss.str();
if (!str.empty())//str中存放数字串
{
ss.str("");//清空流
num.push(trans(str.c_str()));
}
}
void caculator::caucEveTime()//由符号栈弹出符号决定调用
{
y = num.top();
num.pop();
x = num.top();
num.pop();
switch (op.top())
{
case'+':num.push(x + y); break;
case'-':num.push(x - y); break;
case'*':num.push(x*y); break;
case'/':num.push(x / y); break;
default:break;
}
}
float caculator::antipoland()
{
for (int i = ; i < s.size(); i++)
switch (s[i])
{
case '(':op.push(s[i]);readnum(); break;
case '+':
case '-':
readnum();
if (op.top() == '(')
op.push(s[i]);
else if(op.empty())
op.push(s[i]);
else
{
while (!op.empty())
{
if (op.top() != '('&&op.top() != ')')
{
sb << op.top();
caucEveTime();
}
op.pop();
}
op.push(s[i]);
}
break;
case ')':
readnum();
while (op.top() != '(')
{
sb << op.top();
caucEveTime();
op.pop();
}op.pop(); break;
case '*':
case'/':
readnum();
while (op.top() == '*' || op.top() == '/')
{
sb << op.top();
caucEveTime();
op.pop();
}op.push(s[i]); break;
default:
sb << s[i];
ss <<s[i];
break;
}
str = ss.str();
num.push(trans(str.c_str()));
while (!op.empty())
{
if (op.top() != '('&&op.top() != ')')
{
sb<< op.top();
caucEveTime();
}
op.pop();
} strs = sb.str();
return num.top();
}
void main()
{
char ch[];
char *p=ch;
cin >> p;
caculator a(p);
//a.antipoland();//两次重复调用改变数字栈中的数字!
// a.show();
cout <<"="<<a.antipoland()<<endl;
// cout << endl;
//a.shownum();
//a.showop();
}