《C primer plus》 Chapter 14.1
程序员文章站
2024-02-29 18:09:52
...
# include <stdio.h>
# include <string.h>
# define LEN 10
struct months
{
char month[LEN];
char abbr[LEN];
int days;
int number;
};
char *s_get(char *st,int n);
int main(void)
{
struct months year[12]=
{
{"January","Jan",31,1},
{"February","Feb",28,2},
{"March","Mar",31,3},
{"April","Apr",30,4},
{"May","May",31,5},
{"June","Jun",30,6},
{"July","Jul",31,7},
{"August","Aug",31,8},
{"September","Sep",30,9},
{"October","Oct",31,10},
{"November","Nov",30,11},
{"December","Dec",31,12}
};
int sumdays=0;
int i,n;
char name[LEN];
//enum mon {January,Febuary,March,April,May,June,July,August,September,October,November,December};
printf("Please enter a month(empty to quit):\n");
while(s_get(name,LEN)!=NULL && name[0]!='\0')
{
for(i=0;i<12;i++)
{
if(strcmp(name,year[i].month)==0)
{
for(n=0;n<=i;n++)
{
sumdays+=year[n].days;
}
printf("The sumdays before this month are %d days.\n",sumdays);
break;
}
}
printf("Please enter a month(empty to quit):\n");
}
puts("Bye!");
return 0;
}
char *s_get(char *st,int n)
{
char *find;
char *ret_val;
ret_val=fgets(st,n,stdin);
if(ret_val)
{
find=strchr(st,'\n');
if(find)
*find='\0';
else
while(getchar()!='\n')
continue;
}
return ret_val;
}
推荐阅读
-
《C primer plus》 Chapter 14.1
-
C++ Primer Plus chapter2
-
C Primer Plus: 第十章 数组和指针
-
C Primer Plus 第10章 数组和指针 10.3 指针和数组
-
C_primer_plus Chapter 2 Notes
-
C Primer Plus--- Chapter 14---Structures and Other Data Forms ---2. 向函数传递结构的信息
-
C Primer Plus ---- Chapter 11 ----Character Strings and String Functions ---- 2.字符串输入函数
-
C Primer Plus--- Chapter 9---Functions ---2.编译多个文件
-
C Primer plus 第六版 第二章课后习题答案
-
C Primer Plus第二章课后习题参考答案