我必须做的作业:
a。创建包含12个以上学生记录的三个数组,包括ID,学生姓名和
相应的电子邮件地址-学生ID未按顺序排序。
b。使用气泡排序或选择排序,按学生证对上述数据进行排序。
c。二进制搜索五个ID(来自已排序数组)和第六个ID(非数组)。
我已经完成了所有工作,但排序仅对学生ID进行排序,而且我似乎不明白如何进行冒泡排序来对“ studentArray”中的名称和电子邮件变量进行排序的逻辑。
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
using namespace std;
struct Student {
string name;
int stuID;
string email;
};
void showInfo(Student *studentArray, int stuCount) {
cout << "Student Info: " << endl << endl <<
"\t\tStudent Name" << "\t\tStudent ID" << "\t\tStudent Email" << endl << endl;
for (int i = 0; i < stuCount; i++)
{
cout << "\t\t" << studentArray[i].name << "\t\t" << studentArray[i].stuID << "\t\t\t" << studentArray[i].email << endl;
}
cout << endl;
}
void displayResult(Student studentArray[], int result, int studCount){
if (result==-1){
cout << "Student Id was Not Found"<<endl<< endl;
}
else
cout << "Student Name: " << studentArray[result].name<<endl
<< "Student ID: " <<studentArray[result].stuID<< endl
<< "Student Email: "<< studentArray[result].email<<endl<< endl;
}
int binarySearch(Student studentArray[], int stuCount, int idSearch)
{
int first = 0,
last = idSearch - 1,
middle,
position = -1;
bool found = false;
while (!found && first <= last)
{
middle = (first + last) / 2;
if (studentArray[middle].stuID == stuCount)
{
found = true;
position = middle;
}
else if (studentArray[middle].stuID > stuCount)
last = middle - 1;
else
first = middle + 1;
}
return position;
}
void bubblesortArray(Student *studentArray[], int stuCount)
{
bool swap;
int temp;
do{
swap = false;
for (int count = 0; count < (stuCount - 1); count++)
{
if (studentArray[count] > studentArray[count + 1])
{
temp = studentArray[count];
studentArray[count] = studentArray[count + 1];
studentArray[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void recordSearch(Student studentArray[], int stuCount)
{
int result;
int idSearch;
bool isChar=false;
for (int i=0; i<13;i++)
{
if (i<6)
{
cout << "Binary Search: Enter ID Number: ";
cin >> idSearch;
}
else if (idSearch>12||idSearch<0)
{
i--;
}
if(isChar)
{
cout<< "ERROR: Please enter valid number:";
}
result=binarySearch(studentArray,idSearch,stuCount);
displayResult(studentArray,result,idSearch);
}
}
int main() {
Student studentArray[13];
studentArray[0].name = "Bob McBoberston";
studentArray[0].stuID = 12;
studentArray[0].email = "BMcboberts@txstate.edu";
studentArray[1].name = "Shelby Donald";
studentArray[1].stuID = 1;
studentArray[1].email = "SDonald@txstate.edu";
studentArray[2].name = "Ronald Mcdonald";
studentArray[2].stuID = 11;
studentArray[2].email = "RMcdonald@txstate.edu";
studentArray[3].name = "Dick Cheney";
studentArray[3].stuID = 2;
studentArray[3].email = "DCheney@txstate.edu";
studentArray[4].name = "Ben Dover";
studentArray[4].stuID = 10;
studentArray[4].email = "BDover@txstate.edu";
studentArray[5].name = "Ash Katchum";
studentArray[5].stuID = 3;
studentArray[5].email = "AKatchum@txstate.edu";
studentArray[6].name = "Morty Smith";
studentArray[6].stuID = 9;
studentArray[6].email = "MSmith@txstate.edu";
studentArray[7].name = "Rick Sanchez";
studentArray[7].stuID = 4;
studentArray[7].email = "RSanchez@txstate.edu";
studentArray[8].name = "Johnny Bravo";
studentArray[8].stuID = 8;
studentArray[8].email = "JBravo@txstate.edu";
studentArray[9].name = "Tom N. Jerry";
studentArray[9].stuID = 5;
studentArray[9].email = "Tnjerry@txstate.edu";
studentArray[10].name = "Fred Flinstone";
studentArray[10].stuID = 7;
studentArray[10].email = "FFlinstone@emial.com";
studentArray[11].name = "Son Goku";
studentArray[11].stuID = 6;
studentArray[11].email = "sGoku@txstate.edu";
studentArray[12].name = "Johnny Test";
studentArray[12].stuID = 00;
studentArray[12].email = "JTest@txstate.edu";
int stuCount = 13;
showInfo(studentArray, stuCount);
bubblesortArray(studentArray,stuCount);
showInfo(studentArray,stuCount);
recordSearch(studentArray,stuCount);
return 0;
}
答案 0 :(得分:2)
temp = studentArray[count].stuID;
studentArray[count].stuID = studentArray[count + 1].stuID;
studentArray[count + 1].stuID = temp;
您仅交换ID。交换整个Student对象:
temp = studentArray[count];
studentArray[count] = studentArray[count + 1];
studentArray[count + 1] = temp;
(显然,您需要更改temp
的类型)。
基本上,这是使用struct
和class
es的要点-将它们作为一个整体对待。