那位大侠能给出下面一道题的C++源代码。
编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:Enterthenumberofhou...
编写一个程序,要求用户输入小时数和分钟数。在main()函数中,将这两个值传递给一个void函数,后者以下面这样的格式显示这两个值:
Enter the number of hours:8
Enter the number of minutes:28
Time:9:28
谢谢!! 展开
Enter the number of hours:8
Enter the number of minutes:28
Time:9:28
谢谢!! 展开
2个回答
展开全部
下面是给你写的C++的代码:
--------------------------------------------------------
#include<iostream>
using namespace std;
void printTime(int hour,int minute)
{
cout<<"Time:"<<hour<<":"<<minute;
}
int main()
{
int hour,minute;
cout<<"Enter the number of hours:";
cin>>hour;
cout<<"Enter the number of minutes:";
cin>>minute;
printTime(hour,minute);
return 0;
}
------------------------------------------------
PS:楼上的好像没搞清楚楼主的要求啊,要求很简单,而你写的好像很复杂。。
--------------------------------------------------------
#include<iostream>
using namespace std;
void printTime(int hour,int minute)
{
cout<<"Time:"<<hour<<":"<<minute;
}
int main()
{
int hour,minute;
cout<<"Enter the number of hours:";
cin>>hour;
cout<<"Enter the number of minutes:";
cin>>minute;
printTime(hour,minute);
return 0;
}
------------------------------------------------
PS:楼上的好像没搞清楚楼主的要求啊,要求很简单,而你写的好像很复杂。。
展开全部
#include <iostream>
using namespace std;
class Time{
public:
Time();
void setTime(int,int,int);
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
Time::Time() {hour=minute=second=0;}
void Time::setTime(int h, int m, int s)
{
hour=(h>=0&&h<24)?h:0;
minute=(m>=0&&m<60)?m:0;
second=(s>=0&&s<60)?s:0;
}
void Time::printMilitary()
{
cout <<(hour<10?"0":"")<<hour<<":"
<<(minute<10?"0":"")<<minute;
}
void Time::printStandard()
{
cout<<(( hour==0||hour==12)?12:hour%12)
<<":"<<(minute<10?"0":"")<<minute
<<":"<<(second<10?"0":"")<<second
<<(hour<12?"AM":"PM");
}
int main()
{
Time t;
cout<<"The initial military time is";
t.printMilitary();
cout<<"\nThe initial standard time is";
t.printStandard();
t.setTime(13,27,6);
cout<<"\n\nMilitary time after setTime is";
t.printMilitary();
cout<<"\nStandard time after setTime is";
t.printStandard();
t.setTime(99,99,99);
cout<<"\n\nAfter attempting invalid settings:"
<<"\nMilitary time:";
t.printMilitary();
cout<<"\nStandard time:";
t.printStandard();
cout<<endl;
return 0;
}
using namespace std;
class Time{
public:
Time();
void setTime(int,int,int);
void printMilitary();
void printStandard();
private:
int hour;
int minute;
int second;
};
Time::Time() {hour=minute=second=0;}
void Time::setTime(int h, int m, int s)
{
hour=(h>=0&&h<24)?h:0;
minute=(m>=0&&m<60)?m:0;
second=(s>=0&&s<60)?s:0;
}
void Time::printMilitary()
{
cout <<(hour<10?"0":"")<<hour<<":"
<<(minute<10?"0":"")<<minute;
}
void Time::printStandard()
{
cout<<(( hour==0||hour==12)?12:hour%12)
<<":"<<(minute<10?"0":"")<<minute
<<":"<<(second<10?"0":"")<<second
<<(hour<12?"AM":"PM");
}
int main()
{
Time t;
cout<<"The initial military time is";
t.printMilitary();
cout<<"\nThe initial standard time is";
t.printStandard();
t.setTime(13,27,6);
cout<<"\n\nMilitary time after setTime is";
t.printMilitary();
cout<<"\nStandard time after setTime is";
t.printStandard();
t.setTime(99,99,99);
cout<<"\n\nAfter attempting invalid settings:"
<<"\nMilitary time:";
t.printMilitary();
cout<<"\nStandard time:";
t.printStandard();
cout<<endl;
return 0;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询