Sie sind auf Seite 1von 4

#include <iostream>

#include <cstring>
#include <cstdlib>
#include <string>

//6 functions int total


// Sortgpa and sortname are same, just use .gpa and .name for each
// 8 steps total

//syntax: student[x].class[y].name

using namespace std;

// Make sure to use this structure


struct Class
{
string name;
int units;
char grade;
};

struct Student
{
string name;
int NumOfClasses;
double gpa = 0.0; // Use so can use float integers
Class classes[20]; // Going inside Class structure, b/c classes involve info
from struct Class
};

void display(Student students[], int iSize);


void sort_name(Student students[], int iSize);
int get_info(Student students[], int iSize);
int get_gpa(Student students[], int iSize);
int search_name(Student students[], int iSize);
void sort_gpa(Student students[], int iSize);

int main()
{
string sName;
Student students[100];
int iSize = 100;
int iIndex = -1;
int iNumOfStudents = get_info(students, iSize);
double findgpa;
int iFind;
int iX;
iX = get_info(students, iSize);

for(iX = 0; iX < iSize; iX++)


{
findgpa = get_gpa(students[iX]);
students[iX].gpa = findgpa;
}

cout<<"\nList of Students entered:\n";


display(students,iSize);
cout<<"\nList of Students sorted on Name:\n";
sort_name(students,iSize);
cout<<"\nEnter a name to search for : ";
cin.ignore();
getline(cin,sName);
iFind = search_name(students,sName);
if(iFind==-1)
cout<<"\nStudent not found";
cout<<"\nList of Students sorted by GPA:\n";
sort_gpa(students,iSize);
return 0;
}

int get_info(Student students[], int iSize)


{
int iX = 0; //Number of students entered

while(students[iX].name != "")
{
students[iX].gpa = 0.0;
}

cout << "Enter Name: ";


cin.getline(students[iX].name);

cout << "Enter number of classes: ";


cin.getline(students[iX].NumOfClasses);

cout << "Enter Name of Class: ";


cin.ignore();
cin.getline(students[iX].classes[].name);
cout << "Enter units for class: ";
cin.getline(students[iX].classes[].units);
cout << "Enter grade for class: ";
cin.getline(students[iX].classes[].grade);

iX++;
cin.ignore();
cout << "Enter student name";

return iX;
}

void display(Student students[], int iSize)


{
int iX;
int iY;
for (iX = 0; iX < iSize; iX++)

cout << "Student: " << students[iX].name << students[iX].

for (iY = 0; iY < students[iX].NumOfClasses; iY++)

cout << students[iX].classes[iY].name << ", "


<< students[iX].classes[iY].units << " Units, Grade " <<
students[iX].classes[iY].grade;
cout << endl;

cout << "GPA: " << students[iX].gpa;


cout << endl;

int get_gpa(Student students[])


{
double gpa = 0.0;
int total_units = 0;
int grade;
int units;

for (int i = 0 i < students; i++)

total_units += students.classes[iX].units;

if (students.classes[iX].grade == 'A' || 'a')


grade = 4;
if (students.classes[iX].grade == 'B' || 'b')
grade = 3;
if (students.classes[iX].grade == 'C' || 'c')
grade = 2;
if (students.classes[iX].grade == 'D' || 'd')
grade = 1;
if (students.classes[iX].grade == 'F' || 'f')
grade = 0;
gpa += units*grade;
}
gpa = gpa/total_units;
return gpa;

void sort_gpa(Student students[], int iSize)


{
Student temp;
int i;
int j;

for(i = 0; i < iSize; i++){


for( j = 0; j < iSize - (i+1) ;j++){
{

if(students[j].gpa < students[j+1].gpa)


{
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;
}

display(students, iSize);
}

void sort_name(Student students[], iSize)


{
Student temp;
int i,j;
for(i=0;i<iSize;i++)
for(j=0;j<iSize-(i+1);j++)
{
if(students[j].gpa < students[j+1].gpa)
{
temp = students[j];
students[j] = students[j+1];
students[j+1] = temp;

display(students,iSize);
}

int search_name(Student students[])


{
int low = 0;
int high = iSize
int mid = z;

while(low<=high)
{
mid = (low + high) / 2;
if(students[mid].name.compare(sName)==0)
{
cout << "Student: " << students[mid].ID << " " << students[mid].name <<
endl;
for(z = 0; z < students[mid].num; z++)
{
cout << students[mid].classes[z].name << ", " <<
students[mid].classes[z].units << " units, Grade " <<
students[mid].classes[k].grade;
cout << endl;
}
cout << "GPA: " << students[mid].gpa;
return mid;
}
else if (students[mid].name.compare(sName) > 0)
high = mid -1;
else if (students[mid].name.compare(sName) < 0)
low = mid+1;
}
return -1;
}
}