1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
class Student //Class
{
public: //Main can access
string name;
int age;
void print() //Print them
{
cout << "Student: " << name << endl;
cout << "Age: " << age << endl;
}
void store(string _name, int _age) //Store them
{
name = _name;
age = _age;
}
};
int LinearSearch(Student* studentArray, int age, int students) //Linear Search Function
{
for (int i = 0; i <= students; i++) //Loop through ages of students
{
if (studentArray[i].age == age) //If age of student == Input age
{
return i; //Return Student ID
}
}
return -1; //Outside loop, didn't return a value, so return -1
}
int BinarySearch(Student* studentArray, int age, int min, int max) //Binary Search Function
{
Student student[5];
int s;
int search;
s = (max - min) / 2;
search = studentArray[s].age;
if (age == search)
{
return search;
}
else if (age < search)
{
BinarySearch(student, age, min, s);
}
else if (age > search)
{
BinarySearch(student, age, s, max);
}
else if (search == max)
{
return -1;
}
}
bool sort(const Student& lhs, const Student& rhs) //Sort Function
{
return lhs.age < rhs.age; //Return lowest values
}
int main()
{
Student student[5]; //Create 5 slots for Student Class
string name;
string teacher;
int age;
int students;
students = 4;
cout << "Welcome, what's your name?: ";
cin >> teacher;
cout << "Welcome to your new job, " << teacher << ".\n\n";
cin.get();
cout << "As a new teacher, you will have to set up your class in this program.";
cin.get();
cout << "Note that you're only allowed to teach 5 students as max.";
cin.get();
cout << "Please enter your students' information below\n";
for (int i = 0; i <= students; i++) //Loop through amount of students inputted
{
cout << "Enter student " << i << " name: ";
cin >> name;
cout << "Enter student " << i << " age: ";
cin >> age;
student[i].store(name, age); //Store all students information
}
cout << "Let's make sure your students got put in correctly.\n";
cout << "Print student ID: ";
int input;
while (true)
{
cin >> input;
if (input <= students)
{
student[input].print(); //Print student information
break;
}
else
{
cout << "Error, student " << input << " does not exist. Please try again: ";
}
}
cout << "\nTime skips until after spring break...\n";
cin.get();
cout << "Welcome back, " << teacher << ".";
cin.get();
while (true)
{ //Infinite while opening bracket
cout << "\nWhat activity do you want to do today?\n";
cout << "1. Search information about a student (Linear)\n";
cout << "2. Search information about a student (Binary)\n";
cout << "3. Print class in order of age\n";
cout << "4. Quit\n";
cin >> input;
if (input == 1) //Linear Search
{
int search;
cout << "Student age: ";
cin >> age;
search = LinearSearch(student, age, students);
if (search == -1) //Returned not found
{
cout << "Error: There's no student by the age: " << age << endl;
}
else //Returned found
{
cout << "The first match was: " << student[search].name << endl;
cout << student[search].name << " is student number: " << search;
}
}
else if (input == 2) //Binary Search
{
sort(student, student + students, sort); //Uses <algorithm>
int search;
cout << "Student age: ";
cin >> age;
search = BinarySearch(student, age, 0, students);
if (search == -1)
{
cout << "Couldn't find student by age: " << age << endl;
}
else
{
cout << "Student ID: " << search << endl;
student[search].print();
}
}
else if (input == 3) //Sort
{
sort(student, student + students, sort); //Uses <algorithm>
for (int i = 0; i <= students; i++)
{
student[i].print(); //Print student info
}
}
else if (input == 4) //Quit
{
exit (EXIT_SUCCESS); //Only way to exit infinite while
}
else if (input == 5)
{
for (int i = 0; i <= students; i++)
{
student[i].print(); //Print student info
}
}
} //Infinite while closing bracket
cin.sync();
cin.get();
return 0;
}
| |