实验5_C语言指针应用编程

task1_1.c
复制代码
#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 = 1; i < n; ++i)
        if(x[i] < *pmin)
            *pmin = x[i];
        else if(x[i] > *pmax)
            *pmax = x[i];
}
复制代码

 1. 函数 find_min_max 实现的功能是?
找出数组中的最小数和最大数

2. "指针变量在使用之前必须指向确定的地址"。执行到line45时,指针变量pminpmax分别指
向什么?
指向 &x[0]

task1_2.c

复制代码
#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 = 1; i < n; ++i)
        if(x[i] > x[max_index])
            max_index = i;
    
    return &x[max_index];
}
复制代码

 1. 函数 find_max 的功能是(返回的是什么)?
数组x[ ]中,最大的元素x[i]的地址 

2. 把函数 find_max 的实现写成以下代码,可以吗?如果不可以,请给出你的理由。
可以

task2_1.c

复制代码
#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[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;
}
复制代码

问题1:数组s1的大小是多少? sizeof(s1) 计算的是什么? strlen(s1) 统计的是什么?
s1的大小是23

sizeof(s1)计算的是加上后缀"\0"的字符串长度

strlen(s1)计算的是是字符串的实际长度

 

问题2line7代码,能否替换成以下写法?如果不能,写出原因
    char s1[];
    s1 = "Learning makes me happy";
不能,没有定义数组s1[]内的元素个数


问题3line20-22执行后,字符数组s1s2中的内容是否交换?
交换
task2_2.c

复制代码
#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;
}
复制代码

 问题1:指针变量s1中存放的是什么? sizeof(s1) 计算的是什么? strlen(s1) 统计的是什么?
    s1中存放的是字符串 "Learning makes me happy" 的地址

    sizeof(s1)计算的是s1中存放地址的长度,等于4或8,因为指针变量在32位计算机中占用4字节,在64位计算机中占用8字节

    strlen(s1) 统计的是字符串的长度

 

问题2line7代码能否替换成下面的写法?对比task2_1.c中的line7, 描述二者的语义区别。

char *s1;
s1 = "Learning makes me happy";
 
可以 ; 
指针 *s1 指向字符串的地址, 可以间接给字符串赋值
 
问题3:line20-line22,交换的是什么?字符串常量"Learning makes me happy"和字符串常
量"Learning makes me sleepy"在内存存储单元中有没有交换?
 
交换 s1 s2 指向的地址 ; 没有
 
task3.c
复制代码
#include <stdio.h>

#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: 使用指向元素的指针变量p间接访问二维数组元素\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: 使用指向一维数组的指针变量q间接访问二维数组元素\n");
    for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
        for (j = 0; j < 4; ++j)
            printf("%d ", *(*ptr2 + j));
        printf("\n");
    }

    return 0;
}
复制代码

 

task4_1.c
复制代码
#include <stdio.h>
#define N 80

void replace(char *str, char old_char, char new_char); // 函数声明

int main() {
    char text[N] = "c 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++;
    }
}
复制代码

1. 函数replace的功能是?
将字符由旧的值换成新值
2. line24, 圆括号里循环条件可以改写成*str!='\0'吗?
可以
 
task4_2.c
复制代码
#include <stdio.h>
#define N 80

void str_trunc(char *str, char x);

int main() {
    char str[N];
    char ch;

    printf("输入字符串: ");
    gets(str);

    printf("输入一个字符: ");
    ch = getchar();

    printf("截断处理...\n");
    str_trunc(str, ch);

    printf("截断处理后的字符串: %s\n", str);

}

void str_trunc(char *str, char x) {
    while(*str) {
        if(*str == x) {
            *( ++str )= '\0';     // blank1
        }
        str++;   // blank2
    }

}
复制代码

 

task5_1.c (冒泡排序算法实现版本)
复制代码
#include <stdio.h>
#include <string.h>
void sort(char *name[], int n);

int main() {
    char *course[4] = {"C Program",
                       "C++ Object Oriented Program",
                       "Operating System",
                       "Data Structure and Algorithms"};
    int i;

    sort(course, 4);

    for (i = 0; i < 4; i++)
        printf("%s\n", course[i]);

    return 0;
}

void sort(char *name[], int n) {
    int i, j;
    char *tmp;

    for (i = 0; i < n - 1; ++i)
        for (j = 0; j < n - 1 - i; ++j)
            if (strcmp(name[j], name[j + 1]) > 0) {
                tmp = name[j];
                name[j] = name[j + 1];
                name[j + 1] = tmp;
            }
}
复制代码

 

task5_2.c
复制代码
#include <stdio.h>
#include <string.h>
void sort(char *name[], int n);

int main() {
    char *course[4] = {"C Program",
                       "C++ Object Oriented Program",
                       "Operating System",
                       "Data Structure and Algorithms"};
    int i;

    sort(course, 4);
    for (i = 0; i < 4; i++)
        printf("%s\n", course[i]);

    return 0;
}

void sort(char *name[], int n) {
    int i, j, k;
    char *tmp;

    for (i = 0; i < n - 1; i++) {
        k = i;
        for (j = i + 1; j < n; j++)
            if (strcmp(name[j], name[k]) < 0)
                k = j;

        if (k != i) {
            tmp = name[i];
            name[i] = name[k];
            name[k] = tmp;
        }
    }
}
复制代码

内存中字符串的存储位置发生了交换

task 6

复制代码
#include <stdio.h>
#include <string.h>
#define N 5

int check_id(char *str); // 函数声明

int main() {
    char *pid[N] = {"31010120000721656X",
                    "330106199609203301",
                    "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) {
    int i, j;
    char a;
    i = strlen( str );
    if( i != 18){
        return 0;
    }
    for(j = 0; j < i; j++){
        a = str[j];
        if( a < 48){
            return 0;
        }
        if( a > 57 && a != 88){
            return 0;
        }
    } 
    return 1;  
}
复制代码

 task 7

复制代码
#include <stdio.h>
#include <string.h>
#define N 80
void encoder(char *str); // 函数声明
void decoder(char *str); // 函数声明

int main() {
    char words[N];

    printf("输入英文文本: ");
    gets(words);

    printf("编码后的英文文本: ");
    encoder(words); // 函数调用
    printf("%s\n", words);

    printf("对编码后的英文文本解码: ");
    decoder(words); // 函数调用
    printf("%s\n", words);

    return 0;
}

/*函数定义
功能:对s指向的字符串进行编码处理
编码规则:
对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换
其它非字母字符,保持不变
*/
void encoder(char *str) {
    int i, j;
    i = strlen( str);
    for(j = 0; j < i; j++){
        if( str[j] >= 65 && str[i] <= 122){
            if( str[j] == 122){
                str[j] = 'a';
            }
            else if( str[j] == 90){
                str [j] = 'A';
            }
            else{
                str[j] = str[j] + 1;
            }
        }
    }
}

/*函数定义
功能:对s指向的字符串进行解码处理
解码规则:
对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换
其它非字母字符,保持不变
*/
void decoder(char *str) {
    int i, j;
    i = strlen( str);
    for(j = 0; j < i; j++){
        if( str[j] >= 65 && str[j] <= 144){
        if( str[j] == 'a'){
            str[j] = 'z';
        }
        else if( str[j] == 'A'){
            str [j] = 'Z';
        }
        else{
            str[j] = str[j] - 1;
        }
        }
    }
}
复制代码

 

posted @   liritty  阅读(18)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
点击右上角即可分享
微信分享提示