//四则运算 //xxg #pragma once #include<iostream> using namespace std; typedef struct numNode{//数字栈 char data; struct numNode* next; }numNode,*numListStack; typedef struct opNode { char data; struct opNode* next; }opNode,*opListStack; class c17 { private: numListStack pushnum(numListStack num,char data) { numNode* p = new numNode; p->data = data; p->next = num; num = p; return num; } numListStack popnum(numListStack num ,char &numdata) { if (num != NULL) { numdata = num->data; numNode *p = new numNode; p = num; num = num->next; delete p; return num; } } opListStack pushop(opListStack op,char data) { opNode *p = new opNode; p->data = data; p->next = op; op = p; return op; } opListStack popop(opListStack op, char& numdata) { if (op != NULL) { numdata = op->data; opNode* p = new opNode; p = op; op = op->next; delete p; return op; } } int youxianji(char a) { if (a == '+') { return 2; } if (a == '-') { return 1; } if (a == '*') { return 3; } if (a == '/') { return 3; } } int yunsuan(char a, char b, char op) { int zuo = a - '0'; int you = b - '0'; if (op == '+') { cout << zuo + you; return zuo + you; } if (op == '-') { return zuo - you; } if (op == '*') { return zuo * you; } if (op == '/') { return zuo / you; } } public: void main1() { char a[100];/*输入的表达式*/ char a1[100];/*后缀表达式*/ int i = 0,length; numListStack numLS = NULL; opListStack opLS = NULL; cout << "请输入正确且有意义的算术表达式:" << endl; cout << "按#键退出输入" << endl; cout << "请严格按照一个字符一个空格间隔输入,按空格结束" << endl; while (i < 100) { cin >> a[i++]; if (cin.get() == '\n') break; } length = i; for (int i = 0; i < length; i++) { /*cout << a[i];*/ } for (int j = 0; j < length; j++) { /* if (a[j] >= '0' && a[j] <= '9') { cout << endl << a[j] << endl; numLS = pushnum(numLS, a[j]); } else { opLS = pushop(opLS, a[j]); }*/ } cout << "后缀表达式为:" << endl; int j = 0; coutreoder(opLS, a, length, a1,j); cout << endl << "存储的后缀表达式数组为:" << endl;; for (int k = 0; k < j; k++) { cout << a1[k] <<" "; } cout <<endl<< "计算结果为:" << endl; sum(numLS, a1, j); } //输出后缀表达式 void coutreoder(opListStack op,char arr[],int length,char arr1[],int &j) { for (int i = 0; i < length; i++) {//遍历输入的表达式,数组直接输出,运算符压栈比较优先级后输出 if (arr[i] >= '1' && arr[i] <= '9') { cout << arr[i] << " "; arr1[j] = arr[i];/*将数字存入数组*/ j++; }else{ if (op == NULL) {/*操作符栈为空直接压入*/ op = pushop(op, arr[i]); } else { //判断运算符优先级 int a = youxianji(arr[i]);//入栈优先级 int b = youxianji(op->data);//栈内优先级 if (arr[i] == '(' || arr[i] == ')') { continue; } if (arr[i] == '#') {/*遇到#全部弹出*/ while (op != NULL) { char data; op = popop(op, data); arr1[j] = data; j++; cout << data; } } else { while (a <= b) {/*直到遇到优先级比较低的否则全部弹出*/ char data; op = popop(op, data); arr1[j] = data; j++; cout << data; a = youxianji(arr[i]);//入栈优先级 if(op!=NULL)/*空栈直接退出*/ b = youxianji(op->data);//栈内优先级 else { break; } } /*直接压入*/ op = pushop(op, arr[i]); } } } } } //计算结果 void sum(numListStack num, char arr[], int length) { arr[length] = '#'; for (int i = 0; i < length+1; i++) { if (arr[i]>='0'&&arr[i]<='9') { num = pushnum(num, arr[i]); continue; } if (arr[i] == '#') { char data3; num = popnum(num, data3); int data = data3 - '0'; cout << data; } else { char a; char b; num = popnum(num, a); num = popnum(num, b); char sum = yunsuan(a, b, arr[i]) + '0'; num = pushnum(num, sum); } } } };