该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
贴代码
#include"Stack1.h"
#include"Stack2.h"
#include
bool In(char ch)//判断合法输入
{
return (ch == '+' || ch == '-' || ch == '*' || ch == '/' || ch == '(' || ch == ')' || ch == '#');
}
int num(int n)//判断一个数有几位
{
int m = 1;
while (n % 10) m++;
return m;
}
char Precede(char c1, char c2)//运算符优先级判断函数
{
int i, j;
static char array[49] = {
'>','>','','>',
'>','>','','>',
'>','>','>','>','','>',
'>','>','>','>','','>',
'
'>','>','>','>','!','>','>',
'
switch (c1)
{
case '+':i = 0; break;
case '-':i = 1; break;
case '*':i = 2; break;
case '/':i = 3; break;
case '(':i = 4; break;
case ')':i = 5; break;
case '#':i = 6; break;
default:break;
}
switch (c2)
{
case '+':j = 0; break;
case '-':j = 1; break;
case '*':j = 2; break;
case '/':j = 3; break;
case '(':j = 4; break;
case ')':j = 5; break;
case '#':j = 6; break;
default:break;
}
return array[7 * i + j];
}
int main()
{
Stack1 *OPTR = new Stack1(10);//建立运算符栈
Stack2 *OPND = new Stack2(10);//建立运算数栈
OPTR->Push('#');
char inp[20];
cout << "please enter the operation(end in '#')" << endl;
cin >> inp;
char *ptr = inp;
char c = *ptr++;
char theta;//历次运算运算符
int a, b;//运算数
while (c != '#' || OPTR->GetTop() != '#')
{
if (!In(c))
{
if (!In(*(ptr - 1))) ptr = ptr - 1;
int m = atoi(ptr);
int n = num(m);
OPND->Push2(m);
ptr += n;
c = *ptr++;
}//字符数组当前为数字
else
switch(Precede(OPTR->GetTop(),c))
{
case '
OPTR->Push(c);
c = *ptr++;
break;
case '=':
theta = OPTR->Pop();
c = *ptr++;
break;
case'>':
theta = OPTR->Pop();
a = OPND->Pop2();
b = OPND->Pop2();
int ans;
switch (theta)
{
case '+': ans = a + b; break;
case '-': ans = a - b; break;
case '*': ans = a * b; break;
case '/': ans = a / b; break;
default: break;
}
OPND->Push2(ans);
break;
default:
break;
}
}//字符数组当前为运算符
cout << "The answer is:" << OPND->Pop2() << endl;//输出结果
system("pause");
return 0;
}
#include
using namespace std;
class Stack1 {
public:
Stack1(int size);
bool stackEmpty();
bool stackFull();
void Push(char ch);
char Pop();
char GetTop();
private:
int stacksize;
char *base;
char top;
};
Stack1::Stack1(int size)
{
stacksize = size;
base = new char[size];
top = 0;
}
bool Stack1::stackEmpty()
{
if (0 == top) return true;
return false;
}
bool Stack1::stackFull()
{
if (top >= stacksize) return true;
return false;
}
void Stack1::Push(char ch)
{
if (stackFull())
{
cout << "error" << endl;
return;
}
base[top] = ch;
top++;
}
char Stack1::Pop()
{
if (stackEmpty())
{
cout << "error" << endl;
return 'N';
}
top--;
return base[top];
}
char Stack1::GetTop()
{
if (stackEmpty())
{
cout << "error" << endl;
return 'N';
}
return base[top - 1];
}
#include
using namespace std;
class Stack2 {
public:
Stack2(int size);
bool stackEmpty();
bool stackFull();
void Push2(int ch);
int Pop2();
int GetTop2();
private:
int stacksize;
int *base;
int top;
};
Stack2::Stack2(int size)
{
stacksize = size;
base = new int[size];
top = 0;
}
bool Stack2::stackEmpty()
{
if (0 == top) return true;
return false;
}
bool Stack2::stackFull()
{
if (top >= stacksize) return true;
return false;
}
void Stack2::Push2(int ch)
{
if (stackFull())
{
cout << "error" << endl;
return;
}
base[top] = ch;
top++;
}
int Stack2::Pop2()
{
if (stackEmpty())
{
cout << "error" << endl;
return 0;
}
top--;
return base[top];
}
int Stack2::GetTop2()
{
if (stackEmpty())
{
cout << "error" << endl;
return 0;
}
return base[top - 1];
}
运行显示:“计算器.exe”(Win32): 已加载“C:\Windows\SysWOW64\ucrtbased.dll”。无法查找或打开 PDB 文件。
运行时会输出please enter the operation(end in '#')
但是断点打在该输出语句上时却提示无法命中
求助!!!!!