


版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、C+ 語言編寫#include<iostream>#include<cmath>#include<string>using namespace std;const int SIZE = 1000;typedef struct node/ 為了處理符號而建立的鏈表 ( 如 : 1+(-2) char data;node *next;node;typedef struct stack_num/ 存儲 數 的棧 double *top;double *base;stack_num;typedef struct stack_char/ 存儲 運算符號 的棧char
2、*top;char *base;stack_char;stack_num S_num;/ 定義stack_char S_char;/ 定義char fu18 = 'n', '):'+','-', '*', '/', '%', ' A','Q', 'L', 'C', 'S', 'T', 'c', 's', 't', '('int com
3、pare1000;/ 表現出各運算符號的優先級double shu1000;/ 存儲 " 數 " 的數組double dai_result; 運算的結果,是為了處理 M 運算( 簡介函數里有 M 的定義 ) int biao = 0;/ 和 dia_result 一樣,為了處理 M 運算 char lineSIZE;/ 輸入的所要計算的表達式 void init()/ 初始化comparefu0 = -2;/ 用數字的大小表現出符號的優先級comparefu1 = -1;comparefu2 = 2;comparefu3 = 2; comparefu4 = 4;compar
4、efu5 = 4; comparefu6 = 4;comparefu7 = 5;為棧開辟空for(int i = 8; i <= 15; i+) comparefui = 6;comparefu16 = 7;S_num.base = (double*)malloc(sizeof(double)*SIZE);/同上S_char.base = (char*)malloc(sizeof(char)*SIZE);/S_num.top = S_num.base;S_char.top = S_char.base;void push_num(double n)/ 數字進棧* +S_num.top =
5、n;void push_char(char c)/ 運算符號進棧 * +S_char.top = c;double pop_num()/ 數字出棧double m = *S_num.top;S_num.top-;return m;char pop_char()/ 運算符號出棧char cc = *S_char.top;S_char.top-;return cc;char get_top_char()/ 得到運算符號的棧中最頂端的運算符號return *S_char.top;double operate(double y, char c, double x)/ 對兩個數計算 ( 含是雙目運算符
6、如 *, / 等 等)double r;if(c = '-')r = x - y;else if(c = '+')r = x + y;else if(c = '/' && y != 0)r = x / y;else if(c = '*')r = x * y;else if(c = A )r = 1;for(int i = 1; i <= y; i+)r *= x;else if(c = '%')int r0 = (int)x % (int)y;r = double(r0);return r;對
7、一個數運( 含單目運算符 : 如double operate_one(double one, char cc)/log(L), sin(S) 等等)double r;if(cc = 'Q')r = sqrt(one);else if(cc = 'C')r = cos(one);else if(cc = 'S')r = sin(one);else if(cc = 'T')r = tan(one);else if(cc = 'c')r = acos(one); else if(cc = 's')r =
8、asin(one);else if(cc = 't')r = atan(one);return r;求對數的double operate_L(double a, double b, char dian)/double r = log(b) / log(a);return r;double compute()/ 對整個表達式的計算 char c;/ 表示運算符號int p = 0;/ 用于 shu+p, 先初始化 int i, j; init();/ 進行初始化 push_char('n'); linestrlen(line) = 'n' line
9、strlen(line)+1 = '0'續進if(biao)push_num(dai_result);/ 把運算的結果先進棧 , 在這個結果的根底上繼行運算biao = 0;for(i = 0; linei != '0')/把表達式中的數字字符串轉化成可計算的數int flag = 0;int flag1 = 1;/ 標記是否是運算符號/ int flag2 = 1;/ 標記是否出現 '_'double h = 0;int ge;/ 位數int biao_dian = 0;/ 是否是小數的類型while(1)flag1 = 1;for(j = 0
10、; j <= 16; j+)if(linei = fuj)flag1 = 0;if(linei = '_') break;if(linei = '.') i+;ge = 0;biao_dian = 1;if(linei = 'P')shu+p = pi;i+;break;if(linei = 'E')shu+p = e;if(flag1)h = h * 10 + (linei - '0'); flag = 1;i+;if(biao_dian)ge+;elsebreak;if(flag)if(biao_dian
11、)int r = 1;for(int k = 1; k <= ge; k+) r *= 10;h /= r;if(linei = '+')shu+p = h;/ 把轉化而來的數字存于數組 shu+p = -1; elseif(linei = '-')shu+p = -2; elseif(linei = '*')shu+p = -3; elseif(linei = '/')shu+p = -4; else if(linei = '%')shu+p = -5;else if(li nei = ' A
12、9;)shu+p =-6; else if(linei = 'Q')shu+p =-7; else if(linei = 'L')shu+p =-8; else if(linei = 'C')shu+p =-9;else if(linei = 'S')shu+p = -10; else if(linei = 'T')shu+p = -11;else if(linei = 'c')shu+p = -12;else if(linei = 's')shu+p = -13;else if(l
13、inei = 't')shu+p = -14;else if(linei = '(')shu+p = -15;else if(linei = ')')shu+p = -16;else if(linei = 'n')shu+p = -17;i+;i = 1;while(shui != -17 | get_top_char() != 'n') double m = shui;if(m >= 0)push_num(m);elseif(m = -1)c = '+'else if(m = -2) c =
14、 '-'else if(m = -3)1*1.else if(m = -4)c = '/'else if(m = -5) c = '%'else if(m = -6)c = ' A'else if(m = -7)c = 'Q'else if(m = -8)c = 'L'else if(m = -9)c = 'C'else if(m = -10)c = 'S'else if(m = -11)c = 'T'else if(m = -12)c = '
15、c'else if(m = -13)c = 's'else if(m = -14)c = 't'else if(m = -15)c = '('else if(m = -16)c = ')'else if(m = -17)c = 'n'if(comparech <char ch = get_top_char();/ 得到最頂端運算符號comparec)/ 運算符號級別的比擬 push_char(c);else if(ch = '(' && c = ')')
16、pop_char();i+;else if(comparech >= comparec && ch != '(' && ch != 'n')if(ch = 'Q' | ch = 'C' | ch = 'S'| ch = 'T'| ch = 'c' | ch = 's' | ch = 't')double one = pop_num();char dian = pop_char(); push_num(operate
17、_one(one, dian); else if(ch = 'L')double one_L = pop_num();double two_L = pop_num();char dian = pop_char(); push_num(operate_L(two_L, one_L, dian);elsedouble x = pop_num();double y = pop_num();char dian = pop_char();if(dian = '/' && x = 0)/判斷是否除了 " 零"cout<<&qu
18、ot; 由于您除了零,結果將是錯誤的 "<<endl;push_num(operate(x, dian, y);/ 把進行一次計算的結果入 棧elsepush_char(c);i+;得到結果double result = pop_num();/ return result;int check_kuohao()/ 檢查表達式括號是否匹配int i, f = 0;int kuoSIZE, key = 1; memset(kuo, 0, sizeof(kuo); for(i = 0; linei != '0' i+)if(linei = '(')
19、kuo+f = 1; else if(linei = ')') if(kuof = 1)kuof = 0;f-;elsekey = 0;break;if(key && f = 0)return 1;elseretur n 0;int check_char() 檢查運算符號是否合法(女口 : 1 +* 4) int i, ge;ge = 0;for(i = 0; lin ei != '0')-1*1while(li nei='+' | lin ei='-'| lin ei='八II linei='/&
20、#39; | lin ei='%' | lin ei=III linei=Q | linei='L' | lin ei='S'| linei=C | linei=T | lin ei=:='s'| linei='c' | lin ei='t')ge+; i+;if(ge >= 3)return 0;elsereturn 1;void output(double result)/ 打出結果printf(" 所得結果是 : ");cout<<result<&l
21、t;endl;void check()/ 檢查表達式是否合法void introduce();char cc;/ 決定計算器按哪種功能進行計算double result;/ 結果void input();/ 定義if( check_kuohao() && check_char() )/ 看是否合法 , 合法那么計算result = compute();output(result);coutvv"輸入一個字符M'或D或'F',決定是否繼續:"vvendl;while(cin>>cc)if(cc = 'M')s
22、ystem("cls");introduce();printf(" 您上次所得結果為 : ");請繼續輸入想計算的表達 cout<<result<<endl;cout<<" 在上次計算結果的根底上"<<endl;dai_result = result;biao = 1;input();/ 輸入表達式break;else if(cc = 'D')system("cls");introduce();cout<<" 計算器已清零 , 請
23、輸入您所要計算的表達式 "<<endl;input();/ 輸入表達式break;else if(cc = 'F')system("cls");cout<<" 計算器關閉 , 謝謝使用 !"<<endl;break;elsecout<<" 所 輸 入 字 符 無 效 , 請 輸 入 一 個 字 符 'M' 或 'D' 或 'F'!"<<endl;continue;else/ 不合法,分兩種不合法if(c
24、heck_kuohao() = 0 && check_char() = 1)cout<<" 您所輸入的表達式括號不匹配 , 請重新輸入 :"<<endl;input();/ 輸入表達式else:"<<endl;cout<<" 您所輸入的表達式不合法 , 請重新輸入 input();/ 輸入表達式void tackle_fuhao()/ 處理負號node *root, *head, *p, *q, *p1;root = head = new node;head->next = NULL;
25、int i;for(i = 0; linei != '0' i+)/ 建立鏈表p = new node;p->data = linei;p->next = head->next;head->next = p;head = p;/ delete p;處理第一個q = (node*)malloc(sizeof(node);head = root;if(root->next->data = '+' | root->next->data = '-')/符p = new node; p->data =
26、'0'p->next = head->next; head->next = p;if(root->next != NULL)for(q = root->next; q; q = q->next)if(q->data = '(' && (q->next->data = '-' | q->next->data = '+')p = new node; p->data = '0' p->next = q->next; q-&
27、gt;next = p;/ delete q;pl = new no de;int qi = -1;for(p1 = root->next; p1; pl = p1- >n ext)lin e+qi = p1->data;lin e+qi = '0'void input() 輸入cin> >li ne;if(biao = 0)tackle_fuhao(); 處理負號check();檢查表達式是否合法void introduce()/對計算器的符號功能的簡要介紹cout<<"計算器簡要介紹"<<endl;cout«"C(cos) S(sin) T(ta n) a(arccos) c(arcs in) "<<e ndl;coutvv"7 89/o
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 企業采購的新思路-基于區塊鏈技術的辦公物資采購優化研究報告
- 大型碳鋼鑄件企業ESG實踐與創新戰略研究報告
- 口腔燈企業ESG實踐與創新戰略研究報告
- 脆碎儀企業數字化轉型與智慧升級戰略研究報告
- 電動葫蘆梁式起重機企業數字化轉型與智慧升級戰略研究報告
- 電爐特種硅合金企業縣域市場拓展與下沉戰略研究報告
- 割灌機(田間機械)企業縣域市場拓展與下沉戰略研究報告
- 肉、蛋、禽及水產品超市企業縣域市場拓展與下沉戰略研究報告
- 固定型鉛酸蓄電池企業縣域市場拓展與下沉戰略研究報告
- 高管層及核心技術人員保密協議2篇
- 通 用泛亞在線測評題
- 掛靠公司合同樣本
- 2025屆高考物理講座課件:《對標三新尋求突破》
- TSG 23-2021 氣瓶安全技術規程 含2024年第1號修改單
- 小學教育畢業論文三篇
- 海南省三亞市(2024年-2025年小學三年級語文)統編版階段練習(下學期)試卷(含答案)
- 2024年全國青少年信息素養大賽初賽試題
- 綠化帶改停車位施工方案
- 2023-2024學年浙江省環大羅山聯盟高一下學期4月期中物理試題(解析版)
- 護理人員崗位績效考核評價標準
- 2024年04月陜西西安職業技術學院招考聘用25人筆試筆試歷年典型考題及考點研判與答案解析
評論
0/150
提交評論