佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1039|回复: 5

可以讨论下我的题目吗

[复制链接]
发表于 21-8-2009 08:26 AM | 显示全部楼层 |阅读模式
Write a program that asks users for their name, birth date (month, day, and year), current age, date of application (month, day and year), sex(M/F) and starting salary.
Perform as many validation checks as u can think of to make sure that complete and accurate records are created
以上是题目
刚开始接触programming 3个月
以下是我自己写的

#include <stdio.h>
#include <ctype.h>

void main()
{
    int birthyear, birthdate, birthmonth, age;
    int dateyear, datemonth, datedate, salary;
    char name[51], sex;

    //NAME
    printf("Enter your name (50 character place provided) : ");
    fflush(stdin);
    scanf("%[^\n]", &name);

    //SEX
    printf("Enter your gender (M/F) : ");
    fflush(stdin);
    scanf("%c", &sex);

    while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f')
    {
        printf("An invalid input, Pls reenter : ");
        fflush(stdin);
        scanf("%c", &sex);
    }

    //BIRTHDAY
    printf("Enter your birthday date, month and years : ");
    scanf("%d %d %d", &birthdate, &birthmonth, &birthyear);
    while(birthmonth == 2 && birthdate == 29)
    {
        if(birthyear / 4 != 0)
            printf("The year you enter doesn't has 29days in February\n"
                   "Pls reenter your birthday date, month and years : ");
            scanf("%d %d %d", &birthdate, &birthmonth, &birthyear);
    }
    while(birthdate > 31 && 1 < birthdate)
    {
        printf("An invalid birthday date, Pls reenter day : ");
        scanf("%d", &birthdate);
    }
    while(birthmonth == 2 && birthdate > 29)
    {
        printf("February only has 29days, Pls reenter month : ");
        scanf("%d", &birthmonth);
    }
    while(birthmonth == 4 && birthdate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your birhtday date and month :");
        scanf("%d %d", &birthdate, &birthmonth);
    }
    while(birthmonth == 6 && birthdate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your birhtday date and month :");
        scanf("%d %d", &birthdate, &birthmonth);
    }
    while(birthmonth == 9 && birthdate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your birhtday date and month :");
        scanf("%d %d", &birthdate, &birthmonth);
    }
    while(birthmonth == 11 && birthdate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your birhtday date and month :");
        scanf("%d %d", &birthdate, &birthmonth);
    }

    //current age
    printf("Enter your current age : ");
    scanf("%d", &age);

    //starting salary
    printf("Enter your starting salary : ");
    scanf("%d", &salary);

    //date of application
    printf("Enter your date of application (day month and years) : ");
    scanf("%d %d %d", &datedate, &datemonth, &dateyear);
    while(datemonth == 2 && datedate == 29)
    {
        if(dateyear / 4 != 0)
            printf("The year you enter doesn't has 29days in February\n"
                   "Pls reenter your birthday date, month and years : ");
            scanf("%d %d %d", &datedate, &datemonth, &dateyear);
    }
    while(datedate > 31 && 1 < datedate)
    {
        printf("An invalid date, Pls reenter day : ");
        scanf("%d", &datedate);
    }
    while(datemonth == 2 && datedate > 29)
    {
        printf("February only has 29days, Pls reenter month : ");
        scanf("%d", &datemonth);
    }
    while(datemonth == 4 && datedate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your application date and month :");
        scanf("%d %d", &datedate, &datemonth);
    }
    while(datemonth == 6 && datedate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your application date and month :");
        scanf("%d %d", &datedate, &datemonth);
    }
    while(datemonth == 9 && datedate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your application date and month :");
        scanf("%d %d", &datedate, &datemonth);
    }
    while(datemonth == 11 && datedate > 30)
    {
        printf("The month you enter doesnt has 31days\n"
               "Pls reenter your dateday date and month :");
        scanf("%d %d", &datedate, &datemonth);
    }

    //OUTPUT
    printf("\n\n");
    printf("-----------------------------------------------------------\n");
    if(sex == 'M')
    {
        printf("Your gender is : Male\n");
    }
    else
    {
        printf("Your gender is : Female\n");
    }
    printf("Your name is   : %-50s\n", name);   
    printf("Your currentage: %-2d\n", age);
    printf("Your birthday  : %-2d . %2d . %5d\n", birthdate, birthmonth, birthyear);
    printf("Application day: %-2d . %2d . %5d\n", datedate, datemonth, dateyear);
    printf("Starting salary: %-2d\n", salary);
    printf("-----------------------------------------------------------\n\n\n");
}

以上的programming 有些validating尚未完整
我希望找人讨论下 有什么是我还可以加 可是还没有加的呢?
顺带一提,我用了半至一小时的时间写这program 不知道会不会太长了呢

[ 本帖最后由 qiqimon5566 于 21-8-2009 08:33 AM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 21-8-2009 08:52 AM | 显示全部楼层
没看完,但是我觉得有Redundant的感觉
不能把

    //SEX
    printf("Enter your gender (M/F) : ";
    fflush(stdin);
    scanf("%c", &sex);

    while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f')
    {
        printf("An invalid input, Pls reenter : ";
        fflush(stdin);
        scanf("%c", &sex);
    }

combine起来么?
    sex = '';
    while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f')
    {
        fflush(stdin);
        scanf("%c", &sex);
        if (sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f')
               printf("An invalid input, Pls reenter : ";
    }

年龄应该算出来,Compare With Current Date
建议做个 Function 来算年龄

PS: 你的帖也Redundant哦~
回复

使用道具 举报

发表于 21-8-2009 10:59 AM | 显示全部楼层
在collect date的部分
可以在简易一点
先check month den only date
example: if month is either (4,6,9,11) den only check date
不然很多duplicate code
回复

使用道具 举报

发表于 21-8-2009 11:54 PM | 显示全部楼层
  1. bool IsDateValid(unsigned int nDay, unsigned int nMonth, unsigned int nYear)
  2. {
  3.         unsigned int DayLimit[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
  4.         if (!(nYear % 4))
  5.                 ++DayLimit[1];

  6.         return ((nMonth <= 12) && (nDay <= DayLimit[--nMonth]));
  7. }
复制代码
其实不用每次都要以运算来决定程序运行的。。上面就是一个例子来表示合理的'hardcode' / constant 可以达到更好的效率

[ 本帖最后由 yeenfei 于 21-8-2009 11:55 PM 编辑 ]
回复

使用道具 举报

 楼主| 发表于 22-8-2009 07:13 AM | 显示全部楼层
#include<stdio.h>
#include<ctype.h>

void main()
{
        char name[50], gender;
        int month, day, year, age, Amonth, Aday, Ayear, i;
        double salary;
       

        for(i=0 ; i<50 ; i++)
        name = 0;

        printf("Enter your name > ");
        gets(name);
        for(i=0 ; i<50 ; i++)
        {
                if(name[0]==0)
                {
                        for(i=0 ; i<50 ; i++)
                        name = 0;

                        printf("No input detected. Please re-enter your name > ");
                        fflush(stdin);
                        gets(name);
                        i = -1;
                        continue;
                }
                if(isalpha(name)== 0 && name!= ' ' && name != 0)
                {       
                        for(i=0 ; i<50 ; i++)
                        name = 0;

                        printf("Invalid input. Please re-enter your name > ");
                        fflush(stdin);
                        gets(name);
                        i = -1;
                        continue;
                }
        }

        printf("Enter your birth date\n");
        rintf("Year (1900-2008) > ");
        fflush(stdin);
        scanf("%d", &year);
        while(year < 1900 || year > 2008)
        {
                printf("Invalid input. Please re-enter your year(1900-2008) > ");
                fflush(stdin);
                scanf("%d", &year);
        }
        printf("Month > ");
        fflush(stdin);
        scanf("%d", &month);
        while (month < 1 || month > 12)
        {
                printf("Invalid input. Please re-enter your month > ");
                fflush(stdin);
                scanf("%d", &month);
        }
        printf("Day   > ");
        fflush(stdin);
        scanf("%d", &day);
        if(month == 2)
        {
                if(year % 4 == 0)
        {
                        while (day < 1 || day > 29)
                        {
                                printf("Invalid input. Please re-enter your day > ");
                                fflush(stdin);
                                scanf("%d", &day);
                        }
                }
                else
                {
                while (day < 1 || day > 28)
                        {
                                printf("Invalid input. Please re-enter your day > ");
                                fflush(stdin);
                                scanf("%d", &day);
                        }
                }
        }
        else if(month==1||month==3||month==5||month==7||month==8||month==10||month==12)        //This statement is for months that has 31 days
        {
                while (day < 1 || day > 31)
                {
                        printf("Invalid input. Please re-enter your day > ");
                        fflush(stdin);
                        scanf("%d", &day);
                }
        }
        else
        {
                while (day < 1 || day > 30)
                {
                        printf("Invalid input. Please re-enter your day > ");
                        fflush(stdin);
                        scanf("%d", &day);
                }
        }

        printf("Enter your current age > ");
        fflush(stdin);
        scanf("%d", &age);
        while (age != 2009-year)
        {
                printf("Your age doesn't match your year of birth. Please re-enter > ");
                fflush(stdin);
                scanf("%d", &age);
        }

        printf("Enter your date of application\n");
        printf("Year(1900-2008)  > ");
        fflush(stdin);
        scanf("%d", &Ayear);
        while(Ayear < year || Ayear > 2008)                //I dont allow users to key in year that older than their birth year
        {
                printf("Invalid input. Please re-enter your year(1900-2008) > ");
                fflush(stdin);
                scanf("%d", &Ayear);
        }
        printf("Month > ");
        fflush(stdin);
        scanf("%d", &Amonth);
        while (Amonth < 1 || Amonth > 12)
        {
                printf("Invalid input. Please re-enter your month > ");
                fflush(stdin);
                scanf("%d", &Amonth);
        }
        printf("Day   > ");
        fflush(stdin);
        scanf("%d", &Aday);
        if(Amonth == 2)
        {
                if(year % 4 == 0)
        while (Aday < 1 || Aday > 29)
                        {
                                printf("Invalid input. Please re-enter your day > ");
                                fflush(stdin);
                                scanf("%d", &Aday);
                        }
                }
                else
                {
                        while (Aday < 1 || Aday > 28)
                        {
                                printf("Invalid input. Please re-enter your day > ");
                                fflush(stdin);
                                scanf("%d", &Aday);
                        }
                }
        }
        else if(Amonth==1||Amonth==3||Amonth==5||Amonth==7||Amonth==8||Amonth==10||Amonth==12)        //This statement is for months that has 31 days
        {
                while (Aday < 1 || Aday > 31)
                {
                        printf("Invalid input. Please re-enter your day > ");
                        fflush(stdin);
                        scanf("%d", &Aday);
                }
        }
        else
        {
                while (Aday < 1 || Aday > 30)
                {
                        printf("Invalid input. Please re-enter your day > ");
                        fflush(stdin);
                        scanf("%d", &Aday);
                }
        }
       
        rintf("Enter your gender(M/F) > ");
        fflush(stdin);
        scanf("%c", &gender);
        while(gender != 'M' && gender != 'm' && gender != 'F' && gender != 'f')
        {
                printf("Invalid input. Please re-enter your gender(M/F) > ");
                fflush(stdin);
                scanf("%c", &gender);
        }

        printf("Enter your starting salary > ");
        fflush(stdin);
        scanf("%lf", &salary);
        while (salary <= 0)
        {
                printf("Invalid input. Please re-enter your salary > ");
                fflush(stdin);
                scanf("%lf", &salary);
        }

        printf ("\t------------------------------------------------------------------\n"
                        "\t|  Name : %-50s     |\n"
                        "\t|  Birth day : %2d - %2d - %4d                                    |\n"
                        "\t|  Age : %3d                                                     |\n"
                        "\t|  Date of application : %2d - %2d - %4d                          |\n"
                        "\t|  Gender : %c                                                    |\n"
                        "\t|  Starting Salary : RM%-17.2f                         |\n"
                        "\t------------------------------------------------------------------\n"
                        ,name,day,month,year,age,Aday,Amonth,Ayear,toupper(gender),salary);
}

这是我一个同学写的,好像超越我的好多吧?
回复

使用道具 举报

 楼主| 发表于 22-8-2009 07:36 AM | 显示全部楼层
请问 while(isalpha(name[51]) != 1) 这个statement有错吗?
还是请问怎样检测letter? 为何我都走不了呢?
回复

使用道具 举报

Follow Us
您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 7-12-2025 06:35 AM , Processed in 0.107299 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表