生日蛋糕代码c++语言
时间: 2025-01-07 09:47:06 浏览: 85
### C++ 编写生日蛋糕程序
对于编写一个简单的C++程序来模拟庆祝生日的场景,可以创建一个基本的应用程序,该应用程序打印出带有祝福语句的信息以及一个简单表示蛋糕的文字图案。下面是一个可能实现的例子:
```cpp
#include <iostream>
#include <string>
using namespace std;
void printCake(int layers) {
for (int i = 0; i < layers; ++i) {
// Print spaces before each layer of cake
cout << string(layers - i - 1, ' ');
// Print stars representing the top part of the cake slice
cout << string(2 * i + 1, '*') << endl;
}
// Base of the cake
cout << string(layers, "___") << endl;
}
void celebrateBirthday(const string& name) {
cout << "Happy Birthday to You!" << endl;
cout << "Happy Birthday to You!" << endl;
cout << "Happy Birthday dear " << name << "!" << endl;
cout << "Happy Birthday to You!" << endl;
}
int main() {
string personName;
int numberOfLayers;
cout << "Enter the name of the birthday person: ";
getline(cin, personName);
cout << "How many layers should the cake have? ";
cin >> numberOfLayers;
celebrateBirthday(personName);
cout << "\nHere's your special birthday cake:\n";
printCake(numberOfLayers);
return 0;
}
```
这段代码定义了一个`printCake()`函数用于绘制不同层数的蛋糕图形[^1],还有一个`celebrateBirthday()`函数用来输出传统的生日歌歌词并加入个人的名字作为祝福对象。
阅读全文
相关推荐
















