实验五

task_1.1

#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 max, min;
    
    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;
    
    *pmax = *pmin = x[0];
    
    for(i=0;i<n;i++)
    {
        if(x[i]<*pmin)
        {
            *pmin = x[i];
        }
        else if(x[i]>*pmax)
        {
            *pmax = x[i];
        }
    }
}

 

 问题一:find_min_max功能是找出数组中的最大最小值

问题二:*pmax和*pmin指向x[0]的地址

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

 问题一:返回的是最大值的地址

 问题二:彳亍

 task_2.1

 

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

问题1:s1的大小是80;sizeof(s1)表示的是包含\0的元素个数;strlen(s1)统计的是不包含\0的元素个数

问题2:不能,s1是地址,后面的是字符串

task_2.2

#include<stdio.h>
#include<string.h>
#define N 80

int main()
{
    const char *s1 = "learning makes me happy";
    const char *s2 = "learning makes me sleepy";
    const 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)统计的是非\0元素的个数

 问题2:可以;2.1中是把字符串赋值给地址,2.2中是将第一个元素的地址赋值给s1

 问题3:交换的是地址,内存没有交换

task_3

#include<stdio.h>

int main()
{
int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
int i, j;
int *ptr1;
int(*ptr2)[4];

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

 *(ptr)中ptr是数组指针,指向该数组的指针;另一个是指针数组,数组内元素均为指针

 task_4

#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!='\0')
    {
        if(*str == old_char)
            *str = new_char;
        str++;
    }
}

 问题1:replace功能是把指定字母换成*

问题2:彳亍

task_5

#include<stdio.h>
#define N 80

char *str_trunc(char *str, char x);

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

char *str_trunc(char *str, char x)
{
    char *tmp = str;
    
    for(;*tmp!=x;tmp++);
    
    *tmp = '\0';
    
    return str;
}

 问题:去掉gechar()后,从第二组开始没法输入字符串,起到的作用是存储输入字符后的回车

task_6

#include<stdio.h>
#include<string.h>
#define N 5

int check_id(char *str);
int check_central(char *str);
int check_last(char *str);

int main()
{
char *pid[N] = {"31010120000721656X",//T
"3301061996X0203301",//F
"53010220051126571", //F
"510104199211197977",//T
"53010220051126133Y"};//F

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

int check_id(char *str)
{
int i;

if(strlen(str)==18 && check_central(str)!=0 && check_last(str)==1)
{
return 1;
}
else
{
return 0;
}
}

int check_central(char *str)
{
int i;
for(i=0;i<17;i++)
{
if(*(str+i)>='A' && *(str+i)<='Z')
{
return 0;
}
}
}

int check_last(char *str)
{
if((*(str+17)>='0' && *(str+17)<='9') || *(str+17) == 'X')
return 1;
}

 task_7

#include<stdio.h>
#include<string.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;    
} 

void encoder(char *str, int n)
{
    int i;
    
    for(i=0;*(str+i)!='\0';i++)
    {
        if(*(str+i) >= 'a' && *(str+i) <= 'z' - n || *(str+i) >= 'A' && *(str+i) <= 'Z' - n)
        {
            *(str+i) += n;
        }
        else if(*(str+i) > 'z' - n && *(str+i) <= 'z' || *(str+i) > 'Z' - n && *(str+i) <= 'Z')
        {
            *(str+i) = *(str+i) + n - 26;
        }
    }
}

void decoder(char *str, int n)
{
    int i;
    
    for(i=0;*(str+i)!='\0';i++)
    {
        if(*(str+i) >= 'a' + n && *(str+i) <= 'z' || *(str+i) >= 'A' + n && *(str+i) <= 'Z')
        {
            *(str+i) -= n;
        }
        else if(*(str+i) >= 'a' && *(str+i) < 'a' + n || *(str+i) >= 'A' && *(str+i) < 'A' + n)
        {
            *(str+i) = *(str+i) - n + 26;
        }
    }
}

 task_8

#include<stdio.h>

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

 

posted @ 2024-12-08 18:18  骄傲的蒂花之秀  阅读(6)  评论(0编辑  收藏  举报