Professional Documents
Culture Documents
Sheet1 Solution
Sheet1 Solution
Problem 1
#include<iostream>
using namespace std;
class Time
{
public:
void readTime(bool &errorFlag);
int subtarctTimes(Time t);
void displayTime();
private:
int minutes;
};
void Time::readTime(bool &errorFlag)
{
enum AM_PM {AM,PM} AM_or_PM;
int hour;
int minute;
const char delimiter = ':';
errorFlag = true;
cout<<"enter h";
if(!(cin >>hour))
return;
if(hour<0||hour>12)
return;
char c;
cout<<"enter :";
cin >>c;
if(c!=delimiter)
return;
cout<<"enter m";
if(!(cin>>minute))
return;
cout<<"int";
if(minute<0||minute>59)
return;
cout<<"enter a or p"<<endl;
cin>>c;
if(c=='A'||c=='a')
AM_or_PM=AM;
else if(c=='P'||c=='p')
AM_or_PM=PM;
else
return;
errorFlag = false;
if(hour==12)
minutes=minute;
else
1
minutes = hour*60+minute;
if(AM_or_PM==PM)
minutes+=60*12;
}
int Time::subtarctTimes(Time t)
{
return minutes-(t.minutes);
}
void Time::displayTime()
{int minute,hour;
enum AM_PM {AM,PM} AM_or_PM;
minute=minutes;
hour=0;
if (minute>60*12)
{minute-=60*12;
AM_or_PM=PM;
}
else
AM_or_PM=AM;
int i=1;
while(minute>=60*i)
i++;
minute-=60*(i-1);
hour+=(i-1);
if (AM_or_PM=AM)
cout<<hour<<":"<<minute<<"AM";
else
cout<<hour<<":"<<minute<<"AM";
}
Problem 2
#include <iostream.h> // < > mean that we include a file stored in the c++ 'include' folder
#include "time.h" // " " mean that we include a file stored in the same folder in
// which this file is stored
main()
{
2
// Since it is passed by reference, the changes that the function make, affect
// the variable in main().
while (errorFlag) //or while(errorFlag == true)
{
cout << "Start time was incorrectly formatted; try again: ";
start.readTime(errorFlag);
}
// Read stop time:
cout << "Enter stop time:";
stop.readTime(errorFlag);
while (errorFlag)
{
cout << "Stop time was incorrectly formatted; try again: ";
stop.readTime(errorFlag);
}
cout<<"Enter the distance travelled (in miles):";
cin>>dist;
int diff=stop.subtractTimes (start);
// The object 'stop' calls the function subtractTimes, so the private member
// variable 'minutes' in subtractTimes() belongs to 'stop'.
// The object 'start' is passed to the function, so t.minutes is minutes of 'start'
Problem 4
#include<string>
using namespace std;
class Person
{
private:
string name;
int age;
float salary;
public:
void setName(string n){name=n;}
void setAge(int age1){age=age1;}
void setSalary(float salary1){salary=salary1;}
string getName(){return name;}
int getAge(){return age;}
float getSalary(){return salary;}
};
______________________________________________________________________________
#include<iostream>
#include"person.h"
main()
{
const int num_persons =20;
3
Person p[num_persons];
string n;int age;float salary;int i;
for(i=0;i<num_persons;i++)
{
cout<<"Enter the name of the person number "<<i+1<<" : ";
cin>>n; p[i].setName (n); //not cin>>p[i].name;
float total_salaries=0;
cout<<"Names :\n";
for(i=0;i<num_persons;i++)
{
cout<<p[i].getName()<<endl;
total_salaries=total_salaries+p[i].getSalary ();
// or total_salaries+=p[i].getSalary ();
}
cout<<"Total salaries = "<<total_salaries;
}
Problem 5
#include <iostream.h>
class Poly
{
private:
public:
// Initialization must be placed in the 'Constructor', it can't be placed above
// The constructor is called when we create an object of the class, it must have the same name
// as the class
Poly();
4
bool Attach(float coeff1,int expon); //attaches an element to the polynomial
void Display(); // displays the new polynomial
};
void Poly::Display()
{
cout<<endl<<"p(x) = ";
if (IsZero())cout<<"0";
5
else
{
for(int i=degree;i>=0;i--)
if(coeff[i]!=0)cout<<coeff[i]<<"x^"<<i<<"+";
cout<<"\b "; // go back one place and then write space to delete the last '+'
}
}
#include "poly.h"
main()
{
bool flag;
Poly p; //p is defined, memory is allocated for its member variables and the constructor is
//called to initialize them
flag=p.Attach(5,4); // attach (5x^4), then you can check 'a'
cout<<endl<<p.Lead_Exp()<<endl;
cout<<p.Coef(7)<<endl;
cout<<p.IsZero ()<<endl;
p.Attach(4,7);
Poly p1=p; // you can assign an object to another, then each member variable
// (or array) of p will be copied to the corresponding one in p1
p.Zero();
p1.Display();
p.Display();
}
Problem 6
class InVector
{
private:
int vector[100];
public:
void put(int I,int j);
int at(int I);
};
6
int InVector::at(int I)
{
if(I<0 || I>99)
{
cout<<"Out of range. \n";
return 0 ;
}
return vector[I];
}
#include<iostream.h>
#include "Invector.h"
main()
{
InVector i;
i.put(4,20);
cout<<i.at(4)<<endl;
cout<<i.at(134) <<endl;
i.put(144,3);
}