[C language] final review of Peking University of Posts

Keywords: C

Due to the closure of the school in the first half of 2020, C language is an online homework at the end of the term.

The following is the requirement, which is to make a calculator.

CPF course 2020

Write a program that can help primary school students practice mathematics.

a) The program will first ask the user for his ID number (including two)   Capital letters and 4 digits), for example.

Please enter your ID number: AB1234

       The program should have ID input validation.

The program then prompts for three options:

(1) Start test

(2) Check score

(3) Exit

b)b)   b) Test: the program will give 10 mathematical problems, such as:

12 × 3 × 36

48 × 32 × 80

...

56 / 28 = 2

be careful:

i) Students will answer each question before the next one is given.

ii) the problem shall include four mathematical operations: addition, subtraction, multiplication and division. They are randomly generated. Adjacent problems should be different operations. Each operation must appear at least once.

iii) number of randomly generated problems. However, it is important to ensure that both the problems and the results are   Less than than   100 and greater than 0. Operands and results should be integers.

iv) after completing the ten questions, record the time taken by students to complete the ten questions.

v) Give each student a score. Save this student's ID, his / her score and the time used, and put it into a file called "record.txt". “

vi) print the following information on the screen:

Kris | correct answer|   Your answer

c) Inspection score:   L all historical scores provided to the student from the file "record. txt", for example:

Your previous records include:

AB1234 80 150 seconds

AB1234 50 182 seconds

AB1234 90 98 seconds

You will be marked according to the program:

(1) Corey   Z ness

(2) Readability

(3) Robustness

(4) Succinct

Scoring criteria

  1. The code is first detected by the anti plagiarism program. For the suspected plagiarized code, 0 point is given after manual inspection and confirmation
  2. Chinese cannot appear in the code
  3. All codes are compiled with C-Free5.0 professional and fail to pass 0 point; Deduct 10 points for each warning; More than two 0 points

Serial number

content

Deduct points

remarks

Less than 5 functions

-10

- 10 for each less

No structure used

-10

Except for the FILE pointer

Pointer not used

-10

Except for the FILE pointer

No indentation, etc

-10

Programming style

Nonstandard naming

-10

No comment

-10

Cannot use Chinese

#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
}

Posted by bassguru on Sun, 28 Nov 2021 23:22:22 -0800