求救,输出问题~~
很郁闷的啊,找不到那里出问题了,程序长了点,主要是为了练习格式,帮忙看下哈。#include <stdio.h>
int n,num;
char a[10],x;
void sort(char a[],int n)
{
int i,j,k;
char t;
for(i=0;i<n-1;i++)
{
k=i;
for(j=i+1;j<n;j++)
if(a[k]<a[j])k=j;
if(k!=i)
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
}
void search(char a[],int n,char x)
{
int left=0,right=n-1,mid,flag=0;
if(n>2)
while((flag==0)&&(right>left))
{
mid=(left+right)/2;
if(x==a[mid])
{
flag=1;
num=mid;
}
else if(x<a[mid])right=mid-1;
else left=mid+1;
}
else num=(a[1]>a[2])?1:2;
}
void input(char a[])
{
int i;
printf("please input the quantity of students!\n(Quantity must smaller than 11 and biger than 1):");
scanf("%d",&n);
while((n<=1)||(n>=11))
{
printf("please input the right one:");
scanf("%d",&n);
}
printf("\nplease input the code of students:\n");
for(i=0;i<n;i++)
{
printf("student_%d:",i+1);
scanf("%c",&a[i]);
printf("\n")
}
printf("now,the queue is:");
for(i=0;i<n;i++)
printf("%5c",a[i]);
printf("\n");
printf("please input the element of you want to search:");
scanf("%c",&x);
}
void output1(char a[])
{
int i;
printf("we can get the orderly queue:\n");
for(i=0;i<n;i++)
printf("%5c",a[i]);
printf("\n");
}
void output2(char a[])
{
printf("the element you search at the %d of queue.\n",num);
printf("\n thanks for your test,Bye-bye (*^__^*) \n");
}
void main()
{
input(a);
sort(a,n);
output1(a);
search(a,n,x);
output2(a);
}
在运行input函数,读入数组a时,总是直接跳了一个存储区,如:输入N为2,屏幕会打印出
please input the code of students:
student_1:
student_2:
student_1没读入,直接跳到了student_2,然后读入了一个字符就结束了,我看了半天不明白,谁能帮我看下啊,谢谢了。