Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Assignment_04 #105

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
12 changes: 12 additions & 0 deletions 201816040114/Ex09_11/Ex09_11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include "Rectangle.h"


using namespace std;

int main()
{
Rectangle rectangle(10,5);
rectangle.Range();
cout<<rectangle.Perimeter()<<" "<<rectangle.Area()<<endl;
}
46 changes: 46 additions & 0 deletions 201816040114/Ex09_11/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Rectangle.h"
#include<iostream>
#include <stdlib.h>
using namespace std;

Rectangle::Rectangle(double a,double b)
{
setLength(a);
setWidth(b);
}

void Rectangle::setLength(double a)
{
length=a;
}

void Rectangle::setWidth(double a)
{
width=a;
}

double Rectangle::Perimeter()
{
perimeter=(length+width)*2;
return perimeter;
}

double Rectangle::Area()
{
area=length*width;
return area;
}

void Rectangle::Range()
{
if(length<=0.0||length>=20.0)
{
cout<<"The length over 20 or less than 0"<<endl;
exit(0);
}
if(width<=0.0||width>=20.0)
{
cout<<"The width over 20 or less than 0"<<endl;
exit(0);
}
}
22 changes: 22 additions & 0 deletions 201816040114/Ex09_11/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED

class Rectangle
{
public:
explicit Rectangle(double=1.0,double=1.0);
void setLength(double);
void setWidth(double);
double Perimeter();
double Area();
void Range();
private:
double length;
double width;
double perimeter;
double area;
};



#endif // RECTANGLE_H_INCLUDED
13 changes: 13 additions & 0 deletions 201816040114/Ex09_12/Ex09_12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include "Point.h"
#include "Rectangle.h"

using namespace std;

int main()
{
Point a(2,4),b(2,1),c(6,1),d(6,4);
Rectangle rectangle(a,b,c,d);
rectangle.square();
cout<<rectangle.getPerimeter()<<" "<<rectangle.getArea()<<endl;
}
43 changes: 43 additions & 0 deletions 201816040114/Ex09_12/Point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Point.h"
#include<iostream>
using namespace std;

Point::Point(double a,double b)
{
setX(a);
setY(b);
}

void Point::setX(double a)
{
if(a>0&&a<20)
{
x=a;
}
else
{
throw invalid_argument("x must be 0-20");
}
}

void Point::setY(double a)
{
if(a>0&&a<20)
{
y=a;
}
else
{
throw invalid_argument("y must be 0-20");
}
}

double Point::getX()
{
return x;
}

double Point::getY()
{
return y;
}
20 changes: 20 additions & 0 deletions 201816040114/Ex09_12/Point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
using namespace std;

class Point
{
public:
Point(double,double);
void setX(double);
void setY(double);
double getX();
double getY();
private:
double x;
double y;
};



#endif // POINT_H_INCLUDED
38 changes: 38 additions & 0 deletions 201816040114/Ex09_12/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using namespace std;
#include "Rectangle.h"
#include "Point.h"
#include<iostream>

Rectangle::Rectangle(Point &a,Point &b,Point &c,Point &d)
:A(a),B(b),C(c),D(d)
{

}

void Rectangle::square()
{
if(A.getY()-B.getY()>C.getX()-B.getX())
{
length=A.getY()-B.getY();
width=C.getX()-B.getX();
}
if(A.getY()-B.getY()<C.getX()-B.getX())
{
width=A.getY()-B.getY();
length=C.getX()-B.getX();
}
if(A.getY()-B.getY()==C.getX()-B.getX())
{
throw invalid_argument("It is a square");
}
}
double Rectangle::getPerimeter()
{
return (length+width)*2;
}

double Rectangle::getArea()
{
return length*width;
}

22 changes: 22 additions & 0 deletions 201816040114/Ex09_12/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED
using namespace std;
#include "Point.h"
class Rectangle
{
public:
Rectangle(Point &,Point &,Point &,Point &);
void square();
double getPerimeter();
double getArea();
private:
Point A,B,C,D;
double length;
double width;
double perimeter;
double area;
};



#endif // RECTANGLE_H_INCLUDED
12 changes: 12 additions & 0 deletions 201816040114/Ex09_22/Ex09_22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <iostream>
#include "Time.h"

using namespace std;

int main()
{
Time t1;
t1.printUniversal();
cout<<endl;
t1.printStandard();
}
43 changes: 43 additions & 0 deletions 201816040114/Ex09_22/Time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include "Time.h"
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
Time::Time(int hour,int minute,int second)
{
setTime(hour,minute,second);
}

void Time::setTime(int h,int m,int s)
{
if(h>=0&&h<24&&m>=0&&m<60&&s>=0&&s<60)
second=h*60*60+m*60+s;
else
throw invalid_argument("hour,minute or second was out of range");
}

unsigned Time::getHour() const
{
return (second-second%3600)/3600;
}

unsigned Time::getMinute() const
{
return (second-second%3600)/60;
}

unsigned Time::getSecond() const
{
return second%3600;
}

void Time::printUniversal() const
{
cout<<setfill('0')<<setw(2)<<getHour()<<":"<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond();
}
void Time::printStandard() const
{
cout<<((getHour()==0||getHour()==12)?12:getHour()%12)
<<":"<<setfill('0')<<setw(2)<<getMinute()
<<":"<<setw(2)<<getSecond()<<((second-second%3600)/3600<12?"AM":"PM");
}
20 changes: 20 additions & 0 deletions 201816040114/Ex09_22/Time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef TIME_H_INCLUDED
#define TIME_H_INCLUDED
using namespace std;
class Time
{
public:
explicit Time(int=0,int=0,int=0);
void setTime(int,int,int);
unsigned int getHour()const;
unsigned int getMinute()const;
unsigned int getSecond()const;
void printUniversal()const;
void printStandard()const;
private:
unsigned int second;
};



#endif // TIME_H_INCLUDED
12 changes: 12 additions & 0 deletions 201816040114/Ex09_23/Card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "Card.h"
Card::Card(int a,int b)
{
face=a;
suit=b;
}
string Card::toString()
{
string card=" of ";
card=Face[face]+card+Suit[suit];
return card;
}
20 changes: 20 additions & 0 deletions 201816040114/Ex09_23/Card.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#ifndef CARD_H_INCLUDED
#define CARD_H_INCLUDED
using namespace std;
#include <string>
class Card
{
public:
explicit Card(int,int);
void setCard(int,int);
string toString();
private:
int face;
int suit;
string Face[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
string Suit[4] = {"Spade","Heart","Diamond","Club"};
};



#endif // CARD_H_INCLUDED
50 changes: 50 additions & 0 deletions 201816040114/Ex09_23/DeckofCards.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include "Card.h"
#include "DeckOfCards.h"
#include <bits/stdc++.h>
#define random(x) (rand()%x)

using namespace std;

DeckofCards::DeckofCards()
{
int i,j;
for (i=0;i<4;i++)
{
for(j=0;j<13;j++)
{
Card a(j,i);
deck.push_back(a);
}
}
}
bool DeckofCards::moreCards()
{
if(currentCard>=52)
{
return false;
}


return true;

}

void DeckofCards::shuffle()
{
int i,a;
Card temp(0,0);
srand((int)time(0));
for(i=0;i<52;i++)
{
a=random(52);
Card temp=deck[i];
deck[i]=deck[a];
deck[a]=temp;
}
}

Card DeckofCards::dealCard()
{
currentCard=currentCard+1;
return deck[currentCard-1];
}
Loading