琉璃晴久 2015-10-01 05:19 采纳率: 13.3%
浏览 5216
已采纳

用C语言编写能进行四则运算的程序

我想用C语言编一个能进行简单的四则运算的程序,应该怎么写,请详细解释一下输入和计算部分

  • 写回答

2条回答 默认 最新

  • DukeSeraph 2015-10-01 07:48
    关注

    实现四则运算是比较简单的,你可以使用两个栈,一个栈用来存贮操作的数字(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条)

报告相同问题?

悬赏问题

  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿