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

Ex09_11,Ex09_22 #88

Open
wants to merge 6 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
85 changes: 85 additions & 0 deletions 201816040221/Ex09_11/Rectangle.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@


//#include <Rectangle.h>
#include <iostream>

#include "Rectangle.h"



using namespace std;
Rectangle::Rectangle(float l,float w)

{

setLength(l);

setWidth(w);

}

void Rectangle::setLength(float l)

{

if(length>0&&length<20){

length=l;

}

else

{

length=1;

}

}

void Rectangle::setWidth(float w)

{

if(width>0&&width<20)

{

width=w;

}

else

{

width=1;

}

}

float Rectangle::getLength()

{

return length;

}

float Rectangle::getWidth()

{

return width;

}
float getperimeter()
{
return perimeter=(getlength()+getwidth())*2;
}
float getarea()
{
return aeea=getlength()*getwidth();
}
23 changes: 23 additions & 0 deletions 201816040221/Ex09_11/Rectangle.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

#include <iostream>
#ifndef Rectangle_H
#define Rectangle_H


#include "Rectangle.h"

using namespace std;

class Rectangle

{
public:
Rectangle(float ,float );
void setlength(float);
void setwidth(float );
float getlength();
float getwidth();
float getperimeter();
float getarea();

}//#endif
23 changes: 23 additions & 0 deletions 201816040221/Ex09_11/Rectangletest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include <iostream>


#include "Rectangle.h"

using namespace std;



int main()
{
Rectangle rectangle1(3.3,9.5);

Rectangle rectangle2(20.6,5.7);

cout << "rectangle1: length= "<< rectangle1.getLength()<<" width= "<<rectangle1.getWidth()<<"rectangle1:perimeter="<<rectangle1.getperimeter()<<endl;

cout << "rectangle2: length= "<< rectangle2.getLength()<<" width= "<<rectangle2.getWidth()<<"rectangle2:area"<<rectangle2.getarea()endl;

return 0;
}


111 changes: 111 additions & 0 deletions 201816040221/Ex09_22/Time.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#include <stdexcept>

#include <iomanip>
#include <time.h>
#include <iostream>

#include "Time.h"



using namespace std;



Time::Time(int h,int m,int s)

{

setTime(h ,m , s);

}

void Time::setTime(int h, int m, int s)

{

second=0;

setHour(h);

setMinute(m);

setSecond(s);

}

void Time::setHour(int h)

{
int hours;
if(h>=0&&h<24)

hours=h;
else
throw invalid_argument("hour must be 0-23")
second+=h*3600;


}

int Time::getHour()

{

return *this;

}

void Time::setMinute(int m)

{
if(m>=0&&m<59)
second+=m*60;

}

int Time::getMinute()

{

return second%360/60;

}

void Time::setSecond(int s)

{

second+=s;

}

int Time::getSecond()

{

return second;

}

void Time:: printUniversal()

{

cout<<setfill('0')<<setw(2)<<getHour()<<":"

<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond();

}

void Time:: printStandard()

{

cout<<setfill('0')<<setw(2)<<((getHour()==0||getHour()==12)?12 :getHour()%12)<<":"

<<setw(2)<<getMinute()<<":"<<setw(2)<<getSecond()

<<(getHour()<12?"AM":"PM");

}
50 changes: 50 additions & 0 deletions 201816040221/Ex09_22/Time.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>


#include <time.h>

using namespace std;

class Time

{

public:

explicit Time(int =0,int=0 ,int=0 );

void setTime(int, int, int);



void setHour(int);

void setMinute(int );

void setSecond(int );



int getHour();

int getMinute( );

int getSecond( );



void printUniversal() ;

void printStandard() ;



private:

int hour;
int minute;
int second;



};
50 changes: 50 additions & 0 deletions 201816040221/Ex09_22/Timetest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <iostream>


#include <time.h>

using namespace std;



int main()

{

Time t1;



time_t currenttime=time(0);

struct tm *localtime=localtime(&currenttime);


time( &rawtime );





cout<<("当前的本地时间和日期:%s", asctime(info));

t1.setHour(localtime->tm_hour);

t1.setMinute(localtime->tm_min);

t1.setSecond(localtime->tm_sec);





cout<<"Constructed with :\n\nt1:\n";

t1.printUniversal();

cout<<endl;

t1.printStandard();
return 0;

}