Ⅰ 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