首页 > 技术文章 > 查询(c语言实现)

hare 2013-12-07 20:51 原文

 1 /*
 2 *  Describe:这是一个简单的查询程序
 3 *  Date: 2013/12/7
 4 */
 5 #include <stdio.h>
 6 #include <stdlib.h>
 7 #define MAX     10
 8 
 9 int main()
10 {
11         int i;
12         int n;
13         int array[MAX];
14         n = MAX;
15 
16         printf("请输入 %d 个数\n", n);
17         for (i = 0; i < MAX; i++) {
18                 printf("第 %d 个数 :", i + 1);
19                 scanf("%d", &array[i]);
20                 if (i == MAX - 1)
21                         printf("already completed input!\n");
22         }
23 
24         while (1) {
25                 int key;
26                 printf("输入欲查找的数,并回车:\n");
27                 scanf("%d", &key);
28                 n = search(array, MAX, key);
29                 printf("the number you entered locates in %d  \n ", n);
30         }
31         return 0;
32 }
33 
34 int search(int *array, int n, int key)
35 {
36 
37         while (array[n - 1] != key)
38                 n--;
39         return n;
40 }

 

推荐阅读