实验6

task1

点击查看代码
#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];
}

找到最大值和最小值
执行完后指向数组的第一个元素

点击查看代码
#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];
}

寻找最大值并返回其地址
应该可以

===========================================================================================

task2

点击查看代码
#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;
}

s1大小为80 , sizeof 计算数组大小 , strlen 统计字符串长度
不行 , 数组名是地址常量 不可以改变
数组元素改变

点击查看代码
#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;
}

s1存放第一个字符串的起始地址 , sizeof 计算地址变量所占字节数 ,strlen 计算数组大小
可以 , 先声明再赋值
交换指针,不换内存

=======================================================================================================

task3

点击查看代码
#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;
}

第一个是行指针,第二个是一个数组 其中每一个元素都是一个指针

======================================================================================================

task4

点击查看代码
#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++;
    }
}

将一种字符替换成另一种,
可以

====================================================================================================

task5

点击查看代码
#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;
}

// 函数str_trunc定义
// 功能: 对字符串作截断处理,把指定字符自第一次出现及其后的字符全部删除, 并返回字符串地址

char *str_trunc(char *str, char x)
{
	char *p = str;
	while(*p++ != x );
	p--;
	*p = '\0';
	
	return p;
}

去掉line18 后 无法输入截取字符,用于去回车

===================================================================================================

task6

点击查看代码
#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) {
	int i = 0;
	int map[129];
	map[88] = 0;
	for(i = 48 ;i<=57 ;i++){
		map[i]=1;
	}
	char* p=str; 
    for(i = 0 ; i<18 ;i++)
    {
    	if(i == 17)
    		map[88] = 1;
    	if(*p == '\0')
    		return 0;
    	if(!map[*p] )
    		return 0;
    	p++;
	}
	return 1;
}

========================================================================

task7

点击查看代码
#include <stdio.h>
#include <string.h>
#define N 80
void encoder(char *str, int n); // 函数声明
void decoder(char *str, int n); // 函数声明
struct circle{
    int character;
    int next,before;
}cir1[27],cir2[27];

int main() {
    char words[N];
    int n;
    
	char letter1 = 'a';
    char letter2 = 'A';
    int i = 0;
    for( i = 0 ;i < 26 ;i++)
    {
        cir1[i].character = letter1++;
        cir1[i].next = i + 1;
        cir1[i].before = i-1;
    }
    cir1[26].next = 0;
    cir1[0].before = 26;

    for( i = 0 ;i < 26 ;i++)
    {
        cir2[i].character = letter2++;
        cir2[i].next = i + 1;
    }
    cir2[26].next = 0;
    cir2[0].before = 26;
    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;
}

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

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

===============================================================

task8

点击查看代码
#include <stdio.h>
#include <string.h> 

int main(int argc, char *argv[]) {
    int i=0,j,n=-1;
   
    char ch[30];
    	
    while(argv[i] != NULL )
    {
    	n++;
    	i++;
	}

    for(i = 1 ;i<n ;i++)
    {
    	for(j = 1 ;j<n ;j++)
    		if(strcmp(argv[j] , argv[j+1]) < 0)
    		{ 
    			strcpy(ch ,argv[j]);
    			strcpy(argv[j] , argv[j+1]);
    			strcpy(argv[j+1] ,ch);
			}
	}

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

    return 0;
}

posted @ 2024-12-08 19:45  yuanzhan  阅读(4)  评论(0编辑  收藏  举报