diff --git a/201816040118/Ex09_11/Eectangle.h b/201816040118/Ex09_11/Eectangle.h new file mode 100644 index 00000000..79668485 --- /dev/null +++ b/201816040118/Ex09_11/Eectangle.h @@ -0,0 +1,22 @@ +#ifndef RECTANGLE_H +#define RECTANGLE_H +#include +class Rectangle +{ +public: + explicit Rectangle(double=1,double=1);//声明构造函数并初始化为1 + void setPerimeter(double,double);//声明一个计算周长的函数 + double getPerimeter()const;//声明一个返回周长的函数 + void setArea(double,double);//声明一个计算面积的函数 + double getArea()const;//声明一个返回面积的函数 + void setLength(double);//声明一个计算长度的函数 + double getLength()const;//声明一个返回长度的函数 + void setWidth(double);//声明一个计算宽度的函数 + double getWidth()const;//声明一个返回宽度的函数 +private: + double length; + double width; + double perimeter; + double area;//声明4个数据成员 +}; +#endif diff --git a/201816040118/Ex09_11/Ex09_11.cpp b/201816040118/Ex09_11/Ex09_11.cpp new file mode 100644 index 00000000..74baa78e --- /dev/null +++ b/201816040118/Ex09_11/Ex09_11.cpp @@ -0,0 +1,13 @@ +#include +#include +#include +#include "Rectangle.h" +using namespace std; +int main() +{ + Rectangle t;//定义一个对象 + cout<<"该长方形的长和宽为: "< +#include +#include +#include "Rectangle.h" +using namespace std; +Rectangle::Rectangle(double Length,double Width)//构造函数 +{ + setLength(Length); + setWidth(Width); + setPerimeter(Length,Width); + setArea(Length,Width); +} +void Rectangle::setPerimeter(double Length,double Width)//计算周长 +{ + perimeter=(Length+Width)*2; +} +double Rectangle::getPerimeter()const//返回周长 +{ + return perimeter; +} +void Rectangle::setArea(double Length,double Width)//计算面积 +{ + area=Length*Width; +} +double Rectangle::getArea()const//返回面积 +{ + return area; +} +void Rectangle::setLength(double Length)//计算长度并判断是否合法 +{ + if(Length>0&&Length<20) + length=Length; +} +double Rectangle::getLength()const//返回长度 +{ + return length; +} +void Rectangle::setWidth(double Width)//计算宽度并判断是否合法 +{ + if(Width>0&&Width<20) + width=Width; +} +double Rectangle::getWidth()const//返回宽度 +{ + return width; +} diff --git a/201816040118/Ex09_12/Ex09_12.cpp b/201816040118/Ex09_12/Ex09_12.cpp new file mode 100644 index 00000000..e2f69943 --- /dev/null +++ b/201816040118/Ex09_12/Ex09_12.cpp @@ -0,0 +1,13 @@ +#include +#include +#include +#include "Rectangle.h" +using namespace std; +int main() +{ + Rectangle t(1,3,1,3,1,1,3,3);//定义一个对象并初始化 + cout<<"("< +#include +#include +#include "Rectangle.h" +using namespace std; +Rectangle::Rectangle(double X1,double X2,double X3,double X4,double Y1,double Y2,double Y3,double Y4)//传递坐标初始化 +{ + Length(X1,X3); + Width(Y1,Y3); + Check(X1,X2,X3,X4,Y1,Y2,Y3,Y4); +} +void Rectangle::Length(double X1,double X3)//计算坐标长度 +{ + length=X1-X3; +} +void Rectangle::Width(double Y1,double Y3)//计算坐标宽度 +{ + width=Y1-Y3; +} +void Rectangle::Perimeter(double length,double width)//计算坐标周长 +{ + perimeter=(length+width)*2; +} +void Rectangle::Area(double length,double width)//计算坐标面积 +{ + area=length*width; +} +void Rectangle::Square(double length,double width,double X1,double X2,double X3,double Y1,double Y2,double Y3)//判断函数 +{ + if(length==width&&(X2-X1)*(X3-X1)+(Y2-Y1)*(Y3-Y1)==0) + { + cout<<"该长方形是正方形"<0&&X2>0&&X3>0&&X4>0&&Y1>0&&Y2>0&&Y3>0&&Y4>0&&X1<20&&X2<20&&X3<20&&X4<20&&Y1<20&&Y2<20&&Y3<20&&Y4<20&&(X2-X1)*(X3-X1)+(Y2-Y1)*(Y3-Y1)==0) + { + x1=X1; + x2=X2; + x3=X3; + x4=X4; + y1=Y1; + y2=Y2; + y3=Y3; + y4=Y4; + } + else + { + cout<<"坐标不构成长方形!"; + x1=0; + x2=0; + x3=0; + x4=0; + y1=0; + y2=0; + y3=0; + y4=0; + } +} +//返回各个坐标的值 +double Rectangle::Printf1() +{ + return x1; +} +double Rectangle::Printf2() +{ + return x2; +} +double Rectangle::Printf3() +{ + return x3; +} +double Rectangle::Printf4() +{ + return x4; +} +double Rectangle::Printf5() +{ + return y1; +} +double Rectangle::Printf6() +{ + return y2; +} +double Rectangle::Printf7() +{ + return y3; +} +double Rectangle::Printf8() +{ + return y4; +} diff --git a/201816040118/Ex09_12/Rectangle.h b/201816040118/Ex09_12/Rectangle.h new file mode 100644 index 00000000..353b4e3b --- /dev/null +++ b/201816040118/Ex09_12/Rectangle.h @@ -0,0 +1,37 @@ +#ifndef RECTANGLE_H +#define RECTANGLE_H +#include +class Rectangle +{ +public: + Rectangle(double,double,double,double,double,double,double,double); //构造函数的声明 + void Length(double,double);//声明计算长度的函数 + void Width(double,double);//声明计算宽度的函数 + void Perimeter(double,double);//声明计算周长的函数 + void Area(double,double);//声明计算面积的函数 + void Check(double,double,double,double,double,double,double,double);//检测坐标是否构成长方形 + void Square(double,double,double,double,double,double,double,double);//检测坐标是否构成正方形 + double Printf1();//获取x1的坐标值 + double Printf2();//获取x2的坐标值 + double Printf3();//获取x3的坐标值 + double Printf4();//获取x4的坐标值 + double Printf5();//获取y1的坐标值 + double Printf6();//获取y2的坐标值 + double Printf7();//获取y3的坐标值 + double Printf8();//获取y4的坐标值 + +private: + double x1; + double x2; + double x3; + double x4; + double y1; + double y2; + double y3; + double y4;//声明坐标数据成员 + double length; + double width; + double perimeter; + double area;//声明数据成员 +}; +#endif diff --git a/201816040118/Ex09_22/Ex09_22.cpp b/201816040118/Ex09_22/Ex09_22.cpp new file mode 100644 index 00000000..5fa489bb --- /dev/null +++ b/201816040118/Ex09_22/Ex09_22.cpp @@ -0,0 +1,19 @@ +#include +#include +#include +#include "Time.h" +using namespace std; +int main() +{ + Time t1;//定义一个对象 + cout<<"local time:"< +#include +#include +#include +#include "Time.h" +using namespace std; +Time::Time() //用当前时间初始化对象 +{ + const time_t currentTime =time(0); + const tm *localTime=localtime(¤tTime); + setTime(localTime->tm_hour,localTime->tm_min,localTime->tm_sec); //validate and set time +} +void Time::tick() //使时间增加1秒 +{ + if((getSecond()+1)==60) + { + setSecond(0); + minute+=1; + if(getMinute()==60) + { + setMinute(0); + hour+=1; + } + } + else + second+=1; +} +void Time::setTime(int h,int m,int s) //传递参数用于初始化时间 +{ + setHour(h); //set private filed hour + setMinute(m); //set private filed minute + setSecond(s); //set private filed second +} +void Time::setHour(int h)//传递参数用于初始化小时并判断是否合法 +{ + if(h>0&&h<24) + { + hour=h; + } + else + throw invalid_argument("hour must be 0-23"); +} +void Time::setMinute(int m)//传递参数用于初始化分钟并判断是否合法 +{ + if(m>=0&&m<60) + { + minute=m; + } + else + throw invalid_argument("minute must be 0-59"); +} +void Time::setSecond(int s)//传递参数用于初始化秒并判断是否合法 +{ + if(s>=0&&s<60) + second=s; + else + throw invalid_argument("second must be 0-59"); +} +unsigned int Time::getHour()const//获取小时 +{ + return hour; +} +unsigned Time::getMinute()const//获取分钟 +{ + return minute; +} +unsigned Time::getSecond()const//获取秒 +{ + return second; +} +void Time::printUniversal()const//输出为24小时的时间格式并换算为午夜以来的总秒数 +{ + int tail; + tail=second+minute*60+hour*60*60; + cout< +class Time +{ +public: + Time();//构造函数 + void setTime(int,int,int); + void setHour(int); + void setMinute(int); + void setSecond(int); //传递参数用来初始化时间 + unsigned int getHour()const; + unsigned int getMinute()const; + unsigned int getSecond()const; //获取时间 + void printUniversal()const; //打印12小时格式时间 + void printStandard()const;//打印24小时格式时间 + void tick(); //测试tick函数,使时间增加1秒 +private: + unsigned int hour; + unsigned int minute; + unsigned int second; //定义数据成员 +}; +#endif // TIME_H diff --git a/201816040118/Ex09_23/Card.cpp b/201816040118/Ex09_23/Card.cpp new file mode 100644 index 00000000..334caf67 --- /dev/null +++ b/201816040118/Ex09_23/Card.cpp @@ -0,0 +1,21 @@ +#include +#include +#include "Card.h" +using namespace std; +string Card::Face[13]={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};//初始化它的面值 +string Card::Suit[4] = {"Spade","Heart","Diamond","Club"};//初始化它的花色 +Card::Card(int f,int s)//声明构造函数 +{ + setCard(f,s); +} +void Card::setCard(int f,int s)//接收面值和花色来初始化数据成员 +{ + face=f; + suit=s; +} +string Card::toString()//输出 +{ + string z,p=" of "; + z=Face[face]+p+Suit[suit];//返回正确格式 + return z; +} diff --git a/201816040118/Ex09_23/Card.h b/201816040118/Ex09_23/Card.h new file mode 100644 index 00000000..aa5c977b --- /dev/null +++ b/201816040118/Ex09_23/Card.h @@ -0,0 +1,17 @@ +#ifndef CARD_H +#define CARD_H +#include +using namespace std; +class Card +{ +public: + explicit Card( int,int );//声明构造函数 + string toString ();//声明一个输出函数 + void setCard(int,int);//声明一个传参赋值函数 +private: + int face; + int suit; + static string Face[13]; + static string Suit[4];//声明数据成员 +}; +#endif // CARD_H_INCLUDED diff --git a/201816040118/Ex09_23/DeckOfCards.cpp b/201816040118/Ex09_23/DeckOfCards.cpp new file mode 100644 index 00000000..62eef8a8 --- /dev/null +++ b/201816040118/Ex09_23/DeckOfCards.cpp @@ -0,0 +1,55 @@ +#include "Card.h" +#include +#include +#include +#include "DeckOfCards.h" +#include +#include +#include +#include +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); + } + } +} +void DeckOfCards::shuffle()//洗盘 +{ + int i,z; + srand((unsigned int)time (NULL)); + for (i=0 ;i<52 ;i++) //随机选取两只牌并进行交换 + { + z=rand()%51; + Card f=deck[i]; + deck[i]=deck[z]; + deck[z]=f; + } +} +Card DeckOfCards::dealCard()//获取面值 +{ + currentCard++; + return deck[currentCard-1]; +} +bool DeckOfCards::moreCards()//判断下一个牌是否要处理 +{ + if(currentCard>51) + { + return false; + } + else if(currentCard<=51) + { + return true; + } +} + +Card DeckOfCards::getCards(int i) +{ + return deck[i]; +} diff --git a/201816040118/Ex09_23/DeckOfCards.h b/201816040118/Ex09_23/DeckOfCards.h new file mode 100644 index 00000000..8ca73f8b --- /dev/null +++ b/201816040118/Ex09_23/DeckOfCards.h @@ -0,0 +1,17 @@ +#ifndef DECKOFCARDS_H +#define DECKOFCARDS_H +#include "Card.h" +#include +class DeckOfCards +{ +public: + DeckOfCards();//构造函数 + void shuffle();//洗牌 + Card dealCard();//下一张牌 + bool moreCards();//判断是否要处理 + Card getCards(int ); +private: + vector deck; + int currentCard=0; //声明数据成员 +}; +#endif // DECKOFCARDS_H_INCLUDED diff --git a/201816040118/Ex09_23/Ex09_23.cpp b/201816040118/Ex09_23/Ex09_23.cpp new file mode 100644 index 00000000..2ac1d06a --- /dev/null +++ b/201816040118/Ex09_23/Ex09_23.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include "DeckOfCards.h" +#include "Card.h" +#include +using namespace std; +int main() +{ + int i; + DeckOfCards p; //定义一个DeckOfCards 对象 + p.shuffle();//洗牌 + for (i=0;i<52;i++) //输出 + { + cout <