| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 733 人关注过本帖
标题:[求助]这是个日历程序,运行时提示内存错误,不知道问题在哪
只看楼主 加入收藏
story
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-25
收藏
 问题点数:0 回复次数:3 
[求助]这是个日历程序,运行时提示内存错误,不知道问题在哪

/*
*File:PrintCalendar.c
*---------------------
*用户输入年份(1900年以后),自动打印出此年的日历
*/

#include<stdio.h>

#define Sunday 0
#define Monday 1
#define Tuesday 2
#define Wednesday 3
#define Thursday 4
#define Friday 5
#define Saturday 6

/*Function Prototypes*/

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);

bool IsLeapYear(int year);

/*Main Program*/

main()
{
int year;

GiveInstructions();
year = GetYearFromUser();
PrintCalendar(year);

}

/* GiveInstruction(void)给出用户此程序的功能.*/

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

/* year = GetYearFromUser()让用户输入年份.*/

int GetYearFromUser()
{
int year;

while(1 > 0) {
printf("Which year?\n");
scanf("%d", &year);
if(year >= 1900) return(year);
printf("The year must be at least 1900.\n");
}
}

/* PrintCalendar(year) 调用PrintCalendarMonth(month, year)来输出一整年的日历*/

void PrintCalendar(int year)
{
int month;

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

/* printCalendarMonth(month, year) 用来输出给定年的给定月的日历,weekday是取值在1~7的整型变量*/

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

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

/* IndentFirstLine(weekday) 使每个月的一号显示在正确的位置*/

void IndentFirstLine(int weekday)
{
int i;

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

/* MonthDays(month, year) 求出给定年的某月的天数*/

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);
}
}

/* FirstDayOfMonth(month, year)求出此年此月的一号是星期几(根据1900年1月1
日是星期一来求)*/

int FirstDayOfMonth(int month, int year)
{
int weekday, i;
weekday = Monday;

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

/* IsLeapYear(year) 判断十分为闰年*/

bool IsLeapYear(int year)
{
return ((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0);
}

搜索更多相关主题的帖子: 内存 日历 提示 运行 
2006-11-25 02:04
forever043
Rank: 1
等 级:新手上路
帖 子:37
专家分:0
注 册:2006-10-15
收藏
得分:0 
回复:(story)[求助]这是个日历程序,运行时提示内存...

/* printCalendarMonth(month, year) 用来输出给定年的给定月的日历,weekday是取值在1~7的整型变量*/
void PrintCalendarMonth(int month, int year)
{
int weekday, nDays, day;

printf(" %s %d\n", month, year);

/*
省略
*/
}

中的printf 应该是
printf(" %d %d\n", month, year);


而且你的程序好像也有点问题

[此贴子已经被作者于2006-11-25 7:38:18编辑过]


学操作系统真的好郁闷啊.........
2006-11-25 07:36
ffj0711
Rank: 1
等 级:新手上路
帖 子:4
专家分:0
注 册:2006-11-4
收藏
得分:0 
楼上说的对,还有:
void PrintCalendarMonth(int month, int year)
{
int weekday, nDays, day;

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


应该是weekday == Saturday,再在/*Function Prototypes*/前面加一行"typedef int bool",就可以了

[此贴子已经被作者于2006-11-25 11:36:41编辑过]

2006-11-25 11:35
story
Rank: 1
等 级:新手上路
帖 子:18
专家分:0
注 册:2006-11-25
收藏
得分:0 
真心感谢二楼和三楼的朋友,你们的建议对我很有帮助

等价交换
2006-11-25 15:15
快速回复:[求助]这是个日历程序,运行时提示内存错误,不知道问题在哪
数据加载中...
 
   



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

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