CSI 250L Lab 13 Solution, Spring 2012

American University of Science and Technology · Programming II Lab · 17 pages · Dr. Saeed Raheel · with solution

All CSI 250L files
CSI 250L Lab 13 Solution, Spring 2012, page 1

Stuck on a question? A tutor can walk you through it.

CSI 250L Lab 13 Solution, Spring 2012, page 2 CSI 250L Lab 13 Solution, Spring 2012, page 3 CSI 250L Lab 13 Solution, Spring 2012, page 4 CSI 250L Lab 13 Solution, Spring 2012, page 5 CSI 250L Lab 13 Solution, Spring 2012, page 6 CSI 250L Lab 13 Solution, Spring 2012, page 7 CSI 250L Lab 13 Solution, Spring 2012, page 8 CSI 250L Lab 13 Solution, Spring 2012, page 9 CSI 250L Lab 13 Solution, Spring 2012, page 10

Showing the first 10 of 17 pages. Download the PDF to see all of it.

More CSI 250L files

Show the text of this lab
#include "Undergraduate.h" #include "Graduate.h" #include <iostream> #include <fstream> using namespace std; using std::ofstream; int main() { int Type; string name; int t1, t2, t3; int N; cout << "How many students would you like to create? "; cin >> N; Student **Students = new Student*[N]; cout << "Please enter the information about " << N << " students\n"; for (int i=0;i<N;++i) { cout << "[" << i+1 << "/" << N << "] Type 1 for Undergraduate and 2 for Graduate : "; cin >> Type; cout << "\nPlease enter the student's name : "; cin >> name; cout << "Please enter the student's first grade : "; cin >> t1; cout << "Please enter the student's second grade : "; cin >> t2; cout << "Please enter the student's third grade : "; cin >> t3; if (Type == 1) Students[i] = new Undergraduate(name, t1, t2, t3); else Students[i] = new Graduate(name, t1, t2, t3); cout << endl; } cout << "\nBefore swapping ..." << endl; for (int i=0;i<N;++i) cout << Students[i]->getName() << endl; for (int i=0;i<N;++i) for (int j=0;j<N-1;++j) { if (Students[j]->getName()>Students[j+1]->getName()) { Student *temp; temp = Students[j]; Students[j] = Students[j+1]; Students[j+1] = temp; } } cout << "\nAfter swapping ..." << endl; for (int i=0;i<N;++i) cout << Students[i]->getName() << endl; ofstream output("Students.txt", ios::out); output << "Name Grade1 Grade2 Grade3 Pass/Fail" << endl; for (int i=0;i<N;++i) { output << Students[i]->getName() << "\t" << Students[i]->getTest1() << "\t" << Students[i]->getTest2() << "\t" << Students[i]->getTest3() << "\t" << Students[i]->get_course_grade() << endl; } output.close(); cout << "\nInfo saved in Students.txt!" << endl; system("pause"); return 0; } #include "Graduate.h" Graduate::Graduate(string n, int t1, int t2, int t3):Student(n, t1, t2, t3) { } string Graduate::get_course_grade() { int Average = 0.25*getTest1()+0.25*getTest2()+0.5*getTest3(); return (Average>=80? "Pass": "Fail"); } #ifndef GRADUATE_H #define GRADUATE_H #include "Student.h" #include <string> using std::string; class Graduate:public Student { public: Graduate(string, int, int, int); string get_course_grade(); }; #endif #include "Student.h" Student::Student() { setName(""); setTest1(-1); setTest2(-1); setTest3(-1); } Student::Student(string n, int t1, int t2, int t3) { setName(n); setTest1(t1); setTest2(t2); setTest3(t3); } string Student::getName() { return Name; } void Student::setName(string n) { Name = n; } void Student::setTest1(int t) { Test1 = t; } void Student::setTest2(int t) { Test2 = t; } void Student::setTest3(int t) { Test3 = t; } int Student::getTest1() { return Test1; } int Student::getTest2() { return Test2; } int Student::getTest3() { return Test3; } #ifndef STUDENT_H #define STUDENT_H #include <string> using std::string; class Student { private: string Name; int Test1, Test2, Test3; public: Student(); Student(string, int , int, int); virtual string get_course_grade()=0; void setName(string); void setTest1(int); void setTest2(int); void setTest3(int); string getName(); int getTest1(); int getTest2(); int getTest3(); }; #endif #include "Undergraduate.h" Undergraduate::Undergraduate(string n, int t1, int t2, int t3):Student(n, t1, t2, t3) { } string Undergraduate::get_course_grade() { int Average = 0.25*getTest1()+0.25*getTest2()+0.5*getTest3(); return (Average>=80? "Pass": "Fail"); } #ifndef UNDERGRADUATE_H #define UNDERGRADUATE_H #include "Student.h" #include <string> using std::string; class Undergraduate:public Student { public: Undergraduate(string, int, int, int); string get_course_grade(); }; #endif #include "Student.h" #include <iostream> #include <fstream> using namespace std; using std::ostream; int main() { const int N = 2; Student *Students[N]; int id; string fn; string ln; string m; float gpa; cout << "Please enter the information about " << N << " students" << endl; for (int i=0;i<N;++i) { cout << "[" << i+1 << "/" << N << "] Please enter the student's ID : "; cin >> id; cout << "Please enter the student's first name : "; cin >> fn; cout << "Please enter the student's last name : "; cin >> ln; cout << "Please enter the student's major : "; cin >> m; cout << "Please enter the student's GPA : "; cin >> gpa; Students[i] = new Student(id, fn, ln, m, gpa); cout << endl; } //Write a C++ program to input information about several students and save it in a binary file, "Students.dat". ofstream output("Students.dat", ios::binary); for (int i=0;i<N;++i) { id = Students[i]->getID(); ln = Students[i]->getLname(); fn = Students[i]->getFname(); m = Students[i]->getMajor(); gpa = Students[i]->getGPA(); output.write((char *)&id, sizeof(id)); output.write((char *)&ln, sizeof(ln)); output.write((char *)&fn, sizeof(fn)); output.write((char *)&m, sizeof(m)); output.write((char *)&gpa, sizeof(gpa)); } cout << "\nInfo saved to Students.dat!\n" << endl; output.close(); //Display a list of student sorted by ascending order of student fname. cout << "\nBefore swapping ..." << endl; for (int i=0;i<N;++i) cout << Students[i]->getFname() << endl; for (int i=0;i<N;++i) for (int j=0;j<N-1;++j) { if (Students[j]->getFname()>Students[j+1]->getFname()) { Student *temp; temp = Students[j]; Students[j] = Students[j+1]; Students[j+1] = temp; } } cout << "\nAfter swapping ..." << endl; for (int i=0;i<N;++i) cout << Students[i]->getFname() << endl; //Save in a Sequential file, HonorList.txt, the information about students who have GPA>2.5. ofstream hl("HonorList.txt", ios::out); hl << "ID FName LName Major GPA" << endl; for (int i=0;i<N;++i) { if (Students[i]->getGPA()>2.5) hl << Students[i]->getID() << "\t" << Students[i]->getFname() << "\t" << Students[i]->getLname() << "\t" << Students[i]->getMajor() << "\t" << Students[i]->getGPA() << endl; } hl.close(); cout << "\nInfo saved in HonorList.txt!" << endl; //Change the students major Computer to be Computer-Science in the Students.dat file. Student student; ifstream input("Students.dat", ios::binary); int count = 1; while ( input && !input.eof()) { input.read((char *)&id, sizeof(id)); input.read((char *)&ln, sizeof(ln)); input.read((char *)&fn, sizeof(fn)); input.read((char *)&m, sizeof(m)); input.read((char *)&gpa, sizeof(gpa)); cout << id << ", " << ln << ", " << fn << ", " << m << ", " << gpa << endl; /*if (m == "Computer") { m = "Computer-Science"; input.seekp(count * sizeof(Student)); input.write(reinterpret_cast< const char * >(&student), sizeof(Student)); }*/ ++count; } /*Printing final results from Students.dat file ifstream final("Students.dat", ios::binary); while ( final && !final.eof()) { final.read( reinterpret_cast< char * >(&student), sizeof(Student)); student.print(); }*/ system("pause"); return 0; } #include "Student.h" #include <iostream> using namespace std; Student::Student() { setID(0); setFname(""); setLname(""); setMajor(""); setGPA(0.0); } Student::Student(int id, string fn, string ln, string major, float gpa) { setID(id); setFname(fn); setLname(ln); setMajor(major); setGPA(gpa); } void Student::setID(int id) { ID = id; } void Student::setFname(string fn) { Fname = fn; } void Student::setLname(string ln) { Lname = ln; } void Student::setMajor(string m) { Major = m; } void Student::setGPA(float gpa) { GPA = gpa; } int Student::getID() { return ID; } string Student::getFname() { return Fname; } string Student::getLname() { return Lname; } string Student::getMajor() { return Major; } float Student::getGPA() { return GPA; } void Student::print() { cout << "ID = " << getID() << ", Last Name = " << getLname() << ", First Name = " << getLname() << ", Major = " << getMajor() << ", GPA = " << getGPA() << endl; } #ifndef STUDENT_H #define STUDENT_H #include <string> using std::string; class Student { private: int ID; string Fname; string Lname; string Major; float GPA; public: Student(); Student(int, string, string, string, float); void setID(int); void setFname(string); void setLname(string); void setMajor(string); void setGPA(float); int getID(); string getFname(); string getLname(); string getMajor(); float getGPA(); void print(); }; #endif #include <fstream> //ifstream, ofstream #include <iostream> //cout using namespace std; //ExampleStruct typedef struct { char chr; int num; } ExampleStruct; //ExampleClass class ExampleClass { public: ExampleClass(): m_chr(' '), m_num(0){} ExampleClass(char c, int n): m_chr(c), m_num(n){} friend ostream& operator<<(ostream&, const ExampleClass&); private: char m_chr; int m_num; }; ostream& operator<<(ostream& out, const ExampleClass& c) { out << "c.chr:" << c.m_chr << endl; out << "c.num:" << c.m_num << endl; return out; } void write_binary_output_example() { char chr = 'A'; int num = 1; ExampleStruct s = {'B', 2}; ExampleClass c('C', 3); ofstream file("example.bin", ios::binary); //The first parameter of method write() is //a pointer to char representing the address of //an array of bytes where the data elements to be //written are taken from. //The second parameter is an integer value that specifies //the number of characters to be written to the memory block. file.write((char *)(&chr), sizeof(chr)); file.write((char *)(&num), sizeof(num)); file.write((char *)(&s), sizeof(s)); file.write((char *)(&c), sizeof(c)); file.close(); } void read_binary_input_example() { char chr = ' '; int num = 0; ExampleStruct s; ExampleClass c; ifstream file("example.bin", ios::binary); //The first parameter of the method read() is //a pointer to char representing the address of //an array of bytes where the read data elements //are stored to. //The second parameter is an integer value that specifies //the number of characters to be read from the memory block. file.read((char *)(&chr), sizeof(chr)); cout << "chr:" << chr << endl; file.read((char *)(&num), sizeof(num)); cout << "num:" << num << endl; file.read((char *)(&s), sizeof(s)); cout << "s.chr:" << s.chr << endl; cout << "s.num:" << s.num << endl; file.read((char *)(&c), sizeof(c)); cout << c << endl; file.close(); } /*int main() { write_binary_output_example(); read_binary_input_example(); getchar(); return 0; }*/