Ⅰ c语言计算表达式的值
逗号运算符
左至右计算
取
表达式
值作
整
语句
值
先算a=3,
a赋值
3
算b=4,
b赋值
4
计算c=a+b
实际
c=3+4
c
值
7
终整条语句
值
c
值7.
Ⅱ 哪个帮我做一个中缀表达式转换成后缀表达式,要C语言,要带括号哦,我写一下午了没写出来,呜呜~~
参考这个:
运行环境,VC , BCB
#include<iostream>
#include <vector>
using namespace std;
/*********************** 栈操作 ************************/
/*********************************************************/
const int MAXSIZE =100;
template <class ElemType> class MyStack
{
public:
ElemType data[MAXSIZE];
int top;
public:
void init(); // 初始化栈
bool empty(); // 判断栈是否为空
ElemType gettop(); // 读取栈顶元素(不出栈)
void push(ElemType x); // 进栈
ElemType pop(); // 出栈
};
template<class T> void MyStack<T>::init()
{
this->top = 0;
}
template<class T> bool MyStack<T>::empty()
{
return this->top == 0? true : false;
}
template<class T> T MyStack<T>::gettop()
{
if(empty())
{
cout << "栈为空!\n";
exit(1);
}
return this->data[this->top-1];
}
template<class T> void MyStack<T>::push(T x)
{
if(this->top == MAXSIZE)
{
cout << "栈已满!\n";
exit(1);
}
this->data[this->top] =x;
this->top ++;
}
template<class T> T MyStack<T>::pop()
{
if(this->empty())
{
cout << "栈为空! \n";
exit(1);
}
T e =this->data[this->top-1];
this->top --;
return e;
}
/*********************** 函数声明 ************************/
bool isoperator(char op); // 判断是否为运算符
int priority(char op); // 求运算符优先级
void postfix(char pre[] , char post[],int &n); // 把中缀表达式转换为后缀表达式
double read_number(char str[],int *i); // 将数字字符串转变成相应的数字
double postfix_value(char post[]); // 由后缀表达式字符串计算相应的中值表达式的值
/*********************************************************/
double read_number(char str[],int *i)
{
double x=0.0;
int k = 0;
while(str[*i] >='0' && str[*i]<='9') // 处理整数部分
{
x = x*10+(str[*i]-'0');
(*i)++;
}
if(str[*i]=='.') // 处理小数部分
{
(*i)++;
while(str[*i] >= '0'&&str[*i] <='9')
{
x = x * 10 + (str[*i]-'0');
(*i)++;
k++;
}
}
while(k!=0)
{
x /= 10.0;
k--;
}
return x;
}
// 把中缀表达式转换为后缀表达式,返回后缀表达式的长度(包括空格)
void postfix(char pre[] ,char post[],int &n)
{
int i = 0 ,j=0;
MyStack<char> stack;
stack.init(); // 初始化存储操作符的栈
stack.push('#'); // 首先把结束标志‘#’放入栈底
while(pre[i]!='#')
{
if((pre[i]>='0' && pre[i] <='9')||pre[i] =='.') // 遇到数字和小数点直接写入后缀表达式
{
post[j++] = pre[i];
n++;
}
else if (pre[i]=='(') // 遇到“(”不用比较直接入栈
stack.push(pre[i]);
else if(pre[i] ==')') // 遇到右括号将其对应左括号后的操作符(操作符栈中的)全部写入后缀表达式
{
while(stack.gettop()!='(')
{
post[j++] = stack.pop();
n++;
}
stack.pop(); // 将“(”出栈,后缀表达式中不含小括号
}
else if (isoperator(pre[i]))
{
post[j++] = ' '; // 用空格分开操作数(
n++;
while(priority(pre[i]) <= priority(stack.gettop()))
{
// 当前的操作符小于等于栈顶操作符的优先级时,将栈顶操作符写入到后缀表达式,重复此过程
post[j++] = stack.pop();
n++;
}
stack.push(pre[i]); // 当前操作符优先级大于栈顶操作符的优先级,将该操作符入栈 }
i++;
}
while(stack.top) // 将所有还没有出栈的操作符加入后缀表达式
{
post[j++] = stack.pop();
n++;
}
}
// 判断是否为运算符
bool isoperator(char op)
{
switch(op)
{
case '+':
case '-':
case '*':
case '/':
return 1;
default :
return 0;
}
}
// 求运算符优先级
int priority(char op)
{
switch(op)
{
case '#':
return -1;
case '(':
return 0;
case '+':
case '-':
return 1;
case '*':
case '/':
return 2;
default :
return -1;
}
return -1;
}
// 将数字字符串转变成相应的数字
double readnumber(char str[],int *i)
{
double x=0.0;
int k = 0;
while(str[*i] >='0' && str[*i]<='9') // 处理整数部分
{
x = x*10+(str[*i]-'0');
(*i)++;
}
if(str[*i]=='.') // 处理小数部分
{
(*i)++;
while(str[*i] >= '0'&&str[*i] <='9')
{
x = x * 10 + (str[*i]-'0');
(*i)++;
k++;
}
}
while(k!=0)
{
x /= 10.0;
k--;
}
return x;}
// 由后缀表达式字符串计算相应的中值表达式的值
double postfix_value(char post[])
{
MyStack<double> stack; // 操作数栈
stack.init();
int i=0 ;double x1,x2;
while(post[i] !='#')
{
if(post[i] >='0' && post[i] <='9')
stack.push(read_number(post,&i));
else if(post[i] == ' ')
i++;
else if (post[i] =='+')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1+x2);
i++;
}
else if (post[i] =='-')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1-x2);
i++;
}
else if (post[i] =='*')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1*x2);
i++;
}
else if (post[i] =='/')
{
x2 = stack.pop();
x1 = stack.pop();
stack.push(x1/x2);
i++;
}
}
return stack.gettop();
}
// main()函数
void main(){
char pre[] ="2*((6-4)+8/4)#";
char post[100] ;
cout <<"中缀表达式为:"<< pre << endl;
int n =0; // 返回后缀表达式的长度
postfix(pre,post,n);
cout <<"后缀表达式为:";
for( int i =0 ;i < n ;i++)
cout << post[i] ;
cout << "\n由后缀表达式计算出的数值结果: ";cout << postfix_value(post) << endl;
system("pause");}
Ⅲ 在c语言中,如何将中缀表达式转换成后缀表达式呢
(A==0&&B!=0)||(B==0&&A!=0)
((x>10&&x<100)||x<0)&&x!=-2.0
Ⅳ 用C语言实现中缀表达式到后缀表达式的转换 求改正!
main函数中:
printf("%2c",queueempty(q));
改为
printf("%2c",q->data[q->front++]);
测试:
1+2*3#
1 2 3 * +
Ⅳ 如何把算术表达式转化为后缀表达式
表达式的三种形式:
中缀表达式:运算符放在两个运算对象中间,如:(2+1)*3
后缀表达式:不包含括号,运算符放在两个运算对象的后面,所有的计算按运算符出现的顺序,严格从左向右进行(不再考虑运算符的优先规则,如:2 1 + 3 *
前缀表达式:同后缀表达式一样,不包含括号,运算符放在两个运算对象的前面,如:* + 2 1 3
Ⅵ 试编写程序,a.将中缀表达式计算转换成后缀表达式。
#include<stdio.h>
#define MaxSize 99
void trans(char str[],char exp[]) /*将算术表达式转换成后追表达式*/
{
struct
{
char data[MaxSize];
int top; /*top为栈顶*/
}op; /*定义一个含data和top的结构体*/
char ch;
int i=0,t=0;
op.top=-1;
ch=str[i]; /*将str的每一个数转换成ch*/
i++;
while(ch!='\0') /*ch对应不同的符号的时候对应的转换情况*/
{
switch(ch)
{
case'(': /*当是(的时候,将此括号存入栈tp*/
op.top++;op.data[op.top]=ch;
break;
case')':
while(op.data[op.top]!='(')
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
op.top--;
break;
case'+':
case'-':
while(op.top!=-1&&op.data[op.top]!='(')
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
op.top++;
op.data[op.top]=ch;
break;
case'*':
case'/':
while(op.top=='/'||op.top=='*')
{
exp[t]=op.data[op.top];
op.top--;
t++;
}
op.top++;
op.data[op.top]=ch;
break;
case' ':
break;
default:
while(ch>='0'&&ch<='9')
{
exp[t]=ch;t++;
ch=str[i];i++;
}
i--;
exp[t]='#';
t++;
}
ch=str[i];
i++;
}
while(op.top!=-1)
{
exp[t]=op.data[op.top];
t++;
op.top--;
}
exp[t]='\0';
}
float compvalue(char exp[])
{
struct
{
float data[MaxSize];
int top;
}st;
float d;
char ch;
int t=0;
st.top=-1;
ch=exp[t];
t++;
while(ch!='\0')
{
switch(ch)
{
case'+':
st.data[st.top-1]=st.data[st.top-1]+st.data[st.top];
st.top--;
break;
case'-':
st.data[st.top-1]=st.data[st.top-1]-st.data[st.top];
st.top--;
break;
case'*':
st.data[st.top-1]=st.data[st.top-1]-st.data[st.top];
st.top--;
break;
case'/':
if(st.data[st.top]!=0)
st.data[st.top-1]=st.data[st.top-1]-st.data[st.top];
else
{
printf("\n\t除0是错误的");
}
st.top--;
break;
default:;
d=0;
while(ch>='0'&&ch<='9')
{
d=10*d+ch-'0';
ch=exp[t];
t++;
}
st.top++;
st.data[st.top]=d;
}
ch=exp[t];
t++;
}
return st.data[st.top];
}
void main() /*可以提到前面去*/
{
char str[MaxSize],exps[MaxSize]; /*str为算术表达式,exps为后缀表达式*/
printf("请输入一个求值表达式\n");
printf("表达式:");
gets(str); /*输入一个算术表达式*/
printf("原表达式是:%s\n",str);
trans(str,exps); /*将算术表达式转换成后追表达式*/
printf("后缀表达式:%s\n",exps);
printf("计算结果:%g\n",compvalue(exps));/*通过后缀表达式来求值*/
}
Ⅶ C语言代码将中缀表达式转换为后缀表达式,参数为字符型数组的中缀表达式,返回字符型数组的后缀表达式,
你自己改为C吧
#include<iostream.h>
const int MAX=40;
void main(void){
char infix[MAX]={'#'};
char oprator[MAX]={'@','#'};
int opr=1;
char postfix[12]={'#'};
int post=0;
int i,j,cnt=0,cntl;
char c;
//输入表达式,以等号结束
cin.get(c);
while(c!='='){
infix[cnt]=c;
cnt++;
cin.get(c);
}
cntl=cnt;
for(i=0;i<cnt;i++){
switch(infix[i]){
//左括号就直接入栈
case '(':
cntl=cntl-2;
oprator[opr]=infix[i];
opr++;
break;
//右括号则先退栈,直到遇见第一个左括号
case ')':
for(j=opr-1;j>0;j--){
if(oprator[j]!='('){
postfix[post]=oprator[j];
oprator[j]='#';
post++;
}
else{
oprator[j] = '#';
break;
}
}
opr=j;
break;
case '*':
case '/':
//如果前一个运算符为*或/,则先退栈,再入栈,否则直接入栈
if (oprator[opr] == '*' || oprator[opr] == '/') {
postfix[post] = oprator[opr];
oprator[opr]='#';
post++;
}
oprator[opr] = infix[i];
opr++;
break;
case '+' :
case '-' :
//如果上一个运算符不是左括号也不是栈顶,则先退栈再入栈
if (oprator[opr-1] != '(' && oprator[opr-1] != '@') {
postfix[post] = oprator[opr];
oprator[opr]='#';
}
oprator[opr] = infix[i];
opr++;
break;
default :
//如果是数字则直接进入后缀表达式数组
postfix[post] = infix[i];
post++;
break;
}
}
//如果扫描完成,则退栈
for(j=opr-1;j>0;j--){
if(oprator[j]!='@'){
postfix[post]=oprator[j];
oprator[j]='#';
}
else
break;
Ⅷ C语言中缀转后缀表达式问题
为啥要出栈,继续进栈。
5*4*3 变成后缀表达式就是 5 4 * 3 *。里面就是有2个乘号哦,完全没问题。
你应该仔细看看后缀表达式的定义。
http://ke..com/subview/339689/339689.htm