21xrx.com
2025-04-10 18:24:17 Thursday
文章检索 我的文章 写文章
C++生日蛋糕图案代码
2023-07-09 10:37:28 深夜i     19     0
C++ 生日 蛋糕 图案 代码

如果你是一名C++编程爱好者,那么你一定会喜欢我们今天要分享的主题——C++生日蛋糕图案代码。这个代码程序是一款非常有趣的C++项目,可以让你在生日给亲朋好友制作一个有趣的生日祝福。

在这个C++生日蛋糕图案中,我们需要用到一些基本的绘图知识和C++语言基础。这个程序首先定义了一个类“Cake”,并在类中实现了绘制蛋糕图案的各种方法和函数。在主函数中,我们创建了一个“Cake”对象,然后调用了类中的各种方法和函数,最终生成了一幅生日蛋糕图案。

下面是完整的代码程序:

#include <iostream>
#include <cmath>
using namespace std;
class Cake {
public:
  void draw_layer(int size) {
    for(int i=0; i<size; i++) {
      for(int j=0; j<size-i; j++) cout << " ";
      for(int j=size-i; j<size+i; j++) cout << "*";
      cout << "\n";
    }
  }
  void draw_baseline(int size) {
    for(int i=0; i<size; i++) cout << " ";
    cout << "*\n";
  }
  void draw_candles(int size) {
    cout << " ";
    for(int i=0; i<size; i++) cout << "|";
    cout << "\n";
  }
  void draw_flame(int size) {
    cout << " ";
    for(int i=0; i<size; i++) cout << "~";
    cout << "\n";
  }
  void draw_message(int size) {
    cout << "Happy Birthday!\n";
    cout << "   !!!!!\n";
    for(int i=0; i<size; i++) cout << "   !!!!!\n";
    cout << "   ------\n";
  }
  void draw_cake(int size) {
    draw_layer(size);   // draw top layer
    draw_layer(size+2);  // draw middle layer
    draw_layer(size+4);  // draw bottom layer
    draw_baseline(size+5); // draw baseline
    draw_candles(size+5); // draw candles
    draw_flame(size+5);  // draw flame
    draw_message(size+5); // draw message
  }
};
int main() {
  int size;
  cout << "Enter the size of the cake: ";
  cin >> size;
  cout << "\n";
  Cake my_cake;
  my_cake.draw_cake(size);
  return 0;
}

当我们运行这个程序时,会出现一个提示,要求我们输入蛋糕的大小。我们可以输入一个数字,然后按下回车键,就会出现一个生日蛋糕图案,效果非常实用。

在学习C++编程的道路上,我们经常希望熟练掌握C++编程语言的基础知识,同时也能够结合自己的兴趣和爱好来实现一些项目。这个C++生日蛋糕图案代码程序正是一个非常好的例子,让我们能够在编程过程中收获乐趣和成果。

  
  

评论区