我最近开始学习 C++,我的代码很蹩脚。您需要输出以下内容:
我怎样才能缩短我的代码(我相信你可以)?
#include <iostream>
#include <string>
using namespace std;
int main() {
setlocale(LC_ALL, "ru");
string simvol = "*";
int control = 7;
for (int j = 0; j < control; j++) {
cout << simvol << " ";
}
cout << endl;
for (int j = 0; j < control; j++) {
if ((j == 0) || (j == control - 1)) {
cout << " ";
}
else {
cout << simvol << " ";
}
}
cout << endl;
for (int j = 0; j < control; j++) {
if ((j == 0) || (j == 1) || (j == control - 1) || (j == control - 2)) {
cout << " ";
}
else {
cout << simvol << " ";
}
}
cout << endl;
for (int j = 0; j < control; j++) {
if ((j == 0) || (j == 1) || (j == 2) || (j == control - 1) || (j == control - 2) || (j == control - 3)) {
cout << " ";
}
else {
cout << simvol << " ";
}
}
cout << endl;
for (int j = 0; j < control; j++) {
if ((j == 0) || (j == 1) || (j == control - 1) || (j == control - 2)) {
cout << " ";
}
else {
cout << simvol << " ";
}
}
cout << endl;
for (int j = 0; j < control; j++) {
if ((j == 0) || (j == control - 1)) {
cout << " ";
}
else {
cout << simvol << " ";
}
}
cout << endl;
for (int j = 0; j < control; j++) {
cout << simvol << " ";
}
}

够好吗?
见https://ideone.com/qzhiIx
PS好吧,或者如果正好是7-
:)