C++程序设计题:设计一个钟表类Clock,该类有Hour,Minute,Second三个数据成员分别用 5
C++程序设计题:设计一个钟表类Clock,该类有Hour,Minute,Second三个数据成员分别用来表示小时,分钟,秒种,两个成员函数SetTime,ShowTim...
C++程序设计题:设计一个钟表类Clock,该类有Hour,Minute,Second三个数据成员分别用来表示小时,分钟,秒种,两个成员函数SetTime,ShowTime分别用来设置时间和显示时间,并设计一个主函数进行测试。
展开
4个回答
展开全部
#include <iostream>
using namespace std;
class sanjiaoxing
{
private:
int a;
int b;
int c;
public:
sanjiaoxing();//无参构造函数
sanjiaoxing(int a,int b,int c);//有参构造函数
void GetZhouChang();//求周长
void GetLeiXing();//判断类型
};
void sanjiaoxing::GetZhouChang()
{
cout<<a+b+c<<endl;
}
void sanjiaoxing::GetLeiXing()
{
if(!(a+b>c&&a+c>b&&b+c>a))
{
cout<<"不能构成三角形"<<endl;
}
else if(a==b&&b==c)
{
cout<<"等边三角形"<<endl;
}
else if(a==b||b==c||c==a)
{
cout<<"等腰三角形"<<endl;
}
else
{
cout<<"普通三角形"<<endl;
}
}
sanjiaoxing::sanjiaoxing()
{
a=0;
b=0;
c=0;
}
sanjiaoxing::sanjiaoxing(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
}
int main()
{
sanjiaoxing x;
sanjiaoxing y(3,2,3);
x.GetLeiXing();
x.GetZhouChang();
y.GetLeiXing();
y.GetZhouChang();
return 0;
}
第二题:
#include <iostream>
using namespace std;
class Clock
{
private:
int hour;
int min;
int sec;
public:
Clock();//构造函数
void SetHour(int hour);//设置时
void SetMin(int min);//设置分
void SetSec(int sec);//设置秒
void PrintTime();//打印时间
};
void Clock::SetHour(int hour)
{
if(hour>=0&&hour<24)
this->hour=hour;
else cout<<"小时不正确"<<endl;
}
void Clock::SetMin(int min)
{
if(min>=0&&min<60)
this->min=min;
else cout<<"分钟不正确"<<endl;
}
void Clock::SetSec(int sec)
{
if(sec>=0&&sec<60)
this->sec=sec;
else cout<<"秒数不正确"<<endl;
}
void Clock::PrintTime()
{
cout<<hour<<":"<<min<<":"<<sec<<endl;
}
Clock::Clock()
{
hour=0;
min=0;
sec=0;
}
int main()
{
int hour;
int min;
int sec;
Clock t;
t.PrintTime();
cout<<"输入小时"<<endl;
cin>>hour;//
cout<<"输入分钟"<<endl;
cin>>min;//
cout<<"输入秒数"<<endl;
cin>>sec;//
t.SetHour(hour);
t.SetMin(min);
t.SetSec(sec);
t.PrintTime();
return 0;
}
using namespace std;
class sanjiaoxing
{
private:
int a;
int b;
int c;
public:
sanjiaoxing();//无参构造函数
sanjiaoxing(int a,int b,int c);//有参构造函数
void GetZhouChang();//求周长
void GetLeiXing();//判断类型
};
void sanjiaoxing::GetZhouChang()
{
cout<<a+b+c<<endl;
}
void sanjiaoxing::GetLeiXing()
{
if(!(a+b>c&&a+c>b&&b+c>a))
{
cout<<"不能构成三角形"<<endl;
}
else if(a==b&&b==c)
{
cout<<"等边三角形"<<endl;
}
else if(a==b||b==c||c==a)
{
cout<<"等腰三角形"<<endl;
}
else
{
cout<<"普通三角形"<<endl;
}
}
sanjiaoxing::sanjiaoxing()
{
a=0;
b=0;
c=0;
}
sanjiaoxing::sanjiaoxing(int a,int b,int c)
{
this->a=a;
this->b=b;
this->c=c;
}
int main()
{
sanjiaoxing x;
sanjiaoxing y(3,2,3);
x.GetLeiXing();
x.GetZhouChang();
y.GetLeiXing();
y.GetZhouChang();
return 0;
}
第二题:
#include <iostream>
using namespace std;
class Clock
{
private:
int hour;
int min;
int sec;
public:
Clock();//构造函数
void SetHour(int hour);//设置时
void SetMin(int min);//设置分
void SetSec(int sec);//设置秒
void PrintTime();//打印时间
};
void Clock::SetHour(int hour)
{
if(hour>=0&&hour<24)
this->hour=hour;
else cout<<"小时不正确"<<endl;
}
void Clock::SetMin(int min)
{
if(min>=0&&min<60)
this->min=min;
else cout<<"分钟不正确"<<endl;
}
void Clock::SetSec(int sec)
{
if(sec>=0&&sec<60)
this->sec=sec;
else cout<<"秒数不正确"<<endl;
}
void Clock::PrintTime()
{
cout<<hour<<":"<<min<<":"<<sec<<endl;
}
Clock::Clock()
{
hour=0;
min=0;
sec=0;
}
int main()
{
int hour;
int min;
int sec;
Clock t;
t.PrintTime();
cout<<"输入小时"<<endl;
cin>>hour;//
cout<<"输入分钟"<<endl;
cin>>min;//
cout<<"输入秒数"<<endl;
cin>>sec;//
t.SetHour(hour);
t.SetMin(min);
t.SetSec(sec);
t.PrintTime();
return 0;
}
展开全部
C++的话直接参考CTIME库文件就好了,除了SetTime这个难处理之外,当然这个如果能用API的话,也就一行代码而已。不用API的话就麻烦点!
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include <iostream.h>
class Clock
{
public:
void SetTime(int Hour,int Minute,int Second){
this->Hour=Hour;
this->Minute=Minute;
this->Second=Second;
}
void ShowTime(){
cout<<"现在时间为:"<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
private:
int Hour;
int Minute;
int Second;
};
void main(){
cout<<"请设置时间"<<endl;
int H,M,S;
cin>>H>>M>>S;
Clock c1;
c1.SetTime(H,M,S);
c1.ShowTime();
}
class Clock
{
public:
void SetTime(int Hour,int Minute,int Second){
this->Hour=Hour;
this->Minute=Minute;
this->Second=Second;
}
void ShowTime(){
cout<<"现在时间为:"<<Hour<<":"<<Minute<<":"<<Second<<endl;
}
private:
int Hour;
int Minute;
int Second;
};
void main(){
cout<<"请设置时间"<<endl;
int H,M,S;
cin>>H>>M>>S;
Clock c1;
c1.SetTime(H,M,S);
c1.ShowTime();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<iostream>
class Clock
{
public
void SetTime(int hour,int minute, int second)
{
Hour = Minute = Second = 0;
if (hour <24 && hour >=0)
{
Hour = hour;
}
if (minute< 60 && minute>=0) Minute=minute;
if (second<60 && second >=0) Second = second;
}
void ShowTime()
{
std::cout << Hour <<":"<<Minute<<":"<<Second<< "(时:分:秒)" << std::endl;
}
private:
int Hour;
int Minute;
int Second;
}
int main()
{
Clock tmp;
tmp.SetTime(12,13,14);
tmp.ShowTime();
return 0;
}
class Clock
{
public
void SetTime(int hour,int minute, int second)
{
Hour = Minute = Second = 0;
if (hour <24 && hour >=0)
{
Hour = hour;
}
if (minute< 60 && minute>=0) Minute=minute;
if (second<60 && second >=0) Second = second;
}
void ShowTime()
{
std::cout << Hour <<":"<<Minute<<":"<<Second<< "(时:分:秒)" << std::endl;
}
private:
int Hour;
int Minute;
int Second;
}
int main()
{
Clock tmp;
tmp.SetTime(12,13,14);
tmp.ShowTime();
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询