首页 > 编程语言 >实验5 C语言指针应用编程

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

时间:2024-05-20 20:45:29浏览次数:20  
标签:int s1 编程 C语言 char ++ str printf 指针

task1_1.c

 1 #include <stdio.h>
 2 #define N 5
 3 
 4 void input(int x[], int n);
 5 void output(int x[], int n);
 6 void find_min_max(int x[], int n, int *pmin, int *pmax);
 7 
 8 int main() {
 9     int a[N];
10     int min, max;
11 
12     printf("录入%d个数据:\n", N);
13     input(a, N);
14 
15     printf("数据是: \n");
16     output(a, N);
17 
18     printf("数据处理...\n");
19     find_min_max(a, N, &min, &max);
20 
21     printf("输出结果:\n");
22     printf("min = %d, max = %d\n", min, max);
23 
24     return 0;
25 }
26 
27 void input(int x[], int n) {
28     int i;
29 
30     for(i = 0; i < n; ++i)
31         scanf("%d", &x[i]);
32 }
33 
34 void output(int x[], int n) {
35     int i;
36     
37     for(i = 0; i < n; ++i)
38         printf("%d ", x[i]);
39     printf("\n");
40 }
41 
42 void find_min_max(int x[], int n, int *pmin, int *pmax) {
43     int i;
44     
45     *pmin = *pmax = x[0];
46 
47     for(i = 1; i < n; ++i)
48         if(x[i] < *pmin)
49             *pmin = x[i];
50         else if(x[i] > *pmax)
51             *pmax = x[i];
52 }

1.找出一组数的最大值和最小值

2.p指向x[0]所在的地址

 

task1_2.c

 1 #include <stdio.h>
 2 #define N 5
 3 
 4 void input(int x[], int n);
 5 void output(int x[], int n);
 6 int *find_max(int x[], int n);
 7 
 8 int main() {
 9     int a[N];
10     int *pmax;
11 
12     printf("录入%d个数据:\n", N);
13     input(a, N);
14 
15     printf("数据是: \n");
16     output(a, N);
17 
18     printf("数据处理...\n");
19     pmax = find_max(a, N);
20 
21     printf("输出结果:\n");
22     printf("max = %d\n", *pmax);
23     
24     return 0;
25 }
26 
27 void input(int x[], int n) {
28     int i;
29 
30     for(i = 0; i < n; ++i)
31         scanf("%d", &x[i]);
32 }
33 
34 void output(int x[], int n) {
35     int i;
36     
37     for(i = 0; i < n; ++i)
38         printf("%d ", x[i]);
39     printf("\n");
40 }
41 
42 int *find_max(int x[], int n) {
43     int max_index = 0;
44     int i;
45 
46     for(i = 1; i < n; ++i)
47         if(x[i] > x[max_index])
48             max_index = i;
49     
50     return &x[max_index];
51 }

1.返回数组中最大元素的地址

2.可以

 

task2_1.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main() {
 6     char s1[] = "Learning makes me happy";
 7     char s2[] = "Learning makes me sleepy";
 8     char tmp[N];
 9 
10     printf("sizeof(s1) vs. strlen(s1): \n");
11     printf("sizeof(s1) = %d\n", sizeof(s1));
12     printf("strlen(s1) = %d\n", strlen(s1));
13 
14     printf("\nbefore swap: \n");
15     printf("s1: %s\n", s1);
16     printf("s2: %s\n", s2);
17 
18     printf("\nswapping...\n");
19     strcpy(tmp, s1);
20     strcpy(s1, s2);
21     strcpy(s2, tmp);
22 
23     printf("\nafter swap: \n");
24     printf("s1: %s\n", s1);
25     printf("s2: %s\n", s2);
26 
27     return 0;
28 }

1.大小为24,sizeof(s1)计算字符个数,strlen(s1)计算整个整个数组所占字节数

2.不能,没有提前定义数组长度,字符串也不能直接赋值给字符数组中,需要借助strcpy函数

3.交换了

 

task2_2.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main() {
 6     char *s1 = "Learning makes me happy";
 7     char *s2 = "Learning makes me sleepy";
 8     char *tmp;
 9 
10     printf("sizeof(s1) vs. strlen(s1): \n");
11     printf("sizeof(s1) = %d\n", sizeof(s1));
12     printf("strlen(s1) = %d\n", strlen(s1));
13 
14     printf("\nbefore swap: \n");
15     printf("s1: %s\n", s1);
16     printf("s2: %s\n", s2);
17 
18     printf("\nswapping...\n");
19     tmp = s1;
20     s1 = s2;
21     s2 = tmp;
22 
23     printf("\nafter swap: \n");
24     printf("s1: %s\n", s1);
25     printf("s2: %s\n", s2);
26 
27     return 0;
28 }

1.s1存放字符串的地址,sizeof(s1)计算指针大小,strlen(s1)计算字符串大小

2.不能,这里的s1是一个指向字符的指针,2_1中的s1是字符数组

3.交换了两个指针的值;没有交换

 

task3.c

 1 #include <stdio.h>
 2 
 3 int main() {
 4     int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
 5     int i, j;
 6     int *ptr1;     // 指针变量,存放int类型数据的地址
 7     int(*ptr2)[4]; // 指针变量,指向包含4个int元素的一维数组
 8 
 9     printf("输出1: 使用数组名、下标直接访问二维数组元素\n");
10     for (i = 0; i < 2; ++i) {
11         for (j = 0; j < 4; ++j)
12             printf("%d ", x[i][j]);
13         printf("\n");
14     }
15 
16     printf("\n输出2: 使用指向元素的指针变量ptr1间接访问二维数组元素\n");
17     for (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {
18         printf("%d ", *ptr1);
19 
20         if ((i + 1) % 4 == 0)
21             printf("\n");
22     }
23                          
24     printf("\n输出3: 使用指向一维数组的指针变量ptr2间接访问二维数组元素\n");
25     for (ptr2 = x; ptr2 < x + 2; ++ptr2) {
26         for (j = 0; j < 4; ++j)
27             printf("%d ", *(*ptr2 + j));
28         printf("\n");
29     }
30 
31     return 0;
32 }
1 #include < stdio.h> 2 3 intmain() {4 int x[2][4] = {{1, 9, 8, 4}, {2, 0, 4, 9}};5 因蒂,J;6 int *ptr1; 指针变量,存放int类型数据的地址 7 int(*ptr2)[4];指针变量,指向包含4个int元素的一维数组 8 9 printf(“输出1: 使用数组名、下标直接访问二维数组元素\n”);10 表示 (i = 0; i < 2; ++i) {11 表示 (j = 0; j < 4; ++j)12 printf(“%d”, x[i][j]);13 printf(“\n”);14 }15 16 printf(“\n输出2: 使用指向元素的指针变量ptr1间接访问二维数组元素\n”);17 表示 (ptr1 = &x[0][0], i = 0; ptr1 < &x[0][0] + 8; ++ptr1, ++i) {18 printf(“%d”, *ptr1);19 20 如果 ((i + 1) % 4 == 0)21 printf(“\n”);22 }23 24 printf(“\n输出3: 使用指向一维数组的指针变量ptr2间接访问二维数组元素\n”);25 表示 (ptr2 = x; ptr2 < x + 2; ++ptr2) {26 表示 (j = 0; j < 4; ++j)27 printf(“%d”, *(*ptr2 +j));28 printf(“\n”);29 }30 31 返回 0;32 }

1.int (*ptr)[4]中ptr表示指向一维数组的指针

2.int *ptr[4]中ptr 是一个指针数组,数组中每个元素都是一个指针

 

task4_1.c

 1 #include <stdio.h>
 2 #define N 80
 3 
 4 void replace(char *str, char old_char, char new_char); // 函数声明
 5 
 6 int main() {
 7     char text[N] = "c programming is difficult or not, it is a question.";
 8 
 9     printf("原始文本: \n");
10     printf("%s\n", text);
11 
12     replace(text, 'i', '*'); // 函数调用 注意字符形参写法,单引号不能少
13 
14     printf("处理后文本: \n");
15     printf("%s\n", text);
16 
17     return 0;
18 }
19 
20 // 函数定义
21 void replace(char *str, char old_char, char new_char) {
22     int i;
23 
24     while(*str) {
25         if(*str == old_char)
26             *str = new_char;
27         str++;
28     }
29 }
1 #include < stdio.h> 2 #define N 80 3 4 void replace(char *str, char old_char, char new_char); //函数声明 5 6 intmain() {7 char text[N] = “c 编程难不难,这是一个问题。8 9 printf(“原始文本: \n”);10 printf(“%s\n”, 文本);11 12 replace(text, 'i', '*');函数调用 注意字符形参写法,单引号不能少 13 14 printf(“处理后文本: \n”);15 printf(“%s\n”, 文本);16 17 返回 0;18 }19 20 //函数定义 21 void replace(char *str, char old_char, charnew_char) {22 inti;23 24 while(*str) {25 if(*str ==old_char)26 *str =new_char;27 str++;28 }29 }

1.替换字符串中的某个字符为指定字符

2.可以

task4_2.c

 1 #include <stdio.h>
 2 #define N 80
 3 
 4 void str_trunc(char *str, char x);
 5 
 6 int main() {
 7     char str[N];
 8     char ch;
 9 
10     printf("输入字符串: ");
11     gets(str);
12 
13     printf("输入一个字符: ");
14     ch = getchar();
15 
16     printf("截断处理...\n");
17     str_trunc(str, ch);
18 
19     printf("截断处理后的字符串: %s\n", str);
20     
21 }
22 
23 void str_trunc(char *str, char x) {
24     while(*str) {
25         if(*str == x)
26             *str='\0';   // blank1
27 
28         str++;// blank2
29     }
30 
31     return;// blank3
32 }

 

task5_1.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 void sort(char *name[], int n);
 4 
 5 int main() {
 6     char *course[4] = {"C Program",
 7                        "C++ Object Oriented Program",
 8                        "Operating System",
 9                        "Data Structure and Algorithms"};
10     int i;
11 
12     sort(course, 4);
13 
14     for (i = 0; i < 4; i++)
15         printf("%s\n", course[i]);
16 
17     return 0;
18 }
19 
20 void sort(char *name[], int n) {
21     int i, j;
22     char *tmp;
23 
24     for (i = 0; i < n - 1; ++i)
25         for (j = 0; j < n - 1 - i; ++j)
26             if (strcmp(name[j], name[j + 1]) > 0) {
27                 tmp = name[j];
28                 name[j] = name[j + 1];
29                 name[j + 1] = tmp;
30             }
31 }

 

task5_2.c

 1 #include <stdio.h>
 2 #include <string.h>
 3 void sort(char *name[], int n);
 4 
 5 int main() {
 6     char *course[4] = {"C Program",
 7                        "C++ Object Oriented Program",
 8                        "Operating System",
 9                        "Data Structure and Algorithms"};
10     int i;
11 
12     sort(course, 4);
13     for (i = 0; i < 4; i++)
14         printf("%s\n", course[i]);
15 
16     return 0;
17 }
18 
19 void sort(char *name[], int n) {
20     int i, j, k;
21     char *tmp;
22 
23     for (i = 0; i < n - 1; i++) {
24         k = i;
25         for (j = i + 1; j < n; j++)
26             if (strcmp(name[j], name[k]) < 0)
27                 k = j;
28 
29         if (k != i) {
30             tmp = name[i];
31             name[i] = name[k];
32             name[k] = tmp;
33         }
34     }
35 }

交换的是指针变量的值

 

task6.c

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 
 5 int check_id(char *str); // 函数声明
 6 
 7 int main() {
 8     char *pid[N] = {"31010120000721656X",
 9                     "330106199609203301",
10                     "53010220051126571",
11                     "510104199211197977",
12                     "53010220051126133Y"};
13     int i;
14 
15     for (i = 0; i < N; ++i)
16         if (check_id(pid[i])) // 函数调用
17             printf("%s\tTrue\n", pid[i]);
18         else
19             printf("%s\tFalse\n", pid[i]);
20 
21     return 0;
22 }
23 
24 // 函数定义
25 // 功能: 检查指针str指向的身份证号码串形式上是否合法。
26 // 形式合法,返回1,否则,返回0
27 int check_id(char *str) {
28     int i,len;
29     len=strlen(str);
30     if(len!=18)
31         return 0;
32     for(i=0;i<len-1;i++)
33     {
34         if(*(str+i)>'9' || *(str+i)<'0')
35         return 0;
36         
37     }
38     if(*(str+len-1)>='0' && *(str+len-1)<='9'){
39     }
40     else if(*(str+len-1)!='X'){
41         return 0;
42     }
43     return 1;
44     // 补足函数实现
45     // ...
46 }
1 #include < stdio.h> 2 #include < string.h> 3 #define N 5 4 5 int check_id(char *str); //函数声明 6 7 intmain() {8 char *pid[N] = {“31010120000721656X”,9 “330106199609203301”,10 “53010220051126571”,11 “510104199211197977”,12 “53010220051126133Y”};13 英寸;14 15 for (i = 0; i < N; ++i)16 if (check_id(pid[i])) //函数调用 17 printf(“%s\tTrue\n”, pid[i]);18 else 19 printf(“%s\tFalse\n”, pid[i]);20 21 返回 0;22 }23 24 //函数定义25 //功能: 检查指针str指向的身份证号码串形式上是否合法。26 //形式合法,返回1,否则,返回0 27 int check_id(char *str) {28 inti,len;29 len=strlen(str);30 if(len!=18)31 返回 0;32 for(i=0;i<len-1;i++)33 {34="" if(*(str+i)="">'9' || *(str+i)<'0')35 return 0;36 37 }38 if(*(str+len-1)>='0' && *(str+len-1)<='9'){39 }40 else if(*(str+len-1)!='X'){41 return 0;42 }43 返回 1;44 //补足函数实现45 //...46 }

 

task.7

 1 #include <stdio.h>
 2 #define N 80
 3 void encoder(char *str); // 函数声明
 4 void decoder(char *str); // 函数声明
 5 
 6 int main() {
 7     char words[N];
 8 
 9     printf("输入英文文本: ");
10     gets(words);
11 
12     printf("编码后的英文文本: ");
13     encoder(words); // 函数调用
14     printf("%s\n", words);
15 
16     printf("对编码后的英文文本解码: ");
17     decoder(words); // 函数调用
18     printf("%s\n", words);
19     system("pause");
20     return 0;
21 }
22 
23 /*函数定义
24 功能:对s指向的字符串进行编码处理
25 编码规则:
26 对于a~z或A~Z之间的字母字符,用其后的字符替换; 其中,z用a替换,Z用A替换
27 其它非字母字符,保持不变
28 */
29 void encoder(char *str) {
30     // 补足函数实现
31     // ×××
32     while(*str)
33     {
34         if(*str=='z')
35         {
36             *str='a';
37         }
38         else if(*str=='Z')
39         {
40             *str='A';
41         }
42         else if(*str>='a' && *str<'z')
43         {
44             (*str)++;
45         }
46         else if(*str>='A' && *str<'Z')
47         {
48             (*str)++;
49         }
50         str++;
51     }
52 }
53 
54 /*函数定义
55 功能:对s指向的字符串进行解码处理
56 解码规则:
57 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换
58 其它非字母字符,保持不变
59 */
60 void decoder(char *str) {
61     // 补足函数实现
62     // ×××
63     while(*str)
64     {
65         if(*str=='a')
66         {
67             *str='z';
68         }
69         else if(*str=='A')
70         {
71             *str='Z';
72         }
73         else if(*str>'a' && *str<='z')
74         {
75             (*str)--;
76         }
77         else if(*str>'A' && *str<='Z')
78         {
79             (*str)--;
80         }
81         str++;
82     }
83 }

 

标签:int,s1,编程,C语言,char,++,str,printf,指针
From: https://www.cnblogs.com/1262438226qqcom/p/18202049

相关文章

  • 蟒蛇书(Python编程:从入门到实践)第17章使用API 17.1.4处理API响应报错Caused by ProxyEr
    书上提供的原始代码:importrequests#执行API调用并存储响应url='https://api.github.com/search/repositories?q=language:python&sort=stars'headers={'Accept':'application/vnd.github.v3+json'}r=requests.get(url,headers=headers)prin......
  • Shell编程之免交互
    目录1.HereDocument免交互2.HereDocument常规用法(1)免交互执行命令(2)免交互创建并编辑文件内容(3)tee3.Expect免交互4.编写expect免交互脚本步骤5.read1.HereDocument免交互使用I/O重定向的方式将命令列表提供给交互式程序标准输入的一种替代品命令<<标记...内容 #标记之......
  • C语言之“if”的注意事项
    近来本人计算机小白开始了学习C语言,然后呢,问题也是非常多的,今天本人想来一个小结,以下便是正文:①注意一定要输入“cin”表示你需要输入东西,要不然就会出现一个问题,那么就是你可以直接输出但是一定没有输入的黑框,所以,小孩儿,要记得输入啊!②“=”与“==”是两个意思,“=”是赋值,把右......
  • C++的四种智能指针及简单实现
    C++的四种智能指针及简单实现参考:C++智能指针讲解及模拟实现一、auto_ptrauto_ptr有拷贝语义,拷贝后源对象变为空指针,可能引发严重问题template<classT>classmy_unique_ptr{private:T*_ptr;public://普通构造函数my_unique_ptr(T*ptr):_ptr(ptr){}......
  • 关于一重指针和二重指针的区别和联系
    一、关键词指针,二重指针二:知识点1.指针的本身地址、指针所指向地址、指针所指向地址内容本身地址:指针也是一个类型,通常是在64位系统上是8字节,32位是4字节。既然是类型对象就需要存储地址,因此本身地址指的是用于存储指针这个变量值的地址。指针指向地址:指针是个变量,这个变......
  • 指针练习班级成绩
    1.求第一门课程的平均分2.找出有两门以上不及格的学生3.找出平均分在90分以上或全部课程在85分以上的学生#include<stdio.h>#include<math.h>#include<string.h>#defineM4#defineN5voidAverage(int*arr,intn);voidTwoFail(int*arr);voidOutputFail(int(*p)[N......
  • 指针数组练习排列字符串
    用指针数组实现排列字符串#include<stdio.h>#include<math.h>#include<string.h>#defineN5voidOrderString(char*p[],intn);intmain(){char*arr[10]={"Hello","Howareyou?","I'mfine","Ilovecomputer......
  • C++ 多线程编程要点总结
    C++多线程编程要点总结:选择合适的线程库:C++11引入了 <thread> 头文件,提供了对线程的原生支持。也可以使用第三方库,如Boost.Thread,它提供了更多高级功能和更好的跨平台兼容性。线程创建与管理:使用 std::thread 类创建新线程,并传入函数或可调用对象作为线程的入口......
  • C语言-文件读写
    C语言文件读写文件分类:二进制文件:把数据的补码直接写入文件,这种文件叫二进制文件。​优点:读写和写入时不需要进行转换,所以读写速度快,数据安全性高。​缺点:不能使用文本编译器打开,无法阅读。文本文件:把数据转换成字符串写入文件,也就是把字符的二进制写入文件,这种文件......
  • C#基于.net framework的应用开发实战编程(一) - 编程手把手系列文章
    上次介绍了C#的基于.netframework的Dll类库和Winform的编程过程,今天就来个实战演练一下,结合上次的内容,让读者能够有一个实战的过程,知道怎么用C#进行Winform的编程过程,实现一个小应用。       准备工作;因为软件研发主要从需求、设计、编码、测试、安装这个过程......