当前位置:三九宝宝网 → 宝宝教育 → 教学论文 → 正文

用c语言编四则运算

更新:12-22 整理:39baobao.com
字体:

[C语言中关于运算符的优先级问题请进]left to right 优先级从上到下依次递减,最上面具有最高的优先级,逗号操作符具有最低的优先级。 所有的优先级中,只有三个优先级是从右至左结合的,它们是单目运算符,因此使 a = b...+阅读

用C++写的,用C的话,函数方面很繁琐... 实现不止是整数,小数也可以,但算式中不要有负数,结果中可以有. 2000字不够用,分两部分,前一部分一些全局变量和类的申明: #include #include #include #include #define MAX 100 //定义运算优先顺序数组,1表示优先,0表示相等,-1表示非优先,2表示表达式有误 int com_value[9][9]= { 1,1,-1,-1,-1,1,1,1,2, 1,1,-1,-1,-1,1,1,1,2, 1,1,1,1,-1,-1,1,1,2, 1,1,1,1,-1,-1,1,1,2, 1,1,1,1,2,-1,1,1,2, -1,-1,-1,-1,-1,-1,0,2,2, 1,1,1,1,1,2,1,1,2, -1,-1,-1,-1,-1,-1,2,0,2, 2,2,2,2,2,2,2,2,2 }; //堆栈类模板 template class stack { public: stack(){top=new type[MAX];}; ~stack(){}; type *top; public: void push(type e){*top=e;top++;} type pop(){top--;return*top;} type GetTop(){return *(top-1);} int GetTopValue(type &e) { if(e=='+')return 0;else if(e=='-')return 1; else if(e=='*')return 2;else if(e=='/')return 3; else if(e=='^')return 4;else if(e=='(')return 5; else if(e==')')return 6;else if(e=='=')return 7; else return 8; } int GetTopValue() { type temp=GetTop(); if(temp=='+')return 0;else if(temp=='-')return 1; else if(temp=='*')return 2;else if(temp=='/')return 3; else if(temp=='^')return 4;else if(temp=='(')return 5; else if(temp==')')return 6;else if(temp=='=')return 7; else return 8; } type calculate(char s) { type b=pop(),a=pop(); if(s=='+')return a+b; if(s=='-')return a-b; if(s=='*')return a*b; if(s=='/')return a/b; if(s=='^')return pow(a,b); } };

本文地址:https://www.39baobao.com/show/29_13330.html

以上内容来自互联网,请自行判断内容的正确性。若本站收录的信息无意侵犯了贵司版权,请联系我们,我们会及时处理和回复,谢谢.

以下为关联文档:

C语言运算符优先级问题注意一下&这个运算符 这个运算符有如下两个特点: 1.该运算符是左结合的(也就是说运算步骤是从左向右进行) 2.一旦当该运算符的左边的表达式的值为假(值为0)时,就认为整个&表达式的...

C语言中运算符优先级别的问题C++中 a++ a--符号的顺序是自右向左.即-a++=-(a++) 但是出现付值时.b=a++ b的值与等号右边的a的原值相等. 其次无论什么顺序都是自右向左(注意(a++)+(a++)+(a++) 这时的如果a的...

c语言的运算符优先级问题&两边的优先级是一样的即(i++ == 4)和(++j == 4 || k++ == 4)优先级一样 所以先执行i++ == 4 i++ 是后自加 所以i++ == 4不成立 对于 if(a&b) 有个截止特性,就是a为0 则 不会判断...

求c语言中各类运算符的优先级别c/C++的优先级 优先级 操作符 结合性 1 :: 左 2 . -&gt; [] () 左 3 ++ -- ~ ! - + & * () sizeof new delete castname_cast<type&gt; 单目操作符 右 4 .* -&gt;* 左 5 * / % 左...

C语言四则运算程序高手帮帮忙#include<stdio.h> #include<stdlib.h> #include<time.h> #include<string.h> int scan() { char s[100]; int i,t,z=0; do { z=0; gets(s); for(i=0;s[i]!='\0';i++) if(s[...

C语言四则运算程序高手帮帮忙!#include #include #include #include int scan() { char s[100]; int i,t,z=0; do { z=0; gets(s); for(i=0;s[i]!='\0';i++) if(s[i]'9') break; if(i>=strlen(s)) for(t=...

C语言问题四则运算程序我怀疑你代码的这段有问题: else if(ch == ')') { optr = MathOptr(ch); while(!Stack1Empty(&OptrStack) & Peek1(&OptrStack).stackprecedence >= optr.inputprecedence) //...

四则运算 c语言编程#include "stdio.h" #include "stdlib.h" #include "ctype.h" int n=0; char record[20]; float product(); float change(); float muli() { float summ; summ=product(); while(...

求c语言编写四则运算程序#include"stdafx.h" #include #include #include char token;/*global token variable*/ /*function prototypes for recursive calls*/ float exp(void); float term(void);...