【C语言】北邮国院大一下期末复习

由于2020年上半年疫情封校,C语言期末是线上大作业形式。

下文是要求,大概就是做一个计算器。

CPF 课程 2020

写一个能帮助小学生练习数学的程序。

a) 程序将首先向用户询问其 ID 号(包括两  个大写字母和 4 位数字),例如。

请输入您的 ID 号:AB1234

       The program should have ID input validation.

然后程序提示三个选项:

(1) 开始测试

(2) 检查分数

(3) 退出

b)b)  b) 测试:该程序将给出 10 道数学问题,例如:

12 × 3 × 36

48 × 32 × 80

...

56 / 28 = 2

注意:

i) 学生将在下一个问题给出之前回答每个问题。

ii) 问题应包括四种数学运算:加法、减法、乘法和除法。它们是随机生成的。相邻的问题应为不同的操作。每个操作必须appear至少出现一次。

iii) 随机生成问题的数字。但是,必须确保问题和结果都  小于 than  100 和大于 0。操作数和结果应为整数。

iv) 十个问题完成后,记录学生完成 completing这十个问题所用的时间。

v) 给每个学生一个分数。保存此学生的ID、他/她的分数以及使用的时间,放入名为"record.txt"的文件中。 “

vi) 在屏幕上打印以下信息:

奎斯. |更正 Answ。 | 你的Answ

c) 检查分数:  L从文件"记录.txt"” 中为该学生提供的所有历史分数,例如:

您以前的记录包括:

AB1234 80 150

AB1234 50 182

AB1234 90 98

您将根据程序的标记:

(1) 科雷  茨ness

(2) 可读性

(3) 稳健性

(4) 简洁

评分标准

  1. 代码首先由反抄袭程序检测,对于疑似抄袭代码,经手工检查确认后0分
  2. 代码中不能出现中文
  3. 所有代码用C-Free5.0 professional编译,不通过0分;每个warning扣减10分;超过两个0分

序号

内容

扣分

备注

函数少于5个

-10

每少一个-10

没使用结构

-10

FILE 指针除外

没使用指针

-10

FILE 指针除外

无缩进等

-10

编程风格

命名不规范

-10

无注释

-10

不能使用中文

#include <stdio.h>
#include <stdlib.h> 
#include <time.h> 
#include <string.h>
int main()
{
	int i , n = 1 , option , score = 0 , ad , su , mu , di ;
	int a[10] , b[10] , oper[10] , result[10] , put[10];
	int *rPtr = result;
	char id[7] ;
	time_t start , end;
	int add(int x , int y);
	int sub(int x , int y);
	int mult(int x , int y);
	int divi(int x , int y);
	void check(char[]);
	FILE *data;
	struct numsym
	{
		int sequ;	
		char opera;	
	};
	struct numsym link[4] = {{0 , '+'},{1 , '-'},{2 , '*'},{3 , '/'}};
	//Determine if the ID number is vaild.
	while(n)
	{
		printf("Please input your ID number(e.g. AB1234): ");
		scanf("%s",&id);
		//Determine letter format
		if(id[0] >= 'A' && id[0] <= 'Z' && id[1] >= 'A' && id[1] <= 'Z' && id[6] == '\0');
		else
		{
			printf("Sorry , please type in the correct ID number.\n");
			continue;
		}
		//Determine number format
		for(i = 2 ; i <= 5 ; i++)
		{
			if(id[i] >= '0' && id[i] <= '9')
			n = 0;
			else
			{
				printf("Sorry , please type in the correct ID number.\n");
				n = 1;
			}
		}
	}
	//Select three choices
	choice://use goto function to return the three choices
	printf("Then the program prompts three choices:\n(1) Start a test\n(2) Check scores\n(3) Exit\n");
	scanf("%d", &option);
	switch(option)
	{
		//Start a test
		case 1:
		printf("---------------------------------------\n");
		printf("The test begins now.\n");
		srand(time(NULL));
		time(&start);//count time
		//Set the question one by one
		//Determine the operator randomly
		withoutnull://use goto function to make each operation will appear at least once
		for(i = 0 ; i < 10 ; i++)
		{
	 		//The adjacent questions should be different operations
			oper[i] = rand() % 4;
			while(i > 0 && oper[i] == oper[i-1])
			{
				oper[i] = rand() % 4;
				continue;
			}
		}
		//make each operation will appear at least once
		ad = 0 ; su = 0 ; mu = 0 ; di = 0;
		for(i = 0 ; i < 10 ; i++)
		{
			switch(oper[i])
			{
				case 0 : ad++ ; break ;
				case 1 : su++ ; break ;
				case 2 : mu++ ; break ;
				case 3 : di++ ; break ;
			} 
		} 
		if(ad == 0 || su == 0 || mu == 0 || di == 0)
			goto withoutnull;
		for(i = 0 ; i < 10 ; i++)
		{
			//Determine the number randomly 
			do
			{
				a[i] = rand() % 100 ;
				b[i] = rand() % 100 ;
				//calculate data
				switch(oper[i])
				{
					case 0:
					result[i] = add(a[i],b[i]);//Quote addition
					break;
					case 1:
					result[i] = sub(a[i],b[i]);//Quote subtration			
					break;
					case 2:
					result[i] = mult(a[i],b[i]);//Quote multiplication				
					break;
					case 3:
					result[i] = divi(a[i],b[i]);//Quote division				
					break;
				}
			}while(result[i] <= 0 || result[i] >= 100 || (oper[i] == 3 && a[i] % b[i] != 0));
			printf("%2d %c %2d = ",a[i] , link[oper[i]].opera , b[i] );
			scanf("%d" , &put[i]);//Student input result
			if(put[i] == result[i])
				score = score + 10 ; 
		}
		time(&end);//count time
 		//Out the data just caculated
		 printf("---------------------------------------\nYou get %d score(s) in %d seconds.\n" , score , end-start); 
   		printf("And that is your records that you have just done.\n    Ques.    | Correct Answ.  |  Ur Answ\n"); 
	    for(i=0;i<10;i++)
	    	printf("%-2d %c %-2d =          %-3d            %d\n",  a[i] , link[oper[i]].opera , b[i] , *(rPtr+i) , put[i]);
   	 	//Save the information into the file
		data = fopen("record.txt", "a+"); //save the information into the file
      	fprintf(data,"%s %d %d\n", id , score , end-start); 
	 	fclose(data);
	 	printf("---------------------------------------\n");
	 	goto choice;
		break; 
		//Check scores 
		case 2:
		check(id);
		printf("---------------------------------------\n");
		goto choice;
		break;
		//quit the program
		case 3:
		printf("---------------------------------------\n");
		printf("This program is about to quit.\n");
		break;
		//save the program
		default:
		printf("Sorry , please type in the correct number.\n");
		goto choice;
		break; 
	} 
	return 0;
}
int add(int x , int y)//Define addition
{
	int result;
	result = x + y;
	return result;
}
int sub(int x , int y)//Define subtration
{
	int result;
	result = x - y;
	return result;
}	
int mult(int x , int y)//Define multiplication
{
	int result;
	result = x * y;
	return result;
}
int divi(int x , int y)//Define division
{
	int result;
	result = x / y;
	return result;
}
//The function to check scores 
void check(char id[])
{
	FILE *file;
    int ggrade , gtime , j = 0;
    char gid[6];
    file=fopen("record.txt","r"); //open file
    printf("---------------------------------------\n");
    if(file == NULL)
    printf("Sorry , there is no record."); 
    fscanf(file,"%s %d %d" , gid , &ggrade , &gtime);
    printf("Your previous records are:\n");//print the previous records
    while(!feof(file))
    {
    	if(strncmp(id , gid , 6) == 0)//compare data
        {
        	j = 1;
			printf("%s %d %dseconds\n\n" , id , ggrade , gtime);//output data
         }
         fscanf(file,"%s %d %d" , gid , &ggrade , &gtime);//read data
    }
    if( j == 0)  
    printf("Sorry, there is no previous grade.\n");
    fclose(file);//close file
}

上一篇:C++ 动态多态


下一篇:简单工厂模式