按字母顺序对结构内数组变量中的名称进行排序

Sorting names in an array variable inside a structure alphabetically

本文关键字:排序 变量 数组 顺序 结构      更新时间:2023-10-16

我正在制作我的教授要求我们做的这个学生记录程序。它一直在说cannot convert 'StudRec' to 'StudRec' in assignment,有时它说cannot convert char* to const char*.StudRec是我的结构变量,这是应该按字母顺序对记录的名称进行排序的函数。

void sort_name(StudRec name[], StudRec temp[], int studcount)
{
if (studcount > 0)
{
for (int i=0; i<studcount; i++)
{
for (int j=studcount; j<=i; j++)
{
if(strcmp(name[j].nm, name[j+1].nm) > 0)
{
temp=name[j];
name[j]= name[j+1];
name[j+1]= temp;
}
}
}
cout << "t| |ttt The records have been sorted alphabetically by name.n";
}
else
{
cout << "t| |ttt There is no current record to sort by name.nn";
}
}

好的,假设StudRec具有所有必要的操作(赋值、默认构造函数等(,则无需传递临时值数组:

void sort_name(StudRec name[], int studcount)
{
StudRec temp;
// ...
}

这应该解决一个问题:您正在尝试将元素分配给整个数组:

temp=name[j];

更好的方法是在使用它的位置定义temp

const StudRec temp = name[j];

无论如何,我猜你正在尝试实现一个 BubbleSort,但由于索引,你的实现是不正确的。应该是:

for (int i = 1; i < studcount; ++i)
for (int j = 0; j < studcount - i; ++j)

这是 fGrade 函数中的代码:

void fGrade(StudRec rec[], int studcount)
{
string id;
if (studcount > 0)
{
cout << "nnt| |tt         =====================================================n";
cout << "t| |tttt         STUDENT'S FINAL GRADE n";
cout << "t| |tt         =====================================================nn";;
check:  cout << "nt| |ttt Enter student's ID Number: ";
cin >> id;
int index = search(rec, id, studcount);
if (index != -1 && studcount > 0)
{
rec[index].fgrd = (((rec[index].quiz / 150) * 100) * 0.2) + (((rec[index].hw / 20) * 100) * 0.1) + (((rec[index].midT / 100) * 100) * 0.15)
+ (((rec[index].fExam / 100) * 100) * 0.15) + (((rec[index].acts / 150) * 100) * 0.25) + (((rec[index].proj / 100) * 100) * 0.15);
cout << left << setw(10) << "n ID No." << setw(30) << "NAME" << setw(15) << "FINAL GRADE" << setw(10) << "REMARKS";
cout << "n ------------------------------------------------------------n";
cout << " " << left << setw(8) << rec[index].id << setw(30) << rec[index].nm << setw(15) << fixed << setprecision(2) << rec[index].fgrd;
if (rec[index].fgrd >= 75 && rec[index].fgrd <= 100)
{
cout << setw(10) << "Passednn";
}
else if (rec[index].fgrd < 75)
{
cout << setw(10) << "Failednn";
}
}
else
{
cout << "t| |ttt This record does not exist. Check your ID and try again.";
goto check;
}
}
else
{
cout << "t| |ttt There is no current record to calculate a final grade.nn";
return;
}
}

这是view_rec函数的代码:

void view_rec(StudRec rec[], int studcount)
{
if (studcount > 0)
{
cout << "nnt| |tt         ===================================================n";
cout << "t| |tttt         VIEW STUDENT RECORD n";
cout << "t| |tt         ===================================================nn";
int i=0;
cout << "nt| |tt         " << left << setw(10) << "ID" << setw(30) << "NAME" << setw(7) << "SEX" << setw(10) << "QUIZ";
cout << setw(14)<< "ASSIGNMENT" << setw(11) << "MIDTERM" << setw(14) << "FINAL EXAM" << setw(12) << "ACTIVITY";
cout << setw(11)<< "PROJECT" << setw(9) << "TOTALn";
cout << "t| |tt         " << "----------------------------------------------------------------------------------------------------------------------------nn";
while(i <= studcount)
{
if(rec[i].id != "")
{
cout << "t| |tt         " << left << setw(10) << rec[i].id << setw(30) << rec[i].nm << setw(7) << rec[i].sex;
cout << setw(10) << rec[i].quiz << setw(14) << rec[i].hw << setw(11) << rec[i].midT;
cout << setw(14) << rec[i].fExam << setw(12) << rec[i].acts << setw(11) << rec[i].proj << setw(9) << rec[i].total;
cout << endl << endl;
}
i+=1;
}
}
else
{
cout << "t| |ttt There is no current record to view.nn";
return;
}

}