/*
*程序的版权和版本声明部分:
*Copyright(c)2013,烟台大学计算机学院学生
*All rights reserved.
*文件名称:
*作者:尚振伟
*完成日期:2014年3月15日
*版本号:v0.1
*对任务及求解方法的描述部分:
*输入描述:无
*问题描述:
*程序输入:
*程序输出:
*问题分析:
*算法设计:
*我的程序:
*/
#include <iostream>
using namespace std;
class Time
{
public:
void set_time();
void show_time();
void add_a_sec()
{
int h,m,s;
h=hour;
m=minute;
s=second;
s+=1;
if(s==60)
{
s=0;
m+=1;
}
if(m==60)
{
m=0;
h+=1;
}
if(h==24)
{
h=0;
}
cout<<h<<":"<<m<<":"<<s;
}
void add_a_min()
{
int h,m,s;
h=hour;
m=minute;
s=second;
m+=1;
if(m==60)
{
m=0;
h+=1;
}
if(h==24)
{
h=0;
}
cout<<h<<":"<<m<<":"<<s;
}
void add_a_hour()
{
int h,m,s;
h=hour;
m=minute;
s=second;
h+=1;
if(h==24)
{
h=0;
}
cout<<h<<":"<<m<<":"<<s;
}
private:
bool is_time(int,int,int);
int hour;
int minute;
int second;
};
void Time::set_time()
{
char c1,c2;
cout<<"请输入时间(格式为时:分:秒):"<<endl;
while(1)
{
cin>>hour>>c1>>minute>>c2>>second;
if(c1!=':'||c2!=':')
cout<<"格式不正确,请重新输入:"<<endl;
else if(!is_time(hour,minute,second))
cout<<"时间错误,请重新输入:"<<endl;
else
break;
}
}
bool Time::is_time(int h,int m,int s)
{
if(h<0||h>24||m<0||m>60||s<0||s>60)
return false;
else
return true;
}
void Time::show_time()
{
cout<<"输入的正确时间为:"<<endl;
cout<<hour<<":"<<minute<<":"<<second;
}
int main()
{
int n;
Time t;
t.set_time();
t.show_time();
while(1)
{
cout<<endl;
cout<<"1 增加一秒 2 增加一分钟"<<endl;
cout<<"3 增加一小时 0 退出"<<endl;
cout<<"请输入你的选择:";
cin>>n;
while(n==0)
{
return 0;
}
switch(n)
{
case 1:
cout<<"增加后时间为:"<<endl;
t.add_a_sec();
break;
case 2:
cout<<"增加后时间为:"<<endl;
t.add_a_min();
break;
case 3:
cout<<"增加后时间为:"<<endl;
t.add_a_hour();
break;
case 0:
cout<<"欢迎使用:"<<endl;
break;
default:
cout<<"输入有误:"<<endl;
break;
}
}
return 0;
}
结果展示:
心得体会:昨天的那篇删了,今天重新发。