CSI 250L Lab 12 Solution, Spring 2012
American University of Science and Technology · Programming II Lab · 9 pages · Dr. Saeed Raheel · with solution
More CSI 250L files
- Lab 1 Solution · Spring 2012Lab · with solutionView
- Lab 1 · Spring 2012LabView
- Lab 10 Solution · Spring 2012Lab · with solutionView
- Lab 10 · Spring 2012LabView
- Lab 11 Solution · Spring 2012Lab · with solutionView
- Lab 11 · Spring 2012LabView
- Lab 12 · Spring 2012LabView
- Lab 13 Solution · Spring 2012Lab · with solutionView
Show the text of this lab
// LabWork12_P1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
class Car
{
private:
int num;
char brand[20];
int model;
public:
Car()
{
setNum( 1 );
setBrand( "BMW" );
setModel( 2000 );
}
Car(int n, string b, int m)
{
setNum( n );
setBrand( b );
setModel( m );
}
void setNum( int n )
{
num = n;
}
void setBrand( string b )
{
const char * brandValue = b.data();
int length = strlen( brandValue );
length = ( length < 20 ? length : 19 );
strncpy( brand, brandValue, length );
brand[length] = '\0';
}
void setModel( int m )
{
model = m;
}
int getNum()
{
return num;
}
string getBrand()
{
return brand;
}
int getModel()
{
return model;
}
void print()
{
cout << "Num: " << num << "\nBrand: " << brand << "\nModel: " <<
model << endl;
}
};
void main()
{
int N, num, model;
string brand;
cout << "Enter the number of cars: ";
cin >> N;
Car * c = new Car[N];
for( int i = 0; i < N; i++ )
{
cout << "Enter the number of the car: ";
cin >> num;
cout << "Enter the brand of the car: ";
cin >> brand;
cout << "Enter the model of the car: ";
cin >> model;
c[i].setNum( num );
c[i].setBrand( brand );
c[i].setModel( model );
}
ofstream myFile( "Car.dat", ios::out );
if( !myFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}
for( int i = 0; i < N; i++ )
myFile.write( reinterpret_cast <const char *> (&c[i]), sizeof( Car )
);
system( "pause" );
}
// LabWork12_P2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;
class Show
{
protected:
const int totalAudience;
public:
Show()
:totalAudience( 2000000 )
{
}
Show(int t)
:totalAudience( t )
{
}
virtual double calculateRating() = 0;
};
class TalkShow:public Show
{
private:
string hostName;
string time;
int audience;
public:
TalkShow()
:Show()
{
setHostName( "Kalam Annas by Marcel Ghanem" );
setTime( "AM" );
setAudience( 880000 );
}
TalkShow( int tA, string h, string t, int a)
:Show(tA)
{
setHostName( h );
setTime( t );
setAudience( a );
}
void setHostName( string h )
{
hostName = h;
}
void setTime( string t )
{
if( t == "AM" || t == "PM" )
time = t;
else
time = "AM";
}
void setAudience( int a )
{
audience = a;
}
string getHostName()
{
return hostName;
}
string getTime()
{
return time;
}
int getAudience()
{
return audience;
}
double calculateRating()
{
if( time == "AM" )
return (double)audience/totalAudience * 0.9;
else
return (double)audience/totalAudience;
}
void print()
{
cout << hostName << "\nNumber of Audience: " << audience
<< "\nRating: " << calculateRating() << endl;
}
};
class ComedyShow:public Show
{
private:
string tName;
string mainActor;
int actorRating;
int audience;
public:
ComedyShow()
:Show()
{
setTName( "Irbit Tinhal" );
setMainActor( "Patricia Dagher" );
setActorRating( 8 );
setAudience( 400000 );
}
ComedyShow( int tA, string t, string m, int r, int a)
:Show(tA)
{
setTName( t );
setMainActor( m );
setActorRating( r );
setAudience( a );
}
void setTName( string t )
{
tName = t;
}
void setMainActor( string m )
{
mainActor = m;
}
void setActorRating( int r )
{
if( r >= 1 && r <= 10 )
actorRating = r;
else
actorRating = 1;
}
void setAudience( int a )
{
audience = a;
}
string getTName()
{
return tName;
}
string getMainActor()
{
return mainActor;
}
int getActorRating()
{
return actorRating;
}
int getAudience()
{
return audience;
}
double calculateRating()
{
if( actorRating > 7 )
return (double)audience/totalAudience * 1.1;
else
return (double)audience/totalAudience;
}
void print()
{
cout << tName << " by " << mainActor << "\nNumber of Audience: " <<
audience
<< "\nRating: " << calculateRating() << endl;
}
};
void main()
{
int N, totalAudience, audience, actorRating, choice;
string hostName, time, tName, mainActor;
cout << "Enter the number of shows: ";
cin >> N;
vector <Show *> s(N);
for( int i = 0; i < N; i++ )
{
cout << "Enter the total number of audience: ";
cin >> totalAudience;
cout << "Enter your choice: 1 for TalkShow & 2 for ComedyShow: ";
cin >> choice;
if( choice == 1 )
{
cout << "Enter the hostName: ";
cin >> hostName;
cout << "Enter the time (AM/PM): ";
cin >> time;
cout << "Enter the number of audience: ";
cin >> audience;
s[i] = new TalkShow( totalAudience, hostName, time,
audience );
}
else if( choice == 2 )
{
cout << "Enter the show Name: ";
cin >> tName;
cout << "Enter the name of the main actor: ";
cin >> mainActor;
cout << "Enter the rating of the actor: ";
cin >> actorRating;
cout << "Enter the number of audience: ";
cin >> audience;
s[i] = new ComedyShow( totalAudience, tName, mainActor,
actorRating, audience );
}
else
cout << "Invalid choice." << endl;
}
ofstream myFile( "ComedyShow.txt", ios::out );
if( !myFile )
{
cerr << "File could not be opened" << endl;
exit( 1 );
}
for( int i = 0; i < N; i++)
{
TalkShow *ptr1 = dynamic_cast <TalkShow *>(s[i]);
ComedyShow *ptr2 = dynamic_cast <ComedyShow *>(s[i]);
if( ptr1 != 0 )
if( ptr1->getTime() == "AM" )
cout << ptr1->getHostName();
if( ptr2 != 0 )
{
myFile << ptr2->getTName() << " " << ptr2->getMainActor() << "
"
<< ptr2->getActorRating() << " " << ptr2-
>getAudience() << endl;
cout << ptr2->getMainActor() << endl;
}
}
system( "pause" );
}