加/减/乘/除四则混合运算(C 语言)

Posted btxz

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了加/减/乘/除四则混合运算(C 语言)相关的知识,希望对你有一定的参考价值。

   逆波兰表达式(也称为后缀表达式) C 语言简单实现,(也称为后缀表达式)

  本示例旨在展示逆波兰表达式原理,作简单的混合运算,不作容错处理也不保证结果,若混合运算字符串中有负数等,自行调试解决 

  列如计算: 20.5+(100-(3+2)*8)/(8-5) - 10

  后缀表达式为:20.5 100 3 2 + 8 * - 8 5 - / 10 - + 

  C  语言代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define CNT 1000

struct node{
   float num;
   char op;
};

struct node datas[CNT];  //后缀表达式栈
int data_top = -1;       //栈顶索引

char ops[CNT];          //操作符栈
int op_top = -1;        //栈顶索引

char nums[100];         //数字字符串
int num_top = -1;       //栈顶索引
//数字入栈
void push_num(){
  if(num_top > -1){
     datas[++data_top].num = atof(nums);
     datas[data_top].op = 0;
     num_top = -1;
     memset(nums, 0, sizeof(nums));  
  }
}
//中缀转后缀
void mtoe(const char* str){
  char *tmp;
  tmp = (char*)str;
  while(*tmp != ‘‘){
    char ch = *tmp;
    ++tmp;
    if(ch == ‘ ‘) continue;

    if(ch == ‘+‘ || ch == ‘-‘ || ch == ‘*‘ || ch == ‘/‘ || ch == ‘(‘){
      push_num();
      if(op_top > -1){
        char op = ops[op_top];
        if( (op == ‘*‘ || op == ‘/‘)&&(ch == ‘+‘ || ch == ‘-‘) ){// 乘/除优先于加/减
          datas[++data_top].op = op;
          --op_top;    
        }
      }
      ops[++op_top] = ch;
    }else if(ch == ‘)‘){
      push_num();
      while(ops[op_top] != ‘(‘){
        datas[++data_top].op = ops[op_top];
        --op_top;
      }
      --op_top; 
    }else{  
      nums[++num_top] = ch;
    }
  }// end of while *tmp 
  push_num();//最后的数据入栈
  while(op_top > -1){//最后的操作符入栈
     datas[++data_top].op = ops[op_top];
     --op_top;   
  } 
    
}
//计算值
float calculating(){
  if(data_top < 0) return 0; 
  float stack[CNT] = {0};
  int top = -1;
  int i = 0;
  while(i <= data_top){
    char op = datas[i].op;
    if(op == 0){
       stack[++top] = datas[i].num;
    }else{
      float a = stack[top -1];
      float b = stack[top];
      --top;
      float c = 0;
      if(op == ‘+‘) c = a + b;
      else if(op == ‘-‘) c = a -b;
      else if(op == ‘*‘) c = a * b;
      else if(op == ‘/‘) c = a / b;
      stack[top] = c;
    }
   ++i;
  }
  if(top < 0) return 0;
  else return stack[top]; 
}

int main(int argc, char *argv[]){
  char *parms = "20.5+(100-(3+2)*8)/(8-5) - 10";
memset(datas, 0, sizeof(datas));
memset(ops, 0, sizeof(ops)); data_top = -1; op_top = -1; num_top = -1; mtoe(parms); printf("%s = ",parms); printf("%f ",calculating()); int i = 0; for(i = 0; i <= data_top; i++){ if(datas[i].op) printf("%c ", datas[i].op); else printf("%f ",datas[i].num); } printf(" "); return 0; }

  

以上是关于加/减/乘/除四则混合运算(C 语言)的主要内容,如果未能解决你的问题,请参考以下文章

用C语言编写一个计算器程序,实现加,减,乘,除,求平方根(正数),倒数等功能.

结对编程练习_四则运算(第一周)

201571030331/201542010208益西卓嘎

作业二: 编写自动生成小学四则运算题目的程序

20165215 结对编程——四则运算第一周

四则运算