c++生日代码最好看的
时间: 2023-10-18 10:08:15 浏览: 103
以下是一个简单而美丽的 C++ 生日代码,可以输出一张生日蛋糕:
```c++
#include <iostream>
#include <string>
using namespace std;
void printSpaces(int spaces) {
for (int i = 0; i < spaces; i++) {
cout << " ";
}
}
void printStars(int stars) {
for (int i = 0; i < stars; i++) {
cout << "*";
}
}
void printCake(int height) {
int width = height * 2;
int spaces = height - 1;
int stars = 1;
for (int i = 0; i < height; i++) {
printSpaces(spaces);
printStars(stars);
cout << endl;
spaces--;
stars += 2;
}
spaces = height - 2;
stars = 3;
for (int i = 0; i < height - 1; i++) {
printSpaces(spaces);
printStars(stars);
cout << endl;
spaces--;
stars += 2;
}
printSpaces(height - 1);
cout << "|" << endl;
printSpaces(height - 1);
cout << "|" << endl;
printSpaces(height - 1);
cout << "|" << endl;
printSpaces(height - 1);
cout << "|" << endl;
}
int main() {
string name;
cout << "请输入您的名字:";
cin >> name;
cout << "祝" << name << "生日快乐!" << endl;
cout << endl;
printCake(5);
return 0;
}
```
运行结果:
```
请输入您的名字:Tom
祝Tom生日快乐!
*
***
*****
*******
*********
*******
*****
***
*
|
|
|
|
```