佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
楼主: qiqimon5566

可以讨论下我的programming题目吗

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



对啊我真的用错了应该是%4=0
可是我不太懂的是1900年为何不是润年呢?
回复

使用道具 举报


ADVERTISEMENT

发表于 21-8-2009 11:37 AM | 显示全部楼层
那你就要问天文学家了,google skip leap year
- 每年不是精准的多出1/4 天, 而是少过1/4 , 精准的 0.2422  
润过多了,就skip 一年不润年

[ 本帖最后由 骇客小子 于 21-8-2009 11:43 AM 编辑 ]
回复

使用道具 举报

发表于 21-8-2009 11:54 AM | 显示全部楼层
别小看这错误,银行发多一天的saving interest, 足够bank亏损数亿
回复

使用道具 举报

 楼主| 发表于 21-8-2009 12:08 PM | 显示全部楼层
那么又要想看什么方法了
请问有什么办法能让它%4是0 可是又不是润年的计算?
回复

使用道具 举报

发表于 21-8-2009 12:27 PM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 12:08 PM 发表
那么又要想看什么方法了
请问有什么办法能让它%4是0 可是又不是润年的计算?


Create Function 来 Determine 吧,全部 Logic 在 Single Program 很乱水下
Function 里要 Handle 一些 Exception, 但是如果你的只是 Assignment 的话,应该没问题的,相性你的 Lecturer 也不懂 骇客小子 所说的冬冬的
回复

使用道具 举报

 楼主| 发表于 21-8-2009 12:34 PM | 显示全部楼层
我刚刚才发现多一个错误。。
月份填超过12可以run==我忘记做多这个statement
回复

使用道具 举报

Follow Us
发表于 21-8-2009 12:34 PM | 显示全部楼层
加都一个 && 那么难?
回复

使用道具 举报

发表于 21-8-2009 12:37 PM | 显示全部楼层
原帖由 骇客小子 于 21-8-2009 12:34 PM 发表
加都一个 && 那么难?


如果要做来 Detect 准确的润年不只是加 && 吧,
要用数学和一些 Logic 来算的
回复

使用道具 举报


ADVERTISEMENT

发表于 21-8-2009 12:39 PM | 显示全部楼层
根据你给的资料

1900 * 0.2422 = 460.18
到1900年为止,只有多出460天

1900 / 4 = 475
而平常的算法却有多出475天

所以也就等于平常的算法导致多出了15个闰年
回复

使用道具 举报

 楼主| 发表于 21-8-2009 12:56 PM | 显示全部楼层
if(birthyear %100 == 0 && birthyear % 1000 != 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);
                }
算不到2000年的?
回复

使用道具 举报

发表于 21-8-2009 12:59 PM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 12:56 PM 发表
if(birthyear %100 == 0 && birthyear % 1000 != 0)
                {
                        printf("The year you enter doesn't has 29days in February\n"
                                   "Pls reenter your birthday date, month and years : ");
                        scanf("%d %d % ...


建议简化,做成 %4, 之前提到,lecturer 可能也不懂,所以可能会当你做错,除非你有注明。
还有,也不是单纯%100或%1000就可以的
回复

使用道具 举报

 楼主| 发表于 21-8-2009 01:23 PM | 显示全部楼层
新的source code
#include <stdio.h>
void menu();
void name();
void sex();
void birthday();
void age();
void applydate();
void salary();
void result();

int selection;
char name1[51], sex1;
int birthdate, birthmonth, birthyear, age1;
int datedate, datemonth, dateyear;
int salary1;
int Correct = 0;

void main()
{
    menu();
}

void menu()
{
    printf("------------------------------\n");
    printf("1.name\n"
           "2.gender\n"
           "3.birthday\n"
           "4.age\n"
           "5.application date\n"
           "6.starting salary\n"
           "7.show result\n"
           "Please select a selection : ");
    scanf("%d", &selection);
    printf("------------------------------\n");

    switch(selection)
    {
    case 1:
        name();
        break;
    case 2:
        sex();
        break;
    case 3:
        birthday();
        break;
    case 4:
        age();
        break;
    case 5:
        applydate();
        break;
    case 6:
        salary();
        break;
    case 7:
        result();
        break;
    default:
        printf("\n--------------------------------------------\n");
        printf("You have enter an invalid input, pls reenter\n");
        printf("--------------------------------------------\n");
        menu();
    }
}

void name()
{
    printf("Enter your name (50 character place provided) : ");
    fflush(stdin);
    scanf("%[^\n]", &name1);
    menu();
}

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

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

    }while(!Correct);
    menu();
}

void 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 > 12 && 1 < birthmonth)
    {
        printf("An invalid birthday month, Pls reenter month : ");
        scanf("%d", &birthmonth);
    }
    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);
    }
    menu();
}

void age()
{
    printf("Enter your current age : ");
    scanf("%d", &age1);
    menu();
}

void applydate()
{
    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(datemonth > 12 && 1 < datemonth)
    {
        printf("An invalid  month, Pls reenter month : ");
        scanf("%d", &datemonth);
    }
    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);
    }
    menu();
}

void salary()
{
    printf("Enter your starting salary : ");
    scanf("%d", &salary1);
    menu();
}

void result()
{
    printf("\n\n");
    printf("-----------------------------------------------------------\n");
    if(sex1 == 'M' || sex1 == 'm')
    {
        printf("Your gender is : Male\n");
    }
    else
    {
        printf("Your gender is : Female\n");
    }
    printf("Your name is   : %-50s\n", name1);   
    printf("Your currentage: %-2d\n", age1);
    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", salary1);
    printf("-----------------------------------------------------------\n\n\n");
}

output:
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 1
------------------------------
Enter your name (50 character place provided) : daniel tan kok liang
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 2
------------------------------
Enter your gender (M/F) : m
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 3
------------------------------
Enter your birthday date, month and years : 2 3 1991
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 4
------------------------------
Enter your current age : 18
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 5
------------------------------
Enter your date of application (day month and years) : 18 5 2009
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 6
------------------------------
Enter your starting salary : 1000
------------------------------
1.name
2.gender
3.birthday
4.age
5.application date
6.starting salary
7.show result
Please select a selection : 7
------------------------------


-----------------------------------------------------------
Your gender is : Male
Your name is   : daniel tan kok liang
Your currentage: 18
Your birthday  : 2  .  3 .  1991
Application day: 18 .  5 .  2009
Starting salary: 1000
-----------------------------------------------------------

[ 本帖最后由 qiqimon5566 于 21-8-2009 01:27 PM 编辑 ]
回复

使用道具 举报

发表于 21-8-2009 01:33 PM | 显示全部楼层
原帖由 qiqimon5566 于 21-8-2009 01:23 PM 发表
新的source code
#include
void menu();
void name();
void sex();
void birthday();
void age();
void applydate();
void salary();
void result();

int selection;
char name1[51], sex1;
int birt ...


越来越复杂了哦
你酱做那是不是要加 Validation 啊?
如果我没有填 Name,要不要紧阿?
回复

使用道具 举报

 楼主| 发表于 21-8-2009 01:54 PM | 显示全部楼层
是的@.@所以还是很有问题==

我现在写char name1[51] = "0", sex1 = '0';
然后validate 只要有一个=0 就不能show result ?
回复

使用道具 举报

发表于 21-8-2009 02:32 PM | 显示全部楼层
原帖由 eddom 于 21-8-2009 12:39 PM 发表
根据你给的资料

1900 * 0.2422 = 460.18
到1900年为止,只有多出460天

1900 / 4 = 475
而平常的算法却有多出475天

所以也就等于平常的算法导致多出了15个闰年


1900 以前的都乱七八糟。。。那里有那么精密
1900后已经调整,就follow 那formula
先除4 , 在除100
回复

使用道具 举报

 楼主| 发表于 21-8-2009 03:44 PM | 显示全部楼层
void menu()
{
    printf("------------------------------\n");
    printf("1.name\n"
           "2.gender\n"
           "3.birthday\n"
           "4.age\n"
           "5.application date\n"
           "6.starting salary\n"
           "7.show result\n"
           "Please select a selection : ");
    scanf("%d", &selection);
    printf("------------------------------\n");
   
    while(selection != 1 && selection != 2 && selection != 3 && selection != 4 && selection != 5 && selection != 6 && selection != 7)
    {
        printf("You have enter invalid input\n");
        printf("Pls enter the digit between 1-7 : ");
        fflush(stdin);
        scanf("%d", &selection);
    }
   
    switch(selection)
    {
    case 1:
        name();
        break;
    case 2:
        sex();
        break;
    case 3:
        birthday();
        break;
    case 4:
        age();
        break;
    case 5:
        applydate();
        break;
    case 6:
        salary();
        break;
    case 7:
        result();
        break;
    }
}

请注意青色字体 如果我没放它 我打英文字母a会一直乱loop不停止 可是放了fflush(stdin) 却不会乱loop了为何呢
回复

使用道具 举报


ADVERTISEMENT

发表于 21-8-2009 10:20 PM | 显示全部楼层
那好像是拿来clear buffer的...
因为你要capture的是integer却输入character,
然后不知道什么原因那个character还逗留在buffer然后a的ASCII又是63(好像),
所以就一直loop,我猜的,不要信

然后。。你的whilte loop有点怪。。为什么要那么多个&&呢?
whilte(selection<1 || selection>7)
酱不是可以了咯...
回复

使用道具 举报

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

使用道具 举报

发表于 22-8-2009 04:23 PM | 显示全部楼层
isalpha(name[51]) 是测试char array的index 51是不是alphabet...
可是我看到的code,name的array size好像只有50..就是index49。。做么你要测试index 51咧。。。

还有其实while(isalpha(name[51]) != 1)可以写成while(!isalpha(name[51]))。。
我觉得比较好看
回复

使用道具 举报

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

本版积分规则

 

所属分类: 欢乐校园


ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


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

GMT+8, 29-4-2026 08:52 AM , Processed in 0.090753 second(s), 10 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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