- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是练习:
Write a function called
monthName()
that takes as its argument a value of typeenum month
(as defined in this chapter) and returns a pointer to a character string containing the name of the month. In this way, you can display the value of anenum month
variable with a statement such as:
printf("%s\n", monthName(aMonth));
我编写了我的版本并获得了预期的输出。但是,我确信必须有更好的方法来实现这一点,而不必每个月都使用一个 case 的 switch 语句。我怎样才能用更好的设计来编写这个函数?
#include <stdio.h>
enum month { January = 1, February, March, April, May, June,
July, August, September, October, November, December };
char *monthName(enum month m){
switch (m) {
case January:
return "January";
break;
case February:
return "February";
break;
case March:
return "March";
break;
case April:
return "April";
break;
case May:
return "May";
break;
case June:
return "June";
break;
case July:
return "July";
break;
case August:
return "August";
break;
case September:
return "September";
break;
case October:
return "October";
break;
case November:
return "November";
break;
case December:
return "December";
break;
default:
return "Not a valid month";
}
}
int main(void)
{
enum month aMonth = 1;
printf("%s\n", monthName(aMonth));
aMonth = 2;
printf("%s\n", monthName(aMonth));
aMonth = 6;
printf("%s\n", monthName(aMonth));
return 0;
}
最佳答案
您可以使用一个简单的查找表,因为枚举以 1 开头:
#include <stdio.h>
enum month { January = 1, February, March, April, May, June,
July, August, September, October, November, December };
const char *months_str[] = {
"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December", NULL
};
const char *monthName(enum month m) {
if(m < January || m > December)
return "Invalid month";
return months_str[m-1];
}
int main(void)
{
enum month aMonth = 1;
printf("%s\n", monthName(aMonth));
aMonth = 2;
printf("%s\n", monthName(aMonth));
aMonth = 6;
printf("%s\n", monthName(aMonth));
return 0;
}
关于c - 枚举和一个以字符串形式返回月份名称的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49438423/
我正在用 javascript 制作日历,我不想在 中显示当前日期、日期名称和月份名称它在我的函数内 Kalender()但由于某种原因,如果我执行函数 volgende() (下个月)它更改 中
有一个输入标签应用了数据掩码。 这允许输入数据,例如 02/12/2014 或 13/05/2014。但不接受 1/1/2013 或 13/5/2014。请帮忙!!! 最佳答案 根据jQuery
我正在尝试创建一个函数,它将接受三个输入变量(开始日期、结束日期、粒度),并将输出一个包含所有日期的数组。输出将根据输入以每日或每月为粒度。例如,如果我查看 2015 年 1 月 - 2015 年 2
我有这段代码可以从数据库中获取一个字段: $end_date=$row1['end_date']; 如果我打印它,它会给我这样的信息:25-09-2012我需要的是获取月份值、年份和日期。类似于: $
我有一个事件表,其中 X 为频率(每 X 个月一次),第一个开始日期和结束日期如下: 如何复制每一行并将其粘贴到新工作表中,并根据 X 和月份日期的增量为每一行添加附加行,如下所示: 最佳答案 这里是
我将以下格式的日期存储在Mailed_Date列中 Mon, 09/20/10 01:04 PM 我使用了一个serde(csv-serde-1.1.2-0.11.0-all.jar)从csv文件中获
我想根据日期和月份值对数组进行排序。我正在使用此代码 NSSortDescriptor *descriptor=[[NSSortDescriptor alloc]initWithKey:@"self
我需要当前年份,月份和日期为3个不同的变量。下面的代码给出了日期时间 val now = Calendar.getInstance().getTime() 2016年9月29日星期四18:27:38
我无法获得正确的查询结果 我正在查询获取在相应月份注册了多少用户。到目前为止,我提出了以下查询,它允许我获得该结果,但没有显示没有用户注册的月份 SELECT YEAR(c.created_at) a
在 Programming in the Key of C# 中,作者给出了一个示例(附源代码),说明如何将日期(年、月、日 -- 数字)打包为 32 位整数。在示例中,作者将信息打包如下: int
在表中,我有一列名为“service_time”的列,它是服务的开始日期,另一列“times_year”是每年应完成服务的次数。 我遇到的问题是如何在 -> (thisMonth == service
使用:Python 3.6, Pandas 0.22 我有一个 .csv 文件,我需要从中获取基于月份和位置的平均值。这是数据中的一行,还有更多具有多个位置和日期的行: 名称日期雪 大急流城杰拉尔德福
我想循环遍历给定开始时间以来的月份,并打印第一天和最后一天。我可以手动跟踪它是哪个月份和年份,并使用 calendar.monthrange(year, Month) 来获取天数......但这是最好
我正在做试卷的最后一题,但我已经迷失在算法的创建过程中。我的定义图看起来不错,但我就是无法确定计算月份方面的顺序。 题目如下: A file of transaction records includ
我正在获取系统当前日期并尝试在 TextView 中显示它。 尝试下面的代码后 private OnClickListener listener1 = new OnClickListener() {
我正在使用以下格式来格式化 DateTime: DateTime CusDate = dateTimePicker1.Value; string Date = CusDate.ToString("dd
废话不多少,上代码 复制代码 代码如下: // 获取指定日期所在星期的开始时间与结束时间 function getWeekRange($date){ &nb
今天我运行了自动化测试,并想知道为什么我收到了有关某些特定日期内容的错误。 事实证明,设置固定的 UTC 月份不再有效。但昨天确实如此。据我所知,没有任何变化。 我尝试运行以下代码 var d = n
我有一个包含两个日期字段的 Grails 域 Date updated Date created 我想根据月、小时或年的更新时间来计算行数。我怎样才能做到这一点。互联网上显示的方法不起作用。我正在使用
是否有任何内置函数可用于数据框对象以生成类 Date 时间序列上的变量以创建星期几、月份、年份、年份中的星期,等在 R 中? 基础包中的weekdays、months、quarters函数生成文本输出
我是一名优秀的程序员,十分优秀!