实验5

实验结论

Part1. 二分查找

1.补足程序ex1_1.cpp,附上补足后的程序源码,修改程序中数组元素值,给出运行结果截图

// 练习:使用二分查找,在一组有序元素中查找数据项
// 形参是数组,实参是数组名
#include <stdio.h>
#include<stdlib.h>
const int N = 5;
int binarySearch(int x[], int n, int item);
int main() {
    int a[N] = { 1,3,9,16,21 };
    int i, index, key;
    printf("the data in the array:\n");
    for (i = 0; i < N; i++)
        printf("%d ", a[i]);
    printf("\n");
    printf("print the data you want to search: ");
    scanf_s("%d", &key);
// 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果给index
// 补足代码①
    index = binarySearch(a, N, key);
if (index >= 0)
printf("%d is in the array,the index is %d\n", key, index);
else
printf("%d is not in the array\n", key);
system("pause");
return 0;
}
//函数功能描述:
//使用二分查找算法在数组x中查找特定值item,数组x大小为n
// 如果找到,返回其下标
// 如果没找到,返回-1
int binarySearch(int x[], int n, int item) {
    int low, high, mid;
    low = 0;
    high = n - 1;
    while (low <= high) {
        mid = (low + high) / 2;
        if (item == x[mid])
            return mid;
        else if (item<x[mid])
            high = mid - 1;
        else
            low = mid + 1;
    }
    return -1;
}

 

2.补足程序ex1_2.cpp,附上补足后的程序源码,修改程序中数组元素值,给出运行结果截图

// 练习:使用二分查找,在一组有序元素中查找数据项
// 形参是指针变量,实参是数组名
#include <stdio.h>
#include<stdlib.h>
const int N = 5;
int binarySearch(int *x, int n, int item);
int main() {
    int a[N] = { 1,3,9,16,21 };
    int i, index, key;
    printf("the data in the array:\n");
    for (i = 0; i < N; i++)
        printf("%d ", a[i]);
    printf("\n");
    printf("print the data you want to search: ");
    scanf_s("%d", &key);
    // 调用函数binarySearch()在数组a中查找指定数据项item,并返回查找结果
    // 补足代码①
    index = binarySearch(a, N, key);
    if (index >= 0)
        printf("%d is in the array,the index is %d\n", key, index);
    else
        printf("%d is not in the array\n", key);
    system("pause");
    return 0;
}
//函数功能描述:
//使用二分查找算法在x指向的数据项开始的n个数据中,查找item
// 如果找到,返回其位置
// 如果没找到,返回-1
int binarySearch(int *x, int n, int item) {
    int low, high, mid;
    low = 0;
    high = n - 1;
    while (low <= high) {
        mid = (low + high) / 2;
        if (item == *(x + mid))
            return mid;
        else if (item<*(x+mid))
            high = mid - 1;
        else
            low = mid + 1;
    }
    return -1;
}

注意:修改数组元素值时,要确保数组元素值是有序的,这样才能使用二分查找算法

 

Part.2选择法排序

补足程序ex2_2.cpp,附上补足后的程序源码,修改程序中字符串数据数据,给出运行结果截图

// 练习:使用选择法对字符串按字典序排序
#include <stdio.h>
#include <string.h>
#include<stdlib.h>
void selectSort(char str[][20], int n); // 函数声明,形参str是二维数组名
int main() {
    char name[][20] = { "John", "Alex", "Joseph", "Candy", "Geoge" };
    int i;
    printf("print the initial name list:\n");
    for (i = 0; i < 5; i++)
        printf("%s\n", name[i]);
    selectSort(name, 5); // 调用选择法对name数组中的字符串排序
    printf("print the name list in alphabetical order:\n");
    for (i = 0; i < 5; i++)
        printf("%s\n", name[i]);
    system("pause");
    return 0;
}
// 函数定义
// 函数功能描述:使用选择法对二维数组str中的n个字符串按字典序排序
void selectSort(char str[][20], int n) {
    // 补足代码
    int i, j, m;
    char temp[20];
    for (i = 0; i < n - 1; i++) {
        m = i;
        for (j = i + 1; j < n; j++)
            if (strcmp(str[j], str[m]) < 0)
                m = j;
        if (m != i) {
            strcpy_s(temp, str[i]);
            strcpy_s(str[i], str[m]);
            strcpy_s(str[m], temp);
        }
    }
}

g

 

Part3. 用指针处理字符串

1. s[i] = '\0'; 这行用于截断,如果删掉,后面的*就不能去掉了

2. 为啥gets在vs里不能用,part3全程用devc

 

实验总结和体会

不要着急!一着急就狂写错

 写的时候一直在看课件 strcat() strcpy() strcmp() strlen() strupr()和strlwr() 都是啥全忘了

 各种知识要记住才能继续写,现在抛开课本就啥也写不出来orz

posted @ 2019-05-27 23:48  blublum  阅读(85)  评论(2编辑  收藏  举报