初心者はどのようにC++で1つの時間のクラスを書きます
17404 ワード
初心者はどのようにC++で1つの時間のクラスを書きます
プログラミング環境はVS 2019プロセスは簡単です.メンバー関数を定義すれば次のようになります.
プログラミング環境はVS 2019プロセスは簡単です.メンバー関数を定義すれば次のようになります.
#include
using namespace std;
class Time //
{
private :
int hour, minute, second;
public:
Time(int a=0 ,int b=0,int c=0);
void settime();
void show();
void add();
void reduce();
};
Time::Time(int a,int b,int c)
{
if (a < 0 || a>24 || b < 0 || b>59 || c < 0 || c>59)
{
cout << "wrong time" << endl;
}
else
{
hour = a; minute = b;second = c;
}
}
void Time::settime()
{
int a, b, c;
cout << " / / " << endl;
cin >> a >> b >> c;
if (a < 0 || a>24 || b < 0 || b>59 || c < 0 || c>59)
{
cout << "wrong time" << endl;
}
else
{
hour = a; minute = b;second = c;
cout << " " << endl;
}
}
void Time::show()
{
cout << "Hour--- " << hour << " Minute--- " << minute << " Second--- " << second << endl;
cout << " " << endl;
}
void Time::add()
{
int n,x=0;
cout << " :"<<endl;
cin >> n;
x = 3600 * hour + 60 * minute + second+n;
hour = x / 3600;
minute = (x % 3600) / 60;
second = x % 60;
cout << "done" << endl;
cout << " " << endl;
}
void Time::reduce()
{
int n, x=0;
cout << " :" << endl;
cin >> n;
x = 3600 * hour + 60 * minute + second - n; // ,
hour = x / 3600;
minute = (x % 3600) / 60;
second = x % 60;
cout << "done" << endl;
cout << " " << endl;
}
int main() //
{
Time time;
cout << " :" << endl;
cout << "------- -------1" << endl;
cout << "------- -------2" << endl;
cout << "------- -------3" << endl;
cout << "------- -------4" << endl;
cout << "------- -------0" << endl;
int m; // do while
do {
cin >> m;
switch (m)
{
case 1:
{time.settime();
break;}
case 2:
{time.add();
break;}
case 3:
{time.reduce();
break;}
case 4:
{time.show();
break;}
case 0:
{
cout << " " << endl;
return 0;}
default:
{cout << "wrong num" << endl;}
}
} while (m);
return 0;
}