我已经将我的解决方案提交给LCD Diplay编程断言(详细的这里),请检查我的代码,并让我知道您的想法。
// ConsoleApplication3.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
/*We use this array to store the graphical LCD
representation of each number each digit is stored in an array of size 5 that
represents each of 5 rows of the LCD output. */
const string lcd_digits_images[10][5] = { { " - ","| |"," ","| |"," - " },
{ " "," |"," "," |"," " },
{ " - "," |"," - ","| "," - " },
{ " - "," |"," - "," |"," - " },
{ " ","| |"," - "," |"," " },
{ " - ","| "," - "," |"," - " },
{ " - ","| "," - ","| |"," - " },
{ " - "," |"," "," |"," " },
{ " - ","| |"," - ","| |"," - " },
{ " - ","| |"," - "," |"," - " } };
/*draws any section that could occupy a '-' in the LCD representation of a number.
These are sectons 1, 3 and 5
digits_to_draw represents the digits to be drawn.
rowNo is the section number.
size represents the size parameter mentioned in the spec.
number_of_digits is the number of digits that for the number to be drawn.*/
void draw_horizontal_row(int* digits_to_draw, int rowNo, int size, int number_of_digits ){
for (int i = 0; i < number_of_digits; i++) {
if (lcd_digits_images[digits_to_draw[i]][rowNo].compare(" - ") == 0) {
cout << " ";
for (int j = 0; j < size; j++) {
cout << "-";
}
cout << " ";
if (i < number_of_digits - 1) {
cout << " ";
}
}
else {
for (int i = 0; i < size + 2; i++) {
cout << " ";
}
if (i < number_of_digits - 1) {
cout << " "; //A space follows each digit drawn except the last one.
}
}
}
cout << endl; //After drawing the digits, we add a blank line to seperate our output.
}
/*draws any section that could occupy a '|' in the LCD representation of a number.
These are sectons 2 and 4 of the
digits_to_draw represents the digits to be drawn.
rowNo is the section number.
size represents the size parameter mentioned in the spec.
number_of_digits is the number of digits that for the number to be drawn.*/
void draw_vertical_row(int* digits_to_draw, int rowNo, int size, int number_of_digits) {
for (int i = 0; i < size; i++) {
for (int i = 0; i < number_of_digits; i++) {
if (lcd_digits_images[digits_to_draw[i]][rowNo].compare(" |") == 0) {
for (int i = 0; i < size+1; i++) {
cout << " ";
}
cout << "|";
}
else if (lcd_digits_images[digits_to_draw[i]][rowNo].compare("| ") == 0) {
cout << "|";
for (int i = 0; i < size + 1; i++) {
cout << " ";
}
}
else {
cout << "|";
for (int i = 0; i < size; i++) {
cout << " ";
}
cout << "|";
}
if (i < number_of_digits - 1) {
cout << " ";//A space follows each digit drawn except the last one.
}
}
cout << endl; //After drawing the digits, we add a blank line to seperate our output.
}
}
int main()
{
int size, number;
string number_to_draw;
while (cin >> size >> number_to_draw) {
number = stoi(number_to_draw);
if (number == 0 && size == 0) {
return 0;
}
int* numbers = new int[number_to_draw.length()];
for (int i = 0; i < number_to_draw.length(); i++) {
numbers[i] = number_to_draw.at(i) - '0' ;
}
int rowNo = 0;
draw_horizontal_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_vertical_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_horizontal_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_vertical_row(numbers, rowNo, size, number_to_draw.length());
rowNo++;
draw_horizontal_row(numbers, rowNo, size, number_to_draw.length());
cout << endl;
}
}发布于 2016-10-26 19:45:02
字符之间的空格有问题,特别是在输入"2 18“时(见下文):
2 18
--
| | |
| | |
--
| | |
| | |
--而不是:
2 18
--
| | |
| | |
--
| | |
| | |
--问题解决了:在draw_horizontal_row()中,主循环的索引被第二个循环覆盖。
使用变量'j‘如下:
else {
for (int j = 0; j < size + 2; j++) {
std::cout << " ";
}
if (i < number_of_digits - 1) {
std::cout << " "; //A space follows each digit drawn except the last one.
}
}而不是:
else {
for (int i = 0; i < size + 2; i++) { // the local 'i' override loop 'i'
std::cout << " ";
}
if (i < number_of_digits - 1) {
std::cout << " "; //A space follows each digit drawn except the last one.
}
}在draw_vertical_row()中也要小心,其中'i‘在循环的三个级别中被重用。使用3个不同的变量名( i,j& k):
void draw_vertical_row(int* digits_to_draw, int rowNo, int size, int number_of_digits) {
for (int i = 0; i < size; i++) {
for (int j = 0; j < number_of_digits; j++) {
if (lcd_digits_images[digits_to_draw[j]][rowNo].compare(" |") == 0) {
for (int k = 0; k < size+1; k++) {
std::cout << " ";
}
std::cout << "|";
}
else if (lcd_digits_images[digits_to_draw[j]][rowNo].compare("| ") == 0) {
std::cout << "|";
for (int k = 0; k < size + 1; k++) {
std::cout << " ";
}
}
else {
std::cout << "|";
for (int k = 0; k < size; k++) {
std::cout << " ";
}
std::cout << "|";
}
if (j < number_of_digits - 1) {
std::cout << " ";//A space follows each digit drawn except the last one.
}
}
std::cout << std::endl; //After drawing the digits, we add a blank line to seperate our output.
}
}https://codereview.stackexchange.com/questions/145278
复制相似问题