Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
scanf()的双重使用取决于调用顺序_C_Input_Scanf_Ternary Search - Fatal编程技术网

scanf()的双重使用取决于调用顺序

scanf()的双重使用取决于调用顺序,c,input,scanf,ternary-search,C,Input,Scanf,Ternary Search,我用C写了一个程序,它以一个值和一个有序的整数数组作为输入,并执行三元搜索以查找数组中的值(如果存在) 在Stackoverflow中,我已经看到了使用scanf的所有可能问题以及相关主题 我注意到,如果我以相反的顺序调用2个scanf函数,会有不同 如果我使用下面的代码。首先读取值,然后从用户处读取数组,程序和scanf按预期运行 printf("Enter the value to be searched in the Array: "); int k; scanf(" %d", &

我用C写了一个程序,它以一个值和一个有序的整数数组作为输入,并执行三元搜索以查找数组中的值(如果存在)

在Stackoverflow中,我已经看到了使用scanf的所有可能问题以及相关主题

我注意到,如果我以相反的顺序调用2个scanf函数,会有不同

如果我使用下面的代码。首先读取值,然后从用户处读取数组,程序和scanf按预期运行

printf("Enter the value to be searched in the Array: ");
int k;
scanf("  %d", &k);

printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");

i = 0;  
while(scanf("%d", &A[i]) == 1) {
    i++;
}//while
虽然如果我以相反的顺序使用scanf输入,第二个scanf永远不会停止获取用户输入并读取留在缓冲区中的值

printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");

i = 0;  
while(scanf("%d", &A[i]) == 1) {
    i++;
}//while

printf("Enter the value to be searched in the Array: ");
int k;
scanf("  %d", &k);
我不明白打电话的顺序有什么不同。 我尝试过其他线程中提到的解决方案,但没有一个有效

整个代码(按预期工作)仅供参考:

#包括
#包括
#包括
内部搜索(内部A[],内部l,内部r,内部k){
int i;
int第一,第二;
如果(l>r){
返回-1;
}
i=(r-l)/3;
如果(i==0){
i++;
}
第一个=i+l-1;
秒=i*2+l-1;
如果(A[第一]==k){
先返回;
}
else如果(A[第一]>k){
三元搜索(A,l,first-1,k);
}
其他的
{
如果(A[秒]==k)
返回第二;
其他的
如果(A[秒]>k)
三元搜索(A,第一+1,第二-1,k);
其他的
三元搜索(A,second+1,r,k);
}
}
int main(){
常量int maxaraylen=1000;
int i;
int n;
int A[maxaraylen];
字符串[250];
printf(“在数组中输入要搜索的值:”);
int k;
scanf(“%d”和“&k”);
printf(“键入由空格分隔的(已排序)元素(键入'end'停止):”;
i=0;
而(scanf(“%d”,&A[i])==1){
i++;
}//当
n=i-1;
//我们假设数组已排序,否则我们可以使用任何排序算法,例如task1中的代码
scanf(“%d”和“&k”);
int结果;
结果=三元搜索(A,0,n,k);
如果(结果==-1){
printf(“在数组中找不到该值。\n”);
}
否则{
printf(“在位置%d中找到该值。\n”,结果);
}
返回0;
}

您的问题是您没有“跨过”您的
结束
输入

我们可以通过使用以下程序进行实验来了解这一点:

#包括
#包括
真空总管(真空){
文件*f;
长f_pos;
int ret;
int i;
int数据[5];
int data_last;
整数搜索;
f=fopen(“./input.txt”,“r”);
如果(f==NULL){
perror(“fopen()”);
返回;
}
/*读入数组的值*/
数据_last=-1;
对于(i=0;i<5;i++){
ret=fscanf(f,“%d”和(数据[i]);
printf(“fscanf(数据[%d]):ret:%d\n”,i,ret);
f_pos=ftell(f);
printf(“ftell():%ld\n”,f_pos);
如果(ret!=1){
打破
}
数据_last=i;
}
/*检查我们是否读取了至少一个值*/
如果(数据_last==-1){
printf(“无输入数据!\n”);
返回;
}
/*在此处插入“修复”*/
/*用已知垃圾预加载“搜索”*/
搜索=987;
/*现在读入搜索值*/
ret=fscanf(f、%d、&search);
printf(“fscanf(搜索):ret:%d\n”,ret);
f_pos=ftell(f);
printf(“ftell():%ld\n”,f_pos);
/*打印出我们的信息*/

对于(i=0;i而言,您的问题在于您没有“跨过”您的
end
输入

我们可以通过使用以下程序进行实验来了解这一点:

#包括
#包括
真空总管(真空){
文件*f;
长f_pos;
int ret;
int i;
int数据[5];
int data_last;
整数搜索;
f=fopen(“./input.txt”,“r”);
如果(f==NULL){
perror(“fopen()”);
返回;
}
/*读入数组的值*/
数据_last=-1;
对于(i=0;i<5;i++){
ret=fscanf(f,“%d”和(数据[i]);
printf(“fscanf(数据[%d]):ret:%d\n”,i,ret);
f_pos=ftell(f);
printf(“ftell():%ld\n”,f_pos);
如果(ret!=1){
打破
}
数据_last=i;
}
/*检查我们是否读取了至少一个值*/
如果(数据_last==-1){
printf(“无输入数据!\n”);
返回;
}
/*在此处插入“修复”*/
/*用已知垃圾预加载“搜索”*/
搜索=987;
/*现在读入搜索值*/
ret=fscanf(f、%d、&search);
printf(“fscanf(搜索):ret:%d\n”,ret);
f_pos=ftell(f);
printf(“ftell():%ld\n”,f_pos);
/*打印出我们的信息*/

对于(i=0;i),您应该缩进您的代码,并使用适当的变量名来帮助理解。完整的代码仅在任何人想要运行程序时用作参考。与此问题相关的唯一两行重要行是
scanf(“%d”、&k);
scanf(“%d”、&a[i]”)==1
。一个只读一个整数变量,另一个从命令行读取多个整数。因此,scanf删除了回车符“\r”和/或新行字符“\n”?这就是为什么一种方法有效,而另一种方法无效的原因吗?您能解释一下%*s的工作原理吗?值得注意的是,scanf格式之间没有区别
%d“
%d”
%d"
。格式字符串中的空格与零个或多个空格字符匹配;
%d
格式从跳过零个或多个空格字符开始。xing,rici感谢您提供的有用信息!您应该缩进代码并使用适当的变量名来帮助理解。完整的代码仅在您想运行该程序。与该问题相关的唯一两行重要代码是
scanf(“%d”、&k);
scanf(“%d”、&A[i])==1
。一个只读一个整数变量,另一个从命令行读取多个整数。因此,scanf删除了回车符“\r”和/或新行字符“\n”?这就是为什么一种方法有效,而另一种方法无效的原因吗?您能解释一下%*s是如何工作的吗?对于它的价值,scanf和
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


int ternarySearch(int A[], int l, int r, int k){
int i;
int first,second;

if(l>r){
    return -1;
}

i= (r - l)/3;

if(i==0){
    i++;
}

first = i+l-1;
second = i*2+l-1;

if(A[first]==k){
    return first;
}
else if(A[first]>k){
    ternarySearch(A, l, first-1, k);
}
else
 {
    if(A[second]==k)
      return second;
    else
    if(A[second]>k)
       ternarySearch(A, first+1,second-1, k);
    else
       ternarySearch(A, second+1,r, k);
 }
}


int main(){
const int maxarraylen = 1000;
int i;
int n;
int A[maxarraylen];
char string[250];

printf("Enter the value to be searched in the Array: ");
int k;
scanf("  %d", &k);

printf("Type elements of A(sorted) separated by spaces (type 'end' to stop): ");

i = 0;  
while(scanf("%d", &A[i]) == 1) {
    i++;
}//while
n=i-1;


//We assume the array is sorted otherwise we can use any sorting algorithm e.g. code from task1

scanf("  %d", &k);






int result;
result=ternarySearch(A, 0, n, k);

if(result==-1){
    printf("The value was not found in the Array.\n");
}
else{
    printf("The value was found in position no. %d.\n", result);
}

return 0;
}