这个源程序在vc6.0编译很正常,但为什么到了TC上就出现很多错误呢?哪位大侠提供帮助,多谢了! #include <stdio.h> #include <stdlib.h> bool exit_process(); void print_menu(int c); bool isLeapYear(int year); void LeapYear(int year); int GetWeek(int year,int month,int day); void printWeek(int year,int month,int day); void printCalendar(int year); int GetDaysOfYear(int year,int month,int day); void Calendar(int year,int month,int weekDay); void main(){ //choice变量用于存储用户操作参数。 int choice,year,month,day; /*print_menu(n) n为参数 意义如下: 0 打印主菜单; 1. 查询某年某月某日是星期几操作菜单 2. 查询某年是否是闰年操作菜单 3. 打印某年的全年日历操作菜单 */ print_menu(0); /*scanf() 函数返回读取的记录数,如果读取记录数为0说明 读取失败,原因可能是输入的字符不合法,这里要求字符为 数字,如果输入为字符,则读取失败退出循环。 如果忽略错误可以考虑下面的程序段 */ while(true){ scanf("%d",&choice); switch(choice){ case 1: print_menu(1); scanf("%d,%d,%d",&year,&month,&day); printWeek(year,month,day); break; case 2: print_menu(2); scanf("%d",&year); LeapYear(year); break; case 3: print_menu(3); scanf("%d",&year); printCalendar(year); break; case 4: print_menu(4); if(exit_process()) exit(0); else print_menu(0); break; default: //去掉不合法的字符 getchar(); print_menu(0); } } //如果不忽略错误可以使用下面的程序段 /*while(scanf("%d",&choice)){ switch(choice){ case 1: print_menu(1); scanf("%d,%d,%d",&year,&month,&day); printWeek(year,month,day); break; case 2: print_menu(2); scanf("%d",&year); LeapYear(year); break; case 3: print_menu(3); scanf("%d",&year); printCalendar(year); break; case 4: print_menu(4); if(exit_process()) exit(0); else print_menu(0); break; } }*/ printf("输入不正确\n"); }
//菜单函数 void print_menu(int c){ switch(c){ case 0: //system(str) 为系统调用,这里调用cmd的cls清屏命令 system("cls"); printf("1. 查询某年某月某日是星期几\n2. 查询某年是否是闰年\n3. 打印某年的全年日历\n4. 退出\n"); break; case 1: system("cls"); printf("请输入年月日(XXXX,XX,XX)\n"); break; case 2: system("cls"); printf("请输入要查哪一年?(XXXX)\n"); break; case 3: system("cls"); printf("请输入要打印的年份(XXXX)\n"); break; case 4: system("cls"); printf("是否要真的退出(Y/N)?\n"); break; default: system("cls"); printf("1. 查询某年某月某日是星期几\n2. 查询某年是否是闰年\n3. 打印某年的全年日历\n4. 退出\n"); break; } } //判断是否闰年函数 bool isLeapYear(int year){ if(!(year % 400)) return true; else if(!(year % 100)) return false; else if(!(year % 4)) return true; else return false; } //获取距年初天数 int GetDaysOfYear(int year,int month,int day){ bool leapYear = false; int days = 0; leapYear = isLeapYear(year); /* 注意:下面每条case下均未加break语句 举例说明, 若月份为4则 switch(month - 1)匹配到case 3: 执行case 3:下的语句 days += 31; 由于没有break语句,所以执行完这句后继续执行下面一段, case 2: if(leapYear) days += 29; else days += 28; 任旧没有break继续执行下面一段, case 1: days += 31; case 0: days += 0; 最终得出结果保存在days中 */ switch(month - 1){ case 12: days += 31; case 11: days += 30; case 10: days += 31; case 9: days += 30; case 8: days += 31; case 7: days += 31; case 6: days += 30; case 5: days += 31; case 4: days += 30; case 3: days += 31; case 2: if(leapYear) days += 29; else days += 28; case 1: days += 31; case 0: days += 0; } days += day; return days; } //判断闰年 void LeapYear(int year){ char choice; if(isLeapYear(year)) printf("%d 年是闰年,是否继续查询(Y/N)?\n",year); else printf("%d 年不是闰年,是否继续查询(Y/N)?\n",year); //getchar()读取多余的回车符 getchar();
if(scanf("%c*",&choice)) if(choice == 'y' || choice == 'Y'){ print_menu(2); scanf("%d",&year); LeapYear(year); } else print_menu(0); }
void printWeek(int year,int month,int day){ char choice; printf("%d年%d月%d日是星期",year,month,day); switch(GetWeek(year,month,day)) { case 0: printf("日"); break; case 1: printf("一"); break; case 2: printf("二"); break; case 3: printf("三"); break; case 4: printf("四"); break; case 5: printf("五"); break; case 6: printf("六"); } printf(",是否继续查询(Y/N)?"); getchar();
if(scanf("%c*",&choice)) if(choice == 'y' || choice == 'Y'){ print_menu(1); scanf("%d,%d,%d",&year,&month,&day); printWeek(year,month,day); } else print_menu(0); }
//判断星期几函数 int GetWeek(int year,int month,int day){ int daysOfYear=0,S=0; float X; daysOfYear = GetDaysOfYear(year,month,day); //将年份转换为float类型减少运算精度丢失 X = (float)year; //经验证原计算公式第一个减号改为加号结果正确 S = (X + 1)+(X-1)/4+(X-1)/100+(X-1)/400+ daysOfYear;
//以下为 基姆拉尔森计算公式 /*if(month == 1 || month == 2) month += 12; S = day+2*month+3*(month+1)/5+year+year/4-year/100+year/400 + 1; */ return S % 7; }
//退出函数 bool exit_process(){ char choice; //getchar()读取多余的回车符 getchar(); if(!scanf("%c*",&choice)) return false; else { if(choice == 'y' || choice == 'Y') return true; else return false; } } void Calendar(int year,int month,int weekDay){ bool leapYear = isLeapYear(year); int i = 0,SevenCount = 7; int monthDayArr[]={31,28,31,30,31,30,31,31,30,31,30,31}; for (i = 0;i<monthDayArr[month-1];i++){ if(weekDay == 0) printf("%d \t",i+1); else{ i--; printf(" \t"); weekDay--; } SevenCount --; if(SevenCount == 0){ printf("\n"); SevenCount = 7; } } printf("\n"); } void printCalendar(int year){ int i = 1,weekDay = 0; char choice; for (i = 1;i<=12;i++){ printf("\t\t\t\t%d\n\t\t\t\t%d\n",year,i); printf("0\t1\t2\t3\t4\t5\t6\nS\tM\tT\tW\tT\tF\tS\n"); weekDay = GetWeek(year,i,1); Calendar(year,i,weekDay); } printf("是否继续打印(Y/N)?"); getchar(); if(scanf("%c*",&choice)) if(choice == 'y' || choice == 'Y'){ print_menu(3); scanf("%d",&year); printCalendar(year); } else print_menu(0); }
----------------解决方案--------------------------------------------------------