实验5


#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]; }

 

 find_max_min   的功能是在输入的5个数里面找出最大与最小的数字。

 指向x[0],即x[]的第一个元素

 

task 1_2

#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* ptr = &x[0];
    int i;
    for (i = 1; i < n; ++i)
        if (x[i] > *ptr)
            ptr = &x[i];
    return ptr;
}

 功能:找出5个数据中最大的。

可以。

 

task 2—2

#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;
}

 s1的大小是24

sizeof(s1)计算的是算上字符串0/的总长度

strlen(s1)统计的是不算0/的长度

不能,因为形式错误

交换了。

task 3

#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;
}

 

task 4_1

#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++;
    }
}

 

replace功能:将字符串中算有的‘i’替换成‘*’

可以。

 

 

task4_2

#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';
        ++str;
    }

    printf("%s",str);
}

  

#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;
            }
}

 

 

task 5_2

#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;
    if(strlen(str)!=18){
        return 0;
    }
    for(i=0;i<17;++i){
        if(str[i]<'0'||str[i]>'9'){
            return 0;
        }
    }
    if(str[17]<'0'||(str[17]>'9'&&str[17]<'X')||str[17]>'X'){
        return 0;
    }
}

 

task 7

#include <stdio.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=0;
    while(str[i]!='\0'){
        if(str[i]>='a'&&str[i]<'z'||str[i]>='A'&&str[i]<'Z'){
            str[i]++;
        }
        else if(str[i]=='z'||str[i]=='Z'){
            str[i]-=25;
        }
        ++i;
    }
}

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

 

task 8

#include <stdio.h>

int main(int argc, char *argv[]) {
    int i;

    for(i = 1; i < argc; ++i)
        printf("hello, %s\n", argv[i]);

    return 0;
}

 

posted @ 2023-11-28 21:48  吕江楠  阅读(11)  评论(0编辑  收藏  举报