| 2 | 1/1 | 返回列表 |
| 查看: 649 | 回復: 1 | ||
[求助]
[數(shù)據(jù)結(jié)構(gòu)/C++]新人求助,表達式求值程序無法運行
|
|
#pragma once //順序棧類定義 template <class T> class SqStack { private: T *base;//棧底指針 int top;//棧頂 int stacksize;//棧容量 public: SqStack(int m);//構(gòu)建函數(shù) ~SqStack() { delete[] base; top = 0; stacksize = 0; }//析構(gòu)函數(shù) void Push(T x);//入棧 T Pop();//出棧 T GetTop();//獲取棧頂元素 int StackEmpty();//測棧空 void ClearStack();//清空棧 void StackTop();//返回棧頂指針 void StackTranverse();//顯示棧中元素 }; //順序棧類實現(xiàn) template <class T> SqStack<T>::SqStack(int m) { base = new T[m]; if (base == NULL) { cout << "棧創(chuàng)建失敗,退出!" << endl; exit(1); } stacksize = m; top = -1; } template <class T> void SqStack<T>: ush(T x){ if (top == stacksize - 1) throw "棧滿,無法入棧"; top++; base[top] = x; //cout<<"top:"<<top<<endl; } template <class T> T SqStack<T>: op(){ T x; if (top == -1) throw "?,不能出棧"; x = base[top--]; //cout<<"top:"<<top<<endl; return x; } template <class T> T SqStack<T>::GetTop() { if (top == -1) throw "棧空,棧頂無元素"; //cout<<"top:"<<top<<endl; return base[top]; } template <class T> int SqStack<T>::StackEmpty() { if (top == -1) return 1; else return 0; } template <class T> void SqStack<T>::ClearStack() { top = -1; } template <class T> void SqStack<T>::StackTop() {//返回棧頂指針 cout << "棧頂top= " << top; } template <class T> void SqStack<T>::StackTranverse() { int i = top; while (i >= 0) cout << base[i--] << '\t'; cout << endl; } //SqStack.h,堆棧的類 //下面是源程序代碼 #include<iostream> #include"process.h" #include"stdio.h" #include<string.h> #include<stdlib.h> #include"SqStack.h" using namespace std; char pause; char Precede(char t1,char t2) { char f; switch(t2) { case '+': case '-':if(t1=='('||t1=='=') f='<'; else f='>'; break; case '*': case '/':if(t1=='*'||t1=='/'||t1==')') f='>'; else f='<'; break; case '(':if(t1==')') { cout<<"ERROR1"<<endl; exit(0); } else f='<'; break; case ')':switch(t1) { case '(':f='='; break; case '=':cout<<"ERROR2"<<endl; exit(0); default: f='>'; } break; case '=':switch(t1) { case '=':f='='; break; case '(':cout<<"ERROR2"<<endl; exit(0); default: f='>'; } } return f; } bool IsOperator(char c) { switch(c) { case'+': case'-': case'*': case'/': case'(': case')': case'=':return true; default:return false; } } double Operate(double a,char theta,double b) { double c; switch(theta) { case'+':c=a+b; break; case'-':c=a-b; break; case'*':c=a*b; break; case'/':c=a/b; } return c; } double Val_Exp(char *exp) { SqStack<char> OP(20); SqStack<double> OD(20); char theta; double a,b,d; char c,x; char z[6]; int i; OP.Push('='); c=*exp++; x=OP.GetTop(); while(c!='='||x!='=') { if(IsOperator(c)) switch(Precede(x,c)) { case'<':OP.Push(c); c=*exp++; break; case'=':x=OP.Pop(); c=*exp++; break; case'>':theta=OP.Pop(); b=OD.Pop(); a=OD.Pop(); OD.Push(Operate(a,theta,b)); } else if(c>='0'&&c<='9'||c=='.') { i=0; do { z=c; i++; c=*exp++; }while(c>='0'&&c<='9'||c=='.'); z='\0'; d=atof(z); OD.Push(d); } else { cout<<"ERROR3"<<endl;; exit(0); } x=OP.GetTop(); } d=OD.GetTop(); return d; } void CreatePostExp(char * exp,char * &postexp) { char c,x; int i=0; SqStack<char> OP(20); OP.Push('='); cout<<"exp:"<<exp<<endl; c=*exp++; while(c) { if((c>='0'&&c<='9')||c=='.') { postexp[i++]=c; c=*exp++; } if(IsOperator(c)) { postexp[i++]=' '; x=OP.GetTop(); switch(Precede(x,c)) { case'<':OP.Push(c); c=*exp++; break; case'=':x=OP.Pop(); c=*exp++; break; case'>':postexp[i++]=OP.Pop(); break; } } postexp='\0'; } cout<<"后綴表達式為:"<<postexp<<endl; } double Val_PostExp(char *postexp) { int i; char z[6]; double v=0,d=0,a,b; char c; SqStack<double> OD(20); c=*postexp++; while(c!='\0') { if((c>='0'&&c<='9')||c=='.') { i=0; do { z[i++]=c; c=*postexp++; }while(c>='0'&&c<='9'||c=='.'); z='\0'; d=atof(z); OD.Push(d); } if(IsOperator(c)) { b=OD.Pop (); a=OD.Pop (); OD.Push (Operate(a,c,b)); c=*postexp++; } c=*postexp++; } v=OD.Pop (); return v; } void CreatePreFax(char *exp, char *&prefax) { char q; char m[20]; int i, j, k; i = j = k = 0; SqStack<char> OP(20); SqStack<char> OM(20); SqStack<char> ON(20); while (*exp != '=') OM.Push(*exp++); while (!OM.StackEmpty()) { q = OM.Pop(); if ((q >= '0'&&q <= '9') || q == '.') { m[i++] = q; } if (q == ')') OP.Push(q); while ((q == '+') || (q == '-') || (q == '*') || (q == '/')) { m[i++] = ' '; if (OP.StackEmpty() || OP.GetTop() == ')' || Precede(q, OP.GetTop()) == '>' || Precede(q, OP.GetTop() )== '=') { OP.Push(q); break; } else m[i++] = OP.Pop(); } if (q == '(') { while (OP.GetTop() != ')') m[i++] = OP.Pop(); OP.Pop(); } } while (!OP.StackEmpty()) { m[i++] = OP.Pop(); } m = '\0'; while (m[j] != '\0') { ON.Push(m[j++]); } while (!ON.StackEmpty()) prefax[k++] = ON.Pop(); prefax[k] = '\0'; cout << "前綴表達式為:" << prefax << endl; } double Val_PreFax(char *prefax) { int i, j; char c; char z[6]; char s[6]; SqStack<char>OS(20); SqStack<double>OD(20); double v = 0, d = 0, a, b; while (*prefax != '\0') { OS.Push(*prefax++); } while (!OS.StackEmpty()) { c = OS.Pop(); if ((c>='0'&&c<='9')||c=='.') { i = 0; do { z[i++] = c; c = OS.Pop(); } while ((c>='0'&&c<='9')||c=='.'); i--; for (int j = 0; i >= 0; i--) s[j++] = z; s = '\0'; d = atof(s); OD.Push(d); } if (IsOperator(c)) { b = OD.Pop(); a = OD.Pop(); OD.Push(Operate(b,c,a)); } } v = OD.Pop(); return v; } int main(void) { char exp[20]="(2.2+5)+4*(5-3.1)="; char *postexp; postexp=new char[20]; *postexp='\0'; char *prefax; prefax = new char[20]; *prefax = '\0'; double v1, v2; system("cls" ;int choice; do { cout<<"主菜單\n"; cout<<"1-創(chuàng)建表達式\n"; cout<<"2-表達式求值\n"; cout<<"3-求后綴表達式\n"; cout<<"4-后綴表達式求值\n"; cout<<"5-求前綴表達式\n"; cout << "6-前綴表達式求值\n"; cout << "7-顯示表達式\n"; cout<<"8-退出\n"; cout<<"Enter choice:"; cin>>choice; switch(choice) { case 1: cout<<"請輸入表達式,以=結(jié)束"<<endl; cin>>exp; cin.get(pause); system("pause" ;break; case 2: v1=Val_Exp(exp) ; cout<<exp; cout<<v1<<endl; cin.get(pause); system("pause" ;break; case 3: CreatePostExp(exp,postexp); cin.get(pause); system("pause" ;break; case 4: v1=Val_PostExp(postexp); cout<<postexp<<"="<<v1<<endl; cin.get(pause); system("pause" ;break; case 5: CreatePreFax(exp, prefax); cin.get(pause); system("pause" ;break; case 6: v2 = Val_PreFax(prefax); cout << prefax << "=" << v2 << endl; cin.get(pause); system("pause" ;break; case 7: cout<<endl; cout<<"已創(chuàng)建的表達式為:"; cout<<exp<<endl; CreatePostExp(exp,postexp); CreatePreFax(exp,prefax); cin.get(pause); system("pause" break; case 8: cout<<"退出運算"<<endl; break; default: cout<<"Invalid choice\n"; break; } }while(choice!=8); } 發(fā)自小木蟲Android客戶端 |
金蟲 (小有名氣)

| 2 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 324求調(diào)劑 +6 | lucky呀呀呀鴨 2026-03-20 | 6/300 |
|
|---|---|---|---|---|
|
[考研] 一志愿北京化工大學 070300 學碩 336分 求調(diào)劑 +3 | vv迷 2026-03-22 | 3/150 |
|
|
[考研] 0856材料專碩353求調(diào)劑 +4 | NIFFFfff 2026-03-20 | 4/200 |
|
|
[基金申請] 山東省面上項目限額評審 +4 | 石瑞0426 2026-03-19 | 4/200 |
|
|
[考研] 326求調(diào)劑 +5 | 諾貝爾化學獎覬?/a> 2026-03-15 | 8/400 |
|
|
[考研] 材料學碩333求調(diào)劑 +3 | 北道巷 2026-03-18 | 3/150 |
|
|
[考研] 0703化學297求調(diào)劑 +3 | Daisy☆ 2026-03-20 | 3/150 |
|
|
[考研] 336求調(diào)劑 +5 | rmc8866 2026-03-21 | 5/250 |
|
|
[考研] 299求調(diào)劑 +5 | shxchem 2026-03-20 | 7/350 |
|
|
[考研] 316求調(diào)劑 +6 | 梁茜雯 2026-03-19 | 6/300 |
|
|
[考研] 083200學碩321分一志愿暨南大學求調(diào)劑 +3 | innocenceF 2026-03-17 | 3/150 |
|
|
[考研] 華東師范大學-071000生物學-293分-求調(diào)劑 +3 | 研究生何瑤明 2026-03-18 | 3/150 |
|
|
[考研] 350求調(diào)劑 +5 | weudhdk 2026-03-19 | 5/250 |
|
|
[考研] 材料學碩297已過四六級求調(diào)劑推薦 +11 | adaie 2026-03-19 | 11/550 |
|
|
[考研] 一志愿 南京航空航天大學大學 ,080500材料科學與工程學碩 +5 | @taotao 2026-03-20 | 5/250 |
|
|
[考研] 材料考研調(diào)劑 +3 | xwt。 2026-03-19 | 3/150 |
|
|
[考研] 328求調(diào)劑,英語六級551,有科研經(jīng)歷 +4 | 生物工程調(diào)劑 2026-03-16 | 12/600 |
|
|
[考博] 26博士申請 +3 | 1042136743 2026-03-17 | 3/150 |
|
|
[考研] 321求調(diào)劑 +5 | 大米飯! 2026-03-15 | 5/250 |
|
|
[考研] 中科院材料273求調(diào)劑 +4 | yzydy 2026-03-15 | 4/200 |
|