In: Computer Science
***IN C++***
Create student structure with the following fields:
Name (cstring or null-terminated character array)
Student ID (int – unique random value between 1000 and 9999)
grade (char – Values A thru F)
birthday (myDate – random value: range 1/1/2000 to 12/31/2005)
Home Town (string)
Create an array of pointers to students of size 10.
Example: Student *stuPtr[10];
Write a function that populates the array with 10 students.
Example: populate(stuPtr);
Write a display function that displays the contents of the array on the screen as shown below –
nicely formatted and left justified.
The displayed list should be nicely formatted with column names like this: All columns should
be left-justified.
Name
Student ID
Grade
Birthday
Home Town
Tom Thumb
1002
C
January 1, 2002
Small Ville
Fred Flintstone
1995
D
February 3, 2003
Bedrock
Sponge Bob
2987
B
June 3, 2001
Bikini Bottom
Create a menu that shows the following options:
1)
Display list sorted by Name
2)
Display list sorted by Student ID
3)
Display list sorted by Grade
4)
Display list sorted by Birthday
5)
Display list sorted by Home Town
6)
Exit
You need to write a sorting function for each of the menu items – 5 options needs 5 functions.
Note:
You must create a function that returns a date between a range of 2 dates.
You will use the myDate class in this program – you will not create any other class. The Student
structure is NOT a class.
Take advantage of your myDate class that you just wrote. Also, it might be helpful to create a
new function that returns a string for the date format:
string myDate::toString( );
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
myDate.cpp
#include "myDate.h"
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
//convert gregorian date to julian date
int Greg2Julian(int month, int day, int year)
{
//declare local variables
int I = year;
int J = month;
int K = day;
int JD= K-32075+1461*(I+4800+(J-14)/12)/4+367*(J-2-(J-14)/12*12)/12-3*((I+4900+(J-14)/12)/100)/4;
//return julian
return JD;
}
//convert julian date to gregorian date
void Julian2Greg(int JD, int & month, int & day, int & year)
{
//declare local variables
int L = JD + 68569;
int N = 4 * L / 146097;
L = L - (146097 * N + 3) / 4;
int I = 4000 * (L + 1) / 1461001;
L = L - 1461 * I / 4 + 31;
int J = 80 * L / 2447;
int K = L - 2447 * J / 80;
L = J / 11;
J = J + 2 - 12 * L;
I = 100 * (N - 49) + I + L;
year = I;
month = J;
day = K;
}
//constructor
myDate::myDate()
{
month = 5;
day = 11;
year = 1959;
}
//myDate constructor with parameters
myDate::myDate(int M, int D, int Y)
{
// calling functions
JD = Greg2Julian(M, D, Y);
Julian2Greg(JD, month, day, year);
//using if loop
if (month == M && day == D && year == Y);
else
{
month = 5;
day = 11;
year = 1959;
JD = Greg2Julian(month, day, year);
}
}
//display method used to display month, day and year
void myDate::display()
{
//declare month usinng array
string months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
//display month, day and year
cout << months[month - 1] << " " << day << ", " << year;
}
//randomly displaying start date and end date
void myDate::random()
{
myDate start = myDate(01, 01, 1999);
myDate end = myDate(12, 31, 2004);
this->JD = (rand() % start.daysBetween(end)) + start.JD;
Julian2Greg(JD, month, day, year);
}
//display month, day and year
string myDate::toString()
{
string months[12] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };
stringstream buffer;
buffer << months[month - 1] << " " << day << ", " << year;
return buffer.str();
}
//increasing date
void myDate::increaseDate(int N)
{
JD += N;
Julian2Greg(JD, month, day, year);
}
//decreasing date
void myDate::decreaseDate(int N)
{
JD -= N;
Julian2Greg(JD, month, day, year);
}
//calculating inbetween day, month and year
int myDate::daysBetween(myDate D)
{
JD = Greg2Julian(month, day, year);
return Greg2Julian(D.month, D.day, D.year) - JD;
}
//getting month
int myDate::getMonth()
{
return month;
}
//getting day
int myDate::getDay()
{
return day;
}
//getting year
int myDate::getYear()
{
return year;
}
int myDate::dayOfYear()
{
return JD - Greg2Julian(1, 1, year) + 1;
}
//display day name
string myDate::dayName()
{
string days[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
return days[JD % 7];
}
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
myDate.h
#ifndef MyDate_h
#define MyDate_h
#include <string>
using namespace std;
// class name as myDate
class myDate
{
public:
//declare functions as public
myDate();
myDate(int M, int D, int Y);
void display();
void random();
string toString();
void increaseDate(int N);
void decreaseDate(int N);
int daysBetween(myDate D);
int getMonth();
int getDay();
int getYear();
int dayOfYear();
string dayName();
private:
//declare function as private
int month, day, year;
int JD;
};
#endif
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
main.cpp
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include "myDate.h"
#include <string.h>
#include <cstdlib>
#include <cstring>
using namespace std;
//creating a structure called student
struct Student
{
//declare variables
char name[20];
int id;
char grade;
myDate bday;
string homeTown;
};
// function prototypes
void populateStudents(Student *students[]);
void sortName(Student *students[]);
void sortID(Student *students[]);
void sortGrade(Student *students[]);
void sortBday(Student *students[]);
void sortTown(Student *students[]);
//main function
int main()
{
//declare pointer array variable
Student *students[10];
//calling function
populateStudents(students);
//display following details using while loop
while (true)
{
cout << setw(20) << left << "Name";
cout << setw(13) << left << "Student ID";
cout << setw(10) << left << "Grade";
cout << setw(20) << left << "Birthday";
cout << setw(10) << left << "Home Town" << endl;
for (int i = 0; i < 10; i++)
{
cout << setw(20) << left << students[i]->name;
cout << setw(13) << left << students[i]->id;
cout << setw(10) << left << students[i]->grade;
cout << setw(20) << left << students[i]->bday.toString();
cout << setw(10) << left << students[i]->homeTown << endl;
}
//display menu options
cout << "\nWelcome to Solitaire Prime!\n"
<< "1) Display list sorted by Name\n"
<< "2) Display list sorted by Student ID\n"
<< "3) Display list sorted by Grade\n"
<< "4) Display list sorted by Birthday\n"
<< "5) Display list sorted by Home Town\n"
<< "6) Exit\n";
//getting menu option from user
int i;
cin >> i;
//checking the condition using switch loop
switch (i)
{
case 1:
sortName(students);
break;
case 2:
sortID(students);
break;
case 3:
sortGrade(students);
break;
case 4:
sortBday(students);
break;
case 5:
sortTown(students);
break;
case 6:
exit(0);
default:
cout << "Invalid input";
break;
}
}
return 0;
}
void populateStudents(Student *students[])
{
char grades[] = { 'A', 'B', 'C', 'D', 'F' };
string names[] = { "Clark Kent", "Fred Flintstone", "Sponge Bob", "John Smith", "Jeff Bezos", "Bill Gates", "Bernie Sanders", "Gabe Newell", "Nathanael Gastelum", "Hank Hill" };
string towns[] = { "Los Angeles", "Barcelona", "San Francisco", "New York", "London", "Jakarta", "Hong Kong", "Singapore", "Long Beach", "Chicago" };
for (int i = 0; i < 10; i++)
{
students[i] = new Student;
strcpy(students[i]->name, names[i].c_str());
students[i]->id = rand() % 9000 + 1000;
students[i]->grade = grades[rand() % 5];
students[i]->bday = myDate();
students[i]->bday.random();
students[i]->homeTown = towns[i];
}
}
//sorting student name
void sortName(Student *students[])
{
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
{
if (strcmp(students[i]->name, students[j]->name) < 0)
{
Student *temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
//sorting student id
void sortID(Student *students[])
{
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
{
if (students[i]->id < students[j]->id)
{
Student *temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
//sorting student grade
void sortGrade(Student *students[])
{
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (students[i]->grade < students[j]->grade)
{
Student *temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
//sorting birthday
void sortBday(Student *students[])
{
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
{
if (students[i]->bday.daysBetween(students[j]->bday) > 0)
{
Student *temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}
//sorting town
void sortTown(Student *students[])
{
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++)
{
if (students[i]->homeTown.compare(students[j]->homeTown) < 0)
{
Student *temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
}