【求助】想问一下关于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);}