|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
getchar()疑惑???
我今天编写了个程序,用getchar()函数获得字符,然后根据getchar()获得的字符来确定输出的结果。
具体程序如下:
#include<stdio.h>
void main()
{
int x[10],i,*p,*py;
char ss;
void sort(int a[],int *px);
printf("Please input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
p=x;
sort(x,p);
printf("Please input a or d:\n");
getchar();
if(getchar()=='a')
{
for(p=x;p<(&x[10]);p++)
printf("%d",*p);
}
if(getchar()=='d')
{
for(py=&x[9];py>=&x[0];py--)
printf("%d",*py);
}
}
void sort(int a[],int *px)
{
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
{
if(a[j]<*px)
{
temp=*px;
*px=a[j];
a[j]=temp;
}
}
px++;
}
}
修改后是:
#include<stdio.h>
void main()
{
int x[10],i,*p,*py;
char ss;
void sort(int a[],int *px);
printf("Please input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",&x[i]);
p=x;
sort(x,p);
printf("Please input a or d:\n");
if(getchar()=='a')
{
for(p=x;p<(&x[10]);p++)
printf("%d",*p);
}
if(getchar()=='d')
{
for(py=&x[9];py>=&x[0];py--)
printf("%d",*py);
}
}
void sort(int a[],int *px)
{
int i,j,temp;
for(i=0;i<10;i++)
{
for(j=i;j<10;j++)
{
if(a[j]<*px)
{
temp=*px;
*px=a[j];
a[j]=temp;
}
}
px++;
}
}
这个是经人指点后的修改。不明白为什么?
错误的程序,输入d后回车确认,没有任何结果。所以想详细了解下getchar()的用法,和为什么会错。 |
|