简体   繁体   English

如何将month(int)转换为月份名称(字符串)

[英]How to convert month(int) into month name (string)

I'm having a problem with a function i'm working on. 我正在使用的功能有问题。 I am able to get the proper data, just not print out the names of the month as well. 我能够获得正确的数据,只是不能同时打印出月份名称。

sample output i'm getting: 我得到的样本输出:

|   Month   | High  |  Low  |  Avg  | Precip  |
|-----------|-------|-------|-------|---------|
1    | 9.8 | -26.2 | -7.8 | 55.3    |
2    | 7.5 | -23.3 | -8.6 | 33.1    |
3    | 14.2 | -19.6 | -4.7 | 33.2   |
4    | 23.7 | -5.3 | 6.2 | 56.8 |
5    | 33.0 | -0.6 | 13.9 | 62.7    |
6    | 32.1 | 8.0 | 19.7 | 69.7 |
7    | 34.9 | 12.6 | 22.2 | 181.8   |
8    | 31.5 | 11.0 | 20.9 | 69.2    |
9    | 34.1 | 5.0 | 16.1 | 69.0 |
10    | 24.8 | -2.9 | 10.8 | 56.9   |
11    | 16.0 | -12.8 | 2.1 | 36.2   |
12    | 15.6 | -17.8 | -4.2 | 65.8

I want to conver digits 1-12 to there proper month names. 我想将数字1-12转换为正确的月份名称。 Ie: 1 = January. 即:1 =一月。

void printMonthlyStatistic(int month,const struct MonthlyStatistic* monthly)

is what the function looks like 函数是什么样子

for(i=0;i<12;i++){
   printMonthlyStatistic(i+1,&monthly[i])

and is called in main as such. 并因此而被称为。

Any help onto where to look for the proper method to complete this would be great. 任何在何处寻找适当方法来完成此操作的帮助都将非常有用。 Thanks! 谢谢!

You should use an array to store the name of the month, like: 您应该使用数组存储月份名称,例如:

const char * months[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

You can get the name of the month easily, for example, if index = 1 , then 您可以轻松获取月份名称,例如,如果index = 1 ,则

months[index -1]

will give you the first month name: January . 将给您第一个月的名字: 一月


The following code: 如下代码:

for (int i = 0; i < 12; ++i) {
    printf("%s ", months[i]);
}

will output: 将输出:

January February March April May June July August September October November December 一月二月三月四月五月六月七月八月九月十月十一月十二月

You can maintain Month Array of String like 您可以像这样维护字符串的月份数组
char months_names[12] = {Jan, Feb, Mar, Apr, Jun, Jul, Aug, Sep, Oct, Nov, Dec}; char months_names [12] = {1月,2月,3月,4月,6月,7月,8月,9月,10月,11月,12月};

You can Just paas integer number month to array for getting String value 您可以将整数月份停顿到数组以获取字符串值

months_names["Integrer Value"] You will get String name of month. months_names [“ Integrer Value”]您将获得月份的字符串名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM