1. 程式人生 > >一個小學生練習題 能實現基本的加減乘除

一個小學生練習題 能實現基本的加減乘除

設計一個程式,用來實現幫助小學生進行算術運算練習,它具有以下功能:
(1)提供10道加、減、乘、除四種基本算術運算的題目,每道題中的運算元是隨機產生的;
(2)加減是100以內的數;乘除為乘法表裡的;被減數大於減數;除法要求能整除;被乘數小與乘數;(若不合格重新產生)
(3)練習者根據顯示的題目輸入自己的答案,程式自動判斷輸入的答案是否正確並顯示出相應的資訊。最後顯示做對了幾道題;

(4)每次答題要記錄做錯的題目,下次做題可選擇上次做錯的題;

#include<iostream>
#include<stdio.h>
#include<cstdlib>
#include<time.h>
#define N 10
using namespace std;
struct math
{
	int i;
	int a,b;
	char op;
	int result;
	int ans;
 	bool test; 
	struct math *next;
};
struct math *creat()
{	int j;
	struct math *head, *p1, *p2;
	srand(time(0));
	head=p2=(struct math *)malloc(sizeof(struct math));
	for(int i=0; i<N; i++)
	{	
		j=rand()%4;
		p1=(struct math *)malloc(sizeof(struct math));
		switch(j)
		{
			case 1: p1->op='+';
					p1->a=rand()%100;
					p1->b=rand()%100;
					p1->result=p1->a+p1->b;
					break;
			case 2: p1->op='-';
					p1->a=rand()%100;
					p1->b=rand()%100;
					while(p1->a<p1->b)
					p1->a=rand()%100;
					p1->result=p1->a-p1->b;
					break;
			case 3: p1->op='x';
					p1->a=rand()%10;
					p1->b=rand()%10;
					while(p1->a<p1->b)
					p1->a=rand()%10;
					p1->result=p1->a*p1->b;
					break;
			default : p1->op='/';
					p1->b=rand()%10;
					p1->a=(rand()%10)*p1->b;
					p1->result=p1->a/p1->b; 
					break;	
		}
		p2->next=p1;
		p2=p1;
	}	
	p1->next=NULL;
	return head;
}
void baocun(struct math *head)
{
	FILE *fp;
	struct math *p;
	p=head->next;
	if((fp=fopen("tian.txt","w"))==NULL)
	{
		cout<<"open error"<<endl;
		exit(0);
	}
	else 
	while(p!=NULL)
	{	
	if(fwrite(p,sizeof(struct math),1,fp)!=1)
	printf("error\n");
	else
	p=p->next;
	}
	//printf("儲存成功!\n");
	fclose (fp);	
}
struct math *read (struct math *head)
{
	FILE *fp;
	struct math *p1,*p2;
	if((fp=fopen("tian.txt","r"))==NULL)
	{
	printf("tttttttttttttttttttt\n");
	printf("error\n");
	exit (0);
	}	
	p2=head=(struct math *)malloc(sizeof(struct math));
	p1=(struct math *)malloc(sizeof(struct math));
	head->next=p1;
	while(fread(p1,sizeof(struct math),1,fp)==1)
	{
	p2->next=p1;
	p2=p1;
	p1=(struct math *)malloc(sizeof (struct math));
	}
	p1->next=NULL;
	fclose(fp);
	return head;
}	
void test()
{
	struct math *p,*head;
	int i=1;
	int x;
	int count=0;
	cin>>x;
	switch(x)
	{
		case 1: head=creat();
				p=head->next;
				while(p!=NULL)
				{
					p->i=i;
				cout<<"第"<<p->i<<"道題:"<<p->a<<p->op<<p->b<<"=";
				cin>>p->ans;
				if(p->ans==p->result)
				{
				cout<<"正確!"<<endl;				
				count++;
				p->test=true;
				}
				else 
				{
					cout<<"錯誤!"<<endl;
					p->test=false;
				}
				p=p->next;
				i++;
				}
				cout<<N<<"道題做對了"<<count<<"道"<<endl;
				baocun(head);
				break;
		case 2:	
				head=read(head);
				p=head->next;
				while(p!=NULL)
				{
					if(p->test==false)
					{
					cout<<"第"<<p->i<<"道題:"<<p->a<<p->op<<p->b<<"=";
					cin>>p->ans;
					if(p->ans==p->result)
					{
					cout<<"正確!"<<endl;
					p->test=true;	
					}
					else 
					{
					cout<<"錯誤!"<<endl;
					p->test=false;
					count++;
					}	
					}
					p=p->next;
				} 
				if(count==0)
				{
					cout<<"全部都答對了!"<<endl;
				}
				else
				cout<<"還有"<< count<<"道沒答對"<<endl;
				baocun(head);
				break;
		 case 3: exit(0);
	}
}
void menu()
{
	cout<<" 小學生練習題"<<endl;
	cout<<"1. 開始練習"<<endl;
	cout<<"2. 練習錯題"<<endl;
	cout<<"3. 退出程式"<<endl; 
	cout<<"選擇執行的命令"<<endl;
}
int main()
{
	while(1)
	{
	menu();
	test();	
	}	
	return 0;	 
}

執行主介面為


可以隨機產生十道題,做錯的題目可以下次通過練習錯題來繼續做。