【发布时间】:2019-07-20 15:22:35
【问题描述】:
节目规格:
设计并实施一个程序,该程序将创建和使用三个 PERSON 类型的不同变量。
使用 typedef 命令为 DATE 创建一个结构。
使用以下字段为 PERSON 创建一个结构。
name [这将是一个字符串] 生日 [这将是一个 DATE] 性别 [这将是一个字符] 年度收入 [这将是浮动或 double, your choice] 创建三个 PERSON 类型的变量。创建一个 为每个人(全部 3 个)填充数据的函数。 创建一个函数,输出关于每个人的所有数据 很好的格式化方式。
数据验证:
必须验证输入的所有日期。确保您考虑到 每个月的天数,事实上正好有 12 月,每四年有一个闰年。
每个人的名字将被存储在句子大小写中。
性别可以是 M、F 或 O。
年收入将在 0 美元到 100 万美元之间。
/*Programmer: John B.*/
/*Date: 2/26/19*/
/*Program to demonstrate structs for a date and person with specified fields.*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define PAUSE system("pause")
#define CLS system("cls")
#define FLUSH nothingFlush()
void nothingFlush() {
char nothing;
while (scanf("%c", ¬hing) == NULL);
}
typedef struct {
int month;
int day;
int year;
} DATE;
typedef struct {
char name[100];
char lastName[100];
DATE dob;
char gender;
float anualIncome;
} PERSON;
//Prototype Functions
void displayDate(DATE birthday);
void displayWorkers(PERSON wOne, PERSON wTwo, PERSON wThree);
DATE getDate();
float getAnualIncome(PERSON *wOne, PERSON *wTwo, PERSON *wThree);
void getThings(PERSON *wOne, PERSON *wTwo, PERSON *wThree);
//Main
main() {
DATE birthday;
PERSON workerOne, workerTwo, workerThree;
getThings(&workerOne, &workerTwo, &workerThree);
displayWorkers(workerOne, workerTwo, workerThree);
PAUSE;
}//End Main
//Write Functions
void displayDate(DATE birthday) {
printf("\n\t%i/%i/%i\n", birthday.month, birthday.day, birthday.year);
}//End Display Date
void displayWorkers(PERSON wOne, PERSON wTwo, PERSON wThree) {
CLS;
strcpy(wOne.name, "Austin");
strcpy(wOne.lastName, "Warner");
strcpy(wTwo.name, "Lee");
strcpy(wTwo.lastName, "Cousins");
strcpy(wThree.name, "McKinley");
strcpy(wThree.lastName, "Alitolof");
printf("\n\tFirst: %s\n", wOne.name);
printf("\n\tLast: %s\n", wOne.lastName);
displayDate(wOne.dob);
printf("\n\tGender: %c\n", wOne.gender);
printf("\n\tAnual Income: %.2f\n", wOne.anualIncome);
printf("\n\tFirst: %s\n", wTwo.name);
printf("\n\tLast: %s\n", wTwo.lastName);
displayDate(wTwo.dob);
printf("\n\tGender: %c\n", wTwo.gender);
printf("\n\tAnual Income: %.2f\n", wTwo.anualIncome);
printf("\n\tFirst: %s\n", wThree.name);
printf("\n\tLast: %s\n", wThree.lastName);
displayDate(wThree.dob);
printf("\n\tGender: %c\n", wThree.gender);
printf("\n\tAnual Income: %.2f\n", wThree.anualIncome);
}//End Display Workers
float getAnualIncome(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
float result;
do {
printf("\n\tEnter The Anual Income Of The Worker: ");
scanf_s("%f", &result); FLUSH;
if (result < 0 || result > 1000000)
printf("Invalid Number--- Try Again\n");
} while (result < 0 || result > 1000000);
return result;
}
DATE getDate() {
DATE result;
do {
printf("\n\tEnter Year: ");
scanf_s("%i", &result.year); FLUSH;
if (result.year < 1900 || result.year > 5000)
printf("\tInvalid Number--- Try Again\n");
} while (result.year < 1900 || result.year > 5000);
do {
printf("\n\tEnter Month: *****Please enter single digit months as a single digit***** ");
scanf_s("%i", &result.month); FLUSH;
if (result.month < 0 || result.month > 12)
printf("\tInvalid Number--- Try Again\n");
} while (result.month < 0 || result.month > 12);
do {
printf("\n\tEnter Day: ");
scanf_s("%i", &result.day); FLUSH;
if (result.day < 1 || result.day > 31) {
printf("\tInvalid Number--- Try Again\n");
}
if (result.month == 2 || result.month == 02) { //Check for leap year
if (((result.year % 4 == 0) && result.year % 100 != 0) ||
(result.year % 400 == 0)) {
while (result.day < 1 || result.day > 29) { //Leap year feb dates 1-29
printf("\tLeap Year--- Try Again\n");
printf("\n\tEnter Day: ");
scanf_s("%i", &result.day); FLUSH;
}
}
else {
while (result.day < 1 || result.day > 28) { //Leap year feb dates 1-29
printf("\tInvalid Number--- Try Again\n");
printf("\n\tEnter Day: ");
scanf_s("%i", &result.day); FLUSH;
}
}
}
if (result.month == 4 || result.month == 04 || result.month == 6 || // Check if month has only 30 days
result.month == 06 || result.month == 9 || result.month == 09 || result.month == 11) { //Invalid Octal Digit??
while (result.day < 1 || result.day > 30) {
printf("\tInvalid Day--- Try Again\n");
printf("\n\tEnter Day: ");
scanf_s("%i", &result.day); FLUSH;
}
}
} while (result.day < 1 || result.day > 31);
return result;
}//End Get Date
char getGender(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
char result;
do {
printf("\n\tEnter The Gender For The Worker: ");
scanf_s("%c", &result); FLUSH;
if (result != 'F' && result != 'f' && result != 'M' && result != 'm' &&
result != 'O' && result != 'o')
printf("\n\tERROR-- Try Again...\n");
} while (result != 'F' && result != 'f' && result != 'M' && result != 'm' &&
result != 'O' && result != 'o');
return result;
}//End Get Gender
void getThings(PERSON *wOne, PERSON *wTwo, PERSON *wThree) {
CLS;
printf("\n\tEnter The Date Of Birth Of Austin: \n");
wOne->dob = getDate();
wOne->gender = getGender(&wOne, &wTwo, &wThree);
wOne->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);
printf("\n\tEnter The Date Of Birth Of Lee: \n");
wTwo->dob = getDate();
wTwo->gender = getGender(&wOne, &wTwo, &wThree);
wTwo->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);
printf("\n\tEnter The Date Of Birth Of McKinley: \n");
wThree->dob = getDate();
wThree->gender = getGender(&wOne, &wTwo, &wThree);
wThree->anualIncome = getAnualIncome(&wOne, &wTwo, &wThree);
}//End Get Things
所以我的程序运行得相当稳定,但由于 result.month == 09,我确实收到了一个错误,说 Invalid Octal Digit (它检查它是否只有 30 天,第 139 行)。我将个位数的月份包括为 9 和 09,以防人们以这种方式放置它们。直到我超过 7 才成为问题。如何解决此问题以允许将月份输入为 9 或 09。目前当有人输入 09 时,result.month 的值为 0 或一些奇怪的数字,它不会阻止输入数字,并允许 09 的天数为 31 天,而它应该只有 30 天。希望这足以让某人理解,我试图提供尽可能多的信息。感谢他们的帮助!
【问题讨论】:
-
去掉所有以 0 开头的数字,除非你故意表示一个八进制数。
result.month == 09在 base-8 中无效。如果你想允许前导零,你必须将输入作为文本处理并适当地解析它。 -
@RetiredNinja 写答案而不是 cmets。
-
请注意,当人们输入 8 月或 9 月的日期时,
%i可能会导致意外结果。 -
编译时启用所有警告(至少
-W3。结果可能会让您大吃一惊。 -
@Swordfish 认为它会因为错字或过于宽泛而被关闭。它也有点杂乱无章,感觉就像一个变形器。另一个人跳上了手榴弹。 :)
标签: c date struct digits octal