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

Assigment_04 #102

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 201816040112/Ex09_11/Ex09_11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include "Rectangle.h"
using namespace std;

int main()
{
Rectangle myRectangle(10,15);
cout<<"长方形的周长为:"<<myRectangle.perimeter()<<"\n长方形的面积为:"<<myRectangle.area();
return 0;
}
40 changes: 40 additions & 0 deletions 201816040112/Ex09_11/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "Rectangle.h"
#include <iostream>

Rectangle::Rectangle(double x,double y)
{
setlength(x);
setwidth(y);
}
void Rectangle::setlength(double le)
{
if(le>0&&le<20)
length=le;
else
length=0;
}
double Rectangle::getlength()
{
return length;
}
void Rectangle::setwidth(double wi)
{
if(wi>0&&wi<20)
width=wi;
else
width=0;
}
double Rectangle::getwidth()
{
return width;
}
double Rectangle::perimeter()
{

return 2*(getlength()+getwidth());
}
double Rectangle::area()
{

return getlength()*getwidth();
}
21 changes: 21 additions & 0 deletions 201816040112/Ex09_11/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef RECTANGLE_H_INCLUDED
#define RECTANGLE_H_INCLUDED

class Rectangle{

public:
explicit Rectangle(double=1.0,double=1.0);
void setlength(double);
double getlength();
void setwidth(double);
double getwidth();
double perimeter();
double area();
private:
double width;
double length;

};


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

int main()
{
Point point1(2,3);
Point point2(7,3);
Point point3(2,0);
Rectangle myRectangle(point1,point2,point3);
cout<<"这个四方形的长为:"<<myRectangle.Relength()<<"\n这个四边形的宽度为:"<<myRectangle.Rewidth()<<"\n这个四边形的周长:"<<myRectangle.perimeter()<<"\n这个四边形的面积:"<<myRectangle.arear();
if(myRectangle.square()==true)
cout<<"\n此为正方形";
else
cout<<"\n此为长方形";
return 0;
}
29 changes: 29 additions & 0 deletions 201816040112/Ex09_12/Point.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "Point.h"

Point::Point(double tx,double ty)
{
setxPoint(tx);
setyPoint(ty);
}
void Point::setxPoint(const double tx)
{
if(tx>=0&&tx<=20)
x=tx;
else
x=0;
}
void Point::setyPoint(const double ty)
{
if(ty>=0&&ty<=20)
y=ty;
else
y=0;
}
double Point::getxPoint()
{
return x;
}
double Point::getyPoint()
{
return y;
}
16 changes: 16 additions & 0 deletions 201816040112/Ex09_12/Point.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#ifndef POINT_H_INCLUDED
#define POINT_H_INCLUDED
class Point
{
public:
explicit Point(double=0.0,double=0.0);
void setxPoint (const double);
double getxPoint ();
void setyPoint(const double);
double getyPoint();
private:
double x,y;
};


#endif // POINT_H_INCLUDED
38 changes: 38 additions & 0 deletions 201816040112/Ex09_12/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include "Rectangle.h"
#include "Point.h"
#include <math.h>
Rectangle::Rectangle(Point &x,Point &y,Point &z)
{
point1=x;
point2=y;
point3=z;


}
double Rectangle::Relength()
{


return fabs(point1.getxPoint()-point2.getxPoint());


}
double Rectangle::Rewidth()
{
return fabs(point3.getyPoint()-point1.getyPoint());
}
double Rectangle::arear()
{
return Relength()*Rewidth();
}
double Rectangle::perimeter()
{
return 2*(Relength()+Rewidth());
}
bool Rectangle::square()
{
if(Relength()==Rewidth())
return true;
else
return false;
}
22 changes: 22 additions & 0 deletions 201816040112/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
#include "Point.h"
class Rectangle
{
public:
explicit Rectangle(Point &,Point &,Point &);
double Relength();
double Rewidth();
double perimeter();
double arear();
bool square();
private:
Point point1;
Point point2;
Point point3;


};


#endif // RECTANGLE_H_INCLUDED
10 changes: 10 additions & 0 deletions 201816040112/Ex09_22/Ex09_22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <iostream>
#include "Time.h"
using namespace std;

int main()
{
Time mytime(10,30,49);
mytime.display();
return 0;
}
48 changes: 48 additions & 0 deletions 201816040112/Ex09_22/Time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include "Time.h"
#include <bits/stdc++.h>
using namespace std;
Time::Time(int th,int tm,int ts)
{
sec=3600*th+60*tm+ts; //get the sum of seconds;
}
int Time::gethour()
{
int a=0;
while(sec>3600)
{
sec=sec-3600;
a++;
}

return a;
}
int Time::getminute()
{
int a=0;
while(sec>60)
{
sec=sec-60;
a++;
}

return a;
}
int Time::getsecond()
{
int a=0;
while(sec>0)
{
sec--;
a++;
}

return a;
}
void Time::display()
{
int a,b,c;
a=gethour();
b=getminute();
c=getsecond();
cout<<setfill('0')<<setw(2)<<a<<":"<<setw(2)<<b<<":"<<setw(2)<<c;
}
17 changes: 17 additions & 0 deletions 201816040112/Ex09_22/Time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef TIME_H_INCLUDED
#define TIME_H_INCLUDED
class Time
{
public:
explicit Time(int=0,int=0,int=0);
int gethour();
int getminute();
int getsecond();
void display();
private:
int sec=0;

};


#endif // TIME_H_INCLUDED
14 changes: 14 additions & 0 deletions 201816040112/Ex09_23/Card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include "Card.h"


Card::Card(int a,int b)
{
face=a;
suit=b;
}
string Card::toString()
{
string res="";
res=color[suit]+" "+number[face];
return res;
}
24 changes: 24 additions & 0 deletions 201816040112/Ex09_23/Card.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef CARD_H_INCLUDED
#define CARD_H_INCLUDED

#include <string>
using namespace std;
class Card
{
friend class Doc;
public:
explicit Card(int,int);
string toString();
private:
int face,suit;

string number[13]={"1","2","3","4","5","6","7","8","9","10","11","12","13"};
string color[4]={"Spade","Heart","Diamond","Club"};




};


#endif // CARD_H_INCLUDED
49 changes: 49 additions & 0 deletions 201816040112/Ex09_23/DeckofCards.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "Card.h"
#include "DeckOfCards.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include<iomanip>
Doc::Doc()
{
int i,j;
for(i=0;i<4;i++)
{
for(j=0;j<13;j++){
Card temp(j,i);
deck.push_back(temp);

}


}
}
void Doc::shuffle()
{
vector<int> temp;
int j=0,i;
for(i=0;i<52;i++){
temp.push_back(i);
}
std::random_shuffle(temp.begin(), temp.end());
for(i=0;i<52;i++){
j=i+1;
swap(deck[temp[i]],deck[temp[j]]);
i++;
}


}
void Doc::dealCards()
{

cout<<deck[k].toString()<<endl;
k++;
}
bool Doc::moreCards()
{
if(k>=52)
return false;
else
return true;
}
22 changes: 22 additions & 0 deletions 201816040112/Ex09_23/DeckofCards.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef DECKOFCARDS_H_INCLUDED
#define DECKOFCARDS_H_INCLUDED
#include "Card.h"
#include <vector>
class Doc
{
public:
explicit Doc();
void shuffle();
vector<Card> deck;
bool moreCards();
void dealCards();

private:
int currentCard;
int k=0;


};


#endif // DECKOFCARDS_H_INCLUDED
Loading