structural morphology
R7-1 calculated average score (20 points)
Given the basic information of N students, including student number (string composed of 5 numbers), name (non empty string with length less than 10 and excluding blank characters) and score ([0100] integer in the interval), it is required to calculate their average score and output the list of students below the average line in sequence.
#include<stdio.h> struct score { char num[100]; char name[100];//Student number and name are entered separately double score; }; int main() { struct score s[10000]; int i, n; double average = 0; scanf("%d", &n); for (i = 0; i<n; i++) { scanf("%s %s %lf", &s[i].num, &s[i].name, &s[i].score); average += s[i].score; } average = average / n; printf("%.2f\n", average); for (i = 0; i < n; i++) { if (s[i].score <average)//Select the students who are below average { printf("%s %s\n", s[i].name, s[i].num); } } return 0; }
R7-2 plane vector addition (20 points)
This problem requires writing a program to calculate the sum vector of two two-dimensional plane vectors.
#include <stdio.h> #include <math.h> int main(){ double x1, y1, x2, y2; scanf("%lf %lf %lf %lf", &x1, &y1, &x2, &y2); double x = x1 + x2; double y = y1 + y2; if( x > -0.05 ) x = fabs(x); if( y > -0.05 ) y = fabs(y); printf("(%.1f, %.1f)\n", x, y); return 0; }
R7-3 finding books (20 points)
Given n the name and price of this book, this topic requires writing a program to find and output the name and price of the book with the highest and lowest price.
#include <stdio.h> struct shu//Structure of definition book { char a[31];//title double b;//Price }; int main() { int n,i,max=0,min=0; double temp; struct shu m[10];//Define an array whose elements are the structure of 10 books scanf("%d",&n);//Number of books getchar();//Absorb the carriage return left by scanf function for(i=0;i<n;i++)//To assign a value to the array is to fill in the information of the book { gets(m[i].a);//name scanf("%lf",&m[i].b);//price getchar();//Similarly, absorb carriage return } for(i=1,temp=m[0].b;i<n;i++)//Find the most expensive { if(m[i].b>temp) { max=i; temp=m[i].b;} } for(i=1,temp=m[0].b;i<n;i++)//Find the cheapest { if(m[i].b<temp) { min=i; temp=m[i].b;} } printf("%.2lf, %s\n",m[max].b,m[max].a); printf("%.2lf, %s",m[min].b,m[min].a); return 0; }
R7-4 address book sorting (20 points)
Input the information of n friends, including name, birthday and telephone number. This problem requires writing a program to output the address book in order of age. The title ensures that everyone's birthdays are different.
#include <stdio.h> #include<string.h> struct dian{ char a[1000],b[1000]; double n; }; int main() { int i,j,t; double n; char g[1000]; struct dian c[1000]; scanf("%lf",&n); for(i=0;i<n;i++) { getchar(); scanf("%s%lf%s",c[i].a,&c[i].n,c[i].b); } for(i=1;i<n;i++) { for(j=0;j<n-i;j++) { if(c[j].n>c[j+1].n) { t=c[j].n; c[j].n=c[j+1].n; c[j+1].n=t; strcpy(g,c[j+1].a); strcpy(c[j+1].a,c[j].a); strcpy(c[j].a,g); strcpy(g,c[j+1].b); strcpy(c[j+1].b,c[j].b); strcpy(c[j].b,g); } } } for(i=0;i<n;i++) printf("%s %.0f %s\n",c[i].a,c[i].n,c[i].b); return 0; }
R7-5 calculate employee salary (20 points)
Given the information of N employees, including name, basic salary, floating salary and expenditure, it is required to write a program to output the name and paid salary of each employee in sequence (paid salary = basic salary + floating salary - expenditure).
#include<stdio.h> #define N 1000 struct yuan { char name[11]; double z1;//If int is used here, an error is displayed double z2; double z3; double sum; }ren[N]; int main() { int n,i; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%s %lf %lf %lf",ren[i].name,&ren[i].z1,&ren[i].z2,&ren[i].z3); ren[i].sum=ren[i].z1+ren[i].z2-ren[i].z3; } for(i=0;i<n;i++) { printf("%s %.2lf\n",ren[i].name,ren[i].sum); } return 0; }
function
R7-1 output student achievement (20 points)
This problem requires the preparation of a program to count and output the students' average score, maximum score and minimum score according to the input students' scores. Dynamic memory allocation is recommended.
#include<stdio.h> #include<stdlib.h> int main() { int n; scanf("%d",&n); double *s=malloc(n*sizeof(double)); double avg=0,max,min; int i; for(i=0;i<n;i++) scanf("%lf",s+i); max=s[0]; min=s[0]; for(i=0;i<n;i++) { if(s[i]>max) { max=s[i]; } if(s[i]<min) { min=s[i]; } avg+=s[i]; } printf("average = %.2f\n",avg/n); printf("max = %.2f\n",max); printf("min = %.2f\n",min); return 0; }
R7-2 delete the specified letter in the string (20 points)
Please use the pointer method to write the program. The function of the program is to input a string from the keyboard (the string length is less than 100), delete the letter A and output it. For example, enter the string abcaca and output bcc.
#include<stdio.h> #include<string.h> int main() { char ch[100]; int i,k,j=0; char ah[100]; gets(ch); k=strlen(ch); for(i=0;i<k;i++) { if(ch[i]!='a') ah[j++]=ch[i]; } puts(ah); return 0; }
R7-3 calculate the difference between the maximum and minimum values of 10 numbers by pointer method (20 points)
Please use the pointer method to write the program. The function of the program is to input 10 numbers from the keyboard to find the difference between the maximum and minimum values.
#include<stdio.h> int sub(int *a, int n) { int t,i; int max = a[0], min = a[0]; for(i=0;i<n;i++) { if(a[i]>max) max = a[i]; if(a[i]<min) min = a[i]; } printf("difference value = %d", (max-min)); } int main() { int a[10]; int i; for(i=0;i<10;i++) scanf("%d", &a[i]); sub(a,10); return 0; }