Sunteți pe pagina 1din 7

Solutions of Sheet 1

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()
{

Time start,stop; //define two objects of the class Time


float dist;
bool errorFlag;
// Read start time:
cout << "Enter start time:";
start.readTime(errorFlag);
// The variable 'errorFlag' is passed to the function
// by reference('&' in the function prototype),
// so that the function writes in it true (if there is an error)
// or false (if there is no error in the input Time)

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'

cout<<"The speed ="<<dist*60/diff<<" miles/hour";


}

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;

cout<<"Enter the age of the person number "<<i+1<<" : ";


cin>>age; p[i].setAge (age); //not cin>>p[i].age;

cout<<"Enter the salary of the person number "<<i+1<<" : ";


cin>>salary;p[i].setSalary (salary);
}

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:

int degree; // maximum exponent (maximum power) in the polynomial


float coeff[100]; // the index (0-99) is the exponent, the number in the array is the coefficient

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();

void Zero(); // sets the polynomial to zero

bool IsZero(); // returns true if the polynomial is zero

float Coef(int expon) ; // returns the coefficient of expon

int Lead_Exp() ; // returns the degree

4
bool Attach(float coeff1,int expon); //attaches an element to the polynomial
void Display(); // displays the new polynomial

};

Poly::Poly() // start with a zero polynomial, when an object is created, it is p(x)= 0


{
for(int i=0;i<100;i++)coeff[i]=0;
degree=0;
}

void Poly::Zero() // sets the polynomial to zero


{
for(int i=0;i<100;i++)coeff[i]=0;
degree=0;
}

bool Poly::IsZero() // returns true if the polynomial is zero


{
if(degree == 0 && coeff[0]==0)return true;
//if the degree is zero, there may be a constant term, so we check that coeff[0]==0
else // optional
return false;
}

float Poly::Coef(int expon) // returns the coefficient of expon


{
if(expon>99 || expon <0)return 0; //check if expon is in the range of the array
return coeff[expon];
}

int Poly::Lead_Exp() // returns the degree


{
return degree;
}

bool Poly::Attach(float coeff1,int expon)


{
if(expon>99 || expon<0)return true;
if(coeff[expon]!=0)return true; // it is required in the problem that if there is already a term with
// the given exponent, an error is returned
coeff[expon]=coeff1;
if(expon>degree)degree=expon; //update degree if the power of the new term is >degree
return false;
}

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);
};

void InVector::put(int I,int j)


{
if(I<0 || I>99)
{
cout<<"Out of range."<<endl;
return; //the function is void, so return terminates the function without returning anything
}
vector[I]=j;
}

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);
}

S-ar putea să vă placă și