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       23 阅读
  2. C++程序设计3版)谭浩强 9 习题

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

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

最近更新

  1. .Net Core WebAPI参数的传递方式

    2024-05-12 17:12:03       0 阅读
  2. QT--气泡框的实现

    2024-05-12 17:12:03       1 阅读
  3. LeetCode 968.监控二叉树 (hard)

    2024-05-12 17:12:03       0 阅读
  4. leetcode热题100.完全平方数(动态规划进阶)

    2024-05-12 17:12:03       0 阅读
  5. leetcode328-Odd Even Linked List

    2024-05-12 17:12:03       0 阅读
  6. C 语言设计模式(结构型)

    2024-05-12 17:12:03       0 阅读
  7. v-if 与 v-show(vue3条件渲染)

    2024-05-12 17:12:03       0 阅读
  8. kafka防止消息丢失配置

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

热门阅读

  1. matlab实现机器学习

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

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

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

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

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

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

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

    2024-05-12 17:12:03       3 阅读
  9. SSL VPN

    SSL VPN

    2024-05-12 17:12:03      3 阅读
  10. 2024.5.12 ubuntu + latex + vscode

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