使用 for 循环编写一个 C++ 程序来打印 W 星星图案。在此 C++ 示例中,printingSpaces 从 0 迭代到 rows 并打印空空格,printingStars 将打印星星。
#include<iostream>
using namespace std;
void printingStars(int rows)
{
for (int i = 0; i < rows; ++i)
{
cout << "*";
}
}
void printingSpaces(int rows)
{
for (int i = 0; i < rows; ++i)
{
cout << " ";
}
}
int main()
{
int rows;
cout << "Enter W Shape Star Pattern Rows = ";
cin >> rows;
cout << "Printing W Shape Pattern\n";
for (int i = 0; i < rows; i++)
{
printingStars(i + 1);
printingSpaces(rows - i - 1);
printingStars(rows - i + 1);
printingSpaces(2 * i);
printingStars(rows - i);
printingSpaces(rows - i - 1);
printingStars(i + 1);
cout << "\n";
}
}

此 C++ 程序使用 while 循环打印星星的字母 W 图案。
#include<iostream>
using namespace std;
void printingStars(int rows)
{
int i = 0;
while (i < rows)
{
cout << "*";
++i;
}
}
void printingSpaces(int rows)
{
int i = 0;
while (i < rows)
{
cout << " ";
++i;
}
}
int main()
{
int rows;
cout << "Enter W Shape Star Pattern Rows = ";
cin >> rows;
cout << "Printing W Shape Pattern\n";
int i = 0;
while (i < rows)
{
printingStars(i + 1);
printingSpaces(rows - i - 1);
printingStars(rows - i + 1);
printingSpaces(2 * i);
printingStars(rows - i);
printingSpaces(rows - i - 1);
printingStars(i + 1);
cout << "\n";
i++;
}
}
Enter W Shape Star Pattern Rows = 13
Printing W Shape Pattern
* *************************** *
** ************* ************ **
*** ************ *********** ***
**** *********** ********** ****
***** ********** ********* *****
****** ********* ******** ******
******* ******** ******* *******
******** ******* ****** ********
********* ****** ***** *********
********** ***** **** **********
*********** **** *** ***********
************ *** ** ************
*************** **************
在此 C++ 示例中,printingStars 函数允许输入任何字符并打印给定字符的 W 图案。
#include<iostream>
using namespace std;
void printingStars(int rows, char ch)
{
for (int i = 0; i < rows; ++i)
{
cout << ch;
}
}
void printingSpaces(int rows)
{
for (int i = 0; i < rows; ++i)
{
cout << " ";
}
}
int main()
{
int rows;
char ch;
cout << "Enter Character for W Pattern = ";
cin >> ch;
cout << "Enter W Shape Star Pattern Rows = ";
cin >> rows;
cout << "Printing W Shape Pattern\n";
for (int i = 0; i < rows; i++)
{
printingStars(i + 1, ch);
printingSpaces(rows - i - 1);
printingStars(rows - i + 1, ch);
printingSpaces(2 * i);
printingStars(rows - i, ch);
printingSpaces(rows - i - 1);
printingStars(i + 1, ch);
cout << "\n";
}
}
Enter Character for W Pattern = $
Enter W Shape Star Pattern Rows = 16
Printing W Shape Pattern
$ $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $
$$ $$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$ $$
$$$ $$$$$$$$$$$$$$$ $$$$$$$$$$$$$$ $$$
$$$$ $$$$$$$$$$$$$$ $$$$$$$$$$$$$ $$$$
$$$$$ $$$$$$$$$$$$$ $$$$$$$$$$$$ $$$$$
$$$$$$ $$$$$$$$$$$$ $$$$$$$$$$$ $$$$$$
$$$$$$$ $$$$$$$$$$$ $$$$$$$$$$ $$$$$$$
$$$$$$$$ $$$$$$$$$$ $$$$$$$$$ $$$$$$$$
$$$$$$$$$ $$$$$$$$$ $$$$$$$$ $$$$$$$$$
$$$$$$$$$$ $$$$$$$$ $$$$$$$ $$$$$$$$$$
$$$$$$$$$$$ $$$$$$$ $$$$$$ $$$$$$$$$$$
$$$$$$$$$$$$ $$$$$$ $$$$$ $$$$$$$$$$$$
$$$$$$$$$$$$$ $$$$$ $$$$ $$$$$$$$$$$$$
$$$$$$$$$$$$$$ $$$$ $$$ $$$$$$$$$$$$$$
$$$$$$$$$$$$$$$ $$$ $$ $$$$$$$$$$$$$$$
$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$