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

Assignment_04 #101

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
22 changes: 22 additions & 0 deletions 201816040118/Ex09_11/Eectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
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
13 changes: 13 additions & 0 deletions 201816040118/Ex09_11/Ex09_11.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Rectangle.h"
using namespace std;
int main()
{
Rectangle t;//定义一个对象
cout<<"该长方形的长和宽为: "<<endl;
cout<<t.getLength()<<" "<<t.getWidth()<<endl;//输出它的长度和宽度
cout<<"该长方形的周长和面积为: "<<endl;
cout<<t.getPerimeter()<<" "<<t.getArea();//输出它的周长和面积
}
46 changes: 46 additions & 0 deletions 201816040118/Ex09_11/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
#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;
}
13 changes: 13 additions & 0 deletions 201816040118/Ex09_12/Ex09_12.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include "Rectangle.h"
using namespace std;
int main()
{
Rectangle t(1,3,1,3,1,1,3,3);//定义一个对象并初始化
cout<<"("<<t.Printf1()<<","<<t.Printf5()<<")"<<endl;//打印它的第一个定点
cout<<"("<<t.Printf2()<<","<<t.Printf6()<<")"<<endl;//打印它的第二个定点
cout<<"("<<t.Printf3()<<","<<t.Printf7()<<")"<<endl;//打印它的第三个定点
cout<<"("<<t.Printf4()<<","<<t.Printf8()<<")"<<endl;//打印它的第四个定点
}
95 changes: 95 additions & 0 deletions 201816040118/Ex09_12/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
#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<<"该长方形是正方形"<<endl;
}
else
cout<<"该长方形不是正方形"<<endl;
}
void Rectangle::Check(double X1,double X2,double X3,double X4,double Y1,double Y2,double Y3,double Y4)//判断函数
{
if(X1>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;
}
37 changes: 37 additions & 0 deletions 201816040118/Ex09_12/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#ifndef RECTANGLE_H
#define RECTANGLE_H
#include <iostream>
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
19 changes: 19 additions & 0 deletions 201816040118/Ex09_22/Ex09_22.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include <iostream>
#include <stdexcept>
#include <string>
#include "Time.h"
using namespace std;
int main()
{
Time t1;//定义一个对象
cout<<"local time:"<<endl;
t1.printStandard();//输出12小时时间格式
cout<<endl;
t1.printUniversal();//输出24小时时间格式和总秒数
cout<<endl;
cout<<"after tick:"<<endl;
t1.tick();//测试tick函数来使秒数加1
t1.printStandard();//输出12小时时间格式
cout<<endl;
t1.printUniversal();//输出24小时时间格式和总秒数
}
82 changes: 82 additions & 0 deletions 201816040118/Ex09_22/Time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#include <iostream>
#include <iomanip>
#include <stdexcept>
#include <time.h>
#include "Time.h"
using namespace std;
Time::Time() //用当前时间初始化对象
{
const time_t currentTime =time(0);
const tm *localTime=localtime(&currentTime);
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<<setfill('0')<<setw(2)<<getHour()<<":"<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond()<<endl;
cout<<"午夜以来的总秒数为: "<<endl<<tail;

}
void Time::printStandard()const//输出为12小时的时间格式
{
cout<<((getHour()==0||getHour()==12)?12:getHour()%12)<<":"<<setfill('0')<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond()<<(hour<12 ?"AM":"PM");
}
23 changes: 23 additions & 0 deletions 201816040118/Ex09_22/Time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TIME_H
#define TIME_H
#include <iostream>
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
21 changes: 21 additions & 0 deletions 201816040118/Ex09_23/Card.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <iostream>
#include <string>
#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;
}
17 changes: 17 additions & 0 deletions 201816040118/Ex09_23/Card.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef CARD_H
#define CARD_H
#include <string>
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
Loading