#include <iostream>
#include <string>
using namespace std;
class opnode
{
public:
int level;
char c;
opnode(char op, int level)
{
this->level = level;
this->c = op;
}
opnode(){}
};
class MyStack//数栈
{
public:
int top;
double array[100];
MyStack()
{
top = 0;
}
double pop();
void push(double c);
double looktop();
bool isEmpty();
//void change(char str[], char ch[]);
};
double MyStack::pop()//定义pop函数
{
top--;
return array[top];
}
void MyStack::push(double c)//定义push函数
{
array[top] = c;
top++;
}
double MyStack::looktop()//看栈顶
{
--top;
double a = array[top];
top++;
return a;
}
class MyStack2//操作符栈
{
public:
int top;
opnode array[100];
MyStack2()
{
top = 0;
}
opnode pop();
void push(opnode c);
opnode looktop();
bool isEmpty();
};
opnode MyStack2::pop()//定义pop函数
{
top--;
return array[top];
}
void MyStack2::push(opnode c)//定义push函数
{
array[top] = c;
top++;
}
opnode MyStack2::looktop()//看栈顶
{
--top;
opnode a = array[top];
top++;
return a;
}
class Jisuan
{
public:
double change(char str[])//定义转化为后缀表达式函数,并计算
{
char ch[100];
int i = 0;//*str的索引,str中缀表达式
int k = 0;//ch的索引,ch后缀表达式
char c;//将字符放在c中
double d1, d2, dr;
MyStack2 st;// 存放操作符
MyStack numst;//建立数字栈
opnode ops;//临时储存操作符
char trans[100];
st.top = 0;
c = str[0];
cout << "开始扫描字符串" << endl;
while (c !=*\0*)//对str字符串进行扫描
{
if ((c >= *0*&&c <= *9*) || c == *.*)//当扫描到数字或者小数点时
{
while ((c >= *0*&&c <= *9*) || c == *.*)
{
ch[k++] = c;
c = str[i++];
}
ch[k++] = * *;
}
if (c == *(*)//如果是左括号则直接入栈
{
opnode *a = new opnode (c, -1);
st.push(*a);
c = str[i++];
}
if (c == *)*)//如果是右括号,从不入栈
{
ops = st.looktop();//看栈顶
while (st.top != 0 && st.top != *(*)//如果没有左括号且栈不为空
{
ops = st.pop();//栈内元素出栈
ch[k++] = ops.c;
ops = st.looktop();
}
i++;
st.pop();//扔掉左括号
}
if (c == *+* || c == *-*)//如果是+-
{
opnode *a = new opnode(c, 1);
if (st.top == 0)//栈为空则直接入栈
{
st.push(*a);
cout << "入栈成功" << endl;
}
else
{
ops = st.looktop();//否则看栈顶
while (ops.level >= a->level)//如果栈顶元素level值较高
{
ops = st.pop();
ch[k++] = ops.c;//将栈顶元素取出放入数组
}
i++;
st.push(*a);//入栈
}
}
cout<<"1111111"<<endl;
if (c == *** || c == */* || c == *%*)//如果是*/%
{
opnode *a = new opnode(c, 2);
if (st.top == 0)
{
st.push(*a);//栈为空直接入栈
}
else
{
ops = st.looktop();//看栈顶
while (ops.level >= a->level)//如果栈顶优先级较高
{
ops = st.pop();
ch[k++] = ops.c;//取出栈顶元素放入数组
}
i++;
st.push(*a);//入栈;
}
}
}
cout<<"222222222"<<endl;
while (st.top != 0)
{
cout << "已进入循环" << endl;
ops = st.pop();//如果栈不为空,则取出栈内元素放入数组
ch[k++] = ops.c;
cout << "出栈符号为" << ops.c << endl;
}
ch[k] = *\0*;//将\0作为结尾放入数组
cout << "后缀表达式为" << endl;
for (i = 0; i < k; i++)
{
cout << ch[i]<<" ";
}
cout << "222222222" << endl;
k = 0;//后缀表达式索引
i = 0;//将字符转化为浮点数的索引
//char *s;
cout << "11111111111" << endl;
c = ch[k++];
cout << "即将扫描后缀表达式" << endl;
while (c != *\0*)//扫描表达式
{
if (c >= *0*&&c <= *9* || c == *.*)
{
while (c >= *0*&&c <= *9* || c == *.*)
{
trans[i++] = c;
c = ch[k++];
}
trans[i++] = *\0*;//表示数字字符串结束
i = 0;//将字符转化为浮点数的索引,归零以便下次使用
//s = trans;//将指针指向该数组
d1 = atof(trans);//利用函数将字符串转化为浮点数
numst.push(d1);
cout << "已入栈数字" << d1<< endl;
}
c = ch[k++];
if (c == *+* || c == *-* || c == *** || c == */* || c == *%*)
{
switch (c)
{
case *+*:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 + d2;
numst.push(dr);
cout << "+号计算成功" << dr<<endl;
break;
case *-*:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 - d2;
numst.push(dr);
break;
case ***:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 * d2;
numst.push(dr);
break;
case */*:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 / d2;
numst.push(dr);
break;
case *%*:
d2 = numst.pop();
d1 = numst.pop();
dr = (double)((int)d1 % (int)d2);
numst.push(dr);
break;
}
}
}
return numst.pop();//返回结果
}
};
int main()
{
char str[100];
cout << "请输入中缀表达式" << endl;
cin >> str;
cout << str<<endl;
Jisuan js;
double result=js.change(str);
cout << "结果为" << result << endl;
system("pause");
}
#include <string>
using namespace std;
class opnode
{
public:
int level;
char c;
opnode(char op, int level)
{
this->level = level;
this->c = op;
}
opnode(){}
};
class MyStack//数栈
{
public:
int top;
double array[100];
MyStack()
{
top = 0;
}
double pop();
void push(double c);
double looktop();
bool isEmpty();
//void change(char str[], char ch[]);
};
double MyStack::pop()//定义pop函数
{
top--;
return array[top];
}
void MyStack::push(double c)//定义push函数
{
array[top] = c;
top++;
}
double MyStack::looktop()//看栈顶
{
--top;
double a = array[top];
top++;
return a;
}
class MyStack2//操作符栈
{
public:
int top;
opnode array[100];
MyStack2()
{
top = 0;
}
opnode pop();
void push(opnode c);
opnode looktop();
bool isEmpty();
};
opnode MyStack2::pop()//定义pop函数
{
top--;
return array[top];
}
void MyStack2::push(opnode c)//定义push函数
{
array[top] = c;
top++;
}
opnode MyStack2::looktop()//看栈顶
{
--top;
opnode a = array[top];
top++;
return a;
}
class Jisuan
{
public:
double change(char str[])//定义转化为后缀表达式函数,并计算
{
char ch[100];
int i = 0;//*str的索引,str中缀表达式
int k = 0;//ch的索引,ch后缀表达式
char c;//将字符放在c中
double d1, d2, dr;
MyStack2 st;// 存放操作符
MyStack numst;//建立数字栈
opnode ops;//临时储存操作符
char trans[100];
st.top = 0;
c = str[0];
cout << "开始扫描字符串" << endl;
while (c !=*\0*)//对str字符串进行扫描
{
if ((c >= *0*&&c <= *9*) || c == *.*)//当扫描到数字或者小数点时
{
while ((c >= *0*&&c <= *9*) || c == *.*)
{
ch[k++] = c;
c = str[i++];
}
ch[k++] = * *;
}
if (c == *(*)//如果是左括号则直接入栈
{
opnode *a = new opnode (c, -1);
st.push(*a);
c = str[i++];
}
if (c == *)*)//如果是右括号,从不入栈
{
ops = st.looktop();//看栈顶
while (st.top != 0 && st.top != *(*)//如果没有左括号且栈不为空
{
ops = st.pop();//栈内元素出栈
ch[k++] = ops.c;
ops = st.looktop();
}
i++;
st.pop();//扔掉左括号
}
if (c == *+* || c == *-*)//如果是+-
{
opnode *a = new opnode(c, 1);
if (st.top == 0)//栈为空则直接入栈
{
st.push(*a);
cout << "入栈成功" << endl;
}
else
{
ops = st.looktop();//否则看栈顶
while (ops.level >= a->level)//如果栈顶元素level值较高
{
ops = st.pop();
ch[k++] = ops.c;//将栈顶元素取出放入数组
}
i++;
st.push(*a);//入栈
}
}
cout<<"1111111"<<endl;
if (c == *** || c == */* || c == *%*)//如果是*/%
{
opnode *a = new opnode(c, 2);
if (st.top == 0)
{
st.push(*a);//栈为空直接入栈
}
else
{
ops = st.looktop();//看栈顶
while (ops.level >= a->level)//如果栈顶优先级较高
{
ops = st.pop();
ch[k++] = ops.c;//取出栈顶元素放入数组
}
i++;
st.push(*a);//入栈;
}
}
}
cout<<"222222222"<<endl;
while (st.top != 0)
{
cout << "已进入循环" << endl;
ops = st.pop();//如果栈不为空,则取出栈内元素放入数组
ch[k++] = ops.c;
cout << "出栈符号为" << ops.c << endl;
}
ch[k] = *\0*;//将\0作为结尾放入数组
cout << "后缀表达式为" << endl;
for (i = 0; i < k; i++)
{
cout << ch[i]<<" ";
}
cout << "222222222" << endl;
k = 0;//后缀表达式索引
i = 0;//将字符转化为浮点数的索引
//char *s;
cout << "11111111111" << endl;
c = ch[k++];
cout << "即将扫描后缀表达式" << endl;
while (c != *\0*)//扫描表达式
{
if (c >= *0*&&c <= *9* || c == *.*)
{
while (c >= *0*&&c <= *9* || c == *.*)
{
trans[i++] = c;
c = ch[k++];
}
trans[i++] = *\0*;//表示数字字符串结束
i = 0;//将字符转化为浮点数的索引,归零以便下次使用
//s = trans;//将指针指向该数组
d1 = atof(trans);//利用函数将字符串转化为浮点数
numst.push(d1);
cout << "已入栈数字" << d1<< endl;
}
c = ch[k++];
if (c == *+* || c == *-* || c == *** || c == */* || c == *%*)
{
switch (c)
{
case *+*:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 + d2;
numst.push(dr);
cout << "+号计算成功" << dr<<endl;
break;
case *-*:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 - d2;
numst.push(dr);
break;
case ***:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 * d2;
numst.push(dr);
break;
case */*:
d2 = numst.pop();
d1 = numst.pop();
dr = d1 / d2;
numst.push(dr);
break;
case *%*:
d2 = numst.pop();
d1 = numst.pop();
dr = (double)((int)d1 % (int)d2);
numst.push(dr);
break;
}
}
}
return numst.pop();//返回结果
}
};
int main()
{
char str[100];
cout << "请输入中缀表达式" << endl;
cin >> str;
cout << str<<endl;
Jisuan js;
double result=js.change(str);
cout << "结果为" << result << endl;
system("pause");
}