admin 管理员组

文章数量: 887018

表达式计算c语言源程序实例,C语言

1. 目标

编写一个支持浮点数及括号的加减乘除计算器。html

输入:中缀表达式git

输出:后缀表达式及计算结果express

注意:该代码在VS13上运行经过。数组

运行示例:url

2. 实现流程spa

2.1 中缀表达式转换为后缀表达式请参考以下连接:点击打开连接code

3. 源代码htm

#include

#include

#include

#define stacksize 30

#define stackincrease 30

#define maxvalume 30

#define expvalume 200

#define title "------------------------------Life is a fight!------------------------------------"

typedef float Elemtype;

typedef struct STACK{

Elemtype* top;

Elemtype* base;

int len;

} stack;

stack* InitStack()

{

Elemtype* t;

stack* s;

t=(Elemtype*)malloc(stacksize*sizeof(Elemtype));

s=(stack*)malloc(sizeof(stack));

s->top=t;

s->base=t;

s->len=stacksize;

return s;

}

void PushStack(stack *s, Elemtype d)

{

if(s->top-s->base>=s->len-1)

s->base=(Elemtype*)realloc(s->base,(s->len+stackincrease)*sizeof(Elemtype));

*s->top=d;

s->top++;

}

void PopStack(stack* s, Elemtype* d)

{

if(s->top==s->base)

{

printf("\nErr!\n");

return;

}

*d=*--(s->top);

}

int LengthStack(stack *s)

{

return (s->top-s->base);

}

int GetExp(Elemtype* str)//读取输入的中缀表达式,保存到str数组中

{

char c;

int i=0;

fflush(stdin);

scanf("%c",&c);

while(c!='\n')

{

if(i>expvalume)

{

printf("Err: The expression is too long!\n");

return -1;

}

*str++=c;

i++;

scanf("%c",&c);

}

if(i==0)

{

*str++='#';

}

*str='\0';

return i;

}

void NiftoSuf(Elemtype* nif, Elemtype* suf)//将中缀表达式(nif)转换为后缀表达式(suf)

{

int i, j, n;

Elemtype c, t;

stack *s;

i=0;

j=0;

s=InitStack();

c=(char)nif[i];

while(c!='#')//'#'为输入结束标志

{

while(isdigit(c)||c=='.')//以字符的方式读取数字,将数字复制到suf中

{

suf[j++]=c;

c=(char)nif[++i];

if((c'9')&&c!='.')

{

suf[j++]=' ';

}

suf[j]='\0';

}

if(c==')')

{

PopStack(s, &t);

while(t!='(')

{

suf[j++]=t;

suf[j++]=' ';

PopStack(s,&t);

}

suf[j]='\0';

}

else if(c=='(')

PushStack(s,c);

else if(c=='+'||c=='-')

{

if(!LengthStack(s))

{

PushStack(s,c);

}

else

{

do{

PopStack(s,&t);

if(t=='(')

{

PushStack(s,t);

}

else

{ suf[j++]=t;

suf[j++]=' ';

suf[j]='\0';

}

} while(LengthStack(s)&&t!='('&&c!='(');

PushStack(s,c);

}

}

else if(c=='*'||c=='/'||c=='(')

{

PushStack(s,c);

}

else if(c=='#' )

{

while(LengthStack(s))

{

PopStack(s,&t);

suf[j++]=t;

suf[j++]=' ';

}

break;

}

else

{

printf("\nErr:Expression Error\n");

return -1;

}

i++;

c=nif[i];

}

if(c=='#')

while(LengthStack(s))

{

PopStack(s,&t);

suf[j++]=t;

suf[j++]=' ';

}

suf[j++]='#';

suf[j]='\0';

free(s);

}

Elemtype Cal(Elemtype* suf)

{

int i, j;

char c;

Elemtype f, r, d1, d2;

stack *s;

i=0;

j=0;

s=InitStack();

char t[maxvalume];

c=suf[j++];

while(c!='#')

{

while(isdigit(c)||c=='.')

{

if(i>maxvalume)

{

printf("Err: The data is too large!\n");

return -1;

}

t[i++]=c;

t[i]='\0';

c=suf[j++];

if(c==' ')

{

f=atof(t);

PushStack(s,f);

i=0;

}

}

switch (c){

case '+':

PopStack(s,&d2);

PopStack(s,&d1);

PushStack(s,d1+d2);

break;

case '-':

PopStack(s,&d2);

PopStack(s,&d1);

PushStack(s,d1-d2);

break;

case '*':

PopStack(s,&d2);

PopStack(s,&d1);

PushStack(s,d1*d2);

break;

case '/':

PopStack(s,&d2);

if(d2==0)

{

printf("Err: data error!\n");

return -1;

}

PopStack(s,&d1);

PushStack(s,d1/d2);

break;

}

c=suf[j++];

}

PopStack(s,&r);

printf("Result: %f",r);

free(s);

return r;

}

int main(void)

{

int i,j,k;

char c,c1,y;

Elemtype nif[expvalume], suf[expvalume];

printf("%s\n\n",title);

do{

i=0;

j=0;

k=0;

printf("Please enter the expression:\n");

GetExp(nif);

NiftoSuf(nif,suf);

printf("\n");

while(suf[k]!='\0')

{

c1=suf[k];

printf("%c",c1);

k++;

}

printf("\n\n");

Cal(suf);

printf("\n\n\n");

printf("\nPlease enter 'Y' for continual\n");

scanf("%c",&y);

printf("\n");

fflush(stdin);

} while(y=='Y'||y=='y');

return 0;

}

本文标签: 表达式计算c语言源程序实例 C语言