佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
查看: 1549|回复: 42

可以讨论下我的programming题目吗

[复制链接]
发表于 21-8-2009 08:27 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:49 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 来算年龄

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

使用道具 举报

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

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

    while(sex != 'M' &&sex != 'm' && sex != 'F' && se ...


Enter your name (50 character place provided) : a
Enter your gender (M/F) : o
An invalid input, Pls reenter : i
An invalid input, Pls reenter : o
An invalid input, Pls reenter : m
Enter your birthday date, month and years :

这是刚刚复制出来的前半output
其实我不太清楚你的意思
可以请你仔细点吗, 谢谢^^
回复

使用道具 举报

 楼主| 发表于 21-8-2009 09:00 AM | 显示全部楼层
恩谢谢你的建议^^
我再去改一改年龄的东西,
其实我我的program里面还有一些没完整的 例如名字也没有validating
可以给我一个怎样测试名字的方法吗?
我是打算使用while loop加isalpha ?不知道适合吗
回复

使用道具 举报

发表于 21-8-2009 09:06 AM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 08:57 AM 发表


Enter your name (50 character place provided) : a
Enter your gender (M/F) : o
An invalid input, Pls reenter : i
An invalid input, Pls reenter : o
An invalid input, Pls reenter : m
Enter your  ...


这是经验谈,Programming 要尽量减少 Redundant Code

-------------------------------
    //SEX
    printf("Enter your gender (M/F) : ";
    fflush(stdin);
    scanf("%c", &sex);
--------------------------------
以上的 Code 是要 User Key In Gender, 然后存在 sex 这个 Variable 里面

--------------------------------------------------------------------------------
    while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f')
    {
        printf("An invalid input, Pls reenter : ";
        fflush(stdin);
        scanf("%c", &sex);
    }
--------------------------------------------------------------------------------
这一个也有类似的功能,为甚么不把他们 Combine 起来呢?主要是 Combine Scanf 的部份
    sex = ''
    printf("Enter your gender (M/F) : "; <--- 刚刚忘了加
    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 : ";
    }

PS: Syntax 可能有错,我不是 C Guy~
回复

使用道具 举报

 楼主| 发表于 21-8-2009 09:08 AM | 显示全部楼层
恩恩
我大概明白了
就是我写了太多不需要的吗?
的确是,这是我很大的弱点,经常使用很多个statement做一个东西><
回复

使用道具 举报

Follow Us
发表于 21-8-2009 09:09 AM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 09:00 AM 发表
恩谢谢你的建议^^
我再去改一改年龄的东西,
其实我我的program里面还有一些没完整的 例如名字也没有validating
可以给我一个怎样测试名字的方法吗?
我是打算使用while loop加isalpha ?不知道适合吗


名字通常是没有 Validation 的
除非 Requirement 有明讲,比如一定要 xxx yyy zzz, 但是没有一个系统是只给酱的名字的人用的,也有人的名字 Pattern 是 xxx yyy 而已,也有人是 www xxx yyy zzz。除非你有 Database 可以对,不然是不知道怎样 Validate 的
回复

使用道具 举报

发表于 21-8-2009 09:17 AM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 09:08 AM 发表
恩恩
我大概明白了
就是我写了太多不需要的吗?
的确是,这是我很大的弱点,经常使用很多个statement做一个东西>


你还在读书吧?
有些东西是从经验来的。多多练习,没问题的。
你的 Code 不是不能用,只是通常我都会避免 Redundant Code, Redundant Code 的问题是如果 Client 要改 Requirement, 你就有两个地方要改,Maintain 起来不容易。就你的例子来看,分开来没有问题,因为 Code 不多。所以感觉不到不同。
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 21-8-2009 09:36 AM | 显示全部楼层
原帖由 eddom 于 21-8-2009 09:17 AM 发表


你还在读书吧?
有些东西是从经验来的。多多练习,没问题的。
你的 Code 不是不能用,只是通常我都会避免 Redundant Code, Redundant Code 的问题是如果 Client 要改 Requirement, 你就有两个地方要改,Mainta ...


是啊我还在读书
我刚进学院3个月而已^^
我想问你哦 你说的那个名字validation 如果题目需要 xxx yyy zzz 那我要怎样validate呢?
然后我用do while loop做gender会比较好 并且不会redundant对吗?
回复

使用道具 举报

发表于 21-8-2009 09:48 AM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 09:36 AM 发表


是啊我还在读书
我刚进学院3个月而已^^
我想问你哦 你说的那个名字validation 如果题目需要 xxx yyy zzz 那我要怎样validate呢?
然后我用do while loop做gender会比较好 并且不会redundant对吗?


通常是不会要 Validate 名字的,但如果真的要酱 Validate 的话,首先把 Input 用 '[SpaceBar]' 来做 Split, 然后 Count,看是不是3个。以下是 C# Code, 和 C 不同,只是给你一个概念。

public  Boolean ValidateName(String Name)
{
     //--- 如果Name是空的话,Return False ---//
     If (Name.trim() == String.empty)
          return false;

     String[] NameChunt = Name.trim().split(' ');

     //--- 如果Split了出来数目少过3,Return False ---//
     If (NameChunt.length < 3)
          return false;

     //--- 如果有哪个 Chunt 里的 Value 是空的话,Return False ---//
     foreach (String NameItem in NameChunt)
     {
          if (NameItem.trim() == String.empty)
               return false;
     }
}

你的 Do While 是没法避免的,因为要 Check Input 对不对。
回复

使用道具 举报

 楼主| 发表于 21-8-2009 09:52 AM | 显示全部楼层
//sex       
        do
        {
                printf("Enter your gender (M/F) : ");
                fflush(stdin);
                scanf("%c", &sex);
        }while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f');
回复

使用道具 举报

发表于 21-8-2009 09:54 AM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 09:52 AM 发表
//sex        
        do
        {
                printf("Enter your gender (M/F) : ");
                fflush(stdin);
                scanf("%c", &sex);
        }while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f');


可以跑,只是User看不到对的message,比如,如果我key in x,output 可能像酱:
Enter your gender (M/F) : X
Enter your gender (M/F) : X
Enter your gender (M/F) : X
Enter your gender (M/F) : X
Enter your gender (M/F) : F

[Continue Next Question]
回复

使用道具 举报

 楼主| 发表于 21-8-2009 09:57 AM | 显示全部楼层
是的 所以我在想 要怎样出invalid input
回复

使用道具 举报

 楼主| 发表于 21-8-2009 10:00 AM | 显示全部楼层
do
    {
        if(i >= 1)
        printf("You entered an invalid input\n");
        printf("Enter your gender (M/F) : ");
        fflush(stdin);
        scanf("%c", &sex);
        i++;
    }while(sex != 'M' &&sex != 'm' && sex != 'F' && sex != 'f');
回复

使用道具 举报

发表于 21-8-2009 11:11 AM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 10:00 AM 发表
do
    {
        if(i >= 1)
        printf("You entered an invalid input\n");
        printf("Enter your gender (M/F) : ");
        fflush(stdin);
        scanf("%c", &sex);
        i++;
    } ...


怎麼要用DO呢? DO是先做, 才VALIDATE, 還沒有INPUT就給INVALID INPUT?
然後後面那個while(sex!=M&&sex!=m......);
可以用UPPERCASE的FUCNTION
回复

使用道具 举报

 楼主| 发表于 21-8-2009 11:16 AM | 显示全部楼层
经过eddom大大的建议
变成了以下的
        printf("Enter your gender (M/F) : ");
        do
        {
                fflush(stdin);
                scanf("%c", &sex);

                if (sex == 'M' || sex == 'm' || sex == 'F' || sex == 'f')
                        Correct = 1;
                else
                        printf("Please re-enter your gender (M/F) : ");

        }while(!Correct);
接下来的问题应该是还有什么validating应该有呢
回复

使用道具 举报


ADVERTISEMENT

发表于 21-8-2009 11:25 AM | 显示全部楼层
我想指出,skip leap year
1900/4 = 475(整数)  但这不是润年,因为1900/100 = 19(整数)
但2000/4 = 500 (整数, 这年是特别年,因为2000/400 = 5 (整数)是润年的

回复

使用道具 举报

发表于 21-8-2009 11:30 AM | 显示全部楼层
原帖由 天魔神 于 21-8-2009 11:11 AM 发表


怎麼要用DO呢? DO是先做, 才VALIDATE, 還沒有INPUT就給INVALID INPUT?
然後後面那個while(sex!=M&&sex!=m......);
可以用UPPERCASE的FUCNTION


C 有 ToUpperCase 的 Function 阿?
有点老了,好久没有用 C 了~
回复

使用道具 举报

发表于 21-8-2009 11:32 AM | 显示全部楼层
原帖由 骇客小子 于 21-8-2009 11:25 AM 发表
我想指出,skip leap year
1900/4 = 475(整数)  但这不是润年,因为1900/100 = 19(整数)
但2000/4 = 500 (整数, 这年是特别年,因为2000/400 = 5 (整数)是润年的



不应该是用除法吧?
应该用 Mod
C 的 Syntax 很像是%
回复

使用道具 举报

发表于 21-8-2009 11:35 AM | 显示全部楼层
除法也能用,注意casting 就可以了
1900/4.0
回复

使用道具 举报

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

本版积分规则

 

所属分类: 欢乐校园


ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 28-4-2026 11:12 PM , Processed in 0.093401 second(s), 13 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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