Summary of the fourth computer experiment of C + + (extension and advancement of Experiment 3)

Basic tasks:

    Create a Score class to complete the following functions:

        ① . continuously input the float scores of multiple students (score = subject A score + subject B score + subject C score);

        ② . the number of students can be customized by the user (2 by default), up to 100;

        ③ . display the score and average score of each subject of each student;

        ④ . display the average score of each subject;

        ⑤ Rank the scores of each course and display them from high to low;

        ⑥ Package the whole document;

Program code:

.h file


#include<iostream>
#include<string>;
#include<stdio.h>;
using namespace std;


.cpp file


#include  "homework4.h";

class Score {

public:
	Score()
	{
		student_nums = 2;
		cout << "Constructor 1 is called" << endl;
	}
	Score(int subject_num)
	{
		student_nums = subject_num;
		cout << "Constructor 2 is called" << endl;
	}
	~Score()    
	{
		cout << "Destructor called" << endl;
	}
	void InputStudentInformation()   //Student name and grade input
	{

		int i = 0;
		for (i = 0; i < student_nums; i++)
		{
			cout << "Please enter student name:" << endl;
			cin >> name[i];
			cout << "Please enter account A Achievements:" << endl;
			cin >> score[i][0];
			cout << "Please enter account B Achievements:" << endl;
			cin >> score[i][1];
			cout << "Please enter account C Achievements:" << endl;
			cin >> score[i][2];
		}
	}

	void ShowStudentScore()     //Students' grades in various subjects
	{
		int i;
		float student_ave;
		cout << "" << endl;   //Split lines for easy typesetting
		for (i = 0; i < student_nums; i++)
		{
			cout << "full name:" << name[i] << "   subject A Achievement:" << score[i][0];
			cout << "   subject B Achievement:" << score[i][1];
			cout << "   subject C Achievement:" << score[i][2] << endl;
			student_ave = (score[i][0] + score[i][1] + score[i][2]) / 3;
			cout << "full name:" << name[i] << "   The average score is:" << student_ave << endl << endl;

		}

	}
	void ShowClassScore()     //Average score of each subject
	{
		float a_ave = 0, b_ave = 0, c_ave = 0;
		int i;
		for (i = 0; i < student_nums; i++)
		{
			a_ave = a_ave + score[i][0];
			b_ave = b_ave + score[i][1];
			c_ave = c_ave + score[i][2];
		}
		a_ave = a_ave / student_nums;
		b_ave = b_ave / student_nums;
		c_ave = c_ave / student_nums;
		cout << "subject A The average score is:" << a_ave << endl
			<< "subject B The average score is:" << b_ave << endl
			<< "subject C The average score is:" << c_ave << endl << endl;  //Split lines for easy typesetting
	}
	void ScoreSort()   //Ranking of single subject scores
	{
		int i, j, k, end = student_nums;
		string mingzi_temp;
		float score_temp;
		string mingzi[50];


		for (j = 0; j < 3; j++)     //bubble sort 
		{
			for (i = 0; i < student_nums; i++)       //Name initialization
			{
				mingzi[i] = name[i];
			}
			for (i = 0; i < student_nums - 1; i++)
			{
				for (k = i + 1; k < student_nums; k++)
				{
					if (score[i][j] < score[k][j])
					{
						score_temp = score[i][j];
						score[i][j] = score[k][j];
						score[k][j] = score_temp;
						mingzi_temp = mingzi[i];
						mingzi[i] = mingzi[k];
						mingzi[k] = mingzi_temp;
					}

				}

			}

			end = student_nums;
			switch (j)
			{
			case 0:cout << "Course name A:" << endl << endl; break; //Split lines for easy typesetting
			case 1:cout << "Course name B:" << endl << endl; break; //Split lines for easy typesetting
			case 2:cout << "Course name C:" << endl << endl; break; //Split lines for easy typesetting
			}
			for (i = 0; i < student_nums; i++)
			{
				cout << "full name:" << mingzi[i] << "  Achievement:" << score[i][j] << endl;
				if (i == student_nums - 1)
				{
					cout << "" << endl; //Split lines for easy typesetting
				}
			}

		}
	}



private:
	int student_nums;
	float score[100][3] = {};
	string name[100] = {};

};

int main()
{

	int num;
	cout << "Do you need to change the number of students entered(The default value is 2,Up to 100)?Press 1 if you need to change, or 0 if you don't need to change:";
	cin >> num;
	while ((num != 0) && (num != 1))
	{
		cout << "Input error!! Press 1 if you want to change, or 0 if you don't want to change" << endl;
		cin >> num;
	}
	if (num == 1)
	{
		int student_numebr;
		cout << "Please enter the number of students to change:";
		cin >> student_numebr;

		Score x(student_numebr);
		x.InputStudentInformation();
		x.ShowStudentScore();
		x.ShowClassScore();
		x.ScoreSort();
	}
	if (num == 0)
	{
		Score x;
		x.InputStudentInformation();
		x.ShowStudentScore();
		x.ShowClassScore();
		x.ScoreSort();
	}
}

Difficulties encountered in the Debug process and solutions:

1, For the storage of student names, I wanted to use a two-dimensional array of char type at the beginning. I found that the array of char type was a little chaotic, so I used a one-dimensional array of string type.

2, Sorting method:   reference: Six sorting algorithms

      1. Insertion sort: time complexity: in the worst case, it is O(N*N). At this time, the sequence to be arranged is in reverse order, or close to reverse order. In the best case, it is O(N). At this time, the sequence to be arranged is in ascending order, or close to ascending order. Spatial complexity: O(1);

      2. Hill sorting: time complexity average: O(N^1.3), space complexity: O(1);

      3. Select Sorting: time complexity: worst case: O(N^2), best case: O(N^2), space complexity: O(1);

      4. Bubble sorting: time complexity: worst case: O(N^2), best case: O(N), space complexity: O(1);

      5. Heap sorting: the time complexity is O(N*log2N);

      6. Quick sorting: the time complexity is O(N*log2N);

3, At first, the name was not initialized after taking the scores of one subject, resulting in errors when sorting the scores of the second subject, the student's name was inconsistent with the test scores;

  Error condition:

  resolvent:

Program running results:

1, When using the default value (2):

​​​​​​        

2, When the user sets the number of students (take 4 as an example)

 

Experiment summary:  

        There are many things learned in this experiment, including the definition of string array, using string array to store names, bubble sorting, saving student scores into objects, and saving one of the initial states when both groups of contents need to be changed for later initialization. This experiment takes a long time, and the learned things also increase in proportion, In the experiment, I also realized my shortcomings. In the later time, I have to strengthen learning and constantly expand myself.

Tags: C++

Posted on Mon, 22 Nov 2021 13:12:03 -0500 by theorok