C語言數(shù)據(jù)結(jié)構(gòu)之簡易計算器
本文實例為大家分享了C語言簡易計算器的具體代碼,供大家參考,具體內(nèi)容如下
主要解決了處理負(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,ch1,ch2;
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;
ch2 = stk2.top();
stk2.pop();
ch1 = stk2.top();
stk2.pop();
switch(PostfixExp[i]){
case '+': tmpnum = ch1 + ch2; break;
case '-': tmpnum = ch1 - ch2; break;
case '*': tmpnum = ch1 * ch2; break;
case '/': tmpnum = ch1 / ch2;
if(ch2==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<<"輸入錯誤數(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á)式錯誤!";
}
else{
tmpfuhao = stk.top();
while(tmpfuhao!='('){
if(stk.empty()){
cout<<"表達(dá)式錯誤!";
}
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;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:詳解C語言用malloc函數(shù)申請二維動態(tài)數(shù)組的實例
欄 目:C語言
本文標(biāo)題:C語言數(shù)據(jù)結(jié)構(gòu)之簡易計算器
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1057.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對數(shù)怎么表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機(jī)閱讀
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-10C#中split用法實例總結(jié)
- 04-02jquery與jsp,用jquery
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10使用C語言求解撲克牌的順子及n個骰子


