#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
enum DayOfWeek {
Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, ErrDOW=-1
};
static const char *nameOfDOW[] = {
"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"
};
enum DayOfWeek strToDOW(const char *str);
int main(void){
char in_str[16];
enum DayOfWeek aDOW;
int wday, addDay, n;
printf("Please enter the name day of the week : ");
scanf(" %15s", in_str);
if(ErrDOW==(aDOW = strToDOW(in_str))){
printf("invalid name\n");
return EXIT_FAILURE;
}
printf("Please enter the number of days to add : ");
scanf("%d", &addDay);
n= (aDOW + addDay) % 7;
printf("%d\n", n);
printf("%c%s\n", toupper(nameOfDOW[n][0]), nameOfDOW[n]+1);
return EXIT_SUCCESS;
}
enum DayOfWeek strToDOW(const char *str){
char *s = strlwr(strdup(str));//note : strdup and strlwr are not standard.
int i, len = strlen(s);
for(i=Sunday; i<= Saturday; ++i){
if(3 == len){
if(strncmp(nameOfDOW[i], s, 3)==0)
break;
} else {
if(strcmp(nameOfDOW[i], s)==0)
break;
}
}
free(s);
if(i > Saturday)
return ErrDOW;
else
return i;
}
不看書? – 2014-09-27 12:30:11
你需要先學習'C'的基本知識。從這裏良好的開始http://www.tutorialspoint.com/cprogramming/index.htm – 2014-09-27 12:32:26
我的問題是,如何給字符串賦值,假設m給出輸入爲星期一,ab爲7,結果將爲0,但它不是geting – 2014-09-27 12:32:39