C语言课程设计 小学生四则运算练习系统 源程序

如题所述

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define maxsize 50
void trans(char str[],char exp[])/*将算术表达式str转换成后缀表达式exp*/
{
struct
{ char data[maxsize]; /*存放运算符*/
int top; /*栈指针*/
}opr; /*定义运算符栈*/
char ch;
int i=0,t=0; /*t作为exp的下标,i作为str的下标*/
opr.top=-1; /*初始化设定top的值为负一*/
ch=str[i];i++; /*逐个读取字符串中的字符*/
while (ch!='\0') /*str表达式未扫描完时循环*/
{ switch(ch) /*判定*/
{
case '(':
opr.top++;opr.data[opr.top]=ch; /*判定为'('号,则将其入栈opr*/
break;
case ')':
while (opr.data[opr.top]!='(') /*判定为')'号*/
{ exp[t]=opr.data[opr.top]; /*将栈opr中'('以后的字符依次删除并存入数组exp中*/
opr.top--;
t++;
}
opr.top--; /*将左括号删除*/
break;
case '+': /*判定为加号或减号*/
case '-':
while (opr.top!=-1 &&opr.data[opr.top]!='(')
{ exp[t]=opr.data[opr.top]; /*将当前栈opr中(以前的所有字符依次删除并存入数组exp中*/
opr.top--;
t++;
}
opr.top++;opr.data[opr.top]=ch; /*将ch存入栈opr中*/
break;
case '*':
case '/':
while (opr.data[opr.top]=='*'||opr.data[opr.top]=='/'||opr.data[opr.top]=='^')
{ exp[t]=opr.data[opr.top]; /*将当前栈opr中连续的'*'或'/'或'^'依次删除并存入数组exp中*/
opr.top--;
t++;
}
opr.top++;opr.data[opr.top]=ch; /*将ch存入栈opr中*/
break;
case '^': /*判定为乘方号*/
while (opr.data[opr.top]=='^')
{ exp[t]=opr.data[opr.top]; /*将当前栈opr中连续的'^'依次删除并存入数组exp中*/
opr.top--;
t++;
}
opr.top++;opr.data[opr.top]=ch; /*将ch存入栈opr中*/
break;
case ' ': break; /*过滤掉空格*/
default:
while(ch>='0'&& ch<='9'||ch=='.') /*判定为数字*/
{ exp[t]=ch;t++; /*将后续数字依次存入数组中*/
ch=str[i];i++;
}
i--;
exp[t]='#';t++; /*用#标示一个数值串结束*/
}
ch=str[i];i++;
}
while (opr.top!=-1) /*此时str扫描完毕,栈不空时循环*/
{ exp[t]=opr.data[opr.top];
t++;opr.top--;
}
exp[t]='\0'; /*给exp表达式添加结束标示*/
}
float compvalue(char exp[]) /*计算后缀表达式的值*/
{
struct
{ float data[maxsize]; /*存放数值*/
int top; /*栈指针*/
} st; /*定义数值栈*/
float d,d2;double po;
char ch;
int t=0,flag=1,i,count; /*t作为exp的下标*/
st.top=-1;
ch=exp[t];t++;
while (ch!='\0') /*exp字符串为扫描完时循环*/
{ 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除零错误!\n");
exit(0); /*除数为零,异常退出*/
}
st.top--;break;
case '^':
po=pow(st.data[st.top-1],st.data[st.top]); st.data[st.top-1]=(float)po;/*调用pow子函数进行乘方运算*/
st.top--;break;
default:
d=0; flag=1; d2=0; /*将数字字符转换成对应的数值存放到d中*/
while(ch>='0'&&ch<='9'&&flag) /*判定为数字字符*/
{ d=10*d+ch-'0';
ch=exp[t];t++;
if(ch=='.')
flag=0;

}
if(flag==0)
{ ch=exp[t];t++;count=0;
while(ch>='0'&&ch<='9') /*判定为数字字符*/
{d2=10*d2+ch-'0';
ch=exp[t];t++;count++;
}
for(i=1;i<=count;i++)
d2=0.1*d2;
}
d+=d2;
st.top++;
st.data[st.top]=d;
}
ch=exp[t];t++;
}
return st.data[st.top];
}
int main()
{
char str[maxsize],exp[maxsize]; /*str存储原算术表达式,exp存储对应的后缀表达式*/
printf("the arithmetic expression is:\n");
gets(str);
trans(str,exp);
printf("the postfix expression is:%s\n",exp);
printf("the result is %g\n",compvalue(exp));
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-02-20
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <assert.h>
int comp(int);
int init(void);
int main(int argc, char* argv[])
{
int in;
do
{
init();
scanf("%d",&in);
assert(in>=0&&in<=4);
system("cls");
if (in!=0) comp(in);
}
while (in!=0);
return 0;
}
int init(void)
{
system("cls");
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n0. EXIT\n");
return 0;
}

int comp(int op)
{
int a,b,c;
float cfd;
printf("Input \'-1\' to exit!\n\n");
srand(time(NULL));
cfd=(float)(a/b);
do
{
a=rand()%100+1;
b=rand()%100+1;
switch(op){
case 1:printf("%d+%d=",a,b);
b=a+b;
break;
case 2:while (a-b<0) {
a=rand()%100+1;
b=rand()%100+1;
}
printf("%d-%d=",a,b);
b=a-b;
break;
case 3:printf("%d*%d=",a,b);
b=a*b;
break;
case 4:while (a<b||cfd*b!=a){
a=rand()%100+1;
b=rand()%100+1;
cfd=a/b;
}
printf("%d/%d=",a,b);
b=a/b;
break;
}
scanf("%d",&c);
b==c?printf("\nRight!\n"):printf("\nWrong! It's %d\n",b);
}while (c!=-1);
return 0;
第2个回答  2010-06-30
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void main()
{
int a,b;
while(true)
{printf("请输入a和b:");
scanf("%d %d",&a,&b);
printf("%d=%d+%d\n",a+b,a,b);
printf("%d=%d-%d\n",a-b,a,b);
printf("%d=%d×%d\n",a*b,a,b);
printf("%d=%d÷%d\n",a/b,a,b);
printf("按回车返回\n");
fflush(stdin);
getchar();
system("cls");
}
}
你的要求不是太明确,我弄了一个,不知道符不符合
第3个回答  2010-07-09
#include <stdio.h>
#define MAXSIZE 16

typedef struct{
int data[MAXSIZE];
int top;
int base;
}seqstack;

void InitStack(seqstack *s);
int Empty(seqstack *s);
void Push(seqstack *s, int x );
int Pop(seqstack *s);
int GetTop(seqstack *s);
int Operate(int a,char r,int b);
char Proceed(char op ,char c);
int In(char );
int EvalExpres(void);

seqstack StackR,StackD;

int main()
{
int v;
char ch;

v = EvalExpres();
printf("The result is:%d",v);
getch();
}

void InitStack(seqstack *s)
{ s->top = 0;
s->base = 0;
}

int Empty(seqstack *s)
{ if(s->top == s->base)
return 1;
else
return 0;
}

void Push(seqstack *s, int x)
{
if(s->top == MAXSIZE)
{ printf("OVER FLOW!\n");
exit(0);
}
else
{ s->data[s->top] = x;
s->top++;
}
}

int Pop(seqstack *s)
{ int e;
if(Empty(s))
{ printf("Under flow!\n");
return 0;
}
else
{ s->top--;
e = s->data[s->top];
return e;
}
}

int GetTop(seqstack *s)
{
if(Empty(s))
{ printf("Under flow!\n");
return 0;
}
else
return s->data[s->top-1];
}

int EvalExpres(void)
{
int a,b,i=0,s=0;
char c[80],r;
InitStack(&StackR);
Push(&StackR,'#');
InitStack(&StackD);
printf("Please enter a expression end with '#': ");
gets(c);
while(c[i]!='#' || GetTop(&StackR)!='#')
{
if(!In(c[i]))
{ if(c[i] >= '0' && c[i] <= '9')
{
s += c[i]-'0';
while(!In(c[++i]))
{ s*=10;
s += c[i]-'0';
}
Push(&StackD,s+'0');
s = 0;
}

}
else
switch(Proceed(GetTop(&StackR),c[i]))
{
case '<':
Push(&StackR,c[i]);
i++;
break;
case '=':
Pop(&StackR);
i++;
break;
case '>':
r = Pop(&StackR);
a = Pop(&StackD)-'0';
b = Pop(&StackD)-'0';
Push(&StackD,Operate(a,r,b)) ;
break;
}
}
return (GetTop(&StackD)-'0');
}

int In(char c)
{
char ch[8]={'+','-','*','/','%','#','(',')'};
int i;
for(i = 0; i < 8; i++)
if(c == ch[i])
return 1;

return 0;
}

char Proceed(char op,char c)
{
char ch;
if(op=='(' && c==')' || op=='#' && c=='#' )
ch = '=';
else
if(op=='+' || op=='-')
switch(c)
{
case '+':
case '-':
case ')':
case '#': ch = '>'; break;
case '*':
case '/':
case '%':
case '(': ch = '<';
}
else
if(op=='*' || op=='/'|| op=='%')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case ')':
case '#': ch = '>'; break;
case '(': ch = '<';
}
else
if(op=='(')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '(': ch = '<'; break;
case '#': printf("Error!\n"); exit(0);
}
else
if(op==')')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '#': ch = '>'; break;
case '(': printf("Error!\n"); exit(0);
}
else
if(op=='#')
switch(c)
{
case '+':
case '-':
case '*':
case '/':
case '%':
case '(': ch = '<'; break;
case ')': printf("Error!\n"); exit(0);
}
return ch;
}

int Operate(int a,char r,int b)
{
int s;
int d1 = a;
int d2 = b;
switch(r)
{
case '+': s = d1+d2; break;
case '-': s = d2-d1; break;
case '*': s = d1*d2; break;
case '/': s = d2/d1; break;
case '%': s = d2%d1; break;
}
return (s+'0');
}
相似回答