所以我必须为以下程序编写代码:
编写一个带有主函数和菜单的程序,用于选择一个函数: a)为学生输入数据(教师编号,年龄,性别)(最多25个) b)将男女学生的数据重写为两个新阵列并输出数组和平均年龄 c)输出最年轻的学生并使数组按年龄的升序排列并输出数组 d)按教师编号搜索学生并输出他的信息
好的,到目前为止一切顺利。 a)和d)正在按预期工作,但b)和c)给了我一些麻烦。在c)它说最年轻的学生是-88758375岁,并没有输出阵列。在b)它给我一个逻辑错误,它说整数除零并崩溃程序。我真的试图找到任何错误,但我被困住了,所以我请你帮忙:)))
#include "stdafx.h"
#include <iostream>
using namespace std;
const int N = 25;
struct student
{
int fN;
int age;
char sex;
};
// a)
void input(student fN[N], int numberOfStudents)
{
for (int i = 0; i<numberOfStudents; i++)
{
cout << "Faculty number: ";
cin >> fN[i].fN;
cout << "Age: ";
cin >> fN[i].age;
cout << "Sex: ";
cin >> fN[i].sex;
cout << endl;
}
}
// b)
void rearrange(student fN[N], student fNm[N], student fNf[N], int numberOfStudents, int m, int f)
{
int avgAgeM = 0, avgAgeF = 0;
for (int i = 0; i < numberOfStudents; i++)
{
if (fN[i].sex == 'm')
{
fNm[m].fN = fN[i].fN;
fNm[m].age = fN[i].age;
fNm[m].sex = fN[i].sex;
m++;
avgAgeM = avgAgeM + fN[i].age;
}
else if (fN[i].sex == 'f')
{
fNf[f].fN = fN[i].fN;
fNf[f].age = fN[i].age;
fNf[f].sex = fN[i].sex;
f++;
avgAgeF = avgAgeF + fN[i].age;
}
cout << endl;
for (int i = 0; i < m; i++)
{
cout << "\tFaculty number: " << fNm[i].fN << "\tAge: " << fNm[i].age << "\tSex: " << fNm[i].sex << endl;
}
cout << "Average male age: " << avgAgeM / m << "\n\n";
for (int i = 0; i<f; i++)
{
cout << "\tFaculty number: " << fNf[i].fN << "\tAge: " << fNf[i].age << "\tSex: " << fNf[i].sex << endl;
}
cout << "Average female age: " << avgAgeF / f << "\n\n";
}
}
// c)
void ascendingAge(student fNm[N], student fNf[N], int m, int f)
{
int x, y;
char z;
for (int i = 0; i < m-1; i++)
for (int j = 0; j < m-i-1; j++)
{
if (fNm[j].age > fNm[j + 1].age)
{
x = fNm[j].age;
y = fNm[j].fN;
z = fNm[j].sex;
fNm[j + 1].age = fNm[j].age;
fNm[j].age = x;
fNm[j + 1].fN = fNm[j].fN;
fNm[j].fN = y;
fNm[j + 1].sex = fNm[j].sex;
fNm[j].sex = z;
}
}
for (int i = 0; i < f-1; i++)
for (int j = 0; j < f-i-1; j++)
{
if (fNf[j].age > fNf[j + 1].age)
{
x = fNf[j].age;
y = fNf[j].fN;
z = fNf[j].sex;
fNf[j + 1].age = fNf[j].age;
fNf[j].age = x;
fNf[j + 1].fN = fNf[j].fN;
fNf[j].fN = y;
fNf[j + 1].sex = fNf[j].sex;
fNf[j].sex = z;
}
}
cout << "The youngest female student is " << fNf[0].age << " year-old." << endl;
for (int i = 0; i < m; i++)
cout << "\tFaculty number: " << fNm[i].fN << "\tAge: " << fNm[i].age << "\tSex: " << fNm[i].sex << endl;
for (int i = 0; i<f; i++)
cout << "\tFaculty number: " << fNf[i].fN << "\tAge: " << fNf[i].age << "\tSex: " << fNf[i].sex << endl;
cout << endl;
}
//d
void searchStudent(student fN[N], int numberOfStudents)
{
int x, index;
bool yes = false;
cout << "Enter a faculty number: ";
cin >> x;
for (int i = 0; i < numberOfStudents; i++)
if (fN[i].fN == x)
{
yes = true;
index = i;
}
cout << endl;
if (yes == true)
cout << "\tFaculty number: " << fN[index].fN << "\tAge: " << fN[index].age << "\tSex: " << fN[index].sex << endl;
else
cout << "No such faculty number.\n\n";
}
int main()
{
student fN[N], fNm[N], fNf[N];
int numberOfStudents, m = 0, f = 0;
char check;
cout << "Enter number of students: ";
cin >> numberOfStudents;
BACK:
cout << "\n\n";
cout << "\t a) \n\t b) \n\t c) \n\t d)\n Press'q' to exit.\n\n";
cin >> check;
switch (check)
{
case 'a':
input(fN, numberOfStudents);
goto BACK;
break;
case 'b':
rearrange(fN, fNm, fNf, numberOfStudents, m, f);
goto BACK;
break;
case 'c':
ascendingAge(fNm, fNf, m, f);
goto BACK;
break;
case 'd':
searchStudent(fN, numberOfStudents);
goto BACK;
break;
case 'q':
return 0;
break;
default:
cout << "Wrong input.\n";
goto BACK;
}
system("pause");
return 0;
}
答案 0 :(得分:2)
在函数重新排列中,您需要通过引用传递m
和k
:
void rearrange(student fN[N], student fNm[N], student fNf[N], int numberOfStudents, int& m, int& f):
Оr他们不会被改变
答案 1 :(得分:1)
您似乎希望m
和f
成为此功能的输出
void rearrange(student fN[N], student fNm[N], student fNf[N], int numberOfStudents, int m, int f)
但只有在通过引用传递时才会起作用:
void rearrange(student fN[N], student fNm[N], student fNf[N], int numberOfStudents, int& m, int& f)
你应该通过测试防止除以零。而不是:
cout << "Average male age: " << avgAgeM / m << "\n\n";
使用
if (m)
cout << "Average male age: " << avgAgeM / m << "\n\n";
else
cout << "There are zero males\n\n";
,同样适用于f