c + + freshman basic information statistics software

Posted by heshan on Sun, 02 Jan 2022 22:02:47 +0100

catalogue

Basic software functions:

1. Enter new student's personal information

2. Newly added information

3. Delete all information according to the new student's name

4. Display the new student information that has been entered and added

​ 5. Search for information by student's name

6. Ranking from high to low according to the freshmen's English entrance score

​7. Statistics of information according to students' major, gender or age

​ 8. Save the data entered in the linked list to the specified file in a certain format

 9. sign out

Main menu

Basic software functions:

Define structure

typedef struct StuNode {
	char Name[20];
	char sex[10];
	int birthday;
	char zhuanye[20];
	char add[100];
	int score;
	struct StuNode *next;
} Student, *StuPtr;

The data field of the structure stores the student's name, gender, major, date of birth, home address, English enrollment score. The next stored in the pointer field is a member of the pointer type, which points to the data of struct type.

According to the object-oriented idea and method, a student single linked list class is created

class StuListLink {
	public:
		StuListLink() {};
		void Luru();
		void Liulan();
		void Shanchu();
		void Chaxun();
		void Paixu();
		void Tongji();
		void Baocun();
		void tuichu();
	private:
		StuPtr head;
		StuPtr tail;
		int count;
};

The head pointer and tail pointer of the single linked list are defined. The public class is the function function to be implemented by the program, including the constructor of the single linked list class. Create a single linked list function of the leading node, insert it into the linked list node by using the head insertion method, traverse the linked list from the head node to find the node to be deleted, output the node of the single linked list, traverse the linked list to find the corresponding node output, Use bubble sort to sort the nodes of the linked list, and save the data fields in the linked list to the file.

1. Enter new student's personal information

void StuListLink::Luru() {
	system("cls");
	StuPtr p,pre;
	StuPtr PNode;
	int n;
	cout<<"Please enter 1 for the first time"<<endl<<"Subsequent new student input 2"<<endl;
	cout<<"******";
	cin>>n;
	switch (n) {
		case 1:
			cout<<"Enter the total number of students:";
			cin>>count;
			if(count<0) {
				cout<<"ERROR MESSAGE!"<<endl;
				return;
			}
			head=new Student;
			head->Name[0]='\0';
			head->sex[0]='\0';
			head->zhuanye[0]='\0';
			head->birthday=0;
			head->add[0]='\0';
			head->score='\0';
			head->next=NULL;
			tail=head;
			for(int i=0; i<count; i++) {
				p=new Student;
				cout<<"Input No"<<i+1<<"Student information:"<<endl;
				cout<<"full name:";
				cin>>p->Name;
				cout<<"Gender:";
				cin>>p->sex;
				while(strcmp(p->sex,"male")!=0&&strcmp(p->sex,"female")!=0) {
					cout<<"The entered student gender does not meet the requirements, please re-enter!!"<<endl;
					cout<<"Please re-enter student gender:";
					cin>>p->sex;
				}
				cout<<"major:";
				cin>>p->zhuanye;
				cout<<"birthday(Example:20000000):";
				cin>>p->birthday;
				cout<<"Home address:";
				cin>>p->add;
				cout<<"English score:";
				cin>>p->score;
				while(p->score<0) {
					cout<<"The score entered does not meet the requirements, please re-enter!!"<<endl;
					cout<<"Please re-enter the student's English score:";
					cin>>p->score;
				}
				p->next=NULL;
				tail->next=p;
				tail=p;
			}
			cout<<"Entered successfully,altogether"<<count<<"Students"<<endl;
			system("pause");
			getchar();
			system("cls");
			return;

In the function of adding student information, first define a p pointer, and then use the switch function to select whether to enter student information for the first time, If "1" is entered for the first time, otherwise "2" is entered for subsequent new student information ". enter the total number of students to be entered for the first time. If the number is less than 0, an error message will be prompted, and then open up memory space for the head header pointer. All the data fields of the head pointer are null, the header node pointer field points to null data, and the header is also the footer. Then use the for loop, the number of cycles is the total number of students entered, and then open up a new memory space for the P pointer When inputting students' gender, the strcmp comparison can only be male or female, and the format of English score can only be positive. Then p is connected to the end of the table, and node P becomes a new end of the table. In the subsequent increase, the memory space of PNode is opened up, and the newly added node is inserted by header insertion method.

2. Newly added information

		case 2:
			StuPtr p,pre;
			StuPtr PNode;
			PNode=new Student;
			cout<<"full name:";
			cin>>PNode->Name;
			cout<<"Gender:";
			cin>>PNode->sex;
			while(strcmp(PNode->sex,"male")!=0&&strcmp(PNode->sex,"female")!=0) {
				cout<<"The entered student gender does not meet the requirements, please re-enter!!"<<endl;
				cout<<"Please re-enter student gender:";
				cin>>PNode->sex;
			}
			cout<<"major:";
			cin>>PNode->zhuanye;
			cout<<"birthday(Example:20010101):";
			cin>>PNode->birthday;
			cout<<"Home address:";
			cin>>PNode->add;
			cout<<"English score:";
			cin>>PNode->score;
			while(PNode->score<0) {
				cout<<"The score entered does not meet the requirements, please re-enter!!"<<endl;
				cout<<"Please re-enter the student's English score:";
				cin>>PNode->score;
			}
			p=head->next;
			pre=head;
			PNode->next=pre->next;
			pre->next=PNode;
			cout<<"Successfully added!"<<endl;
	}

}

3. Delete all information according to the new student's name

void StuListLink::Shanchu() {
	system("cls");
	char n[20];
	StuPtr p,pre;
	cout<<"Please enter the name of the student you want to delete:";
	cin>>n;
	pre=head;
	p=head->next;
	while(p!=NULL) {
		if(strcmp(p->Name,n)==0) {
			pre->next=p->next;
			delete p;
			p=NULL;
			cout<<"Delete succeeded!";
			return;
		}
		pre=p;
		p=p->next;
	}
	cout<<"The student was not found, deletion failed!";
}

Enter the student's name and delete all the information of the student. Define p as the currently traversed node and pre as its forward node. Use the while loop to traverse the student list to the end. If the specified student's name is found, point the pointer field of its forward node to the subsequent node of the node to be deleted, release the node to be deleted, assign it NULL and delete the node, If no student with this name is found, relevant prompt information will be output.

4. Display the new student information that has been entered and added

void StuListLink::Liulan() {
	system("cls");
	StuPtr p;
	int n=1;
	p=head->next;
	if(p==NULL) {
		cout<<"Student does not exist!"<<endl;
		return;
	}
	while(p!=NULL) {
		cout<<"The first"<<n<<"Student information:"<<endl;
		cout<<"full name:"<<p->Name<<"\t"<<"Gender:"<<p->sex<<"\t"<<"major:"<<p->zhuanye<<"\t"<<"birthday:"<<p->birthday<<"\t"<<"Home address:"<<p->add<<"\t"<<"English achievement:"<<p->score<<endl;
		p=p->next;
		n++;
	}
	cout<<"Shared students"<<n-1<<"name!";
}

Browse the student information, define an int type N, and assign a value of 0. Use the pointer variable p to point to each node in the linked list from beginning to end. When the pointer points to the node, the content in the node data field will be output. Every cycle, n will add one until the end flag of the linked list is NULL. Finally, several students will be output. If it is an empty linked list, only "output" students do not exist! "And return to the main function.

 5. Search for information by student's name

void StuListLink::Chaxun() {
	system("cls");
	StuPtr p;
	char n[20];
	bool flag=false;
	cout<<"Enter the name of the query:"<<endl;
	cin>>n;
	p=head;
	while(p->next!=NULL) {
		if(strcmp(p->next->Name,n)==0) {
			cout<<"full name:";
			cout<<p->next->Name;
			cout<<"Gender:";
			cout<<p->next->sex;
			cout<<"major:";
			cout<<p->next->zhuanye;
			cout<<p->next->birthday;
			cout<<"birthday:";
			cout<<"Home address:";
			cout<<p->next->add;
			cout<<"English achievement:";
			cout<<p->next->score<<endl;
			flag=true;
		}
		p=p->next;
		continue;
	}
	if(!flag) {
		cout<<"No such name!"<<endl;
		return;
	}
}

Query according to the student's name: assign the flag of bool type to false, enter the student's name to be queried, use the while loop to traverse the linked list, compare the student's name, and then output all the information of the student. If there is a duplicate name, all the student's information of the name will be output. If the student is found, the flag is assigned to true, and the relevant prompt information will be output.

6. Ranking from high to low according to the freshmen's English entrance score

void StuListLink::Paixu() {
	system("cls");
	StuPtr p,pre,q;
	StuPtr end=NULL;
	while(head->next!=end) {
		pre=head;
		p=head->next;
		q=p->next;
		while(p->next!=end) {
			if(p->score>q->score) {
				pre->next=q;
				p->next=q->next;
				q->next=p;
			} else {
				p=p->next;
			}
			q=p->next;
			pre=pre->next;
		}
		end=p;
		cout<<"full name:";
		cout<<p->Name;
		cout<<"Gender:";
		cout<<p->sex;
		cout<<"major:";
		cout<<p->zhuanye;
		cout<<"birthday:";
		cout<<p->birthday;
		cout<<"Home address:";
		cout<<p->add;
		cout<<"English achievement:";
		cout<<p->score<<endl;
	}

}

Sort according to the students' English entrance scores, define the pointers of three structure types, pre is defined as the head node, p is the backward node of the head node, q is the backward node of p, and then use the while loop to sort from large to small until the end is traversed, and output the sorted new linked list.

7. Make statistics according to students' major, gender or age

void StuListLink::Tongji() {
	system("cls");
	StuPtr p,pre;
	p=head;
	char i[20];
	int n;
	int k=0;
	int nl;
	cout<<"Statistical method:"<<endl<<"1.Statistics by specialty"<<endl<<"2.Statistics by sex"<<endl<<"3.Statistics by age"<<endl;
	cout<<"Please enter the statistics method:";
	cin>>n;
	switch (n) {
		case 1:
			cout<<"Please enter student major:";
			cin>>i;
			while(p->next!=NULL) {
				if(strcmp(p->next->zhuanye,i)==0) {
					cout<<"full name:";
					cout<<p->next->Name;
					cout<<"Gender:";
					cout<<p->next->sex;
					cout<<"major:";
					cout<<p->next->zhuanye;
					cout<<"birthday:";
					cout<<p->next->birthday;
					cout<<"Home address:";
					cout<<p->next->add;
					cout<<"English achievement:";
					cout<<p->next->score<<endl;
					k++;
				}
				p=p->next;
				continue;
				if(p->next==NULL) {
					cout<<"Students of this major do not exist!";
					return;
				}
			}
			break;
		case 2:
			cout<<"Please enter student gender:";
			cin>>i;
			while(p->next!=NULL) {
				if(strcmp(p->next->sex,i)==0) {
					cout<<"full name:";
					cout<<p->next->Name;
					cout<<"Gender:";
					cout<<p->next->sex;
					cout<<"major:";
					cout<<p->next->zhuanye;
					cout<<"birthday:";
					cout<<p->next->birthday;
					cout<<"Home address:";
					cout<<p->next->add;
					cout<<"English achievement:";
					cout<<p->next->score<<endl;
					k++;
				}
				p=p->next;
				continue;
				if(p->next==NULL) {
					cout<<"Students of this gender do not exist!";
					return;
				}
			}
			break;
		case 3:
			cout<<"Please enter student age:";
			cin>>nl;
			while(p->next!=NULL) {
				if(2021-p->next->birthday/10000==nl) {
					cout<<"full name:";
					cout<<p->next->Name;
					cout<<"Gender:";
					cout<<p->next->sex;
					cout<<"major:";
					cout<<p->next->zhuanye;
					cout<<"birthday:";
					cout<<p->next->birthday;
					cout<<"Home address:";
					cout<<p->next->add;
					cout<<"English achievement:";
					cout<<p->next->score<<endl;
					k++;
				}
				p=p->next;
				continue;
				if(p->next==NULL) {
					cout<<"Students of this age do not exist!";
					return;
				}
			}
			break;
	}
	cout<<"Students in this way share"<<k<<"name!";
}

Make statistics of relevant student information in a certain way, define p as the head node, and use switch to select the statistics method. Enter 1 for statistics by student major, enter 2 for statistics by gender, and enter 3 for statistics by age. The three methods are the same. All use the while loop to traverse the student linked list, find the relevant nodes of the selected method, and use continue to output all the freshmen information conforming to the method. If p is an empty linked list, the relevant prompt information will be output.

Enter 1 to make statistics by major. Enter a major. All students in the major will be printed and the total number of students will be displayed

Enter 2 to make statistics by gender, enter the gender, print all the students of that gender, and display the total number of students

Enter 3 to count by age, enter the age, print all the students of this age, and display the total number of students

 8. Save the data entered in the linked list to the specified file in a certain format

void StuListLink::Baocun() {
	system("cls");
	ofstream outfile("D:\\Freshman information management system.txt");
	StuPtr p;
	p=head->next;
	while(p!=NULL) {
		outfile<<"full name:"<<p->Name<<"\t"<<"Gender:"<<p->sex<<"\t"<<"major:"<<p->zhuanye<<"\t"<<"birthday:"<<p->birthday<<"\t"<<"Home address:"<<p->add<<"\t"<<"English achievement:"<<p->score<<endl;
		p=p->next;
	}
	outfile.close();
	cout<<"Student information saved successfully!"<<endl;
}

Save the student information to the file, open the file freshman information management system. txt, define the pointer of P, and the pointer field of P points to the next node of the head node. When p is not empty, write the data in the linked list data field to the file until P is a null pointer, the cycle ends, and close the file.

Saved files

 9. sign out

Main menu

int main() {
	StuListLink stuListLink;
	system("color b4");
	int a;
	system("cls");
	while(1) {
		cout<<endl;
		cout<<endl;
		cout<<endl;
		cout<<endl;
		cout<<"    \t\t\t\t*******Freshman information statistics management system*****"<<endl;
		cout<<"    \t\t\t\t********************************"<<endl;
		cout<<"    \t\t\t\t*         1.Add student information       *"<<endl;//*
		cout<<"    \t\t\t\t*         2.Delete student information       *"<<endl;//*
		cout<<"    \t\t\t\t*         3.Import student information       *"<<endl;//*
		cout<<"    \t\t\t\t*         4.Student information search       *"<<endl;//*
		cout<<"    \t\t\t\t*         5.Student information statistics       *"<<endl;//*
		cout<<"    \t\t\t\t*         6.English score ranking       *"<<endl;//*
		cout<<"    \t\t\t\t*         7.Student information storage       *"<<endl;//*
		cout<<"    \t\t\t\t*         8.sign out               *"<<endl;//*
		cout<<"    \t\t\t\t********************************"<<endl;
		cout<<"******Please select:";
		cin>>a;
		switch(a) {
			case 1:
				stuListLink.Luru();
				system("pause");
				system("cls");
				break;
			case 2:
				stuListLink.Shanchu();
				system("pause");
				system("cls");
				break;
			case 3:
				stuListLink.Liulan();
				system("pause");
				system("cls");
				break;
			case 4:
				stuListLink.Chaxun();
				system("pause");
				system("cls");
				break;
			case 5:
				stuListLink.Tongji();
				system("pause");
				system("cls");
				break;
			case 6:
				stuListLink.Paixu();
				system("pause");
				system("cls");
				break;
			case 7:
				stuListLink.Baocun();
				system("pause");
				system("cls");
				break;
			case 8:
				exit(0);
		}
	}
}

Topics: C++ data structure linked list