程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程綜合問答 >> c語言-用C語言編寫能進行四則運算的程序

c語言-用C語言編寫能進行四則運算的程序

編輯:編程綜合問答
用C語言編寫能進行四則運算的程序

我想用C語言編一個能進行簡單的四則運算的程序,應該怎麼寫,請詳細解釋一下輸入和計算部分

最佳回答:


實現四則運算是比較簡單的,你可以使用兩個棧,一個棧用來存貯操作的數字(0~n),一個棧用來存儲操作符(+-等).
你先把四則運算表達式存為一個字符串,然後遍歷各個字符,如果是數字,那就存起來,如果是操作符,那就與前一個操作符比較,看看優先級是不是比前一個大,如果是,那就存起來,如果不是,那就把數字棧裡的前兩個數按照前一個操作符進行運算。結果用一個變量保存起來。
下面是以前學棧時的一個代碼,是關於四則運算的,雖然是用C++寫的,但是希望對你能有幫助。
#include"stdafx.h"
#include"targetver.h"
#include
#include
#include
#include
using namespace std;
class list{
public:
int weight;
string str;
};
class node{
public:
string s;
node* next;
public:
node(){
next=nullptr;
}
};
class Stack{
public:
node* top;
int length;
public:
Stack(){
top=new node();
}
node getTop(){
return top->next;
}
void push(string str){//cout<<str<<endl;
node
n=new node();
n->s=str;
n->next=top->next;
top->next=n;
}
void pop(){
node* n=top->next;
if(n!=nullptr){
top->next=n->next;
delete n;
}else{
cout<<"UnderFlow!"< }
}
bool isEmpty(){
if(top->next==nullptr){
return true;
}else{
return false;
}
}
};
string remove(string s);
float calc(string o,string s1,string s2);
string FloatToString(float f);
int main(){
string str,str_c;
cin>>str;str_c=str;
Stack* n_stack=new Stack();
Stack* o_stack=new Stack();
list* lis=new list[4];
lis[0].str="+";lis[0].weight=1;
lis[1].str="-";lis[1].weight=1;
lis[2].str="*";lis[2].weight=2;
lis[3].str="/";lis[3].weight=2;
float sum=0;
for(int i=0;i if(str.at(i)=='('){
string s="";
for(;str.at(i)!=')';){
s=s+str.at(0);
}
}else if(str.at(i)>='0'&&str.at(i)<='9'){
string s="";
for(;i='0'&&str.at(i)<='9';i++){
s=s+str.at(i);
}i--;
n_stack->push(s);
}else{
string s="";
s=str.at(i)+s;
if(!o_stack->isEmpty()){
list curOperate;
list lastOperate;
for(int i=0;i if(o_stack->top->next->s==lis[i].str){
lastOperate=lis[i];
}
if(s==lis[i].str){
curOperate=lis[i];
}
}
if(curOperate.weight>lastOperate.weight){
o_stack->push(s);
}else{
float f=calc(lastOperate.str,n_stack->top->next->next->s,n_stack->top->next->s);
s=FloatToString(f);
n_stack->pop();
n_stack->pop();
o_stack->pop();
n_stack->push(s);
o_stack->push(curOperate.str);
}
}else{
o_stack->push(s);
}

    }
}cout<<o_stack->top->next->s<<"--"<<n_stack->top->next->next->s<<"--"<<n_stack->top->next->s<<endl;
sum=sum+calc(o_stack->top->next->s,n_stack->top->next->next->s,n_stack->top->next->s);
cout<<endl<<"the o_stack is followed:"<<endl;
for(node* p=o_stack->top->next;p!=nullptr;p=p->next){
    cout<<p->s;
}cout<<endl<<"the n_stack is followed:"<<endl;
for(node* p=n_stack->top->next;p!=nullptr;p=p->next){
    cout<<p->s;
}
cout<<endl<<sum<<endl;
int a;cin>>a;
return 0;

}
string FloatToString(float f){
string s="";int a=f;
for(;a!=0;){
int c=a%10;
a=a/10;
s=(char)(c+'0')+s;
}
return s;
}
float calc(string o,string s1,string s2){
int m=0,n=0;
for(int i=0;i m=m+(s1.at(i)-'0')*pow(10,s1.length()-i-1);
}
for(int i=0;i n=n+(s2.at(i)-'0')*pow(10,s2.length()-i-1);
}
if(o=="+"){
return m+n;
}else if(o=="-"){
return m-n;
}else if(o=="*"){
return m*n;
}else if(o=="/"){
return (m*1.0)/n;
}
return -1;
}
string remove(string s){
string s1="";
if(s.length()>2){
return s.substr(1,s.length()-1);
}else if(s.length()==2){
return s.at(1)+s1;
}else{
return "";
}
}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved