课程作业1

修改后代码如下

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
	int n,a[4],key[100],b[100],k=0;
	char c[3];
    void scan();//该函数用于与用户交互,获取输入数据
    void randomNumber(int a[]);//用于随机生成数字
    void randomOperation(char c[]);//用于随机生成运算符
    void generateExpression(int a[],int key[],int b[],char c[],int n);//用于生成运算式
    int caculate(int a[4],char c[3]);//用于计算生成的运算式的结果
    int caculate1(char c,int a1,int a2);//用于判断运算符
    void judge(int key,int b);//用于判断答案正确与否
	void print(int key[],int b[],int n);//将统计结果输出给用户
	scan();//引用用户交互函数
	scanf("%d",&n);
    randomNumber(a);//引用随机数生成函数
    randomOperation(c);//引用随机字符生成函数
    generateExpression(a,key,b,c,n);//引用算式生成函数
    print(key,b,n);//统计结果函数
	return(0);
}
void scan()
{
	int l,ch;
	FILE *fp;
	printf("please type the language:\n");
	printf("1.English\n2.中文\n3.Deutsch\n4.Francais\n5.日本語\n");
	scanf("%d",&l);
	if(l==1)
	{
    if((fp = fopen("W:\\English.txt", "r")) != NULL)
        while((ch = fgetc(fp)) != EOF)
            putchar(ch);
    else
        printf("fail to open! \n");
    fclose(fp);
	}
	if(l==2)
	{
    if((fp = fopen("W:\\中文.txt", "r")) != NULL)
        while((ch = fgetc(fp)) != EOF)
            putchar(ch);
    else
        printf("fail to open! \n");
    fclose(fp);
	}
	if(l==3)
	{
    if((fp = fopen("W:\\Deutsch.txt", "r")) != NULL)
        while((ch = fgetc(fp)) != EOF)
            putchar(ch);
    else
        printf("fail to open! \n");
    fclose(fp);
	}
	if(l==4)
	{
    if((fp = fopen("W:\\Francais.txt", "r")) != NULL)
        while((ch = fgetc(fp)) != EOF)
            putchar(ch);
    else
        printf("fail to open! \n");
    fclose(fp);
	}
	if(l==5)
	{
    if((fp = fopen("W:\\日本語.txt", "r")) != NULL)
        while((ch = fgetc(fp)) != EOF)
            putchar(ch);
    else
        printf("fail to open! \n");
    fclose(fp);
	}
}
void randomNumber(int a[])
{
	int j;
    srand((unsigned)time(NULL));
    for(j=0;j<4;j++)
		a[j]=rand()%11;
}
void randomOperation(char c[])
{
	int j;
	char c1[3]={'+','-','*'};
    srand((unsigned)time(NULL));
	for(j=0;j<3;j++)
		c[j]=c1[rand()%3];
}
int caculate(int a[4],char c[3])
{
	int i,t=0,h=1;
	for(i=0;i<3;i++)
	{
		if(c[i]=='*')
		{
			a[i]=a[i]*a[i+1];
			a[i+1]=0;
			t=i;
			h=i;
		}
		break;
	}
	for(i=t+1;i<3;i++)
	{
		if(c[i]=='*')
		{
			if(i==h+1&&t==h)
			{
				a[t]=a[t]*a[i+1];
				a[i+1]=0;
				h++;
			}
			else
			{
				t=i;
				h=i;
				a[t]=a[t]*a[i+1];
				a[i+1]=0;
			}
		}
	}
	for(i=0;i<3;i++)
	{
		switch(c[i])
		{
		case'+':a[i+1]=a[i]+a[i+1];break;
		case'-':a[i+1]=a[i]-a[i+1];break;
		default:a[i+1]=a[i]+a[i+1];break;
		}
	}
	return(a[i]);
}
int caculate1(char c,int a1,int a2)
{
	int a;
	switch(c)
	{
	case'+':a=a1+a2;break;
	case'-':a=a1-a2;break;
	case'*':a=a1*a2;break;
	}
	return(a);
}
void judge(int key,int b)
{
	if(key==b)
		printf("Congratulations, your answer is correct!\n");
	else 
		printf("Sorry, your answer is wrong!\n");
}
void generateExpression(int a[],int key[],int b[],char c[],int n)
{
	int a1,i;
	srand((unsigned)time(NULL));
	for(i=0;i<n;i++)
	{
		a1=rand()%4;
		if(a1==0)
		{
			printf("%d%c%d%c%d%c%d=",a[0],c[0],a[1],c[1],a[2],c[2],a[3]);
			key[i]=caculate(a,c);
			scanf("%d",&b[i]);
			judge(key[i],b[i]);
		}
		if(a1==1)
		{
			printf("%d%c%d%c(%d%c%d)=",a[0],c[0],a[1],c[1],a[2],c[2],a[3]);
			a[2]=caculate1(c[2],a[2],a[3]);
			a[3]=0;
			c[2]='+';
			key[i]=caculate(a,c);
			scanf("%d",&b[i]);
			judge(key[i],b[i]);
		}
		if(a1==2)
		{
			printf("(%d%c%d)%c%d%c%d=",a[0],c[0],a[1],c[1],a[2],c[2],a[3]);
			a[0]=caculate1(c[0],a[0],a[1]);
			a[1]=0;
			c[0]='+';
			key[i]=caculate(a,c);
			scanf("%d",&b[i]);
			judge(key[i],b[i]);
		}
		if(a1==3)
		{
			printf("(%d%c%d)%c(%d%c%d)=",a[0],c[0],a[1],c[1],a[2],c[2],a[3]);
			a[0]=caculate1(c[0],a[0],a[1]);
			a[1]=0;
			c[0]='+';
			a[2]=caculate1(c[2],a[2],a[3]);
			a[3]=0;
			c[2]='+';
			key[i]=caculate(a,c);
			scanf("%d",&b[i]);
			judge(key[i],b[i]);
		}
	}
}
void print(int key[],int b[],int n)
{
	int i,k=0;
	for(i=0;i<n;i++)
	{
		if(key[i]==b[i])
			k=k+1;
	}
	printf("correct number:%d\n",k);
	printf("wrong number:%d",n-k);
}
posted @ 2017-04-24 21:53  代码小渣渣  阅读(147)  评论(1编辑  收藏  举报