C语言 四则运算程序 高手帮帮忙!!!!

功能:实现小学生100以内四则运算的学习和测试
基本要求:
主界面设计,选择练习或测试,按ESC结束程序。
题型选择界面设计,选择加、减、乘、除或混合运算,按ESC返回主界面。
系统随机出题,运算数及结果均在100以内,乘、除法应能整除,显示算式。
练习时,系统随机出题,键入结果,正确和错误均有提示,出错时允许再输入,最多三次机会,若还不正确,给出答案。继续出题,按ESC,显示总题数,正确数和正确率。
测试时,系统自动出10道题,每题只给一次机会,每题10分,统计得分。结束后,给出总分,显示各题的对错信息,错误的给出答案。中间按ESC键结束。显示已作题的对错信息,错误的给出答案,不显示得分,按任意键返回题型选择界面。
相关知识:按键操作、数组、指针、结构体等
功能扩充:1)限制答题时间。
2)测试时不能有重题
3)形成测试排行榜
4)在图形方式下,以更直观的√和╳来显示对错信息
5)对错时辅以不同的音乐

谢谢各位!帮帮忙啊!!!

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
int scan()
{
char s[100];
int i,t,z=0;
do
{
z=0;

gets(s);
for(i=0;s[i]!='\0';i++)
if(s[i]<'0' || s[i]>'9') break;
if(i>=strlen(s))
for(t=0;s[t]!='\0';t++)
z=z*10+((int)s[t]-48);
else
printf("您的输入有误,请重新输入");
}
while(i<strlen(s));
return(z);
}

int x=1,y=1,choice=0,sum=0,right=0,z=0,answer;
float precent;
char fuhao;
void suiji()
{
srand(time(NULL));
x=rand()%100+1;
y=rand()%100+1;
}

void choose()
{
int yunsuan(int,int,int);
if(sum==0) precent=0.0;
else precent=(float)right/sum;
printf("\n如需进行加减乘除运算测试,请分别输入1、2、3、4\n");
printf("如需退出请输入5\n");
choice=scan();
switch(choice)
{
case 1:printf("欢迎来到加法运算测试系统,如需退出请输入9999\n");fuhao='+';break;
case 2:printf("欢迎来到减法运算测试系统,如需退出请输入9999\n");fuhao='-';break;
case 3:printf("欢迎来到乘法运算测试系统,如需退出请输入9999\n");fuhao='*';break;
case 4:printf("欢迎来到除法运算测试系统,如需退出请输入9999\n");fuhao='/';break;
case 5:printf("本次测试愉快,欢迎下次再来测试\n");
printf("本次共做%d道题,其中%d道正确,正确率为%f\n",sum,right,precent);
break;
default:printf("您的输入有误,请重新输入\n");choose();
}
}

int ans(int x,int y,int choice)
{
switch(choice)
{
case 1:z=x+y;break;
case 2:z=x-y;break;
case 3:z=x*y;break;
case 4:z=x/y;break;
}
return(z);
}
void yunsuan()
{
suiji();
printf("%d%c%d=",x,fuhao,y);
z=ans(x,y,choice);
answer=scan();
if(z==answer)
{
printf("正确\n");
right++;sum++;
yunsuan();
}
else if(answer==9999) choose();
else
{
printf("不正确, 正确答案为%5d\n",z);
sum++;
yunsuan();
}
}
void main()
{
printf("欢迎来到四则运算测试系统\n");
choose();
while(choice!=5)
yunsuan();
}

参考资料:^_^

温馨提示:内容为网友见解,仅供参考
第1个回答  2008-06-11
给你个类似程序:
/*** 一共包含四个文件 *************
| Symbol.h
| Stack.h
| Expression.h
| Expression.c
| 这里的代码考虑到通用性,对代码的可
| 重用性作了细致的考虑,略显冗余,核心算法:
| Expression.h里的函数STATUS EvaluateExpression(float*fResult,char*strExpression)
**********************************/
// --Expression.c
#include "Expression.h"

extern EXPRESSION_DEBUG;

STATUS EvaluateExpression(float*,char*);
void Usage(char *);
int HandleOptions(int,char **);

int main(int argc,char*argv[ ])
{
char strLine[30]={0};
float fResult=0;

/* handle the program options */
HandleOptions(argc,argv);
fprintf(stderr,"Input cls to clear the screen\n");
fprintf(stderr," debug to show stack change\n");
fprintf(stderr," nodebug to show no stack change\n");
fprintf(stderr," end to exit\n");
while(TRUE)
{
printf("Input:\n");
gets(strLine);
if(!strcmp(strLine,"end"))
break;
if(!strcmp(strLine,"cls")) {
system("cls");
continue;
}
if(!strcmp(strLine,"debug")){
EXPRESSION_DEBUG=TRUE;
continue;
}
if(!strcmp(strLine,"nodebug")){
EXPRESSION_DEBUG=FALSE;
continue;
}
EvaluateExpression(&fResult,strLine);
printf("Ans=%f\n",fResult);
}
return OK;

}

void Usage(char *programName)
{
fprintf(stderr,"%s usage:%s [-d][-h/?]\n",programName,programName);
fprintf(stderr,"-d Test program, calculate expression and\n");
fprintf(stderr," see changes in the stack at the same time.\n");
exit(OK);
}

/* returns the index of the first argument that is not an option; i.e.
does not start with a dash or a slash
*/
int HandleOptions(int argc,char *argv[])
{
int i,firstnonoption=0;

for (i=1; i< argc;i++) {
if (argv[i][0] == '/' || argv[i][0] == '-') {
switch (argv[i][1]) {
/* An argument -? means help is requested */
case '?':
case 'h':
case 'H':
Usage(argv[0]);
break;
case 'd':
case 'D':
EXPRESSION_DEBUG=TRUE;
break;
default:
fprintf(stderr,"unknown option %s\n",argv[i]);
break;
}
}
else {
firstnonoption = i;
break;
}
}
return firstnonoption;
}

//--Symbol.h
//--symbol.h - definitions/declarations for symbols used by other C Header files

#ifndef SYMBOL_H
#define SYMBOL_H

#define STACK_H
#define TRUE 1
#define OK 1
#define YES 1
#define FALSE 0
#define ERROR 0
#define NO 0
#define OVERFLOW -1
#ifndef NULL
#define NULL 0
#endif

typedef unsigned int UINT;
typedef int STATUS;
typedef int BOOL;

#endif /*SYMBOL_H*/

//--stack.h - definitions/declarations for stack operation

#ifndef STACK_H
#define STACK_H

#include "Symbol.h"
#include <malloc.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#define S_CHAR 1
#define S_SHORT 2
#define S_INT 3
#define S_FLOAT 4
#define S_DOUBLE 5

//-- Stack.h
typedef struct tagNode
{
void*pData;
struct tagNode*pNext;
}Node,*PNode;
typedef struct tagStack
{
UINT uType;
/*1 char
*2 short/short int
*3 int
*4 float
*5 double
*/
UINT uLength;
struct tagNode*pFirst;
struct tagNode*pTop;
}Stack,*PStack;

STATUS InitStack(PStack pStack,UINT uType)
{
pStack->uType=uType;
pStack->uLength=0;
pStack->pFirst=pStack->pTop=NULL;
return OK;
}

STATUS ShowStack(PStack pStack)
{
PNode pNode=pStack->pFirst;
while(pNode)
{
switch(pStack->uType)
{
case S_CHAR: // char
printf("%c ",*(char*)(pNode->pData));break;
case S_FLOAT: // float
printf("%-4.1f ",*(float*)(pNode->pData));
}
pNode=pNode->pNext;
}
putchar(10);
return OK;
}

STATUS Push(PStack pStack,void*pData)
{
PNode pNode=(PNode)malloc(sizeof(Node));
if(!pNode)
{
printf("\nmalloc error!\n");
fflush(stdin);
getch();
exit(ERROR);
}
if(pStack->uType==1)
{
pNode->pData=(char*)malloc(sizeof(char));
*(char*)(pNode->pData)=*(char*)pData;
}
else if(pStack->uType==3)
{
pNode->pData=(int*)malloc(sizeof(int));
pNode->pData=(int*)malloc(sizeof(int));
*(int*)(pNode->pData)=*(int*)pData;
}
else if(pStack->uType==4)
{
pNode->pData=(float*)malloc(sizeof(float));
pNode->pData=(float*)malloc(sizeof(float));
*(float*)(pNode->pData)=*(float*)pData;
}
else if(pStack->uType==5)
{
pNode->pData=(double*)malloc(sizeof(double));
pNode->pData=(double*)malloc(sizeof(double));
*(double*)(pNode->pData)=*(double*)pData;
}
pNode->pNext=NULL;
if(!pStack->pTop)
pStack->pTop=pStack->pFirst=pNode;
else
{
pStack->pTop->pNext=pNode;
pStack->pTop=pNode;
}
pStack->uLength++;
return OK;
}

STATUS Pop(PStack pStack,void*pData)
{
PNode pPre=pStack->pFirst;
if(pStack->pTop!=pStack->pFirst)
while(pPre->pNext!=pStack->pTop)
pPre=pPre->pNext;
else
pPre=NULL;
if(pStack->uType==1)
*(char*)(pData)=*(char*)(pStack->pTop->pData);
else if(pStack->uType==3)
*(int*)(pData)=*(int*)(pStack->pTop->pData);
else if(pStack->uType==4)
*(float*)(pData)=*(float*)(pStack->pTop->pData);
else if(pStack->uType==5)
*(double*)(pData)=*(double*)(pStack->pTop->pData);
free(pStack->pTop->pData);
free(pStack->pTop);
pStack->pTop=pPre;
if(pPre)
pStack->pTop->pNext=NULL;
else
pStack->pFirst=NULL;
pStack->uLength--;
return OK;
}

STATUS GetTop(PStack pStack,void*pData)
{
if(pStack->uType==1)
*(char*)(pData)=*(char*)(pStack->pTop->pData);
else if(pStack->uType==3)
*(int*)(pData)=*(int*)(pStack->pTop->pData);
else if(pStack->uType==4)
*(float*)(pData)=*(float*)(pStack->pTop->pData);
else if(pStack->uType==5)
*(double*)(pData)=*(double*)(pStack->pTop->pData);
return OK;
}
STATUS DestroyStack(PStack pStack)
{
PNode pPre1,pPre2;
pPre1=pPre2=pStack->pFirst;
while(pPre1)
{
pPre1=pPre1->pNext;
free(pPre2->pData);
free(pPre2);
pPre2=pPre1;
}
pStack->pFirst=pStack->pTop=NULL;
pStack->uLength=0;
return OK;
}

#endif /* STACK_H */

//--Expresson.h

#ifndef EXPRESSION_H
#define EXPRESSION_H

#include "Stack.h"

typedef struct tagOptr
{
char cOptr;
UINT uPriority;
}Optr,*POptr;

BOOL EXPRESSION_DEBUG=FALSE;
Optr pOptr[8]={{0,7},{')',1},{'*',2},{'/',2},{'+',3},{'-',3},{'(',4},{'#',4}};
STATUS Operate(float*fTemp3,float fTemp1,char theta,float fTemp2)
{
switch(theta)
{
case '+':*fTemp3=fTemp1+fTemp2;break;
case '-':*fTemp3=fTemp1-fTemp2;break;
case '*':*fTemp3=fTemp1*fTemp2;break;
case '/':
if(fTemp2!=0)
*fTemp3=fTemp1/fTemp2;
else
{
printf("\n0 can not be divisor!\n\nPress any key to continue...\n");
fflush(stdin);
getch();
exit(ERROR);
}// else
break;
}
return OK;
}

int Precede(char cOptrTop,char cChar)
{
UINT i,j;
if(cOptrTop=='#'&&cChar=='#')
return 0;
if(cChar=='(')
return -1;
if(cChar==')')
if(cOptrTop=='(')
return 0;
else
return 1;
for(i=1;i<=pOptr[0].uPriority;i++)
if(pOptr[i].cOptr==cOptrTop)
{
i=pOptr[i].uPriority;
break;
}
for(j=1;j<=pOptr[0].uPriority;j++)
if(pOptr[j].cOptr==cChar)
{
j=pOptr[j].uPriority;
break;
}
if(i<=j)
return 1;
else
return -1;
return -2;
}

STATUS IsIn(char cChar)
{
if(cChar>='0'&&cChar<='9'||cChar=='.')
return YES;
return NO;
}

STATUS Debug(PStack stackOptr,PStack stackOpnd,char*strExpression,int i)
{
// --debug
if(EXPRESSION_DEBUG)
{
printf("-------------------------------\n");
printf("%s\n",strExpression);
printf("Optr:");
ShowStack(stackOptr);
printf("Opnd:");
ShowStack(stackOpnd);
}
return OK;
}
STATUS EvaluateExpression(float*fResult,char*strExpression)
{
char cChar='#',cOptrTop=0,theta=0;
float fTemp1,fTemp2,fTemp3,fTemp4;
int i=0,iTemp;
Stack stackOptr,stackOpnd;
InitStack(&stackOptr,S_CHAR);
InitStack(&stackOpnd,S_FLOAT);
Push(&stackOptr,&cChar);
GetTop(&stackOptr,&cOptrTop);
strcat(strExpression,"#");
if(strExpression[0]=='-'){
fTemp1=0;
Push(&stackOpnd,&fTemp1);
}
cChar=strExpression[0];
while(cChar!='#'||cOptrTop!='#')
{
Debug(&stackOptr,&stackOpnd,strExpression,i);
if(IsIn(cChar))
{
fTemp1=0;
fTemp3=10;
fTemp4=1;
while(IsIn(cChar))
{
if(cChar=='.'){
fTemp3=1;
cChar=strExpression[++i];
continue;
}
fTemp2=(float)(cChar-'0');
if(fTemp3==1)
{
iTemp=i;
while(IsIn(strExpression[i])){
fTemp2/=10;
i++;
}
i=iTemp;
}
fTemp1=fTemp1*fTemp3+fTemp2;
cChar=strExpression[++i];
}
Push(&stackOpnd,&fTemp1);
}
else
{
switch(Precede(cOptrTop,cChar))
{
case -1:Push(&stackOptr,&cChar);cChar=strExpression[++i];break;
case 0:Pop(&stackOptr,&cChar);cChar=strExpression[++i];break;
case 1:
Pop(&stackOptr,&theta);
Pop(&stackOpnd,&fTemp2);Pop(&stackOpnd,&fTemp1);
Operate(&fTemp3,fTemp1,theta,fTemp2);
Push(&stackOpnd,&fTemp3);
break;
}// switch
GetTop(&stackOptr,&cOptrTop);
}// else
}// while
Pop(&stackOptr,&cChar);
Debug(&stackOptr,&stackOpnd,strExpression,i);
GetTop(&stackOpnd,fResult);
DestroyStack(&stackOptr);
DestroyStack(&stackOpnd);
return OK;
}// EvaluateExpression

#endif // EXPRESSION_H
第2个回答  2019-05-25
楼主问用c语言编写,1楼的用c++怎么编写啊
我的c语言代码如下:
#include
#include
#include
#include
#include
void
main()
{
int
s,d=0;
int
a,b,c,z,v,m;
char
ch[4]={'+','-','x','/'};
srand((unsigned)time(null));
m=100;
b=rand()%m;
printf("%d
",b);
z=b;
for(s=0;s<3;s++)
{a=rand()%4;b=rand()%m;
printf("%c
%d
",ch[a],b);
if
(a==0)
{v=z+b;z=b;}
if
(a==1)
{v=z-b;z=b;}
if
(a==2)
{v=z*b;z=b;}
if
(a==3)
{v=z/b;z=b;}
}
printf("=
");
loop:
scanf("%d",&c);
fflush(stdin);
if
(c==v)
{printf("ok!");goto
end;}
while
(c!=v
&&
d++!=2)
{printf("it
does
not
matter
and
try
it
again:
");
goto
loop;
}
printf("the
result
is
:
%d",v);
end:getch();
}
第3个回答  2019-06-28
#include
#include
#include
#include
int
scan()
{
char
s[100];
int
i,t,z=0;
do
{
z=0;
gets(s);
for(i=0;s[i]!='\0';i++)
if(s[i]<'0'
||
s[i]>'9')
break;
if(i>=strlen(s))
for(t=0;s[t]!='\0';t++)
z=z*10+((int)s[t]-48);
else
printf("您的输入有误,请重新输入");
}
while(i
评论
0
0
加载更多

C语言 四则运算程序 高手帮帮忙!!!
case 1:printf("欢迎来到加法运算测试系统,如需退出请输入9999\\n");fuhao='+';break;case 2:printf("欢迎来到减法运算测试系统,如需退出请输入9999\\n");fuhao='-';break;case 3:printf("欢迎来到乘法运算测试系统,如需退出请输入9999\\n");fuhao='*';break;case 4:printf("欢迎来到除法运算...

输入两个整数,进行加减乘除四则运算的c语言程序怎么写啊,拜托了~_百 ...
void function(int a, int b){ printf("%d add %d = %d\\n",a, b, a+b); \/\/加法运算 printf("%d sub %d = %d\\n",a, b, a-b);\/\/加法运算 printf("%d mul %d = %d\\n",a, b, a*b);\/\/乘法运算 printf("%d div %d = %d\\n",a, b, a\/b);\/\/除法运算 } int ...

如何用C语言实现四则运算?
1.定义头文件#include "stdafx.h"、#include <stdio.h>和#include <math.h>。2.写出主函数void main(){},在函数内添加如下代码:\/\/定义变量 int minusNumber=-10; int plusNumber=0; \/\/转换成正数 plusNumber=abs(minusNumber); \/\/输出结果 printf("转换前:%d\\n",minusNumber); printf(...

求c语言编写四则运算程序
include <stdio.h> include <stdlib.h> include <ctype.h> char token;\/*global token variable*\/ \/*function prototypes for recursive calls*\/ float exp(void);float term(void);float factor(void);void error(void){ fprintf(stderr,"Error\\n");exit(1);} void match(char expectedToken)...

c语言则么怎么实现四则运算
void main(){ float a=0,b=0,c=0,key;char d;printf("输入简单的四则运算表达式:\\n");scanf("%f%c%f",&a,&d,&b);switch(d){ case'+': c=a+b;break;case'-': c=a-b;break;case'x':case'*': c=a*b;break;case'\/': if(b>0) c=a\/b;break;default:break;} printf...

c语言用switch编写一个简单的四则运算程序
代码如下:include <stdio.h> int main(){ float fFloat1=.0,fFloat2=.0;char cOP=NULL;printf("请输入要进行四则运算表达式:\\n");scanf("%f%c%f",&fFloat1,&cOP,&fFloat2);switch(cOP){ case '+':printf("%f+%f=%f\\n",fFloat1,fFloat2,fFloat1+fFloat2);break;case '-':p...

C语言编程 设计一个100以内的整数四则运算测试程序
正确答案是:%d\\n",result);return 0;}int main() {int a,b,t,answer,result;int n = MAXSIZE,yes = 0,no = 0;char op,ops[] = {"+-*\/"};srand((unsigned)time(NULL));while(n--) {op = rand()%4;switch(ops[op]) {case '+' : do {a = rand()%90 + 10;...

c语言四则运算程序怎么写
c语言四则运算程序写法如下:四则运算是数学忠最基杰的运算之-信息括加减乘除四种运算。在计算机编程中四则运算也是最基本的运算之一,常用于算法设计、数据处理等方面。本文将介绍如何使用C语言进行四则运算的编程。加法运算 加法运算是最简单的四则运算之一,其基本形式为atb,其中a和b为加数,+为加号,...

用C语言编程实现一个简单的四则运算计算器
\/\/函数,读数操作数 int getNextNum(){ int ret;scanf("%d",&ret);return ret;} \/\/函数,读运算符 char getOpt(){ return getchar();} \/\/函数,计算 int caculate(int op1 , int op2 ,char opt){ if(opt=='+')return op1+op2;if(opt=='-')return op1-op2;if(opt=='*')...

在C语言环境下开发简单的四则运算的命令行计算器
就是说,如果输入1 + 2,你就计算得3。如果输入1 * 2,你就计算得2。这个很简单的。main(int argc, char *argv[]){ int a,b;a= atoi(argv[1]);b= atoi(argv[3]);if (argv[2]=='+')printf( "\\n%d\\n",a+b);else if (argv[2]=='-')printf( "\\n%d\\n",a-b);else ...

相似回答