C++3回目の作業

5504 ワード

プロジェクト1-静的メンバー適用
#include <iostream>
using namespace std;
class Time
{
public:
	Time(int=0,int=0,int=0);
	void show_time();
	void add_seconds(int);
	void add_minutes(int);
	void add_hours(int);
	void print();
	static void change24();
	static void changefrom0();
private:
	static bool is_24;
	static bool from0;
	int hour;
	int minute;
	int sec;
};
bool Time::is_24=0;
bool Time::from0=0;
Time::Time(int h,int m,int s)
{
	sec=s;
	minute=m;
	hour=h;
}
void Time::add_seconds(int s)
{
	sec+=s;
	if(sec>59)
	{
		add_minutes(sec/60);
		sec%=60;
	}
}
void Time::add_minutes(int m)
{
	minute+=m;
	if(minute>59)
	{
		add_hours(minute/60);
		minute%=60;
	}
}
void Time::add_hours(int h)
{
	hour+=h;
	if(hour>23)
	{
		hour%=24;
	}
}
void Time::print()
{
	int th=hour;
	if(!is_24&&th>12)
		th-=12;
	if(from0&&th<10)
		cout << '0';
	cout << th << ':';
	if(from0&&minute<10)
		cout << '0';
	cout << minute << ':';
	if(from0&&sec<10)
		cout << '0';
	cout << sec << "  ";
	if(!is_24)
	{
		if(hour<12)
			cout << "am";
		else
			cout << "pm";
	}
	cout << endl;
}
void Time::change24()
{
	is_24=!is_24;
}
void Time::changefrom0()
{
	from0=!from0;
}

int main()
{
	int h,m,s,a;
	cout << "plz enter hour minute second" << endl;
	cin >> h >> m >> s;
	Time t1(h,m,s);
	cout << "plz enter add seconds" << endl;
	cin >> a;
	t1.add_seconds(a);
	t1.print();
	t1.changefrom0();
	t1.print();
	t1.change24();
	t1.print();
	cout << "plz enter add minutes" << endl;
	cin >> a;
	t1.add_minutes(a);
	t1.print();
	t1.changefrom0();
	t1.print();
	t1.change24();
	t1.print();
	cout << "plz enter add hours" << endl;
	cin >> a;
	t1.add_hours(a);
	t1.print();
	t1.changefrom0();
	t1.print();
	t1.change24();
	t1.print();
	return 0;
}

項目2:友元関数
#include <iostream>
#include <cmath>
using namespace std;
void distance3(double a,double b,double c,double d);
class CPoint
{
private:
	double x;
	double y;
public:
	CPoint(double xx=0,double yy=0):x(xx),y(yy){};
	void distance1(CPoint t1,CPoint t2);
	friend void distance2(CPoint t1,CPoint t2);
};

void CPoint::distance1(CPoint t1,CPoint t2)
{
	double dx=t1.x-t2.x;
	double dy=t1.y-t2.y;
	cout << sqrt(dx*dx+dy*dy) << endl;
}
void distance2(CPoint t1,CPoint t2)
{
	double dx=t1.x-t2.x;
	double dy=t1.y-t2.y;
	cout << sqrt(dx*dx+dy*dy) << endl;
}

int main()
{
	double a,b,c,d;
	cout << "plz enter A(x1,y1),B(x2,y2)" << endl;
	cin >> a >> b >> c >> d;
	CPoint t1(a,b),t2(c,d);
	t1.distance1(t1,t2);
	t2.distance1(t1,t2);
	distance2(t1,t2);
	distance3(a,b,c,d);
	return 0;
}

void distance3(double a,double b,double c,double d)
{
	double dx=a-c;
	double dy=b-d;
	cout << sqrt(dx*dx+dy*dy) << endl;
}

三、プロジェクト3:友元類
#include <iostream>
using namespace std;
const int M[12]={31,28,31,30,31,30,31,31,30,31,30,31};
class Date;
class Time
{
public:
    Time(int,int,int);
    void add_a_second(Date &d1);
    void display(Date d1);
private:
    int hour;
    int minute;
    int sec;
};


class Date
{
public:
    Date(int,int,int);
    friend class Time; //Time Date    
private:
    int month;
    int day;
    int year;
};


Time::Time(int h,int m,int s)
{
    hour=h;
    minute=m;
    sec=s;
}
void Time::add_a_second(Date &d1)
{
    sec++;
    if(sec==60)
    {
        sec=0;
        minute++;
    }
    if(minute==60)
    {
        minute=0;
        hour++;
    }
    if(hour==24)
    {
        hour=0;
        d1.day++;
    }
    if(d1.month!=2)
    {
        if(d1.day>M[d1.month-1])
        {
            d1.day=1;
            d1.month++;
        }
    }
    else
    {
        if((d1.year%4==0&&d1.year%100!=0)||d1.year%400==0)
        {
            if(d1.day>29)
            {
                d1.day=1;
                d1.month++;
            }
        }
        else
        {
            if(d1.day>28)
            {
                d1.day=1;
                d1.month++;
            }
        }
    }
    if(d1.month==13)
    {
        d1.month=1;
        d1.year++;
    }
}
void Time::display(Date d1)
{
    cout << d1.year << '/' << d1.month << '/' << d1.day << ' ' << hour << ':' <<
        minute << ':' << sec << endl;
}
Date::Date(int ye,int mo,int da)
{
    year=ye;
    month=mo;
    day=da;
}


int main()
{
    Time t1(23,59,32);
    Date d1(2013,12,31);
    for (int i=0; i<=100; i++)
    {
        t1.add_a_second(d1);
        t1.display(d1);
    }
    return 0;
}