21xrx.com
2024-09-20 00:15:39 Friday
登录
文章检索 我的文章 写文章
教你如何用Dev-C++语言写生日礼物代码
2023-07-01 10:35:09 深夜i     --     --
Dev-C++ 语言 生日礼物 代码 编程

在生日这个特殊的日子里,送一份特别的礼物给最重要的人,是许多人的心愿。而如果你是一位程序员,用自己的技能来制作一个特别的生日礼物,定会给你的朋友或爱人留下难以忘怀的回忆。下面就来教你使用Dev-C++语言写生日礼物代码。

首先,我们要明确我们要制作一个什么样的生日礼物。比如,我们可以写一个小程序,当我们输入我们的生日时,程序会自动计算并输出我们的星座和生肖,这样的一个小程序既有趣又实用。

接下来,我们要打开Dev-C++软件,在“文件”菜单栏中选择“新增文件”,然后选择“C++源代码文件”,并将它命名为“birthday.cpp”。然后,在代码编辑器中输入以下代码:

#include

using namespace std;

int main()

{

  int year, month, day;

  char zodiac[12][6] = "鸡";//生肖数组

  char constellation[12][12] = "处女座";//星座数组

  cout << "请输入你的出生年份(如2020):" << endl;

  cin >> year;

  cout << "请输入你的出生月份(如10):" << endl;

  cin >> month;

  cout << "请输入你的出生日期(如15):" << endl;

  cin >> day;

  cout << "你的生肖是:" << zodiac[(year - 4) % 12] << endl;

  if (month == 1 && day >=20 || month == 2 && day <= 18)

   cout << "你的星座是:" << constellation[0] << endl;

  if (month == 2 && day >=19 || month == 3 && day <= 20)

   cout << "你的星座是:" << constellation[1] << endl;

  if (month == 3 && day >=21 || month == 4 && day <= 19)

   cout << "你的星座是:" << constellation[2] << endl;

  if (month == 4 && day >=20 || month == 5 && day <= 20)

   cout << "你的星座是:" << constellation[3] << endl;

  if (month == 5 && day >=21 || month == 6 && day <= 21)

   cout << "你的星座是:" << constellation[4] << endl;

  if (month == 6 && day >=22 || month == 7 && day <= 22)

   cout << "你的星座是:" << constellation[5] << endl;

  if (month == 7 && day >=23 || month == 8 && day <= 22)

   cout << "你的星座是:" << constellation[6] << endl;

  if (month == 8 && day >=23 || month == 9 && day <= 22)

   cout << "你的星座是:" << constellation[7] << endl;

  if (month == 9 && day >=23 || month == 10 && day <= 22)

   cout << "你的星座是:" << constellation[8] << endl;

  if (month == 10 && day >=23 || month == 11 && day <= 21)

   cout << "你的星座是:" << constellation[9] << endl;

  if (month == 11 && day >=22 || month == 12 && day <= 21)

   cout << "你的星座是:" << constellation[10] << endl;

  if (month == 12 && day >=22 || month == 1 && day <= 19)

   cout << "你的星座是:" << constellation[11] << endl;

  return 0;

}

代码中,我们首先定义了一个整型数组和两个字符串数组,分别代表了12个生肖和12个星座。然后,我们在主函数中依次输入出生年月日,并通过算法计算出生肖和星座。最后,使用if语句分别判断出生月份和日期,输出对应的星座。

最后,我们将代码保存,并在控制台中编译和运行,即可看到我们的生日礼物:输入自己的生日,程序会告诉你对应的生肖和星座。这个小程序不仅实用,而且很有趣,填补了朋友、爱人在生日这一天收到节日礼物的空缺。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复