2025年3月17日 星期一 甲辰(龙)年 月十六 设为首页 加入收藏
rss
您当前的位置:首页 > 计算机 > 编程开发 > C语言

C语言使用枚举输出一周中的每一天

时间:12-29来源:作者:点击数:94

下面的程序将输出一周中的每一天:

  • #include <stdio.h>
  • // 定义枚举类型
  • enum days {monday,tuesday,wednesday,thursday,friday,saturday,sunday};
  • typedef enum days days; // 我们可以使用 days 来代替 enum days
  • days yesterday(days today){
  • return (today+6)%7;
  • }
  • days tomorrow(days today){
  • return (today+1)%7;
  • }
  • // 常量字符串数组
  • const char * const thedays[] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
  • int main(void){
  • days today;
  • printf("today \tyesterday \ttomorrow\n"
  • "============================================\n");
  • for (today=monday;today<=sunday;today++)
  • printf("%s = %d \t %s = %d \t %s = %d\n",
  • thedays[today], today,
  • thedays[yesterday(today)], yesterday(today),
  • thedays[tomorrow(today)], tomorrow(today));
  • }

输出结果:

输出结果:
today                        yesterday               tomorrow
============================================
monday = 0            sunday = 6              tuesday = 1
tuesday = 1            monday = 0             wednesday = 2
wednesday = 2      tuesday = 1             thursday = 3
thursday = 3           wednesday = 2       friday = 4
friday = 4                 thursday = 3            saturday = 5
saturday = 5           friday = 4                  sunday = 6
sunday = 6             saturday = 5            monday = 0
方便获取更多学习、工作、生活信息请关注本站微信公众号城东书院 微信服务号城东书院 微信订阅号
推荐内容
相关内容
栏目更新
栏目热门