标题:【求助】想问一下关于getchar的用法
取消只看楼主
LINJOSHUA
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2011-6-22
结帖率:25%
已结贴  问题点数:5 回复次数:0 
【求助】想问一下关于getchar的用法
下面是程序源代码的一部分,我要问的是函数nyr里面的"scanf("%c",&yn); yn=getchar();"我一开始的时候是这样的:scanf("%c",&yn); getchar();  结果无论我输入y/Y或者n/N都是直接跳转回项目选择总目录那里,当我把getchar()改成ye=getchar()后就得到我想要的结果,我想问这是为什么,getchar这个函数在这里的作用又是什么??
#include<stdio.h>
#include<time.h>
void screen();                       
void nyr();                           
void printyearday();                 
int leapyear(int);         
int xingqi(int,int);         
int monthdays(int,int);         
void printmonth(int,int);   
void printfmonday();
void gongli(int,int);
int dayofyear(int,int,int);   
int day,mon,year;
char *week[] = {"星期日", "星期一", "星期二","星期三","星期四","星期五","星期六"};
void main()
{
   
    struct   tm   *newtime;                 
    time_t   long_time;
    time(   &long_time   );
    newtime=localtime( &long_time );
    year=newtime-> tm_year +1900,mon=newtime-> tm_mon+1,day=newtime-> tm_mday;
    screen();
}
void screen()
{
   int n;
   while ( 1 )
   {
   printf("万年历查询程序:\n");
   printf("1.查询某年的全年日历\n");
   printf("2.查询某年某月日历\n");
   printf("3.查询某年某月某日\n");
   printf("4.退出\n");
    printf("请输入要查询的选项:");
    scanf("%d",&n);
     switch (n)
     {
       case 1:  printyearday();  break;
       case 2:  printfmonday();  break;
       case 3:  nyr();  break;
       case 4:  return;
       default:  printf("输入错误,请按要求输入!!!\n"); break;
     }
    }
}
void nyr()                 
{
  int y,m,d,s,k=1, sum,n,i,flag,Y,r,SUM,md;
  char yn;
  int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  while ( k )
  {  
      printf("请依次输入年,月,日:\n");  
      scanf("%d,%d,%d",&y,&m,&d);
      printf("今天的日期:%d年%d月%d日\n",year,mon,day);
      flag=leapyear(y);      
      if (y>=year)
      {
         Y=y-year;
         r=Y/4;
      }
      else
      {
        Y=(year-1)-y;
        r=(Y+3)/4;
      }
      if(y>year)
          SUM=Y*365+r+dayofyear(y,m,d)-dayofyear(year,mon,day);
      else
          if(y==year)
              SUM=dayofyear(year,mon,day)-dayofyear(y,m,d);
          else
              SUM=Y*365+r+(365+flag-dayofyear(y,m,d))+dayofyear(year,mon,day);
      printf("%d年%d月%d日距今天%d天\n",y,m,d,SUM);
      s=0;            
      for(i=1;i<m;i++)   
      s=s+a[i];
      s=s+d;
      if(leapyear(y) && m>2) s=s+1;
      sum=s;
      n=(y-1+(y-1)/4-(y-1)/100+(y-1)/400+sum)%7;
      printf("这天是%s\n",week[n]);
      gongli(m,d);
      printf("是否继续查询(Y/N)?");
      scanf("%c",&yn);
      yn=getchar();
      if ( yn=='y' || yn=='Y')  k=1;
      else  k=0;
   }
}
int dayofyear(int year,int mon,int day)
{
    int days[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}},i;
    int flag=leapyear(year);
    int sum=day;
    for(i=1;i<mon;i++)
    {
        sum+=days[flag][i];
    }
    return sum;
}
int leapyear(int year)
{ return (year%4 == 0 &&year%100 != 0)||(year%400 == 0);}
搜索更多相关主题的帖子: screen 源代码 
2011-06-22 21:27



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-343719-1-1.html




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

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