作者hankhank5345 (MPower)
看板C_and_CPP
标题[问题] C++ 时间换算
时间Mon Mar 9 05:12:37 2009
这是一题24小时换12小时的问题?请高手们帮我看一下为什麽我跑出来会是下列的样子.
Enter the hours for the 24 hour time: 13
Enter the minutes for the 24 hour time: 40
The time converted to 12 hour format is: 80:40 P.M.
#include <iostream>
using namespace std;
//
// function declarations
//
void input(int& hours, int& minutes);
void output(int hours, int minutes, char type);
int convertTo12Hour(int hours, char& type);
int main()
{
int hours;
int minutes;
char type;
char answer;
do {
input(hours, minutes);
hours = convertTo12Hour(hours, type);
output(hours, minutes, type);
cout << "Perform another calculation? (y/n): ";
cin >> answer;
} while ((answer == 'Y') || (answer == 'y'));
return 0;
} // function main
//
// function definitions
//
void input(int& hours, int& minutes)
{
//
// prompt the user for the number of hours in 24 hour format
//
cout << "Enter the hours for the 24 hour time: ";
cin >> hours;
//
// prompt the user for the number of minutes
//
cout << "Enter the minutes for the 24 hour time: ";
cin >> minutes;
} // function input
void output(int hours, int minutes, char type)
{
//
// display the output
//
cout << "The time converted to 12 hour format is: " << hours << ":";
//
// special handling for leading 0s on the minutes
//
cout.width(2);
cout.fill('0');
cout << minutes;
//
// Output A.M. or P.M. based on type.
//
if (type == 'A')
cout << " A.M." << endl;
else
cout << " P.M." << endl;
} // function outpu
int convertTo12Hour(int hours, char& type)
{
if (hours == 0)
{
hours = 12;
return 'A';
}
if (hours >= 12)
if (hours == 12)
return 'P';
else
{
hours -= 12;
return 'P';
}
return 'A'
}
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 71.199.79.106
1F:推 LPH66:你确定你的 convertTo12Hour 做了你想让它做的事了吗? 03/09 06:09
2F:推 IDontBite:hours = convertTo12Hour(hours, type);这边错了 03/09 06:54
3F:→ IDontBite:你的convertTo12Hour回传的是A或P,你硬塞到hours里面 03/09 06:55
4F:→ IDontBite:就变成P读ASCII码80了 03/09 06:55
5F:→ IDontBite: 的 03/09 06:56