#include?<cstdlib>
#include?<iostream>
using?namespace?std;
int?main()
{
int?a,b;//a是輸出結果,b是臨時輸入數據
char?x;//x是標點符號輸入
cin>>a;//先輸入第壹個數
while(1)//由於不知道運算式壹***多長,所以用壹個死循環不斷讀取
{
cin>>x;//輸入運算符
if(x=='=')//'='特殊處理,輸出結果
{
cout<<a<<endl;
break;//退出循環,跳到return?0;
}
else//如果是運算符就輸入下壹個數
{
cin>>b;
switch(x)//判斷符號類型,並進行相應計算
{
case?'+':a+=b;break;//每個case後面必須加break;否則將後面所有運算式全走壹遍
case?'-':a-=b;break;
case?'*':a*=b;break;
case?'/':a/=b;break;
}
}
}
return?0;
}?
汗,又改變條件了,這次妳的要求和原來要求可是截然不同的程序啊,涉及到很多算法的,二叉樹,堆棧等,我如果重寫了初學者不壹定能看懂了。我下面就給妳貼壹下差不多的代碼吧。只是這個不需要輸入等號,回車自動計算。如果需要去掉那些繁瑣的代碼估計沒人為了這幾個虛擬分給妳去掉的。
#include<iostream> #include<string> #include<stack> #include?"Tree.h" #include<windows.h> boolisok(stringexp)//此函數驗證式子是否正確,即是否符合運算規則。{
charcheck;
interror=0;
intlb=0;
intrb=0;
if(exp.size()==1?&&?exp[0]!='-')returnfalse;?
elseif((IsOperator(exp[0])&&?exp[0]!='-'?||IsOperator(exp[exp.size()-1]))&&exp[0]!='('&&exp[exp.size()-1]!=')')?//此處若不加,在遇到某些式子時,會出現非法操作。
returnfalse;
for(intm=0;m<exp.size();m++)
{
check=exp[m];?
if(m==0?&&?check=='-'?&&?(isdigit(exp[1])!=0?||?exp[1]=='('?)?)check=exp[++m];
if(IsOperand(check));?//如果是數字,跳過,不管。
elseif(IsOperator(check))
{
if(check==')')
{
rb++;
if(IsOperator(exp[m+1])&&(exp[m+1]=='+'||exp[m+1]=='-'||exp[m+1]=='*'||exp[m+1]=='/'||exp[m+1]=='^'||exp[m+1]==')'))
{
m++;
if(exp[m]==')')
rb++;
}
elseif(IsOperator(exp[m+1]))
error++;
}
elseif(check=='(')
{
lb++;
if(exp[m+1]=='(')
{
m++;
lb++;
}
elseif(IsOperator(exp[m+1])&&exp[m+1]!='-')
error++;
}
else
{
if(IsOperator(exp[m+1])&&exp[m+1]=='(')
{
m++;
lb++;
}
elseif(IsOperator(exp[m+1]))
error++;
}
}
else
error++;
}
if(error==0&&lb==rb)
return(true);
else
return(false);
}
intmain() { HANDLE?hOut?=?GetStdHandle(STD_OUTPUT_HANDLE);? SetConsoleTitle("四則運算器二叉樹版"); SetConsoleTextAttribute(hOut,BACKGROUND_GREEN+FOREGROUND_BLUE); binary_treeetree; stack<binary_tree>NodeStack; stack<char>OpStack; stringinfix; charchoice='y';? system("cls"); cout<<"*******************************************************************"<<endl; cout<<"*?*"<<endl; cout<<"*十進制四則運算計算器 ?※※※※※※※※※※※※*"<<endl; cout<<"* ?※※*"<<endl; cout<<"*(二叉樹版) ?※※※※※※※※※※※※*"<<endl;cout<<"*?*"<<endl;
cout<<"*******************************************************************"<<endl; charc; while(choice=='y'||choice=='Y'){
cout<<"\n請輸入表達式,不要帶空格:\n";
cin>>infix;
cout<<"--------------------------------------------------------------------------------"<<'\n';
cout<<"表達式為:?"<<infix<<'\n';
if(isok(infix))
{
for(inti=0;i<infix.size();i++)
{
c=infix[i];?
if(i==0?&&?c=='-')?//若開始為負,則把零壓入運算數棧,把'-'壓入運算符棧
{
binary_treetemp;
temp.root=build_node("0");?
NodeStack.push(temp);?
OpStack.push('-');
}
else
if(IsOperand(c))
{
stringtempstring;
tempstring=tempstring+c;?
while(i+1<infix.size()&&IsOperand(infix[i+1]))
{
tempstring+=infix[++i];
}
binary_treetemp;
temp.root=build_node(tempstring);
NodeStack.push(temp);
}
elseif(c=='+'||c=='-'||c=='*'||c=='/'||c=='^')
{
if(OpStack.empty())
OpStack.push(c);
elseif(OpStack.top()=='(')
OpStack.push(c);
elseif(TakesPrecedence(c,OpStack.top()))
OpStack.push(c);
else
{
while(!OpStack.empty()&&(TakesPrecedence(OpStack.top(),c)||addition(OpStack.top(),c)))
{
binary_treetemp_tree;
stringthisstring="";
thisstring=thisstring+OpStack.top();
OpStack.pop();
etree.root=build_node(thisstring);
copy(temp_tree.root,NodeStack.top().root);
NodeStack.pop();
etree.root->right_child=temp_tree.root;
temp_tree.root=NULL;
copy(temp_tree.root,NodeStack.top().root);
etree.root->left_child=temp_tree.root;
NodeStack.pop();
temp_tree.root=NULL;
copy(temp_tree.root,etree.root);
NodeStack.push(temp_tree);
etree.root=NULL;
}
OpStack.push(c);
}
}
elseif(c=='(')//若中間遇到括號,則判斷下壹位是否為'-'
{OpStack.push(c);
?if(infix[i+1]=='-')
{
binary_treetemp;
temp.root=build_node("0");?
NodeStack.push(temp);
OpStack.push('-');
++i;
}
}
elseif(c==')')
{
while(OpStack.top()!='(')
{
binary_treetemp_tree;
stringthisstring="";
thisstring=thisstring+OpStack.top();
OpStack.pop();
etree.root=build_node(thisstring);
copy(temp_tree.root,NodeStack.top().root);
NodeStack.pop();
etree.root->right_child=temp_tree.root;
temp_tree.root=NULL;
copy(temp_tree.root,NodeStack.top().root);
etree.root->left_child=temp_tree.root;
NodeStack.pop();
temp_tree.root=NULL;
copy(temp_tree.root,etree.root);
NodeStack.push(temp_tree);
etree.root=NULL;
}
OpStack.pop();
}
}
////////////////////////////////////////////////////////while(!OpStack.empty())
{
binary_treetemp_tree;
stringthisstring="";
thisstring=thisstring+OpStack.top();
OpStack.pop();
etree.root=build_node(thisstring);
copy(temp_tree.root,NodeStack.top().root);
NodeStack.pop();
etree.root->right_child=temp_tree.root;
temp_tree.root=NULL;
copy(temp_tree.root,NodeStack.top().root);
etree.root->left_child=temp_tree.root;
NodeStack.pop();
temp_tree.root=NULL;
copy(temp_tree.root,etree.root);
NodeStack.push(temp_tree);
if(!OpStack.empty())
{
etree.root=NULL;
}
}
cout<<"打印結點如下:?";
etree.print();
cout<<'\n';
cout<<"結點個數為:"<<etree.counter()<<'\n';
cout<<"以下是,中間的計算結果:"<<'\n';
etree.evaluate();
cout<<'\n';
cout<<"結果是:?";
cout<<etree.root->data<<'\n';
cout<<'\n'<<"--------------------------------------------------------------------------------"<<'\n';
cout<<"\n\n是否要重新運行?輸入<Y/N>:?";
cin>>choice;
} else{
cout<<"************************************************"<<'\n';
cout<<"錯誤:輸入的表達試有誤!"<<'\n';
cout<<"************************************************"<<'\n';
cout<<"\n\n是否要重新運行?輸入<Y/N>:?";
cin>>choice;
}
} return0; }?課程設計報告
設計題目:十進制四則運算計算器
實習目的
通過實習,了解並初步掌握設計、實現較大系統的完整過程,包括系統分析、編碼設計、系統集成、以及調試分析,熟練掌握數據結構的選擇、設計、實現以及操作方法,為進壹步的應用開發打好基礎。
二.問題描述
在以二叉樹表示算術表達式的基礎上,設計壹個十進制的四則運算的計算器。[設計要求]實現整數浮點數的四則運算。
三.需求分析
該程序實現的是實數型的四則運算,並在此運算上又加入了冪”^”運算,該程序用壹二叉樹表示整個輸入的算術表達式:
(1)實現對結點的打印,便於結果分析;
(2)實現對結點的統計;
(3)實現中間結果的顯示,可以看打印的結點,驗證運算結果的正確與否。
四.概要設計
系統用到的抽象數據類型定義:
1.ADT?node_type{
數據對象V:壹個集合,該集合中的所有元素具有相同的特性
數據關系R:R={VR}
VR={<x,y>|P(x,y)^(x,y屬於V)}
基本操作:
(1) node_type(stringk);
操作結果:對結點進行初始化
}ADT?node_type
2.ADT?binary_tree{數據對象D:壹個集合,該集合中的所有元素具有相同的特性
數據關系R:若D為空,則為空樹。若D中僅含有壹個數據元素,則R為空集,否則R={H},H為如下二元關系:
(1) 在D中存在唯壹的稱為根的數據元素root,它在關系H中沒有前驅
(2) 除root以外,D中每個結點在關系H下有且僅有壹個前驅。
基本操作:
(1) print(node_type*r)CopyTimeTree(p,q);
操作結果:對結點進行打印
(2) evaluate(node_type*prt);
操作結果:對壹二叉樹進行計算
(3) counter();
操作結果:計算壹棵二叉樹中的結點個數
}ADT?binary_tree
系統中子程序及功能要求:1. ADT?node_type?build_node(stringx):建立壹個結點
2. addition(charOperatorA,charOperatorB):判斷兩操作符是否相等,若相等返回True
3. TakesPrecedence(char?OperatorA,char?OperatorB):判別符號的優先級。A>B,返回為TRUE
4. copy(ADT?node_type?*&r1,?ADT?node_type?*r2):拷貝整個二叉樹
5. isok(stringexp):驗證表達式是否輸入正確,若正確返回TRUE
五.測試分析(運行結果)
第壹個表達式:10-(-3)*(((21+3/5)*8/3)*(-2))
第二個表達式:-(32.7-3210.3)/((8.0+0.9)*8.9)+4.4
依次把運算符和操作數放入堆棧中,過程中依次把中間運算結果打印出來