内射老阿姨1区2区3区4区_久久精品人人做人人爽电影蜜月_久久国产精品亚洲77777_99精品又大又爽又粗少妇毛片

C語言數(shù)據(jù)結(jié)構(gòu)之簡易計(jì)算器-創(chuàng)新互聯(lián)

本文實(shí)例為大家分享了C語言簡易計(jì)算器的具體代碼,供大家參考,具體內(nèi)容如下

海拉爾ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!

主要解決了處理負(fù)數(shù)、小數(shù)等的基礎(chǔ)運(yùn)算操作,無圖形界面

#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<<"除數(shù)為零";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; //判斷多位數(shù)的頭數(shù)是否為零
 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<<"輸入錯(cuò)誤數(shù)字";
  }
  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<<"表達(dá)式錯(cuò)誤!";
  }
  else{
  tmpfuhao = stk.top();
  while(tmpfuhao!='('){
   if(stk.empty()){
   cout<<"表達(dá)式錯(cuò)誤!";
   }
   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<<"符號(hào)錯(cuò)誤!";
  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;
}

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)建站www.rwnh.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。

當(dāng)前文章:C語言數(shù)據(jù)結(jié)構(gòu)之簡易計(jì)算器-創(chuàng)新互聯(lián)
本文來源:http://www.rwnh.cn/article30/cchgpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、動(dòng)態(tài)網(wǎng)站、虛擬主機(jī)品牌網(wǎng)站制作、網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

微信小程序開發(fā)
安仁县| 偏关县| 乐平市| 隆德县| 乐亭县| 开封县| 桂林市| 盐津县| 道孚县| 庆城县| 浙江省| 漳州市| 丰城市| 汽车| 安溪县| 榆树市| 文水县| 余庆县| 永福县| 友谊县| 乐都县| 安义县| 报价| 博白县| 威海市| 华蓥市| 江永县| 霍林郭勒市| 离岛区| 京山县| 开原市| 临江市| 汪清县| 襄樊市| 永仁县| 鄂伦春自治旗| 河曲县| 榆树市| 泸西县| 嘉定区| 策勒县|