bl双性强迫侵犯h_国产在线观看人成激情视频_蜜芽188_被诱拐的少孩全彩啪啪漫画

C語言數據結構之簡易計算器

本文實例為大家分享了C語言簡易計算器的具體代碼,供大家參考,具體內容如下

創新互聯公司于2013年創立,先為南芬等服務建站,南芬等地企業,進行企業商務咨詢服務。為南芬企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。

主要解決了處理負數、小數等的基礎運算操作,無圖形界面

#include <iostream>
#include <stack>
using namespace std;

class Calculator{
private:
 int Priority(char fuhao);
 double CalSuffix(string PostfixExp);

public:
 double Calculate(string InfixExp);

 string InfixToSuffix(string InfixExp);

};

double Calculator::CalSuffix(string PostfixExp){
 double tmpresult,ch2,ch3;
 double tmpnum,tmpxiaoshu=1;
 int i=0,tmpdashu;
 int isfu=0; ///
 stack<double> stk2;
 while(PostfixExp[i]!='\0'){
 isfu=0; ///
 if(PostfixExp[i]>=48&&PostfixExp[i]<=57){
  if(PostfixExp[i-1]=='-'){ /////
  isfu=1;
  }
  tmpxiaoshu=1;
  tmpdashu=10;
  tmpnum = PostfixExp[i]-48;
  while(PostfixExp[++i]>=48&&PostfixExp[i]<=57){
  tmpnum = tmpnum*tmpdashu+ (PostfixExp[i]-48);
  }
  i=i-1;
  if(PostfixExp[++i]=='.'){
  while(PostfixExp[++i]>=48&&PostfixExp[i]<=57){
   tmpxiaoshu=tmpxiaoshu*0.1;
   tmpnum = tmpnum + (PostfixExp[i]-48)*tmpxiaoshu;
  }
  i=i-1;
  }
  else{
  i=i-1;
  }
  if(isfu){ ////
  tmpnum=tmpnum*(-1);
  }
  stk2.push(tmpnum);
 }

 else if(PostfixExp[i]=='&'||PostfixExp[i]==' '){
 }

 else {
  if(PostfixExp[++i]>=48&&PostfixExp[i]<=57){
  i=i-1;
  }
  else {
  i=i-1;
  ch3 = stk2.top();
  stk2.pop();
  ch2 = stk2.top();
  stk2.pop();
  switch(PostfixExp[i]){
   case '+': tmpnum = ch2 + ch3; break;
   case '-': tmpnum = ch2 - ch3; break;
   case '*': tmpnum = ch2 * ch3; break;
   case '/': tmpnum = ch2 / ch3;
   if(ch3==0) cout<<"除數為零";break;
  }
  stk2.push(tmpnum);
  }
 }
 i++;
 }
 if(stk2.empty()!=1){
 tmpresult = stk2.top();
 stk2.pop();
 }
 return tmpresult;
}

double Calculator::Calculate(string InfixExp){
 double result;
 result = CalSuffix(InfixToSuffix(InfixExp));
 return result;
}

int Calculator::Priority(char fuhao){
 switch(fuhao){
 case '+':
 case '-': return 2;
 case '*':
 case '/': return 3;
 case '(':
 case ')': return 1;
 default:
  return 0;
 }
}
string Calculator::InfixToSuffix(string InfixExp){
 stack<char> stk;
 string PostfixExp = "   ";
 int i=0,j=0;
 char tmpfuhao;
 int flag = 0; //判斷多位數的頭數是否為零
 while(InfixExp[i]!='\0'){
 if(InfixExp[i]>=48&&InfixExp[i]<=57){
  flag = 0;
  PostfixExp[j++]='&';
  PostfixExp[j++]=InfixExp[i];
  if(InfixExp[i]=='0'){
  flag = 1;
  }
  while(InfixExp[++i]>=48&&InfixExp[i]<=57){
  if(flag==0)
   PostfixExp[j++]=InfixExp[i];
  else
   cout<<"輸入錯誤數字";
  }
  i=i-1;
  if(InfixExp[++i]=='.'){
  PostfixExp[j++]='.';
  while(InfixExp[++i]>=48&&InfixExp[i]<=57){
   PostfixExp[j++]=InfixExp[i];
  }
  i=i-1;
  }
  else{
  i=i-1;
  }
 }

 else if(InfixExp[i]=='('){
  stk.push(InfixExp[i]);
 }

 else if(InfixExp[i]==')'){
  if(stk.empty()){
  cout<<"表達式錯誤!";
  }
  else{
  tmpfuhao = stk.top();
  while(tmpfuhao!='('){
   if(stk.empty()){
   cout<<"表達式錯誤!";
   }
   else{
   PostfixExp[j++] = '&';
   PostfixExp[j++] = tmpfuhao;
   stk.pop();
   tmpfuhao = stk.top();
   }
  }
  stk.pop();
  }
 }

 else if(InfixExp[i]=='+'||InfixExp[i]=='-'||InfixExp[i]=='*'||InfixExp[i]=='/'){
  if(i==0||((InfixExp[--i]<48||InfixExp[i]>57)&&InfixExp[i]!=')')){
  i++;
  PostfixExp[j++]='&';
  PostfixExp[j++]='-';
  while(InfixExp[++i]>=48&&InfixExp[i]<=57){
   PostfixExp[j++]=InfixExp[i];
  }
  i=i-1;
  if(InfixExp[++i]=='.'){
   PostfixExp[j++]='.';
   while(InfixExp[++i]>=48&&InfixExp[i]<=57){
   PostfixExp[j++]=InfixExp[i];
   }
   i=i-1;
  }
  else{
   i=i-1;
  }
  }
  else{
  i++;
  if(stk.empty()){
  stk.push(InfixExp[i]);
  }
  else{
  tmpfuhao = stk.top();
  if(Priority(tmpfuhao)<Priority(InfixExp[i])){
   stk.push(InfixExp[i]);
  }
  else{
   while(Priority(tmpfuhao)>=Priority(InfixExp[i])){
   PostfixExp[j++] = '&';
   PostfixExp[j++] = tmpfuhao;
   stk.pop();
   if(stk.empty()!=1){
    tmpfuhao = stk.top();
   }
   else break;
   }
   stk.push(InfixExp[i]);
  }
  }
  }
 }
 else{
  cout<<"符號錯誤!";
  break;
 }
 i++;
 }

 while(!stk.empty()){
 tmpfuhao = stk.top();
 PostfixExp[j++] = '&';
 PostfixExp[j++] = tmpfuhao;
 stk.pop();
 }
 PostfixExp[j++] = '\0';
 return PostfixExp;
}

int main(int argc, const char * argv[]) {
 string a;
 Calculator a1;
 cin>>a;
 cout<<a1.Calculate(a)<<endl;
 cout<<a1.InfixToSuffix(a);
 return 0;
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持創新互聯。

標題名稱:C語言數據結構之簡易計算器
網站地址:http://vcdvsql.cn/article34/gghhpe.html

成都網站建設公司_創新互聯,為您提供企業網站制作移動網站建設外貿建站手機網站建設品牌網站建設小程序開發

廣告

聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯

成都定制網站建設