我编写了以下控制台应用程序,要求用户输入一天。
我需要一些帮助以便改进,以便他们为一周中的所有日子提供正确的答案。
如果用户输入除星期一之外的任何其他日期,则输出为"今天","昨天","明天"各自的天数输出低于这些标题。
问题似乎只是周一没有产生正确的输出。
到目前为止,这是我的代码:
#include "stdafx.h"
#include <stdio.h>
#include <string.h>
typedef enum the_days {monday,tuesday,wednesday,thursday,friday,saturday,sunday, noday} day;
day yesterday (day today);
day tomorrow (day today);
char thedays[][10] = {"monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"};
day findDay(char string1[]);
void main(void)
{
day today;
char theDay[10];
puts("Type the day (e.g. 'monday'");
gets(theDay);
today = findDay(theDay);
if(today == noday)
{
puts("Error - invalid input - exiting");
return;
}
printf("today \tyesterday \ttomorrow\n"
"============================================\n");
printf("%s\t %s \t %s\n", thedays[today], thedays[yesterday(today)],thedays[tomorrow(today)]);
}
day findDay(char string1[])
{
int i = 0;
day thisday;
for (i=0;i<7;i++)
{
if (!strcmp(thedays[i],string1))
{
break;
}
}
thisday = (day)i;
return thisday;
}
day yesterday(day today)
{
day before;
before = (day)(today- 1);
return before;
}
day tomorrow(day today)
{
day after;
after = (day)(today + 1);
return after;
}
答案 0 :(得分:1)
周一和周日,您将收到不正确的结果,因为您的yesterday
和tomorrow
功能并未解决问题。您可以像这样修复这些功能:
day yesterday(day today)
{
day before = today > monday ? today - 1 : sunday;
return before;
}
day tomorrow(day today)
{
day after = today < sunday ? today + 1 : monday;
return after;
}