如何调整ASCII花朵中@符号上下行位置并实现居中?
ASCII花朵项目问题修复
问题概述
- @符号上下的行结构不对称:当前下方行是从窄到宽排列,不符合预期的从宽到窄对称样式
- 整个花朵输出未实现居中对齐
问题代码分析
负责@下方行输出的sectAfter@段(原代码114-128行)使用了和上方sectBefore@相同的递增循环,导致下方行顺序与上方相反,破坏对称结构;同时所有行的前置空格数未基于最大宽度计算,无法实现全局居中。
修复方案
1. 修复@上下行对称问题
将sectAfter@的循环改为递减遍历,让下方行从宽到窄排列,与上方行形成对称:
// 修改前 for (int h = 0; h < i; h++) { ... } // 修改后 for (int h = i - 1; h >= 0; h--) { ... }
2. 实现全局居中对齐
首先计算花朵的最大宽度(对应最外层行的宽度):
int maxWidth = 2 * sectionChoice + 3;
然后为每一行添加前置空格,空格数为(maxWidth - 当前行宽度)/2,具体修改如下:
修复顶部/底部虚线的居中
// 修改前的topDashes for (int k = 0; k < sectionChoice; k++) { cout << " "; } cout << "---" << endl; // 修改后的topDashes int dashSpace = (maxWidth - 3) / 2; cout << setw(dashSpace) << "" << "---" << endl; // bottomDashes按相同逻辑修改
修复花头各行的居中
为sectBefore@、midWith@、sectAfter@的每行添加前置空格:
// sectBefore@添加居中空格 for (int h = 0; h < i; h++) { int lineWidth = 2 * h + 3; int space = (maxWidth - lineWidth) / 2; cout << setw(space) << ""; // 原有输出逻辑不变 } // midWith@添加居中空格 int midWidth = 2 * i + 5; int midSpace = (maxWidth - midWidth) / 2; cout << setw(midSpace) << ""; // 原有输出逻辑不变 // sectAfter@添加居中空格(已改为递减循环) for (int h = i - 1; h >= 0; h--) { int lineWidth = 2 * h + 3; int space = (maxWidth - lineWidth) / 2; cout << setw(space) << ""; // 原有输出逻辑不变 }
修复花茎的居中
以最大宽度为基准调整花茎的位置:
// 修改后的stem部分 int stemCenter = (maxWidth + 1) / 2; for (int s = 0; s < sectionChoice * 2; s++) { if (s % 2 != 0) { cout << setw(stemCenter) << "|" << endl; } else if (s % 2 == 0) { if (s % 4 == 2) { cout << setw(stemCenter -1) << "\"" << "|" << endl; } else { cout << setw(stemCenter) << "|" << "/" << endl; } } }
完整修复后代码
#include <iostream> #include <iomanip> #include <cstdlib> using namespace std; int main() { cout << "Program 1: ASCII Flowers\n" << "Choose from the following options:\n" << " 1. Display the HELLO graphic\n" << " 2. Display The Flower\n" << " 3. Exit the program\n" << "Your choice -> "; int menu_option; cin >> menu_option; if (menu_option == 3) { exit(0); } if (menu_option == 1) { char frame; cout << "Enter your frame character: "; cin >> frame; cout << endl; for (int i=0; i<2; i++) { cout << setfill(frame) << setw(36) << ' ' << endl; } cout << setfill(frame) << setw(3) << right << " " << "** ** ***** ** ** *****" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "** ** ***** ** ** *****" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "** ** ** ** ** ** **" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "***** ***** ** ** ** **" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "***** ***** ** ** ** **" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "** ** ** ** ** ** **" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "** ** ***** ***** ***** *****" << setfill(frame) << setw(3) << left << " " << endl << setfill(frame) << setw(3) << right << " " << "** ** ***** ***** ***** *****" << setfill(frame) << setw(3) << left << " " << endl << right; for (int i=0; i<2; i++) { cout << setfill(frame) << setw(36) << " " << endl; } } if (menu_option == 2) { cout << "Enter number of sections: "; int sectionChoice; cin >> sectionChoice; int maxWidth = 2 * sectionChoice + 3; for (int i = 0; i < sectionChoice; i++) { int dashSpace = (maxWidth - 3) / 2; cout << setw(dashSpace) << "" << "---" << endl; for (int h = 0; h < i; h++) { int lineWidth = 2 * h + 3; int space = (maxWidth - lineWidth) / 2; cout << setw(space) << ""; for (int r = 0; r < lineWidth ; r++) { if (r == 0) { cout << "{"; } cout << ":"; if (r == lineWidth - 1) { cout << "}" << endl; } } } int midWidth = 2 * i + 5; int midSpace = (maxWidth - midWidth) / 2; cout << setw(midSpace) << ""; cout << "{"; for (int k = 0; k <= i; k++) { cout << ":"; } cout << "@"; for (int k = 0; k <= i; k++) { cout << ":"; } cout << "}" << endl; for (int h = i - 1; h >= 0; h--) { int lineWidth = 2 * h + 3; int space = (maxWidth - lineWidth) / 2; cout << setw(space) << ""; for (int r = 0; r < lineWidth; r++) { if (r == 0) { cout << "{"; } cout << ":"; if (r == lineWidth - 1) { cout << "}" << endl; } } } } int dashSpace = (maxWidth - 3) / 2; cout << setw(dashSpace) << "" << "---" << endl; int stemCenter = (maxWidth + 1) / 2; for (int s = 0; s < sectionChoice * 2; s++) { if (s % 2 != 0) { cout << setw(stemCenter) << "|" << endl; } else if (s % 2 == 0) { if (s % 4 == 2) { cout << setw(stemCenter -1) << "\"" << "|" << endl; } else { cout << setw(stemCenter) << "|" << "/" << endl; } } } } return 0; }
内容的提问来源于stack exchange,提问作者Bill Brown
相关产品推荐
相关产品推荐

