程序规范:
设计并实施一个程序,该程序将创建并使用三个 类型不同的变量。
使用typedef命令在DATE创建一个结构。
使用以下字段为PERSON创建结构。
姓名[这将是一个字符串]生日[这将是一个DATE]性别 [这将是一个字符] AnnualIncome [这将是浮动或 翻倍,由您选择]创建三个PERSON类型的变量。创建一个 该函数可填充每个人(全部3个人)的数据。 创建一个函数,以输出有关每个人的所有数据 很好的格式化方式。
数据验证:
所有输入的日期必须经过验证。确保您考虑了 每个月的天数,实际上恰好有12天 每个月都有一个four年。
每个人的姓名将以大写形式存储。
性别将是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(它检查是否只有一个30天的月份,第139行),所以我确实收到一条错误消息,指出八进制数字无效。如果有人以这种方式输入,我将9和09都包含个位数月份。直到我达到7以上,这才成为问题。如何解决这个问题,以允许将月份输入为9或09。当前,当有人输入09时,result.month的值为0或一些奇怪的数字,它不会阻止输入数字,并允许09的天数应该是31时才应该是30。希望这一点足够清楚,以便其他人理解,我尝试提供尽可能多的信息。感谢他们的帮助!
答案 0 :(得分:0)
0
中的result.month == 09
开始为八进制整数常量,而其余的(9
)不是八进制。因此,编码错误。请改用result.month == 9
。
将scanf_s("%i",...
更改为scanf_s("%d",...
以仅读取输入的小数。
可能存在其他问题。