Current location - Training Enrollment Network - Mathematics courses - C language programming problem solving
C language programming problem solving
Why are there so many questions? Information management software for students' grades. Here you are. I made it this week, hehe, and I'm using it now.

# include & ltstdio.h & gt

# include & ltconio.h & gt

# include & ltstring.h & gt// contains header files that use strcmp.

void Menu(); //Main menu

void Infobase(); //Information maintenance

void Find(); //Information inquiry

void InfoStat(); //Performance statistics

void Comp(); //Result ranking

void Find _ ID(); //Search by Student ID

void Find _ name(); //Find by name

void Infobase _ add(); //Enter information

void Infobase _ amend(); //Modify the information

void Delete(); //Delete information

Typedef struct // Rename the student information structure.

{

Character name [10]; //Name

char sex[3]; //Gender

Int ID// student number

Int mathematics; //Math scores

Int English; //English scores

int computer _ C; //Computer C language scores

Int sum// total score

} students;

void main()

{

int chg= 1,n;

Printf ("\ t \ tWelcome to use this management system, please use it normally under Debug \ n");

White (chg)

{

menu();

scanf("%d ",& ampn);

Switch (n)

{

Case 1:

infobase(); //Information maintenance

Break;

Case 2:

find(); //Information inquiry

Break;

Case 3:

InfoStat(); //Performance statistics

Break;

Case 4:

comp();

Break; //Result ranking

Case 0:

chg = 0; //Exit the menu

Printf ("(_)) Thanks for using! Goodbye \ n ");

Break;

Default value:

Printf("(~_~)) selection error (0~4)? Please re-enter \ n ");

}

}

getch();

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Invalid menu ()

{

printf(" \ t \ t \ t * * * * * * * * * * * * * * * * * * \ n ");

printf(" \ t \ t \ t * * * * \ n ");

Printf ("\ t \ t * * main menu * * \ n ");

printf(" \ t \ t \ t * * * * \ n ");

printf(" \ t \ t \ t * * * * * * * * * * * * * * * * * * \ n ");

Printf ("\ t \ t1information maintenance \ n");

Printf ("\ t \ t 2 information query \ n ");

Printf("\t\t\t 3 score statistics \ n ");

Printf ("\ t \ t 4 rank sorting \ n");

Printf ("\ t \ t0Exit menu \ n");

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void Infobase()

{

int n,chg = 1;

White (chg)

{

Printf ("\ t \ t1enter student information \ n");

Printf("\t\t\t2 Modify student information \ n ");

Printf ("\ t \ t \ t3Delete student information \ n");

Printf ("\ t \ t \ tReturn to the main menu \ n");

scanf("%d ",& ampn);

Switch (n)

{

Case 1:

infobase _ add(); //Enter information

Break;

Case 2:

infobase _ amend(); //Modify the information

Break;

Case 3:

delete(); //Delete information

Case 0:

chg = 0; //Return to the main menu

Break;

Default value:

Wrong selection of Printf("\t(~_~)), please re-enter \ n: ");

}

}

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void Infobase_add()

{

Student Stu [1]; //Defines an array variable of type Student.

FILE * fp = NULL// Declares a file pointer.

Fp=fopen ("special week. txt "," a "); //Open by appending

Printf ("\ tEnter name, gender and student number (foreign language C for mathematics) \ n");

Scanf("% s% s% d% d% d ",///receives input.

Stu [0]. Name,

Stu [0]. Sex,

& Stu [0]. ID,

& Stu [0]. Math,

& Stu [0]. English,

& Stu [0]. Computer _ c);

Stu [0]. sum=stu[0]。 English +stu[0]. computer_C+stu[0]。 Mathematics; //Calculate the total score

If (fprintf (fp, "%s% s% d% d% d \ n"),//Write to disk to prevent errors.

Stu [0]. Name,

Stu [0]. Sex,

Stu [0]. ID,

Stu [0]. Math,

Stu [0]. English,

Stu [0]. Computer _C,

Stu [0]. Sum) ==0)

{

Printf ("write failure");

}

fclose(FP); //Close the file pointer

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void Infobase_amend()

{

int i,j;

char ch[ 10]; //Declare a character array to save the name to be modified.

Student Stu [50]; //Declare an array of students.

FILE * fp = NULL// Declares a file pointer.

Printf ("\ tEnter the name of the student to be modified"); //Prompt for input

scanf("%s ",ch); //Receive the input name

Fp=fopen ("special week. txt”、“rt+”; //Open in read/write mode.

for(I = 0; ; i++)

{

Fscanf(fp, "%s% s% d% d% d% d", // d% d% d ",//Read memory from disk.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _C,

& Stu [me]. sum);

If (strcmp (stu [I].name, ch)= = 0)// Judge whether you find what you need.

{

Rewind (FP); //The pointer moves to the beginning of the file.

Printf(" \ ten Enter and modify the name, gender and student number (Math Foreign Language C language) \ n ");

Scanf("% s% s% d% d% d ",///receives input.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _ c);

j = I;

Stu [me]. sum=stu[i]。 English +stu[i]. Computer _C+stu[i]. Mathematics;

for(I = 0; I & ltj;; i++)

{

Fprintf (fp, "%s% s% d% d% d \ n",//The original data was written to disk.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum);

}

Fprintf(fp, "%s% s% d% d% d", // d% d% d ",//writes the modified data to disk.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum);

fclose(FP); //Close the file pointer

Break;

}

}

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Void delete ()

{

int i,j,k = 0;

char ch[ 10]; //Declare a character array to save the name to be deleted.

Student Stu [50]; //Declare an array of students.

FILE* fp=NULL,* FP 1 = NULL; //Declare file pointer

Printf ("\ tEnter the name of the student to be deleted"); //Prompt for input

scanf("%s ",ch); //Receive the input name

Fp=fopen ("special week. txt”、“rt+”; //Open in read/write mode.

for(I = 0; ! feof(FP); i++)

{

Fscanf(fp, "%s% s% d% d% d% d", // d% d% d ",//Read memory from disk.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _C,

& Stu [me]. sum);

If (strcmp (stu [I].name, ch)= = 0)// Judge whether you find what you need.

j = I;

}

Rewind (FP); //The pointer moves to the beginning

for(; j & ltI- 1; j++)

{

stu[j]= stu[j+ 1]; //Overwrite the items to be deleted, and then move forward in turn.

}

for(I = 0; I & ltj;; i++)

Fprintf (fp% s% s% d% d% d% d \ n ",///Write to disk.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum);

}

fclose(FP);

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void Find()

{

int n,chg = 1; //Declare the chg variable for the loop.

White (chg)

{

Printf ("\ t \ t1query by student number \ n");

Printf("\t\t\t2 Query by name \ n ");

Printf ("\ t \ t \ tReturn to the main menu \ n");

scanf("%d ",& ampn);

Switch (n)

{

Case 1:

find _ ID(); //Call the function of student number search.

Break;

Case 2:

find _ name(); //Call the function to find by name.

Break;

Case 0:

chg = 0; //Return to the main menu

Break;

Default: printf("\n(~_~) is selected incorrectly, please re-enter \ n ");

}

}

}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void Find_ID()

{

int n,I;

Student Stu [50]; //Declare an array variable of type Student

FILE * fp = NULL// Declares a file pointer.

Printf ("Please enter your student number"); //Prompt for input

scanf("%d ",& ampn); //Receive the input student number.

Fp=fopen ("special week. txt”、“rt”); //Open as read-only

for(I = 0; ! feof(FP); i++)

{

If (fscanf (fp, "%s% s% d% d% d \ n"),//Prevent reading into memory from failing.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _C,

& Stu [me]. Sum) ==0)

Printf ("read failure");

If (Stu [I]. ID==n) // Judge whether to find a student.

{

Printf ("Found: %s Gender %s Student ID %d Math %d Foreign Language %d C Language %d Total Score %d\n",//Output the search target.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum);

Break;

}

If(feof(fp)) // Handle the case that it is not found.

{

Printf ("No information about this student \ n");

}

} f close(FP); //Close the file pointer

}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void Find_name()

{

int I;

char ch[ 10]; //Declare a character array to store the lookup name.

Student Stu [50]; //Declare an array variable of type Student

FILE * fp = NULL// Declares a file pointer.

Printf ("Please enter a name \ n"); //Prompt for input

scanf("%s ",ch); //Receive the input name

Fp=fopen ("special week. txt”、“rt”); //Open as read-only

for(I = 0; ! feof(FP); I++) // loop, ending the loop with a file pointer pointing to the end.

{

Fscanf(fp, "%s% s% d% d% d% d", // d% d% d ",//Read memory from disk.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _C,

& Stu [me]. sum);

If (strcmp (stu [I].name, ch)= = 0)// Judge whether to find students.

{

Printf ("Found: %s Gender %s Student ID %d Math %d Foreign Language %d C Language %d Total Score %d\n",//Output the search target.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum);

Break;

}

If(feof(fp)) // Handle the case that it is not found.

{

Printf ("No information about this student \ n");

}

} f close(FP); //Close the file pointer

}

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

void InfoStat()

{

int n,m,I,count = 0;

char ch[ 15]; //Declare that the character array holds the course name.

Student Stu [60]; //Declare an array variable of type Student

FILE * fp = NULL// Declares a file pointer.

Fp=fopen ("special week. txt”、“rt”); //Open as read-only

Printf ("Enter the course name (foreign language of mathematics C) and fraction segment (format: mathematics x1-x2) \ n");

scanf("%s ",ch); //Receive the entered course name.

scanf("%d-%d ",& ampn & amp; m); //receive an input fraction segment

for(I = 0; ! feof(FP); I++) // End the loop with a file pointer to the end of the file.

{

If (fscanf (fp, "%s% s% d% d% d \ n"),//Prevent reading into memory from failing.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _C,

& Stu [me]. Sum) ==0)

Printf ("read failure");

count++;

}

fclose(FP); //Close the file pointer

* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Printf ("name and gender %s total score \n", ch);

for(I = 0; I < count; i++)

If(strcmp(ch, "mathematics") = = 0&; & Stu [me]. Math & gt= n & amp& amp stu [me]. Mathematics < = m)

{

printf("%s\t%s\t%d\t%d\n ",

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. Math,

Stu [me]. sum);

}

for(I = 0; I < count; i++)

If(strcmp(ch, "foreign language") = = 0&; & Stu [me]. English & gt= n & amp& amp stu [me]. English < = m)

{

printf("%s\t%s\t%d\t%d\n ",

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. English,

Stu [me]. sum);

}

for(I = 0; I < count; i++)

If (! strcmp(ch," C ")& amp; & Stu [me]. Computer _ C>= n & amp& amp stu [me]. Computer _ c < = m)

{

printf("%s\t%s\t%d\t%d\n ",

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. English,

Stu [me]. sum);

}

}

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Invalid compensation ()

{

int i,k,j,count = 0;

char ch[ 15]; //Declare that the character array holds the course name.

Student stu[50], a temporary worker; //Declare a student variable.

FILE * fp = NULL// Declares a file pointer.

Printf ("Please enter the course name to sort"); //Prompt for input

scanf("%s ",ch); //Receive the entered course name.

Fp=fopen ("special week. txt”、“rt”); //Open the file as read.

for(I = 0; ! feof(FP); I++) // End the loop with a file pointer to the end.

{

If (fscanf (fp, "%s% s% d% d% d \ n"),//Prevent reading into memory from failing.

Stu [me]. Name,

Stu [me]. Sex,

& Stu [me]. ID,

& Stu [me]. Math,

& Stu [me]. English,

& Stu [me]. Computer _C,

& Stu [me]. Sum) ==0)

Printf ("read failure");

count++; //Record the number of students in the file.

}

fclose(FP); //Close the file pointer

If(strcmp(ch," mathematics") ==0)

{

for(I = 0; I< count-1; I++) // The following are sorted in descending order.

{

k = I;

for(j = I+ 1; J< counting; j++)

if(stu[k]。 Mathematics & lt Stu [j]. Mathematics)

k = j;

If (me! =k)

{

temp = stu[I];

stu[I]= stu[k];

stu[k]= temp;

}

}

Printf ("name, gender, student number, foreign language of mathematics, total score of C language, ranking of mathematics scores \ n");

for(I = 0; I < count; i++)

{

Printf ("%-6s% 4s% d% d% d% d \ n",//output information.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum,I+ 1);

}

}

If(strcmp(ch, "foreign language") ==0)

{

for(I = 0; I< count-1; I++) // The following are sorted in descending order.

{

k = I;

for(j = I+ 1; J< counting; j++)

if(stu[k]。 English & lt Stu [j]. English)

k = j;

If (me! =k)

{

temp = stu[I];

stu[I]= stu[k];

stu[k]= temp;

}

}

Printf ("name, gender, student number, math, foreign language, C language, total score, foreign language score ranking \ n");

for(I = 0; I < count; i++)

{

Printf ("%-6s% 4s% d% d% d% d \ n",//output information.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum,I+ 1);

}

}

if(strcmp(ch," C")==0)

{

for(I = 0; I< count-1; I++) // The following are sorted in descending order.

{

k = I;

for(j = I+ 1; J< counting; j++)

if(stu[k]。 Computer _ C< stu [j]. Computer _C)

k = j;

If (me! =k)

{

temp = stu[I];

stu[I]= stu[k];

stu[k]= temp;

}

}

Printf ("name, gender, student number, math, foreign language, total score of C language, ranking of C language scores \ n");

for(I = 0; I < count; i++)

{

Printf ("%-6s% 4s% d% d% d% d \ n",//output information.

Stu [me]. Name,

Stu [me]. Sex,

Stu [me]. ID,

Stu [me]. Math,

Stu [me]. English,

Stu [me]. Computer _C,

Stu [me]. sum,I+ 1);

}

}

}