#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
struct Student
{
char name[20];
int count;
char address[50];
}students [3];
int RequestForGrade(Student Student1);
int RequestForAddress(Student Student2);
int FailinExam(Student Student3);
int SortByGrade(Student Student4);
int main()
{
int choice;
char buffer[50];
int n;
for (n = 0;n < 3;n++)
{
cout << "Enter Name:";
cin.getline(students[2].name, 20);
cout << "Enter Grade:";
cin.getline(buffer, 50);
students[n].count = atoi(buffer);
cout << "Enter Address:";
cin.getline(students[2].address, 50);
}
cout << "Please Enter The Function You Want:\n" << endl;
cin >> choice;
switch (choice)
{
case 1:
{
SortByGrade;break;
}
case 2:
{
RequestForGrade;break;
}
case 3:
{
RequestForAddress;break;
}
case 4:
{
FailinExam;break;
}
default:cout << "Exit the Program" << endl;break;
}
}
int RequestForGrade(Student Student1)
{
char Nname[20];
cout << "Please Enter The Name\n";
cin >> Nname;
for (int i = 0;i < 3;i++)
{
if (strcmp(Nname, students[i].name) == 0)
cout << students[i].count;
}
return 0;
}
int RequestForAddress(Student Student2)
{
char Nname[20];
cout << "Please Enter The Name\n";
cin >> Nname;
for (int i = 0;i < 3;i++)
{
if (strcmp(Nname, students[i].name) == 0)
cout << students[i].address;
}
return 0;
}
int FailinExam(Student Student3)
{
int count0 = 0;
for (int i = 0;i < 3;i++)
{
if ((students[i].count) < 60)
count0++;
}
cout << "The number of people who failed the exam is" << count0<<endl;
return 0;
}
int SortByGrade(Student Student4)
{
int lh, rh, k, tmp;
char tmp1[20];
for (lh = 0;lh < 3;++lh)
{
rh = lh;
for (k = lh;k < 3;++k)
{
if (students[k].count < students[rh].count)
rh = k;
tmp = students[lh].count;
students[lh].count = students[rh].count;
students[rh].count = tmp;
strcpy(tmp1, students[lh].name);
strcpy(students[lh].name, students[rh].name);
strcpy(students[rh].name, tmp1);
}
}
for (lh = 0;lh < 2;++lh)
{
cout << students[lh].name;
}
return 0;
}
这个程序要求达到的功能: