实验5

实验5

四、实验结论

1.实验任务1

  • task1_1.c源代码,及,运行结果截图
  • 回答问题

    问题1:函数find_min_max 功能是?
    问题2:"指针变量使用时必须指向确定地址"。执行到line45时,指针变量pmin、pmax分别指向什么?

#include <stdio.h>
#define N 5
void input(int x[], int n);
void output(int x[], int n);
void find_min_max(int x[], int n, int *pmin, int *pmax);
int main() {
    int a[N];
    int min, max;
    printf("录入%d个数据:\n", N);
    input(a, N);
    printf("数据是: \n");
    output(a, N);
    printf("数据处理...\n");
    find_min_max(a, N, &min, &max);
    printf("输出结果:\n");
    printf("min = %d, max = %d\n", min, max);
    return 0;
}
void input(int x[], int n) {
    int i;
    for(i = 0; i < n; ++i)
        scanf("%d", &x[i]);
}
void output(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}
void find_min_max(int x[], int n, int *pmin, int *pmax) {
    int i;
    
    *pmin = *pmax = x[0];
    for(i = 0; i < n; ++i)
        if(x[i] < *pmin)
            *pmin = x[i];
        else if(x[i] > *pmax)
            *pmax = x[i];
}

image

Q1:查找最大值与最小值。
Q2:pmax;x[0]。

  • task1_2.c源代码,及,运行结果截图
  • 回答问题

    问题1:函数find_max 的功能是(返回的是什么)?
    问题2:函数find_max 改写成以下代码,可以实现同样功能吗?如果不可以,请给出你的理由。

#include <stdio.h>
#define N 5
void input(int x[], int n);
void output(int x[], int n);
int *find_max(int x[], int n);
int main() {
    int a[N];
    int *pmax;
    printf("录入%d个数据:\n", N);
    input(a, N);
    printf("数据是: \n");
    output(a, N);
    printf("数据处理...\n");
    pmax = find_max(a, N);
    printf("输出结果:\n");
    printf("max = %d\n", *pmax);
    return 0;
}
void input(int x[], int n) {
    int i;
    for(i = 0; i < n; ++i)
        scanf("%d", &x[i]);
}
void output(int x[], int n) {
    int i;
    
    for(i = 0; i < n; ++i)
        printf("%d ", x[i]);
    printf("\n");
}
int *find_max(int x[], int n) {
    int max_index = 0;
    int i;
    for(i = 0; i < n; ++i)
        if(x[i] > x[max_index])
            max_index = i;
    
    return &x[max_index];
}

image

Q1:返回最大值。
Q2:可以。

2.实验任务2

  • task2_1.c源代码,及,运行结果截图
  • 回答问题

    问题1:数组s1的大小是多少?sizeof(s1) 计算的是什么?strlen(s1) 统计的是什么?
    问题2:line7代码,能否替换成以下写法?如果不能,写出原因。
    c char s1[]; s1 = "Learning makes me happy";
    问题3:line19-21执行后,字符数组s1和s2中的内容是否交换?

#include <stdio.h>
#include <string.h>
#define N 80
int main() {
    char s1[N] = "Learning makes me happy";
    char s2[N] = "Learning makes me sleepy";
    char tmp[N];
    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));
    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);
    printf("\nswapping...\n");
    strcpy(tmp, s1);
    strcpy(s1, s2);
    strcpy(s2, tmp);
    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);
    return 0;
}

image

Q1:80,数组长度,数组有实际意义的长度。
Q2:不能,表达式必须是可修改的左值。
Q3:是。

  • task2_2.c源代码,及,运行结果截图
  • 回答问题

    问题1:指针变量s1中存放的是什么?sizeof(s1) 计算的是什么?strlen(s1) 统计的是什么?
    问题2:line6代码能否替换成下面的写法?对比task2_1.c中的line6, 用文字说明二者的语义区别。
    c char *s1; s1 = "Learning makes me happy";
    问题3:line20-line21,交换的是什么?字符串常量"Learning makes me happy"和字符串常
    量"Learning makes me sleepy"在内存中是否交换?

#include <stdio.h>
#include <string.h>
#define N 80
int main() {
    char *s1 = "Learning makes me happy";
    char *s2 = "Learning makes me sleepy";
    char *tmp;
    printf("sizeof(s1) vs. strlen(s1): \n");
    printf("sizeof(s1) = %d\n", sizeof(s1));
    printf("strlen(s1) = %d\n", strlen(s1));
    printf("\nbefore swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);
    printf("\nswapping...\n");
    tmp = s1;
    s1 = s2;
    s2 = tmp;
    printf("\nafter swap: \n");
    printf("s1: %s\n", s1);
    printf("s2: %s\n", s2);
    return 0;
}

image

Q1:首字符('L')的内存地址;存储地址所需的字节数;字符串的实际长度。
Q2:可以;无区别。
Q3:指针位置;没有。

3.实验任务3

  • task3.c源代码,及,运行结果截图
  • 回答问题

    问题1:int (*ptr)[4]; 中,标识符ptr表示的语义是什么?
    问题3:int *ptr[4]; 中,标识符ptr表示的语义是什么?

#include <stdio.h>
int main() {
    int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
    int i, j;
    int *ptr1;     // 指针变量,存放int类型数据的地址
    int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组
    printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
    for (i = 0; i < 2; ++i) {
        for (j = 0; j < 4; ++j)
            printf("%d ", x[i][j]);
        printf("\n");
    }
    printf("\n输出2: 使用指针变量ptr1(指向元素)访问\n");
    for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
        printf("%d ", *ptr1);
        if ((i + 1) % 4 == 0)
            printf("\n");
    }
                         
    printf("\n输出3: 使用指针变量ptr2(指向一维数组)访问\n");
    for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
        for (j = 0; j < 4; ++j)
            printf("%d ", *(*ptr2 + j));
        printf("\n");
    }
    return 0;
}

image

Q1:ptr 是一个指向含有 4 个 int 元素的数组的指针。
Q2:ptr 是一个包含 4 个元素的数组。

4.实验任务4

  • task4.c源代码,及,运行结果截图
  • 回答问题

    问题1:函数replace 的功能是?
    问题2: line24, 圆括号里循环条件可以改写成*str != '\0' 吗?

#include <stdio.h>
#define N 80
void replace(char *str, char old_char, char new_char); // 函数声明
int main() {
    char text[N] = "Programming is difficult or not, it is a question.";
    printf("原始文本: \n");
    printf("%s\n", text);
    replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少
    printf("处理后文本: \n");
    printf("%s\n", text);
    return 0;
}
// 函数定义
void replace(char *str, char old_char, char new_char) {
    int i;
    while(*str) {
        if(*str == old_char)
            *str = new_char;
        str++;
    }
}

image

Q1:将i替换为*。
Q2:可以。

5.实验任务5

  • task5.c源代码,及,运行结果截图
  • 回答问题

    问题1:去掉main函数line18 getchar(); ,重新编译、运行,此时多组输入测试结果有何不同?line18在这里的
    作用是什么?

#include <stdio.h>
#define N 80
char *str_trunc(char *str, char x){
    int i;
    for(i = 0; str[i] != '\0'; i++){
        if(str[i] == x){
            str[i] = '\0';
            break;
        }
    }
    return str;
};
int main() {
    char str[N];
    char ch;
    while(printf("输入字符串: "), gets(str) != NULL) {
        printf("输入一个字符: ");
        ch = getchar();
        printf("截断处理...\n");
        str_trunc(str, ch);         // 函数调用
        printf("截断处理后的字符串: %s\n\n", str);
        getchar();
    }
    return 0;
}
// 函数str_trunc定义
// 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址
// 待补足...
// xxx

image

Q1:存在操作被跳过了;清除缓冲区的\n。

6.实验任务6

  • task6.c源代码,及,运行结果截图
#include <stdio.h>
#include <string.h>
#define N 5
int check_id(char *str); // 函数声明
int main()
{
    char *pid[N] = {"31010120000721656X",
                    "3301061996X0203301",
                    "53010220051126571",
                    "510104199211197977",
                    "53010220051126133Y"};
    int i;
    for (i = 0; i < N; ++i)
        if (check_id(pid[i])) // 函数调用
            printf("%s\tTrue\n", pid[i]);
        else
            printf("%s\tFalse\n", pid[i]);
    return 0;
    }
// 函数定义
// 功能: 检查指针str指向的身份证号码串形式上是否合法
// 形式合法,返回1,否则,返回0
int check_id(char *str) {
    if(strlen(str) != 18){
        return 0;
    }
    if(str[17] != 'X' && (str[17] < '0' || str[17] > '9')){
        return 0;
    }
    for (int i = 0; i < 17; i++){
        if(str[i] < '0' || str[i] > '9'){
            return 0;
        }
    }
    return 1;
    // 补足函数实现
    // ...
}

image

7.实验任务7

  • task7.c源代码,及,运行结果截图
#include <stdio.h>
#define N 80
void encoder(char *str, int n); // 函数声明
void decoder(char *str, int n); // 函数声明
int main() {
    char words[N];
    int n;
    printf("输入英文文本: ");
    gets(words);
    printf("输入n: ");
    scanf("%d", &n);
    printf("编码后的英文文本: ");
    encoder(words, n);      // 函数调用
    printf("%s\n", words);
    printf("对编码后的英文文本解码: ");
    decoder(words, n); // 函数调用
    printf("%s\n", words);
    return 0;
}
/*函数定义
功能:对str指向的字符串进行编码处理
编码规则:
对于a~z或A~Z之间的字母字符,用其后第n个字符替换; 其它非字母字符,保持不变
*/
void encoder(char *str, int n) {
    for(int i = 0; str[i] != '\0'; i++){
        if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')){
            if(str[i] >= 'a' && str[i] <= 'z'){
                str[i] = (str[i] - 'a' + n) % 26 + 'a';
            }
            else{
                str[i] = (str[i] - 'A' + n) % 26 + 'A';
            }
        }
    }
    // 补足函数实现
    // ×××
}
/*函数定义
功能:对str指向的字符串进行解码处理
解码规则:
对于a~z或A~Z之间的字母字符,用其前面第n个字符替换; 其它非字母字符,保持不变
*/
void decoder(char *str, int n) {
    for(int i = 0; str[i] != '\0'; i++){
        if((str[i] >= 'a' && str[i] <= 'z') || (str[i] >= 'A' && str[i] <= 'Z')){
            if(str[i] >= 'a' && str[i] <= 'z'){
                str[i] = (str[i] - 'a' - n + 26) % 26 + 'a';
            }
            else{
                str[i] = (str[i] - 'A' - n + 26) % 26 + 'A';
            }
        }
    }
    // 补足函数实现
    // ×××
}

image

8.实验任务8

  • task8.c源码(按要求修改后)及在命令行运行测试截图
#include <stdio.h>
#include <string.h>

int main(int argc, char *argv[]) {
    int i, j;
    char *tmp;
    if (argc < 2) {
        return 0;
    }
    for (i = 1; i < argc - 1; ++i) {
        for (j = 1; j < argc - i; ++j) {
            if (strcmp(argv[j], argv[j + 1]) > 0) {
                tmp = argv[j];
                argv[j] = argv[j + 1];
                argv[j + 1] = tmp;
            }
        }
    }
    for (i = 1; i < argc; ++i) {
        printf("hello, %s\n", argv[i]);
    }
    return 0;
}

image


文章已结束:)

posted @ 2026-05-27 11:46  yoro_Mizore  阅读(39)  评论(0)    收藏  举报