算術表達式求值數據結構c語言,表達式求值數據結構 c語言

本文目錄一覽:

C語言編程(數據結構):表達式求值

/*在TC2 和 VC6下都可以順利運行。

做了一個下午。一定要用我這個噢。

有簡單的輸入錯誤檢測。有完整的說明和

注釋*/

#includestdio.h /*庫文件包含*/

#includestring.h /*用於字元串操作*/

#includestdlib.h /*用於exit函數*/

/**************************************************************************

int check(char *c)

輸入參數:

char *c: 輸入的字元串

返回參數:

0:字元串中有不符合規定的字元

1: 字元串字元符合規定,沒有不符合規定的字元.

功能:

檢查字元串中有否除了 0-9, +,-,*,/,(,),之外的其他字元,

如果有,則返回0, 表示出現錯誤。

若沒有,則返回1,表式字元串符合規定。

**************************************************************************/

int check(char *c)

{

int k=0;

while(*c!=’\0′)

{

if((*c=’0′ *c=’9′) || *c==’+’ ||

*c==’-‘ || *c==’*’ || *c==’/’ ||

*c==’.’ || *c=='(‘ || *c==’)’ )

{

}

else

{

printf(“input error, there have the char not the math expression char!\n”);

return 0;

}

if(*c=='(‘)

k++;

else if(*c==’)’)

k–;

c++;

}

if(k!=0)

{

printf(“input error, there is not have correct bracket ‘()’!\n”);

return 0;

}

return 1;

}

/**************************************************************************

void move(char *f, double *s,int p)

輸入參數:

char *f : 運算符數組

double *s: 數值數組

int p: 當前運算符數組位置。

返回參數:

功能:

將當前已經完成運算的運算符消去,同時將數值數組的位置調整以進行下一次運算。

傳入值p若為3

則當前符號的數組位置為3.

f[3]=f[3+1]…….f[len-2]=f[len-1] f[len-1]=’\0′;

s[i]=s[i+1]…….s[len-1]=s[len] 因為數值比運算符多一個。

***************************************************************************/

void move(char *f, double *s,int p)

{

int i=0,len=strlen(f);

for(i=p; ilen; i++) /*將已經運算過的符號,空出來的位置用後面的符號來填充,*/

{ /*即把乘和除號的位置用後面的加和減號填充*/

f[i]=f[i+1];

s[i]=s[i+1];

}

s[i]=s[i+1];

f[len-1]=’\0′;

}

/**************************************************************************

double convnum(char *c)

輸入參數:

char *c :由數字和小數點組成的字元,用以轉換成double型的數值。

返回參數:

num:返迴轉換好的值。

功能:

將輸入的字元串先將其小數點以前的部分複製到temp[]數組中,

若有小數點,則將小數點之後的數值,也就是小數部分先進行計算,值存入num中

計算完成後,再對整數部分進行計算,值加上小數部分的值,存入num中。

***************************************************************************/

double convnum(char *c)

{

double num=0.0;

double a=1.0;

int i=0,p=0,len=0;

char temp[100];

int tempi=0;

int start=0;

int f=1; /*正負符號指示器,若為1則為正數,為-1,此數為負數*/

len=strlen©;

if(c[0]==’-‘)

{

start=1;

f=-1;

}

for(i=start; ilen; i++)

{

if(c[i]==’.’)

{

p=i;

break;

}

temp[tempi++]=c[i]; /*將整數部分複製到temp[]中*/

}

temp[tempi]=’\0′;

if(p!=0)

{

for(i=p+1;ilen;i++) /*將小數部分計算出來*/

{

if(c[i]==’.’) /*如果有多餘的小數點,則表示輸入錯誤*/

{

printf(“there is more that one dot ‘.’ in number!error!\n”);

exit(0);

}

a=a*0.1;

num+=(a*(c[i]-48));

}

}

a=1.0;

len=strlen(temp); /*計算整數部分*/

for(i=len-1;i=0; i–)

{

num=num+(a*(temp[i]-48));

a*=10;

}

num=num*f;

return num;

}

/**************************************************************************

double good(char *c)

輸入參數:

char *c :即將進行運算的字元串型數學表達式。如3.5+(2*3/5)

返回參數:

s[0]:計算結果將放入s[0]中

功能:

將輸入的字元串中的數字分別調用convnum(char *c)函數進行數值變換,再將其依

次存入doulbe s[i]中,將加減乘除運算符依次存入字元串符號數組 char f[i]中,

然後如果遇到括弧,則將括弧內的字元串存入另一字元數組中,然後用此

good(char *c) 遞歸函數進行遞歸運算。 然後根據先乘除,後加減的順序對已

存入數組的數值根 據存入字元串符號數組的運算符進行運算。結果存入s[0]中。

返回最終結果。

***************************************************************************/

double good(char *c) /*可遞歸函數*/

{ /*取得數值字元串,並調用convnum轉換成double*/

char g[100],number[30]; /*g,保存當前的表達式串,number保存一個數的所有字元*/

char f[80]; /*保存所有的符號的堆棧*/

int fi=0; /*保存符號的位置指針*/

double s[80]; /*保存當前所有的數的一個堆棧*/

int si=0; /*保存數字位置指針*/

int k=0; /* 若k=1則表示有一對括弧*/

int num=0,i=0; /*num保存新括弧內的字元數,i 保存number里的字元位置*/

int cc=0; /*乘除符號數量*/

int jj=0; /*加減符號數量*/

while(*c!=’\0′)/*當p==1 和k==0時,表示已經把括弧里的內容全部複製到g[100]中了*/

{

k=0;

num=0;

switch(*c)

{

case ‘+’: /*當前字元為+-乘除時則表示*/

case ‘-‘:

case ‘*’:

case’/’:

f[fi++]=*c;

if(*c==’*’ || *c==’/’)

cc++;

else

jj++;

if(*(c-1)!=’)’)

{

number[i]=’\0′;

i=0;/*完成一個數字的複製,其位置指針i=0*/

s[si++]=convnum(number);

}

break;

case'(‘: /*有括弧,則將當前括弧作用範圍內的全部字元保存,作為*/

k++; /*一個新的字元表達式進行遞歸調用good函數計算。*/

while(k0)

{

c++;

g[num]=*c;

num++;

if(*c==’)’)

{

k–;

}

else if(*c=='(‘)

{

k++;

}

}

g[num-1]=’\0′;

num=0;/*完成一個括弧內容的複製,其位置指針num=0*/

s[si++]=good(g);

break;

default:

number[i++]=*c;

if(*(c+1)==’\0′)

{ number[i]=’\0′;

s[si++]=convnum(number);

}

break;

}

c++;

}

f[fi]=’\0′;

i=0;

while(cc0)

{

switch(f[i])

{

case ‘*’: cc–;

s[i+1]=s[i]*s[i+1];

move(f,s,i);

break;

case ‘/’: cc–;

s[i+1]=s[i]/(float)s[i+1];

move(f,s,i);

break;

default:

i++;

break;

}

}

i=0;

while(jj0)

{

switch(f[i])

{

case ‘+’: s[i+1]=s[i]+s[i+1];

jj–;

move(f,s,i);

break;

case ‘-‘: s[i+1]=s[i]-s[i+1];

jj–;

move(f,s,i);

break;

default:

printf(“operator error!”);

break;

}

}

return s[0];

}

void main()

{

char str[100];

double sum=0;

int p=1;

while(1)

{

printf(“enter expression: enter ‘exit’ end of program\n”);

scanf(“%s”,str);

p=strcmp(str,”exit”);

if(p==0)

break;

p=check(str);

if(p==0)

continue;

sum=good(str);

printf(“%s=%f”,str,sum);

printf(“\n”);

}

printf(“good bye!\n”);

}

例:

enter expression: enter ‘exit’ end of program

3.5+(12.3*15+8-(3/2+1))*2+(3.2*3-5)/6(輸入)

3.5+(12.3*15+8-(3/2+1))*2+(3.2*3-5)/6=384.266667

enter expression: enter ‘exit’ end of program

china(輸入)

input error, there have the char not the math expression char!

enter expression: enter ‘exit’ end of program

exit(輸入)

good bye!

用C語言編寫程序「算術表達式求值」

#include stdio.h

#include math.h

enum state

;

int ctoi( char c)

bool isNum( char a)

bool isOp(char op)

{

switch(op)

{

case ‘+’:

return true;

break;

case ‘-‘:

return true;

break;

case ‘*’:

return true;

break;

case ‘/’:

return true;

break;

default:

return false;

break;

}

}

bool isDot(char dot)

int checkString( char str[], double *a, double * b, char* op, int num)

{

enum state s = BEGIN;

int a_i = 0;

int b_i = 0;

double num1 = 0;

double num2 = 0;

int pointNum = 0;

for( int i = 0; i num; ++i)

{

if(str[i] == ‘ ‘)continue;

switch(s)

{

case BEGIN:

if(isNum(str[i]))

elses = ERROR;

break;

case P2:

if(isNum(str[i]))

else if(isDot(str[i]))

{

s = P3;

}

else if(isOp(str[i]))

{

*op = str[i];

s = P5;

}

else

s = ERROR;

break;

case P3:

if(isNum(str[i]))

{

num1 = num1 + ctoi(str[i]) * pow(0.1, ++pointNum) ;

s = P4;

}

else

s = ERROR;

break;

case P4:

if(isNum(str[i]))

{

num1 = num1 + ctoi(str[i]) * pow(0.1, ++pointNum);

s = P4;

}

else if(isOp(str[i]))

{

*op = str[i];

s = P5;

}

else

s = ERROR;

break;

case P5:

if(isNum(str[i]))

{

num2 = num2 * 10 + ctoi(str[i]);

s = P6;

}

else

s = ERROR;

break;

case P6:

pointNum = 0;

if(isNum(str[i]))

{

num2 = num2 * 10 + ctoi(str[i]);

s = P6;

}

else if(isDot(str[i]))

{

s = P7;

}

else

s = END;

break;

case P7:

if(isNum(str[i]))

{

num2 = num2 + ctoi(str[i]) * pow(0.1, ++pointNum);

s = P8;

}

else

s = END;

break;

case 8:

if(isNum(str[i]))

{

num2 = num2 + ctoi(str[i]) * pow(0.1, ++pointNum);

s = P8;

}

else if(isOp(str[i]))

{

s = END;

}

else

s = END;

break;

case ERROR:

printf(“express error. \n”);

break;

}

if (s == END || s == ERROR)

break;

}

if(s==END)

else

}

int main()

{

char op;

double a;

double b;

char string[128] = ;

scanf(“%s”, string);

printf(“the expression you input is : %s. \n”, string);

getchar();

if (-1 == checkString(string, a, b, op, 128))

{

printf(“error occur while checking expression. Be sure no space in your expression when input\n”);

getchar();

return 0;

}

double result;

switch(op)

{

case ‘+’:

result = a + b;

break;

case ‘-‘:

result = a – b;

break;

case ‘*’:

result = a * b;

break;

case ‘/’:

if(b != 0)

result = a / b;

else

{

printf(” error! %d/%d”, a, b);

return -1;

}

break;

default:

printf(“undefined expression.\n”);

break;

}

printf(“%f %c %f = %f\n”, a, op, b, result);

return 0;

}

C語言表達式求值

//表達式求值

//By:jimly

//10/10/2009

//例如:輸入2+2(4-6*3)=

//以”=”結束,然後回車即出結果

#include stdio.h

#include conio.h

#include windows.h

#include assert.h

typedef float ElemType;

typedef struct Stack

{

ElemType *base; // 棧基址

ElemType *top; // 棧頂

int stacksize; // 棧存儲空間的尺寸

} SqStack;

/*————————————————————

// 棧的基本操作

————————————————————*/

bool InitStack(SqStack *S);

bool InitStack(SqStack *S);

void DestroyStack(SqStack *S);

bool StackEmpty(SqStack S);

int StackLength(SqStack S);

ElemType GetTop(SqStack S, ElemType *e);

void StackTraverse(SqStack S, void (*fp)(ElemType));

bool Push(SqStack *S, ElemType e);

bool Pop(SqStack *S, ElemType *e);

/*————————————————————

// 表達式求值的操作函數定義

————————————————————*/

char Precede(char A1,char A2);

ElemType Operate(ElemType a,ElemType theta,ElemType b);

bool In(char c,char op[]);

ElemType EvaluateExpression();

void Menu();//////////////////////////////////////////////

// Eval_exdivssion.cpp 表達式求值實現函數 //

//////////////////////////////////////////////

/*————————————————————

操作目的: 判定運算符棧的棧頂運算符A1和讀入的運算符A2之間優先關係的函數

初始條件: 無

操作結果: 判斷出優先關係

函數參數:

char A1 運算符

char A2 運算符

返回值:

char 大小關係

————————————————————*/

char Precede(char A1,char A2)

{

if (A1 == ‘+’ || A1 == ‘-‘)

{

if (A2 == ‘+’ || A2 == ‘-‘ || A2 == ‘)’ || A2 == ‘=’)

{

return ”;

}

else

return ”;

}

if (A1 == ‘*’ || A1 == ‘/’)

{

if (A2 == ‘(‘)

{

return ”;

}

else

return ”;

}

if (A1 == ‘(‘)

{

if (A2 == ‘)’)

{

return ‘=’;

}

if (A2 == ‘=’)

{

return ‘E’;

}

else

return ”;

}

if (A1 == ‘)’)

{

if (A2 == ‘(‘)

{

return ‘E’;

}

if (A2 == ‘=’)

{

return ‘E’;

}

else

return ”;

}

if (A1 == ‘=’)

{

if (A2 == ‘=’)

{

return ‘=’;

}

else

return ”;

}

else

return ‘=’;

}

/*————————————————————

操作目的: 二元運算a與b的函數

初始條件: 無

操作結果: 返回運算結果

函數參數:

ElemType a 操作數

ElemType theta 操作符

ElemType b 操作數

返回值:

ElemType 運算結果

————————————————————*/

ElemType Operate(ElemType a,ElemType theta,ElemType b)

{

switch(char(theta))

{

case ‘+’:

return a += b;

break;

case ‘-‘:

return a -= b;

break;

case ‘*’:

return a *= b;

break;

case ‘/’:

if(b==0)

{

printf(“除數不能為0!!\n”);

exit(0);

}

return a /= b;

break;

} return 0;

}

/*————————————————————

操作目的: 判斷字元c是否屬於運算符集合op

初始條件: 無

操作結果: 返回判斷結果

函數參數:

char c 要判斷的字元

char op[] 運算符集合

返回值:

bool 屬於返回true 否則返回false

————————————————————*/

bool In(char c,char op[])

{

for (int i = 0;i7;i++)

{

if (op[i] == c)

return true;

}

return false;

}

/*————————————————————

操作目的: 算術表達式求值的運算符優先演算法

初始條件: 無

操作結果: 返回表達式的值

函數參數:

返回值:

ElemType 運算結果

————————————————————*/

ElemType EvaluateExpression()

{

SqStack OPTR; //運算符棧

SqStack OPND; //運算數棧

char Ct = ‘=’; //判斷是否結束的標識

int i = 0,j = 1;

ElemType e = 0,t = 0,c;

char op[7] = {‘+’,’-‘,’*’,’/’,'(‘,’)’,’=’}; InitStack(OPTR); //初始化

Push(OPTR,Ct);

InitStack(OPND); //初始化 c = (float)getchar();

while (c!=’=’ || GetTop(OPTR,e)!=’=’)

{

if (!In((char)c,op)) //不是運算e符進棧

{

while(!In((char)c,op)) //可以是幾位數

{

t = t*10+(c-48);

c = (float)getchar();

}

Push(OPND,t);

t = 0;

} else

{

switch (Precede((char)GetTop(OPTR,e),(char)c))

{

case ”://棧頂元素優先權低

Push(OPTR,c);

c = (float)getchar();

break;

case ‘=’://脫括弧並接受下個字元

ElemType x;

Pop(OPTR,x);

c = (float)getchar();

break;

case ”://退棧並將運算結果入棧

ElemType b,theta,a;

Pop(OPTR,theta);

Pop(OPND,b);

Pop(OPND,a);

Push(OPND,Operate(a,theta,b));

break;

case ‘E’:

printf(“括弧不匹配!!\n”);

exit(0);

break;

}

}

}

ElemType tem = GetTop(OPND,e);

DestroyStack(OPND);

DestroyStack(OPTR);

return tem;

}/***

*DynaSeqStack.cpp – 動態順序棧,即棧的動態順序存儲實現

****/

const int STACK_INIT_SIZE = 100; // 初始分配的長度

const int STACKINCREMENT = 10; // 分配內存的增量

/*————————————————————

操作目的: 初始化棧

初始條件: 無

操作結果: 構造一個空的棧

函數參數:

SqStack *S 待初始化的棧

返回值:

bool 操作是否成功

————————————————————*/

bool InitStack(SqStack *S)

{

assert(S != NULL);

S-base = (ElemType *)malloc(sizeof(ElemType) * STACK_INIT_SIZE);

if(S-base == NULL) return false; S-top = S-base;

S-stacksize = STACK_INIT_SIZE; return true;

}/*————————————————————

操作目的: 銷毀棧

初始條件: 棧S已存在

操作結果: 銷毀棧S

函數參數:

SqStack *S 待銷毀的棧

返回值:

————————————————————*/

void DestroyStack(SqStack *S)

{

assert(S != NULL); free(S-base);

S-top = S-base = NULL;

}/*————————————————————

操作目的: 判斷棧是否為空

初始條件: 棧S已存在

操作結果: 若S為空棧,則返回true,否則返回false

函數參數:

SqStack S 待判斷的棧

返回值:

bool 是否為空

————————————————————*/

bool StackEmpty(SqStack S)

{

assert((S.base != NULL) (S.top != NULL));

return(S.base == S.top);

}/*————————————————————

操作目的: 得到棧的長度

初始條件: 棧S已存在

操作結果: 返回S中數據元素的個數

函數參數:

SqStack S 棧S

返回值:

int 數據元素的個數

————————————————————*/

int StackLength(SqStack S)

{

assert((S.base != NULL) (S.top != NULL));

return(S.top-S.base);

}/*————————————————————

操作目的: 得到棧頂元素

初始條件: 棧S已存在

操作結果: 用e返回棧頂元素

函數參數:

SqStack S 棧S

ElemType *e 棧頂元素的值

返回值:

bool 操作是否成功

————————————————————*/

ElemType GetTop(SqStack S, ElemType *e)

{

assert((S.base != NULL) (S.top != NULL));

if(StackEmpty(S)) return false; *e = *(S.top-1);

return *e;

}/*————————————————————

操作目的: 遍歷棧

初始條件: 棧S已存在

操作結果: 依次對S的每個元素調用函數fp

函數參數:

SqStack S 棧S

void (*fp)() 訪問每個數據元素的函數指針

返回值:

————————————————————*/

void StackTraverse(SqStack S, void (*fp)(ElemType))

{

assert((S.base != NULL) (S.top != NULL));

for(; S.baseS.top; S.base++) (*fp)(*S.base);

}/*————————————————————

操作目的: 壓棧——插入元素e為新的棧頂元素

初始條件: 棧S已存在

操作結果: 插入數據元素e作為新的棧頂

函數參數:

SqStack *S 棧S

ElemType e 待插入的數據元素

返回值:

bool 操作是否成功

————————————————————*/

bool Push(SqStack *S, ElemType e)

{

if (S-top – S-base=S-stacksize)

{

S-base = (ElemType *)realloc(S-base,(S-stacksize + STACKINCREMENT) * sizeof(ElemType));

if (!S-base)

exit(0);

S-top = S-base + S-stacksize;

S-stacksize += STACKINCREMENT;

}

*S-top++ = e; return true;

}/*————————————————————

操作目的: 彈棧——刪除棧頂元素

初始條件: 棧S已存在且非空

操作結果: 刪除S的棧頂元素,並用e返回其值

函數參數:

SqStack *S 棧S

ElemType *e 被刪除的數據元素值

返回值:

bool 操作是否成功

————————————————————*/

bool Pop(SqStack *S, ElemType *e)

{

if(S == NULL) return false;

assert((S-base != NULL) (S-top != NULL));

if(StackEmpty(*S)) return false; *e = *(–S-top);

return true;

}//////菜單///////

void Menu()

{

printf(“表達式求值模擬程序\n\n”);

printf(“功能菜單:\n”);

printf(“==============\n”);

printf(“[1] 輸入表達式並求值\n”);

printf(“[0] 退出 \n”);

printf(“==============\n”);

printf(“請輸入你的選擇(0~1)(以回車結束):”);

}///////// 主函數 ///////////

//////////////////////////////

int main()

{

char ch = ‘ ‘,tp; do

{

system(“cls”);

Menu();

ch = getchar();

if (ch == ‘0’)

break;

tp = getchar();

printf(“請輸入一個表達式(最後輸入」=「,然後回車出結果):”);

printf(“這個表達式結果為:%g\n”,EvaluateExpression());

tp = getchar();

printf(“任意鍵繼續…”);

getch();

} while (true); return 0;

}//end

算術表達式求值 C語言

cludeiostream.h

//#define MaxLen 100//存儲空間

int tran(char str[], char expr[]) //將中綴表達式轉換成後綴表達式 if(tran(str,expr)==0)//原來表達式,後綴表達式

{

int st[100]; //轉化過程使用的過度棧

char ch;

int i=0,exindex=0,stindex=-1; //i是str下標,exindex是expr下標,stindex是st下標

while((ch=str[i++])!=’\0′)

{

if(ch=’0′ ch=’9′) //判斷是數字

{

expr[exindex]=ch; //壓棧

exindex++; //棧頂指針上移

while((ch=str[i++])!=’\0′ ch=’0′ ch=’9′) //其它位依次入棧

{

expr[exindex]=ch;

exindex++;

}

i–; //str原算術表達式棧向下遍歷

expr[exindex]=’#’; //以特殊字元「#」表示結束

exindex++;

}

else if(ch=='(‘) //判斷為左括弧

{

stindex++;

st[stindex]=ch;

}

else if(ch==’)’) //判斷為右括弧

{

while (st[stindex]!='(‘)

{

expr[exindex]=st[stindex];

stindex–; //依次彈出

exindex++;

}

stindex–;//'(‘出棧

}

else if(ch==’+’ || ch==’-‘)//判斷為加減號

{

while(stindex=0 st[stindex]!='(‘)

{

expr[exindex]=st[stindex];

stindex–;

exindex++;

}

stindex++;

st[stindex]=ch;

}

else if (ch==’*’ || ch==’/’)//判斷為乘除號

{

while(st[stindex]==’*’ || st[stindex]==’/’)

{

expr[exindex]=st[stindex];

stindex–;

exindex++;

}

stindex++;

st[stindex]=ch;

}

}

while (stindex=0)//將棧中所有運算符依次彈出存入expr棧中

{

expr[exindex]=st[stindex];

exindex++;

stindex–;

}

expr[exindex]=’\0′;

return 1;

}

int compvalue(char expr[],int *n)

{

int st[100],d; //st為數棧

char ch;

int exindex=0,stindex=-1; //exindex是expr下標,stindex是st的下標

while((ch=expr[exindex++])!=’\0′)

{

if(ch=’0’ch=’9′)//將數字字元轉換成數字

{

d=0;

do

{

d=10*d+ch-‘0’;

}

while((ch=expr[exindex++])!=’#’);

stindex++;

st[stindex]=d;//數字進棧

}

else//運算符操作

{

switch(ch)

{

case’+’:st[stindex-1]=st[stindex-1]+st[stindex];

break;

case’-‘:st[stindex-1]=st[stindex-1]-st[stindex];

break;

case’*’:st[stindex-1]=st[stindex-1]*st[stindex];

break;

case’/’:

if(st[stindex]!=0)

{ st[stindex-1]=st[stindex-1]/st[stindex]; }

else return 0; //除0錯誤!

break;

}

stindex–;

}

}

(*n)=st[stindex];

return 1;

}

void main()

{

char str[100]; //存儲原來算術表達式

char expr[100]; //存儲轉換成的後綴表達式

int n;

cout”輸入算術表達式:”endl;

cinstr;

if(tran(str,expr)==0)

{

cout”原算術表達式不正確!”endl;

}

else

{

cout”轉換成後綴表達式輸出:”endlexprendl;

if(compvalue(expr,n)==1)

{

cout”表達式求值:”endlnendl;

}

else

{

cout”計算錯誤!”endl;

}

}

如何用C語言數據結構的格式實現簡單的算術表達式求值程序

用棧把中綴表達式(輸入的式子)按優先順序轉為後綴表達式(逆波蘭式,即運算符在前,操作數在後),再利用棧變計算邊保存結果用於下一步計算,最後算出式子的答案

以下代碼輸入一個式子(以

=

作為輸入結束標誌),輸出結果,負數如-3用0-3表示,支持高位運算

#include

stdio.h

#include

stdlib.h

#include

math.h

#include

malloc.h

#define

OK

1

#define

ERROR

-1

typedef

char

SElemType;

typedef

char

Status;

#define

STACK_INIT_SIZE

100000

#define

STACKINCREMENT

2

struct

SqStack

{

SElemType

*base;

SElemType

*top;

int

stacksize;

};

struct

SqStack1

{

int

*base;

int

*top;

int

stacksize;

};

SqStack

OPTR;

SqStack1

OPND;

char

Precede(char

c1,char

c2)

{

if(c1==’+’

||

c1==’-‘)

{

if(c2==’+’

||

c2==’-‘

||

c2==’)’

||

c2==’=’)

return

”;

else

return

”;

}

else

if(c1==’*’

||

c1==’/’)

{

if(c2=='(‘)

return

”;

else

return

”;

}

else

if(c1=='(‘)

{

if(c2==’)’)

return

‘=’;

else

return

”;

}

else

if(c1==’)’)

return

”;

else

if(c1==’=’)

{

if(c2==’=’)

return

‘=’;

else

return

”;

}

else

return

‘\0’;

}

int

In(char

c)

{

if(c==’+’

||

c==’-‘

||

c==’*’

||

c==’/’

||

c=='(‘

||

c==’)’

||

c==’=’)

return

1;

else

return

0;

}

int

Operrate(int

m,char

b,int

n)

{

switch(b)

{

case

‘+’:return

m+n;

case

‘-‘:return

m-n;

case

‘*’:return

m*n;

case

‘/’:return

m/n;

}

return

0;

}

//操作數

int

InitStack1(SqStack1

S)

{

S.base=(int

*)malloc(STACK_INIT_SIZE*sizeof(int));

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

return

OK;

}

int

Push1(SqStack1

S,int

e)

{

if(S.top-S.base=S.stacksize)

{

S.base=(int

*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(int));

S.top=S.base+S.stacksize;

S.stacksize=S.stacksize+STACKINCREMENT;

}

*S.top++=e;

return

OK;

}

int

Pop1(SqStack1

S,int

e)

{

if(S.top==S.base)

return

ERROR;

e=*

–S.top;

return

OK;

}

int

GetTop1(SqStack1

S)

{

if(S.top==S.base)

return

ERROR;

return

*(S.top-1);

}

//算符

int

InitStack(SqStack

S)

{

S.base=(SElemType

*)malloc(STACK_INIT_SIZE*sizeof(SElemType));

S.top=S.base;

S.stacksize=STACK_INIT_SIZE;

return

OK;

}

int

Push(SqStack

S,SElemType

e)

{

if(S.top-S.base=S.stacksize)

{

S.base=(SElemType

*)realloc(S.base,(S.stacksize+STACKINCREMENT)*sizeof(SElemType));

S.top=S.base+S.stacksize;

S.stacksize=S.stacksize+STACKINCREMENT;

}

*S.top++=e;

return

OK;

}

int

Pop(SqStack

S,SElemType

e)

{

if(S.top==S.base)

return

ERROR;

e=*

–S.top;

return

OK;

}

Status

GetTop(SqStack

S)

{

if(S.top==S.base)

return

ERROR;

return

*(S.top-1);

}

int

Calculate()

{

char

c,theta,p;

int

a,b,i=0,ans,x;

InitStack(OPTR);

Push(OPTR,’=’);

InitStack1(OPND);

c=getchar();

while(c!=’=’

||

GetTop(OPTR)!=’=’)

{

if(!In(c)

c=’0′

c=’9′)

{

Push1(OPND,c-‘0’);

c=getchar();

while(c=’0′

c=’9′)

{

Pop1(OPND,x);

Push1(OPND,x*10+c-‘0’);

c=getchar();

}

}

else

if(In(c))

{

switch(Precede(GetTop(OPTR),c))

{

case

”:

Push(OPTR,c);

c=getchar();

break;

case

‘=’:

Pop(OPTR,p);

c=getchar();

break;

case

”:

Pop(OPTR,theta);

Pop1(OPND,b);

Pop1(OPND,a);

ans=Operrate(a,theta,b);

Push1(OPND,ans);

break;

}

}

else

{

c=getchar();

}

}

return

GetTop1(OPND);

}

int

main()

{

int

ans;

ans=Calculate();

printf(“%d\n”,ans);

return

0;

}

原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/279030.html

(0)
打賞 微信掃一掃 微信掃一掃 支付寶掃一掃 支付寶掃一掃
小藍的頭像小藍
上一篇 2024-12-20 15:02
下一篇 2024-12-20 15:02

相關推薦

  • AES加密解密演算法的C語言實現

    AES(Advanced Encryption Standard)是一種對稱加密演算法,可用於對數據進行加密和解密。在本篇文章中,我們將介紹C語言中如何實現AES演算法,並對實現過程進…

    編程 2025-04-29
  • 學習Python對學習C語言有幫助嗎?

    Python和C語言是兩種非常受歡迎的編程語言,在程序開發中都扮演著非常重要的角色。那麼,學習Python對學習C語言有幫助嗎?答案是肯定的。在本文中,我們將從多個角度探討Pyth…

    編程 2025-04-29
  • 數據結構與演算法基礎青島大學PPT解析

    本文將從多個方面對數據結構與演算法基礎青島大學PPT進行詳細的闡述,包括數據類型、集合類型、排序演算法、字元串匹配和動態規劃等內容。通過對這些內容的解析,讀者可以更好地了解數據結構與算…

    編程 2025-04-29
  • Python被稱為膠水語言

    Python作為一種跨平台的解釋性高級語言,最大的特點是被稱為”膠水語言”。 一、簡單易學 Python的語法簡單易學,更加人性化,這使得它成為了初學者的入…

    編程 2025-04-29
  • OpenJudge答案1.6的C語言實現

    本文將從多個方面詳細闡述OpenJudge答案1.6在C語言中的實現方法,幫助初學者更好地學習和理解。 一、需求概述 OpenJudge答案1.6的要求是,輸入兩個整數a和b,輸出…

    編程 2025-04-29
  • Python按位運算符和C語言

    本文將從多個方面詳細闡述Python按位運算符和C語言的相關內容,並給出相應的代碼示例。 一、概述 Python是一種動態的、面向對象的編程語言,其按位運算符是用於按位操作的運算符…

    編程 2025-04-29
  • 數據結構學生成績管理系統

    在現代教育中,學生成績的管理已經成為了一個不可或缺的部分。藉助數據結構,一個高效、可靠的學生成績管理系統可以被輕鬆實現。 一、數據結構的選擇 在構建學生成績管理系統時,選擇合適的數…

    編程 2025-04-29
  • Python語言由荷蘭人為中心的全能編程開發工程師

    Python語言是一種高級語言,很多編程開發工程師都喜歡使用Python語言進行開發。Python語言的創始人是荷蘭人Guido van Rossum,他在1989年聖誕節期間開始…

    編程 2025-04-28
  • Python語言設計基礎第2版PDF

    Python語言設計基礎第2版PDF是一本介紹Python編程語言的經典教材。本篇文章將從多個方面對該教材進行詳細的闡述和介紹。 一、基礎知識 本教材中介紹了Python編程語言的…

    編程 2025-04-28
  • Python語言實現人名最多數統計

    本文將從幾個方面詳細介紹Python語言實現人名最多數統計的方法和應用。 一、Python實現人名最多數統計的基礎 1、首先,我們需要了解Python語言的一些基礎知識,如列表、字…

    編程 2025-04-28

發表回復

登錄後才能評論