C language experiment 7

Click like before copying

6-1 Experiment 7_ 11_ Cyclic shift (100 points)

Design function void shift (int * array, int num, int size);, Realize the circular movement of elements in an integer array to the left by several positions. The meaning of cyclic shift to the left is as follows:

For example, the original array a [0], a [1] The elements in a [9] are: 1 2 3 4 5 6 7 8 9 10. After moving 1 bit to the left, a [0], a [1] The elements in a [9] are: 2 3 4 5 6 7 8 9 10 1. After the cycle moves 2 bits to the left, a [0], a [1] The elements in a [9] are: 3 4 5 6 7 8 9 10 1 2. And so on.

Function interface definition:

void shift(int *array , int num , int size ) ;

Where , array, num and , size , are parameters passed in by the user. Array ¢ is the pointer to the original array; num is the number of bits shifted to the left; Size is the size of the array. Function has no return value.

Example of referee test procedure:

#include <stdio.h>
#include <stdlib.h>

void shift(int *array , int num , int size ) ;

int main()
{
    int        i , n , p , array[100] ;
    
    scanf(" %d%d" , &n , &p ) ;//Test case guarantee 0 < p < n < = 100
    for( i = 0 ; i < n ; i++ )
        scanf( "%d" , &array[i] ) ;//The test case ensures that all inputs can be stored in integer

    shift( array , p , n ) ;//Shift p bit to the left
    for( i = 0 ; i < n - 1 ; i++ )
        printf( "%d " , array[i] ) ;
    printf( "%d\n" , array[i] ) ;
        
    return 0;    
}

/* Please fill in the answer here */

Input example:

10 1
1 2 3 4 5 6 7 8 9 10

Output example:

2 3 4 5 6 7 8 9 10 1

catalogue

Function interface definition:

Example of referee test procedure:

Input example:

Output example:

Function interface definition:

Example of referee test procedure:

Input example:

Output example:

void shift(int *array,int num,int size){
	int a[100];
	for(int i=0;i<size;i++)
		a[i]=*(array+i);
	for(int i=0;i<size;i++){
		if(i<size-num) *(array+i)=*(array+i+num);
		else *(array+i)=a[i-size+num];
			
	}
	
	
}

6-2 Experiment 7_ 10_ Bullets (100 points)

In a live firing training, the monitor asked the soldiers to round up and fire bullets. First, the squad leader sends several bullets to each person, and then adjusts the bullets in each soldier's hands according to the following method: all soldiers check the number of bullets in their hands. If the number of bullets is odd, ask the squad leader for another one. Then each soldier will give half of his bullets to the next soldier at the same time (the last soldier will give half of his bullets to the first soldier). This adjustment will continue until all soldiers have the same number of bullets in their hands. Now please write a function to simulate the adjustment process.

Function interface definition:

void distribute(int * bullets , int size , int number ) ;

Where , bullets, size , and , number , are parameters passed in by the user. Bullets , is a pointer to an int , type array, in which the number of bullets in each soldier's hand is stored in turn. After each adjustment, the array still stores the number of bullets in each soldier's hand in turn; Size is the total number of soldiers; Number is the number of adjustments. Function has no return value.

Example of referee test procedure:

#include<stdio.h>

#define        LEN        100

//Adjustment function 
void distribute(int * bullets , int size , int number ) ;

int main()
{
    int        bullets[LEN] ;
    int        n , m , i ;    
         
    scanf("%d" , &n ) ;    //Total number of soldiers read in 
    for( i = 0 ; i < n ; i++ )
    {
        scanf("%d" , &bullets[i] ) ;//Read the initial number of bullets in each soldier's hand 
    }
    scanf("%d" , &m ) ;//Number of read in adjustments (M > 0) 
    distribute(bullets , n , m ) ;//adjustment 
    for( i = 0 ; i < n - 1 ; i++ )//Output adjusted results 
    {
        printf("%d " , bullets[i] ) ;
    }
    printf("%d\n" , bullets[i] ) ;
    
    return 0;    
}


/* Please fill in the answer here */

Input example:

10
10 2 8 22 16 4 10 6 14 20
1

Output example:

15 6 5 15 19 10 7 8 10 17

Reference code

void distribute(int * bullets , int size , int number ){
	int a[100]={0};
	for(int i=1;i<=number;i++){
	
		
		for(int j=0;j<size;j++){
		if(*(bullets+j)%2==0)  *(bullets+j)=*(bullets+j)/2;
		else *(bullets+j)=(*(bullets+j)+1)/2;}
		
		for(int j=0;j<size;j++) 
			a[j]=*(bullets+j);
	
		for(int j=1;j<size;j++)
		{ 
			*(bullets+j)+=a[j-1];
		}
		*bullets +=a[size-1];
		
	
	}
}

 

6-3 experiment 10_ 8_ Design function void delcharfun(char *str,char ch) (100 points)

The function void delcharfun(char *str,char ch) is designed to delete the specified character ch from the string str. Large and small letters of the same letter are treated as different characters.

Function interface definition:

void delcharfun(char *str,char ch);

Where , str , and , ch , are parameters passed in by the user. STR = pointer to the array to be deleted; CH) specifies the character. Function has no return value.

Example of referee test procedure:

#include<stdio.h>

void delcharfun(char *str,char ch);
 
int main()
{
    char    ch,str[110];
    
    scanf("%s",str);    //Read in string 
    getchar();            //Read carriage return symbol 
    scanf("%c",&ch);    //Read in character 
    delcharfun(str,ch);    //delete 
    printf("%s\n",str);    //Output results after deletion 
    return 0;    
}

/* Please fill in the answer here */

Input example:

abcABCabc#
b

Output example:

acABCac#

Reference code

 

void delcharfun(char *str,char ch){
	for(int i=0;str[i]!='\0';i++) {
        
        if(str[i]==ch) {
            for(int j=i;str[j]!='\0';j++){
                str[j]=str[j+1];
            }
            i--;
            }
            
      
    }

}

6-4 experiment 10_ 4_ Design function locatesubstr (100 points)

Design the function char *locatesubstr(char *str1,char *str2), find the first occurrence position of the string pointed to by str2 in the string pointed to by str1, and return the pointer pointing to this position. If the string pointed to by str2 is not included in the string pointed to by str1, NULL pointer is returned. Note that you must use a pointer instead of an array subscript to access the string.

Function interface definition:

char *locatesubstr(char *str1,char *str2);

Where , str1 , and , str2 , are parameters passed in by the user, and their meanings are described in the topic. If the search is successful, a pointer to the position is returned; if it fails, a null pointer is returned.

Example of referee test procedure:

#include <stdio.h>

char *locatesubstr(char *str1,char *str2);
int main()
{
    char str1[505],str2[505];
    char *p;
    gets(str1);
    gets(str2);
    p=locatesubstr(str1,str2);
    
    if(p==NULL)    printf("NULL!\n");
    else    puts(p);
    
    return 0;
}

/* Please fill in the answer here */

Input example:

didjfsd dabcxxxxxx
abc

Output example:

abcxxxxxx

Reference code

 

char *locatesubstr(char *str1,char *str2){
	int temp=0;
	while(*str1!='\0'&&*str2!='\0'){
		if(*str1!=*str2) {str1++;temp=0;}
		else {
			while(*str2!='\0'){
				if(*str1==*str2){
					str1++;
					str2++;
					temp++;
				}
				else {str2=str2-temp;temp=0;break;}
			}
			
		}
	}
	str1=str1-temp;
	if(temp==0) return NULL;
	else return str1;
}

6-5 data cleaning (100 points)

Data cleaning refers to the last procedure to find and correct identifiable errors in data files, including checking data consistency, dealing with invalid and missing values, etc. Xiao Ming went to a company for an internship, and the company gave him a task of data cleaning. Since Xiaoming is an intern, the algorithm company of data cleaning has given it. Xiaoming's task is only to store and output the qualified data after cleaning in the original order. But Xiao Ming is still unable to complete this task. Smart, can you help him?

The function prototype of data cleaning is:

int dataCleaning(int n);

Here, parameter n is the data to be processed. If the data is correct, the function returns 1. If the data is incorrect, the function returns 0. In your function, you need to call this function (you don't need to implement it, just call it directly) to judge whether the data is correct.

Function interface definition:

int getData(int source[] , int size , int target[] );

Where , source , size , and , target , are parameters passed in by the user. Source is the data to be cleaned; Size is the length of the array source; Target is the correct data. The return value of the function is the number of correct data.

Example of referee test procedure:

#include<stdio.h>

int getData(int source[] , int size , int target[] );

int dataCleaning(int n);

int main()
{
    int        source[110] , target[110];
    int        i , n , size ;
    
    scanf( "%d", &n ) ;
    for ( i = 0 ; i < n ; i++ )
        scanf( "%d", &source[i] ) ; 
    size = getData( source , n, target ) ; 
    for ( i = 0 ; i < size - 1 ; i++ )
        printf( "%d " , target[i] ) ;
    printf( "%d\n" , target[i] ) ;
    
    return 0;    
}

/* Please fill in the answer here */

Input example:

10
62 58 20 69 92 4 99 67 12 88

Output example:

Example explanation: only 69, 99 and 12 of the 10 integers entered are correct data.

69 99 12

Reference code

int getData(int source[] , int size , int target[] ){
	int x=0;
	int test;
	for(int i=0;i<size;i++){
		test=dataCleaning(source[i]);
		if(test==1) {*(target+x)=source[i];x++;}	
	}
	return x;
}

6-6 alpha product of recursive solution (100 points)

Calculates the alpha product of an integer. For an integer x, its alpha product is calculated as follows: if x is a number of digits, its alpha product is itself; Otherwise, the alpha product of X is equal to the alpha product of the integer obtained by multiplying its non-zero digits. For example, the alpha product of 4018224312 is equal to 8, which is calculated according to the following steps:

  4018224312 → 4*1*8*2*2*4*3*1*2 → 3072 → 3*7*2 → 42 → 4*2 → 8

Write a program to input a positive integer (the integer can be stored in long long) and output its alpha product.

Input format: input only one line, that is, a positive integer.

Output format: output the corresponding alpha product.

This question is required to be implemented with recursive functions, and global variables are not allowed. No score will be given in other ways.

Function interface definition:

long long alpha(long long n) ;

Where n is the parameter passed in by the user. The value of n , does not exceed the range of long long. The function must return the alpha product of N +.

Example of referee test procedure:

#include <stdio.h>

//Find a function of integer alpha product 
long long alpha(long long n) ;

int main()
{
    long long n;
    
    scanf("%lld",&n);    
    printf("%lld\n",alpha(n));
    
    return 0 ;
}

/* Please fill in the answer here */

Input example:

4018224312

Output example:

8

Reference code

long long alpha(long long n){
	long long int x=1,test;
	if(n<10) return n;
	else{
		
	while(n!=0){
		test=n%10;
		if(test!=0) {x=x*test;n=n/10;}
		else {n=n/10;continue;}		
	}	
	return alpha(x);
	}
	
}

 

6-7 04_ Split string (100 points)

Any given string s (less than 100 in length) contains only uppercase letters "A-Z" and lowercase letters "A-Z". Your task is to separate the uppercase and lowercase letters in S and store them in the specified character array respectively. The original alphabetical order remains unchanged after splitting. This problem is required to be realized by function.

Function interface definition:

The function interfaces are as follows:
void split(const char *s, char *upper, char *lower) ;

Where , s, upper , and , lower , are parameters passed in by the user. Where s is the string to be split, upper is the character array with uppercase letters saved after splitting, and lower is the character array with lowercase letters saved after splitting. The return value of the function is null.

Example of referee test procedure:

An example of a function being called for testing is as follows:
#include<stdio.h>

void split(const char *s, char *upper, char *lower) ;

int main()
{
       char    s[100] , upper[100] , lower[100] ; 
       
       scanf("%s",s);
      split( s , upper , lower ) ;    
       printf("%s\n%s\n", upper , lower) ;

    return 0;
}

/* Please fill in the answer here */

Input example:

HwEorLLldO

Output example:

HELLO
world

Reference code

void split(const char *s, char *upper, char *lower){
	while(*s!='\0'){
		if(*s>='A'&&*s<='Z'){
			*upper=*s;
			upper++;
			s++;
		}
		else if(*s>='a'&&*s<='z'){
			*lower=*s;
			lower++;
			s++;
		}
		*upper='\0';
		*lower='\0';
		
	}

 

 

6-8 experiment 11_ 4_ Comprehensive score ranking - Swap (100 points)

As we all know, the computer institute should assess the students who apply to be transferred to the computer category. The assessment methods include machine test and interview. The student's comprehensive score adopts the percentage system, of which 50% is calculated by the machine test score and the other 50% is calculated by the weighted score obtained by the student. Those with excellent comprehensive results can enter the interview. Now please write a program to determine the ranking of comprehensive scores according to students' scores.

Input: the first line is an integer n (0 < n < 100), representing the number of students. The following n is the student information in the format of a string representing the student number (no more than 15 in length), followed by two integers, the former represents the computer test score, and the latter represents the weighted score obtained by the student. (both grades are between 0 and 100).

Output: n lines in total. The information of each student is output from high to low according to the comprehensive score (machine test score plus weighted score). If the comprehensive score is the same, the student with high machine test score will be the first. The format of each line is: a string (Student ID) plus three integers (the four data are separated by a space). The three integers are comprehensive score, machine test score and weighted score in turn. The test case ensures that the input is legal and there are no students with exactly the same score.

The definition of structure and the meaning of each member used in the program are as follows:

typedef struct
{
    char    id[16]  ;    //Student account 
    int    total ;    //Comprehensive achievements 
    int    ce ;    //Machine test results 
    int    ws ;    //Weighted score 
}STUDENT;

Three functions are used in the program:

void Swap(STUDENT * s1,STUDENT * s2) ;

The parameters S1 and S2 are pointers to two structures, and their function is to exchange the values in the structures pointed to by the two pointers with each other.

int Comp(STUDENT * s1,STUDENT * s2) ;

The parameters s1 and S2 are pointers to two structures. Their function is to compare the two structures. If s1 > S2, it returns 1, otherwise it returns 0. Here, s1 > S2 refers to the high total score of s1, or the same total score refers to the high machine test score of s1. The test case ensures that the input is legal and there are no students with exactly the same grades.

void Sort(STUDENT a[],int size) ;

Where parameter a is the array of structures to be sorted, and size is the number of students in the array. Its function is to sort a according to the specified rules of the topic.

Example of referee test procedure:

#include <stdio.h>

typedef struct
{
    char    id[16]  ;//Student account 
    int        total ;    //Comprehensive achievements 
    int        ce ;    //Machine test results 
    int        ws ;    //Weighted score 
}STUDENT;

void Sort(STUDENT a[],int size) ; 
void Swap(STUDENT * s1,STUDENT * s2) ;
int  Comp(STUDENT * s1,STUDENT * s2) ;

int main()
{
    STUDENT    stu[100] ;
    int        i , n ;
    
    
    scanf("%d",&n) ;
    for(i=0;i<n;i++)
    {
        scanf("%s%d%d",stu[i].id,&stu[i].ce,&stu[i].ws) ;
        stu[i].total = stu[i].ce+stu[i].ws ;
    }
            
    Sort(stu,n) ;
    for(i=0;i<n;i++)
        printf("%s %d %d %d\n",stu[i].id,stu[i].total,stu[i].ce,stu[i].ws) ;
    
    return 0;    
}

/* Please fill in the answer here */

Input example:

3
bupt_2018210001 78 74
bupt_2018210002 95 71
bupt_2018210003 84 75

Output example:

bupt_2018210002 166 95 71
bupt_2018210003 159 84 75
bupt_2018210001 152 78 74

Reference code

//All three functions are here

void Swap(STUDENT * s1,STUDENT * s2){
	STUDENT stu;
	strcpy(stu.id,s1->id);
	stu.total=s1->total;
	stu.ce=s1->ce;
	stu.ws=s1->ws;
	
	strcpy(s1->id,s2->id);
	s1->total=s2->total;
	s1->ce=s2->ce;
	s1->ws=s2->ws;
	
	strcpy(s2->id,stu.id);
	s2->total=stu.total;
	s2->ce=stu.ce;
	s2->ws=stu.ws;
	
}

int  Comp(STUDENT * s1,STUDENT * s2){
	if(s1->total > s2->total) return 1;
	else if(s1->total==s2->total){
		if(s1->ce>s2->ce) return 1;
		else return 0;
	}
    else if(s1->total<s2->total) return 0;
}



void Sort(STUDENT a[],int size){
	for(int i=0;i<size;i++){
		for(int j=0;j<size-i-1;j++){
			if(Comp(&a[j],&a[j+1])==0){
				Swap(&a[j],&a[j+1]);
			}
		}
	}
}

7-1 word statistics (100 points)

A given string contains only # and lowercase letters, where # is a separator, and consecutive lowercase letters form a word. There is no # sign inside a word, and there is no # between two words. Please write a program to count the number and # of words in a string.

Input format:

The first line is an integer n (0 < n < 10), representing a total of N strings. The next N lines, each of which is a string with a length of no more than 100.

The test case ensures that the input is legal.

Output format:

There are n lines in total, and each line corresponds to each string.

The format of each line is as follows:

The first is an integer representing the number of words, followed by a space; Then there is an integer representing # the quantity, followed by another space; Finally, output all words in turn, separated by a space between the words, and there is no space after the last word. If the number of words is 0, three consecutive words are output where the words should be output #.

Input example:

2
#
hello#world

Output example:

0 1 ###
2 1 hello world

Reference code

#include<stdio.h>
#include<string.h>
int main(){
	int i,j,n;
	char str[105];
	int len;
	int x=0,y=0;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%s",str);
		len=strlen(str);
		x=0;y=0;
		char b[105];
		int ppp=0;
		for(j=0;j<len;j++){
			if(str[j]!='#'){
				if(j==0||str[j-1]=='#') {x++;}
				b[ppp]=str[j];
				ppp++;
			}
			else{
				if(j<len-1&&str[j+1]!='#'&&x>0){
					b[ppp]=' ';
					ppp++;
				}
			y++;	
			}
			
		}
		b[ppp]='\0';
		printf("%d %d ",x,y);
		if(x==0){
			printf("###\n");
			continue;
		}
		else{
			printf("%s\n",b);
		}
	}
}

7-2 experiment 11_ 1_ Initial knowledge of structure (100 points)

The attributes of students include name, student number, score of 5 courses, average score and total score. Given a student's name, student number and the scores of five courses, your task is to calculate the student's average score and total score, sort the student's scores of five courses from top to bottom, and finally output the complete information of the student. Students' names can only contain uppercase and lowercase letters and space characters, no more than 20 characters; The student number is a string with a length of no more than 20, which only contains numeric characters; Course grades are integers from 0 to 100.

requirement:

1. In this question, you need to design a structure to store students' information. In this structure, there needs to be a character array to store the name; A character array to store the student number; An integer array with a length of 5 to store the scores of 5 courses; A double precision floating-point variable stores the average score and an integer variable stores the total score.

2. When assigning and sorting the members of structural variables, you should use the method of "structural variable name + '. + structural member name" to access variables, such as "student.score"; When outputting student information, you should use a structure pointer to point to the structure, and then use the structure pointer to access the variables in the structure, that is, "structure pointer name + '- >' + structure member name", such as "PTR - > score".

Input format:

The input of student information is in the order of name, student number and grade of 5 courses, accounting for three lines. See the example for the specific format.

Output format:

Name in one line; Student number in one line; 5 grades are separated by a space in the middle, and the last grade is followed by a newline character, which is sorted from high to bottom, accounting for one line; The average score and the total score are separated by a space, occupying one line, and the average score retains two decimal places.

Input example:

Liu Mengmeng
0821131666666
88 90 93 91 85

Output example:

Name:Liu Mengmeng
ID:0821131666666
Score:93 91 90 88 85
average:89.40 total:447

 

Reference code

#include<stdio.h>
#include<string.h>
struct student{
	char name[25],id[25];
	int score[6];
	
};
int main(){
	struct student a;
	int total=0;
	int temp;
	float ave;
	gets(a.name);
	gets(a.id);
	for(int i=0;i<5;i++){
		scanf("%d",&a.score[i]);
		total+=a.score[i];
		
	}
	ave=(float)total/5;
	for(int i=0;i<5;i++){
		for(int j=0;j<5-i-1;j++){
			if(a.score[j]<a.score[j+1]){
				temp=a.score[j+1];
				a.score[j+1]=a.score[j];
				a.score[j]=temp;
			}
		}
	}
		
		
	printf("Name:%s\nID:%s\n",a.name,a.id);
	printf("Score:%d %d %d %d %d\n",a.score[0],a.score[1],a.score[2],a.score[3],a.score[4]);
	printf("average:%.2f total:%d",ave,total);
	
	
}

7-3 experiment 11_ 2_ Initial structure array (100 points)

There are n students. The information of each student includes name, student number, score of 5 courses, average score and total score. Given the student's name, student number and grades of 5 courses, your task is to calculate the average and total scores of each student and output them. Students' names can only contain uppercase and lowercase letters and space characters, no more than 20 characters; The student number is a string with a length of no more than 20, which only contains numeric characters; Course grades are integers from 0 to 100.

Requirement: in this question, you need to design a structure to store a student's information. In this structure, there needs to be a character array to store the name; A character array to store the student number; An integer array with a length of 5 to store the scores of 5 courses; A double precision floating-point variable stores the average score and an integer variable stores the total score. Then, you need to design a structure array to store the information of n students.

Input format:

The first input is a positive integer n, representing the number of students, 1 < = n < = 100; The information of each student is entered in the order of name, student number and grade of 5 courses, accounting for three lines in total. See the sample for the specific input format.

Output format:

Name in one line; Student number in one line; The scores of 5 courses are separated by spaces, and the last score is followed by a newline character, accounting for one line; The average score and the total score are separated by a space, accounting for one line, and the average score retains two decimal places; A blank line is output after each student's information. Note: a blank line will be output after each student's information.

Input example:

4
xiaowang
0821131699999
87 98 79 90 68
Liu Mengmeng
0821131666666
88 90 93 91 85
Albert Einstein
0821131477777
75 87 100 66 64
Bill Gates
0821131588888
65 58 77 60 61

Output example:

Name:xiaowang
ID:0821131699999
Score:87 98 79 90 68
average:84.40 total:422

Name:Liu Mengmeng
ID:0821131666666
Score:88 90 93 91 85
average:89.40 total:447

Name:Albert Einstein
ID:0821131477777
Score:75 87 100 66 64
average:78.40 total:392

Name:Bill Gates
ID:0821131588888
Score:65 58 77 60 61
average:64.20 total:321

 

Reference code

#include<stdio.h>
#include<string.h>
struct inform{
	char name[25];
	char num[25];
	int score[5];
	float pin;
	int zong;
};
int main(){
	int i,j;
	int n;
	struct inform stu[101];

	scanf("%d",&n);

	for(i=0;i<n;i++){
		getchar();
		gets(stu[i].name);
		gets(stu[i].num);
		for(j=0;j<5;j++){
			scanf("%d",&stu[i].score[j]);
		}

	}
	for(i=0;i<n;i++){
		stu[i].zong=0;
		for(j=0;j<5;j++){
			stu[i].zong+=stu[i].score[j];
		}
		stu[i].pin=(float)stu[i].zong/5;
	}
	for(i=0;i<n;i++){
		printf("Name:%s\n",stu[i].name);
		printf("ID:%s\n",stu[i].num);
		printf("Score:%d %d %d %d %d\n",stu[i].score[0],stu[i].score[1],stu[i].score[2],stu[i].score[3],stu[i].score[4]);
		printf("average:%.2f total:%d\n",stu[i].pin,stu[i].zong);
		printf("\n");


	}



}

7-4 experiment 11_ 3_ Structure ranking (100 points)

There are n students, and the attributes of each student include name and total score. Given the student's name and total score, your task is to sort the student's information in the following way: first, compare the total score. The one with high total score is in the front and the one with low total score is in the back. When the total score is the same, you should compare the student's name. The student with small name dictionary order is in the front and the student with large name dictionary order is in the back (ASCII code order). The range of n is 1-100; Students' names can only contain uppercase and lowercase letters, no more than 20 characters; The total score is an integer.

Requirements: in this question, you need to design a structure to store students' information. In this structure, you need a character array to store the name and an integer variable to store the total score.

Input format:

First, enter a positive integer n, representing the number of students, 1 < = n < = 100; The information of each student shall be input in the order of name and total score (separate spaces), and each student's information shall occupy one line. See the sample for the specific format.

Output format:

For the information of n students, the name accounts for one line and the total score accounts for one line. The output order shall be in accordance with the requirements of the topic. After the information of each student, a blank line shall be output. Note: a blank line will be output after each student's information.

Input example:

4
AlbertEinstein 1328
GeorgeWalkerBush 860
LiuMengmeng 1475
BillGates 1328

Output example:

Name:LiuMengmeng
total:1475

Name:AlbertEinstein
total:1328

Name:BillGates
total:1328

Name:GeorgeWalkerBush
total:860

Reference code

#include<stdio.h>
#include<string.h>
struct student{
	char name[100];
	int total;
};

int main(){
	struct student a[105];
	struct student tmp;
	int n;
	int test;
	
	scanf("%d",&n);
	
	for(int i=0;i<n;i++){
		scanf("%s %d",a[i].name,&a[i].total);
		
	}
	
	for(int i=0;i<n-1;i++){
		for(int j=0;j<n-i-1;j++){
			if(a[j].total<a[j+1].total){
				tmp=a[j+1];
				a[j+1]=a[j];
				a[j]=tmp;
				
			}
			else if(a[j].total==a[j+1].total){
				test=strcmp(a[j].name,a[j+1].name);
				if(test>0){
					tmp=a[j+1];
					a[j+1]=a[j];
					a[j]=tmp;
					
				}
				
			}
		}
	}
	for(int i=0;i<n;i++){
		printf("Name:%s\ntotal:%d\n\n",a[i].name,a[i].total);
	}
}

7-5 experiment 11_ 7_ Student information management system (100 points)

To create a student information management system, the specific requirements are as follows:

Student information includes: student number, name, math score, English score, computer score

Function 1: when adding student information, enter student number, name and grades of three subjects; "Add success" is output if the student is added successfully, and "Students already exist" is output if the student already exists

Function 2: when deleting student information, enter student number information; If the student does not exist, output "Students do not exist". If it does exist, output "Delete success"

Function 3: change the student's grade information. When executing 3, enter the student number information; If the student does not exist, output "Students do not exist". If it does exist, output "Update success"

Function 4: display the average score of students, enter the student number information when executing 4; If the student does not exist, output "Students do not exist". If it does exist, output the student information in the following format:

Student ID:2019989890

Name:Jerry

Average Score:89.3

Among them, the average score is divided into three subjects. Add and divide by 3, keep one decimal place, and wrap lines between each line.

Input format:

The first line is an integer n (0 < n < 130), followed by N lines, and each line represents the execution of a function. Among them, 1, 2, 3 and 4 respectively perform the above four functions. See the input example for the specific format. The test case ensures that the student number and name are strings with a length of no more than 10, and the scores of each course are integers between 0 and 100.

Output format:

Input example:

8
1 201817123 Tom 89 80 76
1 2019989890 Jerry 78 99 67
4 201817123
2 201817123
4 201817123
4 2019989890
3 2019989890 79 90 99
4 2019989890

Output example:

Add success
Add success
Student ID:201817123
Name:Tom
Average Score:81.7
Delete success
Students do not exist
Student ID:2019989890
Name:Jerry
Average Score:81.3
Update success
Student ID:2019989890
Name:Jerry
Average Score:89.3

Reference code

#include <stdio.h>
#include <string.h>
typedef struct{
 char id[50], name[50];
 int math, english, sc; }stu;
stu students[150];
int n = 0;
void add(char id[], char name[], int math, int english, int sc) {
 int i;
 for (i = 0; i < n; i++) {
 if (strcmp(students[i].id, id) == 0) {
 printf("Students already exist\n");
 return ;
 }
 }
 strcpy(students[n].id, id);
 strcpy(students[n].name, name);
 students[n].math = math;
 students[n].english = english;
 students[n].sc = sc;
 n ++;
 printf("Add success\n");
}
void delete(char id[]) {
 int i, j;
 for (i = 0; i < n; i++) {
 if (strcmp(students[i].id, id) == 0) {
 for (j = i; j < n - 1; j++) {
 students[j] = students[j + 1];
 }
 n --;
 printf("Delete success\n");
 return ;
 }
 }
 printf("Students do not exist\n");
}
void update(char id[], int math, int english, int sc) {
 int i;
 for (i = 0; i < n; i++) {
 if (strcmp(students[i].id, id) == 0) {
 students[i].math = math;
 students[i].english = english;
 students[i].sc = sc;
 printf("Update success\n");
 return ;
 }
 }
 printf("Students do not exist\n");
}
void show(char id[]) {
 int i;
 for (i = 0; i < n; i++) {
 if (strcmp(students[i].id, id) == 0) {
 printf("Student ID:%s\n", students[i].id);
 printf("Name:%s\n", students[i].name);
 printf("Average Score:%.1lf\n", (students[i].math +
students[i].english + students[i].sc) / 3.0);
 return ;
 }
 }
 printf("Students do not exist\n");
}
int main() {
 int i, option, m;
 char id[50], name[50];
 int math, english, sc;
 
 scanf("%d", &m);
 
 for (i = 0; i < m; i++) {
 scanf("%d", &option);
 switch(option) {
 case 1:
 scanf("%s %s %d %d %d", id, name, &math, &english, &sc);
 add(id, name, math, english, sc);
 break;
 case 2:
 scanf("%s", id);
 delete(id);
 break;
 case 3:
 scanf("%s %d %d %d", id, &math, &english, &sc);
 update(id, math, english, sc);
 break;
 case 4:
 scanf("%s", id);
 show(id);
 break;
 }
 }
}

Keywords: C

Added by Alienware on Fri, 21 Jan 2022 22:58:48 +0200