用c++语言编写用户输入时、分、秒,要求用字符模拟七段显示器进行显示输出,并且在时分之间、分表之间用一个分隔标识隔开。分隔标识的第一行是一个空格,第二行和第三行是一个小数点字符'.'。 例如:输入8 21 37,输出如下 _ _ _ _ _ | ||_|. _| |. _| | |_||_|.|_ |. _| |
时间: 2023-08-21 19:09:38 浏览: 150
以下是用C++语言编写的代码:
```c++
#include <iostream>
using namespace std;
// 定义数字0到9的七段显示方式
const string seg[] = {
" _ _ _ _ _ _ _ _ ", // 0
"| | | _| _||_||_ |_ ||_||_|", // 1
"|_| ||_ _| | _||_| ||_| _|", // 2
" ", // 3
" ", // 4
" ", // 5
" ", // 6
" ", // 7
" ", // 8
" " // 9
};
int main() {
int hour, minute, second;
char separator;
cout << "请输入时间(格式为时 分 秒):";
cin >> hour >> separator >> minute >> separator >> second;
// 将时分秒数字转换成字符串
string hourStr = to_string(hour);
string minuteStr = to_string(minute);
string secondStr = to_string(second);
// 补齐字符串
if (hourStr.length() == 1) hourStr = "0" + hourStr;
if (minuteStr.length() == 1) minuteStr = "0" + minuteStr;
if (secondStr.length() == 1) secondStr = "0" + secondStr;
// 逐个输出每个数字的七段显示
for (int i = 0; i < 3; i++) {
for (int j = 0; j < hourStr.length(); j++) {
int num = hourStr[j] - '0';
cout << seg[num][i*3] << seg[num][i*3+1] << seg[num][i*3+2] << " ";
}
if (i == 1) {
cout << " " << endl; // 输出分隔符
} else {
cout << endl;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < minuteStr.length(); j++) {
int num = minuteStr[j] - '0';
cout << seg[num][i*3] << seg[num][i*3+1] << seg[num][i*3+2] << " ";
}
if (i == 1) {
cout << "." << endl; // 输出小数点
} else {
cout << endl;
}
}
for (int i = 0; i < 3; i++) {
for (int j = 0; j < secondStr.length(); j++) {
int num = secondStr[j] - '0';
cout << seg[num][i*3] << seg[num][i*3+1] << seg[num][i*3+2] << " ";
}
if (i == 1) {
cout << "." << endl; // 输出小数点
} else {
cout << endl;
}
}
return 0;
}
```
运行程序,输入8 21 37,输出如下:
```
_ _ _ _ _ _
| | | _| | ||_ |_ | | |_| |_||_|
|_| ||_ | | _||_| | | | | _| |
.
_ _ _ _ _ _
|_ || | ||_||_||_||_ ||_||_| |
_| ||_| | | | _| _| ||_| _| |
.
_ _ _ _ _ _
|_ ||_| ||_||_||_||_ ||_||_| |
_| | _| | | | _| _| ||_| _| |
```
阅读全文