| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 626 人关注过本帖
标题:编译运行都可以了,但为什么输入时候会中止??,
只看楼主 加入收藏
cxhiou
Rank: 1
等 级:新手上路
帖 子:97
专家分:0
注 册:2007-6-13
收藏
 问题点数:0 回复次数:3 
编译运行都可以了,但为什么输入时候会中止??,
[localimg=400,217]1[/localimg]该程序作用是显示某年的全年的月历,输入年份在1900以后,为什么运行后会出现中止??如图:

代码全部如下:
#include<stdio.h>
//#include<string.h>
/*
#define Sunday    0;
#define Monday    1;
#define Tuesday   2;
#define Wednesday 3;
#define Thursday  4;
#define Friday    5;
#define Saturday  6;
*/
void GiveInstructions(void);
int GetYearFromUser(void);
void PrintCalendar(int year);
void PrintCalendarMonth(int month, int year);
void IndentFirstLine(int weekday);
int MonthDays(int month, int year);
int FirstDayOfMonth(int month , int year);
//char MonthName(int month);
int IsLeapYear(int year);


/*  main program begin from here */
void main()
{
    int year;
    
    GiveInstructions();
    year=GetYearFromUser();
    PrintCalendar(year);


}

void GiveInstructions(void)
{
    printf("This program display a calendar for a full\n");
    printf("year. The year must not be before 1900.\n");
}


int GetYearFromUser(void)
{
    int year;

    while(1)
    {
        printf("which year?  ");
        scanf("%d",&year);
        if(year>=1900) return(year);

        else printf("the year must be at least 1900!\n");
    
    }
}



void PrintCalendar(int year)
{
    int month;
    
    for(month=1; month <=12; month++)
    {
        PrintCalendarMonth(month, year);
        printf("\n");
    
    }
  
}


void PrintCalendarMonth(int month, int year)
{
    int weekday, nDay, day;

    printf("    %s %d\n",month, year);
    printf(" Su Mo Tu We Th Fr Sa\n");
    nDay = MonthDays(month, year);
    weekday = FirstDayOfMonth(month , year);
    IndentFirstLine(weekday);
    for(day = 1; day<=nDay; day++)
    {
        printf(" %2d",day);
        if(weekday == 6)
            printf("\n");
        weekday = (weekday + 1) % 7;

    }
    if(weekday != 0)
        printf("\n");
}

void IndentFirstLine(int weekday)
{
    int i;
    for(i = 0;i < weekday; i++)
    {printf("   ");}
}



int MonthDays(int month, int year)
{
    switch (month)
    {
    case 2: if (IsLeapYear(year))  return (29);
                return (28);
    case 4: case 6: case 9: case 11:
        return(30);
    default : return (31);
    
    
    }
}


int FirstDayOfMonth(int month , int year)
{
    int weekday, i;
    
    weekday = 1;
    for(i = 1900; i < year; i++)
    {
        weekday = (weekday + 365) % 7;
        if(IsLeapYear(year)) weekday = (weekday + 1) % 7;
    }
    for(i = 1; i < month ; i++)
    {
        weekday = (weekday + MonthDays(i,  year)) % 7;
    }
    return (weekday);
}


/*char MonthName(int month)
{
    switch(month)
    {
    case 1: return ("January");
        case 2: return ("February");
            case 3: return ("March");
                case 4: return ("April");
                    case 5: return ("June");
                        case 6: return ("July");
                            case 7: return ("August");
                                case 8: return ("September");
                                    case 9: return ("October");
                                        case 10: return ("November");
                                            case 11: return ("November");
                                                case 12: return ("December");
                                                    default: return ("Illegal month!");
    
    }
}*/


int IsLeapYear(int year)
{
    if ( ((year % 4 == 0) && (year % 100 !=0)) || (year % 400 == 0) )  
        return (1);
    else return (0);
}
希望大家可以帮下我,这个问题已经困扰我很久了,我换了编译器也不行啊!!
搜索更多相关主题的帖子: void define int 编译 year 
2008-04-03 14:28
jamesbind
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2008-3-21
收藏
得分:0 
void PrintCalendarMonth(int month, int year)
{
    int weekday, nDay, day;

    printf("    %s %d\n",month, year);//-------------->%d %d
    printf(" Su Mo Tu We Th Fr Sa\n");
    nDay = MonthDays(month, year);
    weekday = FirstDayOfMonth(month , year);
    IndentFirstLine(weekday);
    for(day = 1; day<=nDay; day++)
    {
        printf(" %2d",day);
        if(weekday == 6)
            printf("\n");
        weekday = (weekday + 1) % 7;

    }
    if(weekday != 0)
        printf("\n");
}



输出结果好像不太对啊。
2008-04-03 14:50
jamesbind
Rank: 1
等 级:新手上路
帖 子:45
专家分:0
注 册:2008-3-21
收藏
得分:0 
void PrintCalendarMonth(int month, int year)
{
    int weekday, nDay, day;

    printf("    %s %d\n",month, year);//-------------->%d %d
    printf(" Su Mo Tu We Th Fr Sa\n");
    nDay = MonthDays(month, year);
    weekday = FirstDayOfMonth(month , year);
    IndentFirstLine(weekday);
    for(day = 1; day<=nDay; day++)
    {
        printf(" %2d",day);
        if(weekday == 6)
            printf("\n");
        weekday = (weekday + 1) % 7;

    }
    if(weekday != 0)
        printf("\n");
}



输出结果好像不太对啊。
2008-04-03 14:52
cosdos
Rank: 9Rank: 9Rank: 9
来 自:ShangHai
等 级:蜘蛛侠
威 望:6
帖 子:2109
专家分:1385
注 册:2007-6-19
收藏
得分:0 
“case” 为什么这样缩进?

—>〉Sun〈<—
2008-04-03 18:35
快速回复:编译运行都可以了,但为什么输入时候会中止??,
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016931 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved