C程序设计第八章习题

1.三数排序

代码:

#include "stdio.h"

void sort(int *arr, int count);
void print(int *arr, int count);
void input(int *arr, int count);
void init(int *arr, int count);
void delete(int *arr, int count);

void main() {
    int count = 3;
    int *arr = (int *) malloc(count * sizeof(int));
    input(arr, count);
    print(arr, count);

    sort(arr, count);
    print(arr, count);

    delete(arr, count);

}

void sort(int *arr, int count) {
    int max = 0;
    for(int i = 0; i < count; i++) {
        max = arr[i];
        for(int j = i + 1; j < count; j++) {
            if(arr[j] > max) {
                max = arr[j];
                arr[j] = arr[i];
                arr[i] = max;
            }
        }
    }
}

void print(int *arr, int count) {
    printf("The arr is :");
    for(int i = 0; i < count; i++) {
        printf("%d,", arr[i]);
    }
    printf("\n");
}

void input(int *arr, int count) {
    //int *start = arr;//形参在函数结束时,会释放掉,并且不会改写原始参数
    for(int i = 0; i < count; i++) {
        printf("Please input a number:");
        scanf("%d", arr++);
    }
    //arr = start;
}

void init(int *arr, int count) {
    arr = (int *) malloc(count * sizeof(int));
}

void delete(int *arr, int count) {
    free(arr);
}

输出结果:

 

2. 比较字符串

参考第六章第14题

代码:

#include "stdio.h"
 

int strCompare(char *a, char *b);

void main() {
    char a[100];
    char b[100];
    char c[100];
    printf("Please input string a:");
    gets(a);
    printf("Please input string b:");
    gets(b);
    printf("Please input string c:");
    gets(c);

    //数组名不等价于指针,不能修改其内容
    char *temp;
    char *x = a;
    char *y = b;
    char *z = c;
    int result = strCompare(x, y);
    if(result > 0) {
        temp = x;
        x = y;
        y = temp;
    } 
    result = strCompare(x, z);
    if(result > 0) {
        temp = z;
        z = x;
        x = temp;
    }

    result = strCompare(y, z);
    if(result > 0) {
        temp = z;
        z = y;
        y = temp;
    }

    printf("sort string:%s,%s,%s", x, y, z);

}

int strCompare(char *a, char *b){
    
    int i = 0;
    while (a[i] && b[i])
    {
        if(a[i] != b[i]) {
            return a[i] - b[i];
        }
        i++;
    }
    if(a[i] == b[i]) {
        return 0;
    } else if(a[i] == 0) {
        return -26;
    } else if(b[i] == 0) {
        return 26;
    }
    return 27;
}

输出结果:

 

3.最大值和最小值

代码:

 

#include "stdio.h"

void max(int *arr, int count);
void min(int *arr, int count);
void print(int *arr, int count);
void input(int *arr, int count);
void delete(int *arr, int count);

void main() {
    int count = 10;
    int *arr = (int *) malloc(count * sizeof(int));
    input(arr, count);
    print(arr, count);

    max(arr, count);
    print(arr, count);

    min(arr, count);
    print(arr, count);

    delete(arr, count);

}

void print(int *arr, int count) {
    printf("The arr is :");
    for(int i = 0; i < count; i++) {
        printf("%d,", arr[i]);
    }
    printf("\n");
}

void input(int *arr, int count) {
    //int *start = arr;//形参在函数结束时,会释放掉,并且不会改写原始参数
    for(int i = 0; i < count; i++) {
        printf("Please input a number:");
        scanf("%d", arr++);
    }
    //arr = start;
}

void delete(int *arr, int count) {
    free(arr);
}

void max(int *arr, int count) {
    int *max = arr;

    for(int i = 1; i < count; i++) {
        if(arr[i] > *max) { 
            max = &arr[i];
        }
    }
    int temp = 0;
    temp = *max;
    *max = arr[count-1];
    arr[count-1] = temp;

}
void min(int *arr, int count) {
    int *min = arr;

    for(int i = 1; i < count; i++) {
        if(arr[i] < *min) { 
            min = &arr[i];
        }
    }
    int temp = 0;
    temp = *min;
    *min = arr[0];
    arr[0] = temp;
}

输出结果:

4.数字循环右移m位

代码:

 

#include "stdio.h"

void reverse(int *arr, int count);
void min(int *arr, int count);
void print(int *arr, int count);
void input(int *arr, int *count, int m);
void delete(int *arr, int count);

void main() {

    int n = 0;
    int m = 3;
    int *arr = (int *) malloc (sizeof(int));
    input(arr, &n, m);

    print(arr, n);
    

}

void input(int *arr, int *n, int m) {

    int num = 0;
    printf("Please input a number:");
    scanf("%d", &num);

    int a = 1;
    for(int i = 0; i < m; i++) {
        a = a*10;
    }
    int b = num % a;//a_b = num
    a = num/a;
    
    //printf("%d,%d", a, b);
    int *p = arr;
    while(b) 

相关推荐

  1. C++程序设计3版)谭浩强 8 习题

    2024-05-12 17:12:03       66 阅读
  2. C++程序设计3版)谭浩强 9 习题

    2024-05-12 17:12:03       59 阅读
  3. C++程序设计3版)谭浩强 10 习题

    2024-05-12 17:12:03       60 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-05-12 17:12:03       169 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-05-12 17:12:03       185 阅读
  3. 在Django里面运行非项目文件

    2024-05-12 17:12:03       155 阅读
  4. Python语言-面向对象

    2024-05-12 17:12:03       169 阅读

热门阅读

  1. matlab实现机器学习

    2024-05-12 17:12:03       39 阅读
  2. 以AI对抗AI,瑞数“动态安全+AI”助力在线反欺诈

    2024-05-12 17:12:03       45 阅读
  3. 链表初步之找最大结点

    2024-05-12 17:12:03       42 阅读
  4. ubuntu bind9 主从配置

    2024-05-12 17:12:03       33 阅读
  5. 华为昇腾310B1平台深度学习算法模型转换

    2024-05-12 17:12:03       40 阅读
  6. 前端:零宽字符

    2024-05-12 17:12:03       38 阅读
  7. Hive on Tez 作业优化参数

    2024-05-12 17:12:03       35 阅读
  8. PostgreSQL自带的命令行工具10- pg_basebackup

    2024-05-12 17:12:03       44 阅读
  9. Linux上的监控工具:Zabbix、Prometheus、APM和ELK

    2024-05-12 17:12:03       38 阅读
  10. SSL VPN

    SSL VPN

    2024-05-12 17:12:03      39 阅读
  11. 2024.5.12 ubuntu + latex + vscode

    2024-05-12 17:12:03       37 阅读