scanf风云再起~~
http://expert.csdn.net/Expert/topic/2049/2049786.xml?temp=.926861
接过上次的最后一贴,这次问另外一个问题。
------------------------------------------------------------------
主 题: 最后为这个散100分:我发誓我不想再钻这个牛角尖了~~
作 者: Windward (我是晕倒菜菜籽呀,咿呀咿呀哟)
信 誉 值: 100
所属论坛: C/C++ 基础类
问题点数: 100
回复次数: 32
发表时间: 2003-7-20 12:22:38
------------------------------------------------------------------
==========================
to jscsqb() :
to bigtea(企鹅) :
to listart(开始) :
to dalvin(林夕) :
to GZCompiler(编译器) :
===========================
虽然各位的解释有时是那么的有说服力,而有时大家的解释偏巧又那么矛盾比如(jscsqb和listart两位大哥)~~以至于小弟似懂非懂,不知道该听谁的了。
唉,无所谓了,上次的例子中有个地方,我今天闲来做了个修改——就是main函数那里,把原来正常的getchar部分改用scanf来做,look...
//getchar();
//c=getchar();
scanf(" %c",&c);
遗憾的是,这时无论我用scanf(" %c",&c);还是scanf("%c",&c);都不对了,难道说这里不能用scanf么??如果用,如何才能正确实现?
问题完毕。
期待解答ing...
---------------------
// test.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
// test.cpp : Defines the entry point for the console application.
// 输入职工号和职工名,然后进行排序,还能进行查找。
//#include "stdafx.h"
#include <stdio.h>
#include <string.h>
#define N 3
void input (int num[],char name [N][8])
{
int i;
for (i=0;i<N;i++)
{
printf("\n Input NO.:");
scanf("%d",&num[i]);
printf("Input name:");
getchar();
gets(name[i]);
}
}
void sort(int num[],char name[N][8]) //选择排序
{
int i,j,min,temp1;
char temp2[8];
for (i=0;i<N-1;i++)
{
min=i;
for(j=i;j<N;j++)
if (num[min]>num[j]) min=j;
temp1=num[i];
strcpy(temp2,name[i]);
num[i]=num[min];
strcpy(name[i],name[min]);
num[min]=temp1;
strcpy(name[min],temp2);
}
printf("\n result:\n");
for(i=0;i<N;i++)
printf("\n%5d%10s",num[i],name[i]);
}
void search(int n,int num[],char name[N][8]) //折半查找
{
int top,bott,mid,loca,sign=1;
loca=0;
top=0;
bott=N-1;
if ((n<num[0])||(n>num[N-1]))loca=-1;
while ((sign==1)&&(loca!=-1)&&(top<=bott))
{
mid=(top+bott)/2;
if (n<num[mid])
{
bott=mid-1;
}
else if (n>num[mid])
{
top=mid+1;
}
else
{
printf("NO.%d,his name is %s.\n",n,name[mid]);
sign=0;
}
}
if ((loca==-1)||(sign==1))
printf("Can not find %d.\n",n);
}
void main()
{
int num[N],number,flag=1,c;
char name[N][8];
input (num,name);
sort(num,name);
while (flag==1)
{
printf("\nInput number to look for:");
scanf("%d",&number);
search(number,num,name);
printf("Continue or not(Y/N)?");
//getchar();
//c=getchar();
scanf(" %c",&c);
if (c=='N'||c=='n')
flag=0;
}
}