#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NC 100
void SortBubble(struct studentRed* p, int len);
void SortBubble1(struct studentRed* p, int len);
void SortBubble2(struct studentRed* p, int len);
void SortBubble3(struct studentRed* p, int len);
void SortBubble4(struct studentRed* p, int len);
void SortBubble5(struct studentRed* p, int len);
void SortBubble6(struct studentRed* p, int len);
void SortBubble7(struct studentRed* p, int len);
struct studentRed { //define information of students
char sName[20]; //students' name for the first column
char sID[20]; //students' ID for the second column
float CP; // score for Computer programming
float PE; //score for Physical Education
float CR; //score for Communication and Research
float AverS; //average score
} students[NC] = {//Assigns a value to an array variable
{"John","zy001",84,86,78,0},
{"Xiaoming","zy002",77,82,90,0},
{"Xiaohua","zy003",91,81,76,0},
{"Jianguo","zy004",79,82,85,0},
{"Xiaodong","zy005",60,55,40,0},
};
int main(void) {
int shuliang = 5;
for (int i = 0; i < shuliang; ++i) {//caculate the average
students[i].AverS = (students[i].CP + students[i].PE + students[i].CR) / 3;
}
FILE* fp;
if ((fp = fopen("stuuu.txt", "w")) == NULL)//open a flie
{
puts("Open file failed\n");
}
else
{
//save the structrue
fwrite(students, sizeof(struct studentRed), shuliang, fp);
}
fclose(fp);
//function panel
printf("\n\n Student Information System\n\n");
printf("***************************************************************************************\n");
printf("* *\n");
printf("* Enter '1' to display the table of student information *\n");
printf("* Enter '2' to search information of one student by his/her Name *\n");
printf("* Enter '3' to search information of one student by his/her Student ID *\n");
printf("* Enter '4' to search all students whose names contain specific letters *\n");
printf("* Enter '5' to display the information of students whose average is greater than 59 *\n");
printf("* Enter '6' to display the information of students whose average is less than 60 *\n");
printf("* Enter '7' to sorting the average scores in increasing order *\n");
printf("* Enter '8' to sorting the Computer programming scores in increasing order *\n");
printf("* Enter '9' to sorting the Physical Education scores in increasing order *\n");
printf("* Enter 'a' to sorting the Communication and Research scores in increasing order *\n");
printf("* Enter 'b' to sorting the average scores in decreasing order *\n");
printf("* Enter 'c' to sorting the Computer programming scores in decreasing order *\n");
printf("* Enter 'd' to sorting the Physical Education scores in decreasing order *\n");
printf("* Enter 'e' to sorting the Communication and Research scores in decreasing order *\n");
printf("* Enter 'f' to add a new student *\n");
printf("* Enter 'g' to delete the record of a student *\n");
printf("* *\n");
printf("***************************************************************************************\n");
printf("Enter a character: ");
char choice;
//selection of function
while ((choice = getchar()) != EOF) {
switch (choice) {
//display the table of student information
case '1':
FILE * fp1;
studentRed retStudents[NC];//create a new structure to get the data from the existing file
if ((fp1 = fopen("stuuu.txt", "rb")) == NULL)//open the existing file
{
puts("Open file failed\n");
}//end if
else {
fread(retStudents, sizeof(studentRed), shuliang, fp1);//read data from the existing file
}//end else
// output the information of students
int jishu;
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (jishu=0 ; jishu < shuliang; ++jishu)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", retStudents[jishu].sName, retStudents[jishu].sID, retStudents[jishu].CP, retStudents[jishu].PE, retStudents[jishu].CR, retStudents[jishu].AverS);
}//end for
fflush(fp);
fclose(fp1);//close the file
break;
//search information of one students by his/her Name
case '2':{
int flag = 1;//this used to identify if there have the student you want to search
FILE * fp2;
studentRed retStudents1[NC];//create a new structrue
char chazhao[21];//define a variable to store the name needed to be found
printf("Enter a student name:");
scanf("%s", chazhao);//input the name needed to be found
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
if ((fp2 = fopen("stuuu.txt", "rb")) == NULL)
{
puts("Open file failed\n");
}
else {
fread(retStudents1, sizeof(studentRed), shuliang, fp2);//read data from a file to a new structure
for (int i = 0; i < shuliang; ++i) {
if (strcmp(students[i].sName, chazhao) == 0) {
flag = 0;//if there are corresponding, change the flag.
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[i].sName, students[i].sID, students[i].CP, students[i].PE, students[i].CR, students[i].AverS);
}
}
//if there are no corresponding students, the flag will not change, so print this
if (flag != 0) {
printf("\nThere are no corresponding students.\n\n\n");
}
}
break;
}
//search information of one students by his / her Student ID
case '3': {
int flag = 1;//this used to identify if there have the student you want to search
FILE* fp3;
studentRed retStudents2[NC];
char chazhao1[21];//define a variable to store the ID needed to be found
printf("Enter a student ID:");
scanf("%s", chazhao1);//input the ID needed to be found
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
if ((fp3 = fopen("stuuu.txt", "rb")) == NULL)
{
puts("Open file failed\n");
}
else {
fread(retStudents2, sizeof(studentRed), shuliang, fp3);
for (int i = 0; i < shuliang; ++i) {
if (strcmp(students[i].sID, chazhao1) == 0) {
flag = 0;////if there are no corresponding students, the flag will not change, so print this
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[i].sName, students[i].sID, students[i].CP, students[i].PE, students[i].CR, students[i].AverS);
}
}
//if there are no corresponding students, print this
if (flag != 0) {
printf("\nThere are no corresponding students.\n\n\n");
}
}
break;
}
//search all students whose names contain specific letters
case '4': {
int flag = 1;
int xuehao;
char chazhao2[21];//define a variable to store the character to be found
printf("Enter characters to search:");
scanf("%s", chazhao2);//input a character to be found
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
//output the information
for (xuehao = 0; xuehao < shuliang; ++xuehao) {
if (strstr(students[xuehao].sName, chazhao2) != NULL) {
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[xuehao].sName, students[xuehao].sID, students[xuehao].CP, students[xuehao].PE, students[xuehao].CR, students[xuehao].AverS);
flag = 0;
}
}
if (flag !=0 ) {
printf("\nThere are no corresponding students.\n\n\n");
}
break;
}
//display the information of students whose average is greater than 59
case'5': {
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
int xuehao1 = 0;
for (xuehao1; xuehao1 < shuliang; ++xuehao1) {
//compare the average with the specific grade and output the information
if (students[xuehao1].AverS > 59) {
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[xuehao1].sName, students[xuehao1].sID, students[xuehao1].CP, students[xuehao1].PE, students[xuehao1].CR, students[xuehao1].AverS);
}
}
break;
}
//display the information of students whose average is less than 60
case'6': {
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
int xuehao2 = 0;
for (xuehao2; xuehao2 < shuliang; ++xuehao2) {
//compare the average with the specific grade and output the information
if (students[xuehao2].AverS < 60) {
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[xuehao2].sName, students[xuehao2].sID, students[xuehao2].CP, students[xuehao2].PE, students[xuehao2].CR, students[xuehao2].AverS);
}
}
break;
}
//sorting the average scores in increasing order
case'7': {
SortBubble(students, shuliang);
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu3 = 1; jishu3 < shuliang + 1; ++jishu3)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu3].sName, students[jishu3].sID, students[jishu3].CP, students[jishu3].PE, students[jishu3].CR, students[jishu3].AverS);
}//end for
break; }
//create and insert record
case'8': {
SortBubble1(students, shuliang);
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu4 = 1; jishu4 < shuliang + 1; ++jishu4)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu4].sName, students[jishu4].sID, students[jishu4].CP, students[jishu4].PE, students[jishu4].CR, students[jishu4].AverS);
}//end for
break; }
//sorting the Physical Education scores in increasing order
case'9': {
SortBubble2(students, shuliang);
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu5 = 1; jishu5 < shuliang + 1; ++jishu5)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu5].sName, students[jishu5].sID, students[jishu5].CP, students[jishu5].PE, students[jishu5].CR, students[jishu5].AverS);
}//end for
break; }
//sorting the Communication and Research scores in increasing order
case'a': {
SortBubble3(students, shuliang);
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu6 = 1; jishu6 < shuliang + 1; ++jishu6)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu6].sName, students[jishu6].sID, students[jishu6].CP, students[jishu6].PE, students[jishu6].CR, students[jishu6].AverS);
}//end for
break; }
//sorting the average scores in decreasing order
case'b': {
SortBubble4(students, shuliang);
//print the table
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu7 = 0; jishu7 < shuliang; ++jishu7)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu7].sName, students[jishu7].sID, students[jishu7].CP, students[jishu7].PE, students[jishu7].CR, students[jishu7].AverS);
}//end for
break; }
//sorting the Computer programming scores in decreasing order
case'c': {
SortBubble5(students, shuliang);
//print the table
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu8 = 0; jishu8 < shuliang; ++jishu8)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu8].sName, students[jishu8].sID, students[jishu8].CP, students[jishu8].PE, students[jishu8].CR, students[jishu8].AverS);
}//end for
break; }
//sorting the Physical Education scores in decreasing order
case'd': {
SortBubble6(students, shuliang);
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishu9 = 0; jishu9 < shuliang; ++jishu9)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishu9].sName, students[jishu9].sID, students[jishu9].CP, students[jishu9].PE, students[jishu9].CR, students[jishu9].AverS);
}//end for
break; }
//sorting the Communicationand Research scores in decreasing order
case'e': {
SortBubble7(students, shuliang);
printf("Name\t ID\t\tComputer\tPhysical\tCommunication\t\tAverages\n");
for (int jishua = 0; jishua < shuliang; ++jishua)
{
printf("%-12s%s\t%.2f\t\t%.2f\t\t%.2f\t\t\t%.2f\n", students[jishua].sName, students[jishua].sID, students[jishua].CP, students[jishua].PE, students[jishua].CR, students[jishua].AverS);
}//end for
break; }
case'f':
//add a new student record
printf("Name: ");
scanf("%s", students[shuliang].sName);
printf("StudentID: ");
scanf("%s", students[shuliang].sID);
printf("Grades of Computer programming: ");
scanf("%f", &students[shuliang].CP);
printf("Grades of Physical Education: ");
scanf("%f", &students[shuliang].PE);
printf("Grades of Communication and Research: ");
scanf("%f", &students[shuliang].CR);
if ((fp = fopen("stuuu.txt", "rb+")) == NULL)//open the existing file
{
puts("Open file failed\n");
}//end if
else {
++shuliang;
for (int i = 0; i < shuliang; ++i) {//caculate the average
students[i].AverS = (students[i].CP + students[i].PE + students[i].CR) / 3;
}//end for
fwrite(students, sizeof(struct studentRed), shuliang, fp);//write the information into the flie again
fflush(fp);
}//end else
fclose(fp);
printf("Add successfully\n");
break;
case'g':
int j;
int stut;
char shanchu[21];//define a variable to store the character to be found
printf("Enter a name:");
scanf("%s", shanchu);//input a character to be found
if ((fp = fopen("stuuu.txt", "rb+")) == NULL)//open a flie
{
puts("Open file failed\n");
}//end if
else
{
//output the information
for (stut = 0; stut < shuliang; ++stut) {
if (strcmp(students[stut].sName, shanchu) == 0) {
for (j = stut; j < shuliang - 1; j++)
{
students[j] = students[j + 1]; //The student record in the back moves forward
}//end for
}//end if
}//end for
}//end else
--shuliang;//the total number of students minus one
fwrite(students, sizeof(struct studentRed), shuliang, fp);
fclose(fp);
}//end switch
system("pause");//suspended
system("cls"); //clean the screen
//print the function interface again
printf("\n\n Student Information System\n\n");
printf("***************************************************************************************\n");
printf("* *\n");
printf("* Enter '1' to display the table of student information *\n");
printf("* Enter '2' to search information of one student by his/her Name *\n");
printf("* Enter '3' to search information of one student by his/her Student ID *\n");
printf("* Enter '4' to search all students whose names contain specific letters *\n");
printf("* Enter '5' to display the information of students whose average is greater than 59 *\n");
printf("* Enter '6' to display the information of students whose average is less than 60 *\n");
printf("* Enter '7' to sorting the average scores in increasing order *\n");
printf("* Enter '8' to sorting the Computer programming scores in increasing order *\n");
printf("* Enter '9' to sorting the Physical Education scores in increasing order *\n");
printf("* Enter 'a' to sorting the Communication and Research scores in increasing order *\n");
printf("* Enter 'b' to sorting the average scores in decreasing order *\n");
printf("* Enter 'c' to sorting the Computer programming scores in decreasing order *\n");
printf("* Enter 'd' to sorting the Physical Education scores in decreasing order *\n");
printf("* Enter 'e' to sorting the Communication and Research scores in decreasing order *\n");
printf("* Enter 'f' to add a new student *\n");
printf("* Enter 'g' to delete the record of a student *\n");
printf("* *\n");
printf("***************************************************************************************\n");
printf("Enter a character: \n");
}//end while
}//end function main
void SortBubble(struct studentRed* p, int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].AverS > p[j + 1].AverS){//exchange the two score
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble1(struct studentRed* p, int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].CP > p[j + 1].CP) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble2(struct studentRed* p, int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].PE > p[j + 1].PE) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble3(struct studentRed* p, int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].CR > p[j + 1].CR) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble4(struct studentRed* p, int len)
{
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].AverS < p[j + 1].AverS) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble5(struct studentRed* p, int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].CP < p[j + 1].CP) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble6(struct studentRed* p, int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].PE < p[j + 1].PE) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
void SortBubble7(struct studentRed* p, int len) {
for (int i = 0; i < len; i++)
{
for (int j = 0; j < len; j++)
{
if (p[j].CR < p[j + 1].CR) {
studentRed hold = p[j];
p[j] = p[j + 1];
p[j + 1] = hold;
}
}
}
}
如果是visual studio,你可能需要设置一下这个
dll好像是注册表的东西,是都不能运行吗
别人的电脑无法dll