C++語言計算器源代碼_第1頁
C++語言計算器源代碼_第2頁
C++語言計算器源代碼_第3頁
已閱讀5頁,還剩9頁未讀 繼續免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、C+ 語言編寫。 。#include<iostream>#include<cmath>#include<string> using namespace std;const double pi = 3.14159265;const double e const int SIZE = 1000;typedef struct node/ 為了處理符號而建立的鏈表(如 : 1+(-2)char data;node *next;node;typedef struct stack_num/ 存儲 數 的棧double *top;double *base;stack_nu

2、m;typedef struct stack_char/ 存儲 運算符號 的棧char *top;char *base;stack_char;stack_num S_num;/ 定義stack_char S_char;/ 定義char fu18 = 'n', ')', '+', '-', '*', T,'%', 'A','Q', 'L', 'C', 'S', 'T', 'c', '

3、s', 't', '('int compare1000;/ 表現出各運算符號的優先級double shu1000;/ 存儲 "數 " 的數組double dai_result;運算的結果,是為了處理M運算(簡介函數里有 M的定義)int biao = 0;/ 和 dia_result 一樣,為了處理 M 運算char lineSIZE;/ 輸入的所要計算的表達式void init()/ 初始化comparefu0 = -2;/ 用數字的大小表現出符號的優先級comparefu1 = -1; comparefu2 = 2; compar

4、efu3 = 2;comparefu4 = 4; comparefu5 = 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 pus

5、h_num(double n)/ 數字進棧* +S_num.top = 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

6、y, char c, double x)/ 對兩個數計算 ( 含是雙目運算符 : 如 *, / 等等 )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 = '%

7、')int r0 = (int)x % (int)y;r = double(r0); return r;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

8、= 'c') r = acos(one);else if(cc = 's') r = 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

9、_char('n');linestrlen(line) = 'n' linestrlen(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;in t ge;/ 位數i

10、nt biao_dian = 0;/ 是否是小數的類型while(1)flag1 = 1;for(j = 0; j <= 16; j+)if(linei = fuj)flag1 = 0;break;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; i+; break; if(flag1) h = h * 10 +

11、(linei - '0'); flag = 1; i+;if(biao_dian) ge+; else break; if(flag) if(biao_dian) int r = 1;for(int k = 1; k <= ge; k+) r *= 10;h /= r;shu+p = h;/ 把轉化而來的數字存于數組if(linei = '+') shu+p = -1;else if(linei = '-') shu+p = -2;else if(linei = '*') shu+p = -3;else if(linei =

12、 '/') shu+p = -4;else if(linei = '%') shu+p = -5;else if(li nei = 'A')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 =

13、-11;else if(linei = 'c') shu+p = -12;else if(linei = 's')shu+p = -13;else if(linei = '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() != '

14、n') double m = shui;if(m >= 0)push_num(m); elseif(m = -1)c ='+'else if(m= -2)c =1Jelse if(m= -3)c =1*1.Jelse if(m= -4)c ='/'else if(m= -5)c ='%'else if(m= -6)c ='A';jelse if(m= -7)c ='Q'else if(m= -8)c ='L'else if(m= -9)c ='C'else if(m=

15、-10)c ='S'else if(m= -11)c ='T'else if(m= -12)c ='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'char ch = get_top_char();/ 得到最頂端運算符號 if(comparech < comparec)/ 運算符號級別的比較 pus

16、h_char(c);i+;else if(ch = '(' && c = ')')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'

17、;)double one = pop_num();char dian = pop_char();push_num(operate_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 = 

18、9;/' && x = 0)/ 判斷是否除了 "零 "cout<<" 由于您除了零,結果將是錯誤的 "<<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

19、(kuo); for(i = 0; linei != '0' i+) if(linei = '(') kuo+f = 1;else if(linei = ')') if(kuof = 1) kuof = 0; f-; else key = 0; break; if(key && f = 0) return 1;else return 0;int check_char()/ 檢查運算符號是否合法(如 : 1 +* 4)int i, ge;for(i = 0; linei != '0' ) ge = 0;while(l

20、inei = '+' | linei = '-' | linei = '*'| lin ei = '/' | lin ei = '%' | lin ei = 'A' | linei = 'Q' | linei = 'L' | linei = 'S' | linei = 'C' | linei = 'T' | linei = 's' | linei = 'c' | linei = '

21、t') ge+; i+;if(ge >= 3)return 0;elsereturn 1;void output(double result)/ 打出結果printf(" 所得結果是 : "); cout<<result<<endl;void check()/ 檢查表達式是否合法void introduce();char cc;/ 決定計算器按哪種功能進行計算 double result;/ 結果void input();/ 定義if( check_kuohao() && check_char() )/ 看是否合法 ,

22、合法則計算result = compute(); output(result);cout<<"輸入一個字符'M'或'D'或'F',決定是否繼續:"<<endl; while(cin>>cc) if(cc = 'M') system("cls");introduce();printf(" 您上次所得結果為 : ");cout<<result<<endl;"<<endl;cout<<

23、"在上次計算結果的基礎上,請繼續輸入想計算的表達式dai_result = result;biao = 1;input();/ 輸入表達式 break;else if(cc = 'D')system("cls");introduce();cout<<" 計算器已清零 , 請輸入您所要計算的表達式 "<<endl;input();/ 輸入表達式break;else if(cc = 'F')system("cls");cout<<" 計算器關閉 , 謝

24、謝使用 !"<<endl;break;elsecout<<"所輸入字符無效,請輸入一個字符'M'或D或'F'!"<<endl; continue;else/不合法,分兩種不合法if(check_kuohao() = 0 && check_char() = 1)cout<<" 您所輸入的表達式括號不匹配 , 請重新輸入 :"<<endl;input();輸入表達式elsecout<<" 您所輸入的表達式不合法 , 請重

25、新輸入 :"<<endl;input();/輸入表達式void tackle_fuhao()/ 處理負號node *root, *head, *p, *q, *p1;root = head = new node;head->next = NULL;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*)mallo

26、c(sizeof(node);head = root;if(root->next->data = '+' | root->next->data = '-')/ 處理第一個字符 p = new node; p->data = '0' p->next = head->next; head->next = p;if(root->next != NULL)for(q = root->next; q; q = q->next)if(q->data = '(' &

27、& (q->next->data = '-' | q->next->data = '+') p = new node; p->data = '0' p->next = q->next; q->next = p;/ delete q;p1 = new node;int qi = -1;for(p1 = root->next; p1; p1 = p1->next)line+qi = p1->data; line+qi = '0'void input()/ 輸入c

28、in>>line;if(biao = 0) tackle_fuhao();/ 處理負號check();檢查表達式是否合法void introduce()/ 對計算器的符號功能的簡要介紹cout<<"計算器簡要介紹"<<endl;cout<<"C(cos)S(sin)T(tan)a(arccos)c(arcsin) "<<endl;cout<<"789/ont(arctan) "<<endl;cout<<"456*%L(log)"<<endl;cout<<"123-M(M+)Q(s

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論