2017-03-19 43 views
0

C言語では、値と配列の整数の配列を取り、配列内に値がある場合はその値を見つけるためのプログラムをC言語で作成しました。scanf()の2回の使用は呼び出し順序に依存します

ここでは、scanfの使用に関するすべての問題と関連トピックをStackoverflowで見てきました。

逆の順序で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入力を使用する場合は、2番目の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); 

私は、呼び出し順序の違いを理解できません。 私は他のスレッドで言及されている解決策を試しましたが、うまくいきませんでした。ただ、ここに参照として

は、全体のコード(として期待される作業)である:

#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; 
} 
+0

あなたはあなたのコードをインデントし、理解を助けるためにまともな変数名を使用することが必要です。


ソリューションは、我々は、配列の値を受け取ったことを確認した直後にこのスニペットを挿入することです。 – Attie

+0

完全なコードは、誰かがプログラムを実行したい場合にのみ参照として使用されます。この問題に関連する重要な行は 'scanf("%d "、&k);'と 'scanf("%d "、&A [i])== 1です。コマンドラインからの整数 –

+0

scanfはキャリッジリターン '\ r'および/または改行文字 '\ n'を削除しますか?これは片方向が機能する理由ですが、他方はありませんか? **作品は? –

答えて

1

あなたの問題は、あなたのend入力「ステップオーバー」されていないということです。 input.txtで、次のデータで

#include <stdio.h> 
#include <stdlib.h> 

void main(void) { 
    FILE *f; 
    long f_pos; 
    int ret; 
    int i; 
    int data[5]; 
    int data_last; 
    int search; 

    f = fopen("./input.txt", "r"); 
    if (f == NULL) { 
     perror("fopen()"); 
     return; 
    } 

    /* read in the values for the array */ 
    data_last = -1; 
    for (i = 0; i < 5; i++) { 
     ret = fscanf(f, "%d", &(data[i])); 
     printf("fscanf(data[%d]): ret: %d\n", i, ret); 
     f_pos = ftell(f); 
     printf("ftell(): %ld\n", f_pos); 
     if (ret != 1) { 
      break; 
     } 
     data_last = i; 
    } 

    /* check that we read in at least one value */ 
    if (data_last == -1) { 
     printf("no input data!\n"); 
     return; 
    } 

    /* insert 'fix' here */ 

    /* pre-load the 'search' with known garbage */ 
    search = 987; 

    /* now read in the search value */ 
    ret = fscanf(f, "%d", &search); 
    printf("fscanf(search): ret: %d\n", ret); 
    f_pos = ftell(f); 
    printf("ftell(): %ld\n", f_pos); 

    /* print out our info */ 
    for (i = 0; i <= data_last; i++) { 
     printf("data[%d]: %d\n", i, data[i]); 
    } 
    printf("search for: %d\n", search); 

    return; 
} 

123 
456 
end 
456 

出力は次のとおりです。

fscanf(data[0]): ret: 1 
ftell(): 3 
fscanf(data[1]): ret: 1 
ftell(): 7 
fscanf(data[2]): ret: 0 
ftell(): 8 
fscanf(search): ret: 0 
ftell(): 8 
data[0]: 123 
data[1]: 456 
search for: 987 

私たちは、以下のプログラムを用いた実験を行うことで、これを見ることができます

ftell()は、ファイルのカーソルがどこにあるかを示しています。この場合、それがバイト8 ...入力行のeであることを参照してください。end

それはそれを超えないので、次の数字(%d)の読み込みも失敗します!

戻り値を確認することもお勧めします。 fscanf(&search)コールが番号の読み取りに失敗したことがわかります。

/* this is the 'fix' */ 
ret = fscanf(f, "end"); 
printf("fscanf(end): ret: %d\n", ret); 
f_pos = ftell(f); 
printf("ftell(): %ld\n", f_pos); 
+0

非常に詳細で有益な答えをありがとう! –