当前位置:编程学习 > C/C++ >>

C语言问题求教

#include <stdio.h>
#include <stdlib.h>

int judge_year(int year) /*判断是否闰年*/
{if(year%4==0&&year%100==0||year%400==0) 
{printf("这是闰年\n");return 1;}
else {printf("这不是闰年\n");return 2;}
}

int the_days_of_year(int year,int month,int day)/*求日子数*/
{int n;
 switch(month)
{case 1:n=day;
  return n;
 case 2:n=31+day;
  return n;
 case 3:{if(year%4==0&&year%100==0||year%400==0)
         n=60+day;
       else n=59+day;}
  return n;
 case 4:{if(year%4==0&&year%100==0||year%400==0)
         n=91+day;
      else n=90+day;}
  return n;
 case 5:{if(year%4==0&&year%100==0||year%400==0)
         n=121+day;
      else n=120+day;}
  return n;
 case 6:{if(year%4==0&&year%100==0||year%400==0)
         n=152+day;
      else n=151+day;}
  return n;
 case 7:{if(year%4==0&&year%100==0||year%400==0)
         n=182+day;
      else n=181+day;}
  return n;
 case 8:{if(year%4==0&&year%100==0||year%400==0)
         n=213+day;
      else n=212+day;}
  return n;
 case 9:{if(year%4==0&&year%100==0||year%400==0)
         n=244+day;
      else n=243+day;}
  return n;
 case 10:{if(year%4==0&&year%100==0||year%400==0)
         n=274+day;
      else n=273+day;}
  return n;
 case 11:{if(year%4==0&&year%100==0||year%400==0)
         n=305+day;
      else n=304+day;}
  return n;
 case 12:{if(year%4==0&&year%100==0||year%400==0)
         n=335+day;
      else n=334+day;}
  return n;
}}

 

void chanian()/*查询项1*/
{   int s;
 int year,month,day;
    int k;
    printf("1.判断是否闰年\n2.查看该是是该年的第几天\n3.查看这是第几周\n4.返回主菜单");
    printf("输入你的选择:");scanf("%d",&s);
switch(s)
{   case 1:
{printf("输入年月日,格式如:2012,1,23:");
scanf("%d,%d,%d",&year,&month,&day);
judge_year(year);}
    case 2:
  {printf("输入年月日,格式如:2012,1,23:");
 scanf("%d,%d,%d",&year,&month,&day);
 k=the_days_of_year(year,month,day);
 printf("该日是%d天\n",k);}
    case 3:
  {printf("输入年月日,格式如:2012,1,23:");
      scanf("%d,%d,%d",&year,&month,&day);
      k=the_days_of_year(year,month,day);
         if(k%7==0) printf("这一周是第%d周",k/7);
else printf("这一周是第%d周",k/7+1);}
case 4:exit(0);
default:printf("输入有误");
}}
char *month[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char *week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int month_day(int year,int month)  
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(judge_year(year)&&month==2)
return 29;
else
return(mon_day[month-1]);
}
 
int PrintAllYear(int year)/*打印全年月份表,查询项2*/
{
int temp;
int i,j;
printf("\n\n%d Calander\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month[i-1],i);
printf("0    1    2    3    4    5    6    \n");
printf("S    M    T    W    T    F    S    \n\n");
temp=the_days_of_year(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf("     ");
else if(j-temp<10)
printf("%d    ",j-temp);
else
printf("%d   ",j-temp);

if(j%7==0)
printf("\n");
}
}
return 0;
}

void main()
{int year,month,day;

int a;while(1)
{printf("选择你要选择的选项:\n1.查询日期\n2.查看万年历\n3.结束\n");
printf("请输入你的选择");
scanf("a",&a);
switch(a)
{case 1:while(1)
{chanian();}
 case 2:while(1)
  {printf("输入年月日,格式如2012,1,23");
        scanf("%d,%d,%d",&year,&month,&day);
     PrintAllYear(year);}
 case 3:break;
 default:("输入有误");}
}}

这是源代码,为什么得不到我要的功能?

追问:你有试过调试吗?

答案:楼主仔细看main函数,  switch 里 的case分支,  case 1  应该改为  case '1',相应的 2 和3  也得改,下面是改正了的,错误的地方后面加注释了。

#include <stdio.h>
#include <stdlib.h>

int judge_year(int year) /*判断是否闰年*/
{if(year%4==0&&year%100==0||year%400==0) 
{printf("这是闰年\n");return 1;}
else {printf("这不是闰年\n");return 2;}
}

int the_days_of_year(int year,int month,int day)/*求日子数*/
{int n;
 switch(month)
{case 1:n=day;
  return n;
 case 2:n=31+day;
  return n;
 case 3:{if(year%4==0&&year%100==0||year%400==0)
         n=60+day;
       else n=59+day;}
  return n;
 case 4:{if(year%4==0&&year%100==0||year%400==0)
         n=91+day;
      else n=90+day;}
  return n;
 case 5:{if(year%4==0&&year%100==0||year%400==0)
         n=121+day;
      else n=120+day;}
  return n;
 case 6:{if(year%4==0&&year%100==0||year%400==0)
         n=152+day;
      else n=151+day;}
  return n;
 case 7:{if(year%4==0&&year%100==0||year%400==0)
         n=182+day;
      else n=181+day;}
  return n;
 case 8:{if(year%4==0&&year%100==0||year%400==0)
         n=213+day;
      else n=212+day;}
  return n;
 case 9:{if(year%4==0&&year%100==0||year%400==0)
         n=244+day;
      else n=243+day;}
  return n;
 case 10:{if(year%4==0&&year%100==0||year%400==0)
         n=274+day;
      else n=273+day;}
  return n;
 case 11:{if(year%4==0&&year%100==0||year%400==0)
         n=305+day;
      else n=304+day;}
  return n;
 case 12:{if(year%4==0&&year%100==0||year%400==0)
         n=335+day;
      else n=334+day;}
  return n;
}}

 

void chanian()/*查询项1*/
{   int s;
 int year,month,day;
    int k;
    printf("1.判断是否闰年\n2.查看该是是该年的第几天\n3.查看这是第几周\n4.返回主菜单");
    printf("输入你的选择:");scanf("%d",&s);
switch(s)
{   case 1:
{printf("输入年月日,格式如:2012,1,23:");
scanf("%d,%d,%d",&year,&month,&day);
judge_year(year);}
    case 2:
  {printf("输入年月日,格式如:2012,1,23:");
 scanf("%d,%d,%d",&year,&month,&day);
 k=the_days_of_year(year,month,day);
 printf("该日是%d天\n",k);}
    case 3:
  {printf("输入年月日,格式如:2012,1,23:");
      scanf("%d,%d,%d",&year,&month,&day);
      k=the_days_of_year(year,month,day);
         if(k%7==0) printf("这一周是第%d周",k/7);
else printf("这一周是第%d周",k/7+1);}
case 4:exit(0);
default:printf("输入有误");
}}
char *month[]={"January","February","March","April","May","June","July","August","September","October","November","December"};
char *week[]={"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
int month_day(int year,int month)  
{
int mon_day[]={31,28,31,30,31,30,31,31,30,31,30,31};
if(judge_year(year)&&month==2)
return 29;
else
return(mon_day[month-1]);
}
 
int PrintAllYear(int year)/*打印全年月份表,查询项2*/
{
int temp;
int i,j;
printf("\n\n%d Calander\n",year);
for(i=1;i<=12;i++)
{
printf("\n\n%s(%d)\n",month[i-1],i);
printf("0    1    2    3    4    5    6    \n");
printf("S    M    T    W    T    F    S    \n\n");
temp=the_days_of_year(year,i,1);
for(j=1;j<=month_day(year,i)+temp;j++)
{
if(j-temp<=0)
printf("     ");
else if(j-temp<10)
printf("%d    ",j-temp);
else
printf("%d   ",j-temp);

if(j%7==0)
printf("\n");
}
}
return 0;
}

void main()
{int year,month,day;

int a;while(1)
{printf("选择你要选择的选项:\n1.查询日期\n2.查看万年历\n3.结束\n");
printf("请输入你的选择");
scanf("a",&a);
switch(a)
{case '1':while(1)//  case 后面的1要加单引号,因为这是声明的是char性变量
{chanian();}
 case '2':while(1)
  {printf("输入年月日,格式如2012,1,23");
        scanf("%d,%d,%d",&year,&month,&day);
     PrintAllYear(year);}
 case '3':break;
 default:("输入有误");}
}}

有这么复杂吗?这个是我回答过的 http://wenwen.zzzyk.com/z/q340044346.htm

你要什么功能?这是一个输入年份判断是不是润年,并且判断天数的程序

上一个:C语言真难!
下一个:C语言编译器出错?

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,