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

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

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

Task1

task1_1

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

A1:寻找输入数据中的最大值和最小值。

A2:指向x[0]的地址。

 

task1_2

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

A1:返回一组数据中的最大值的地址。

A2:可以。

 

Task2

task2_1

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

A1: 数组s1的大小是24。

      sizeof(s1)计算的是s1占用的内存字节数。

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

A2:不能。因为s1存放地址,不能直接将字符串赋值给它。

A3:已经交换。

 

task2_2

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

A1:指针变量s1中存放的是字符串地址。

     sizeof(s1) 计算的是单词数。

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

A2:不能。task2_1中定义的是数组,而这里是指针数组。

A3:交换两个指针所指的地址。没有交换。

 

Task3

 1 #include <stdio.h>
 2 #include<stdlib.h>
 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     system("pause");
32     return 0;
33 }

A:int (*ptr)[4]; 中,标识符ptr表示的语义是指针。

    int *ptr[4]; 中,标识符ptr表示的语义是指针数组名 。

 

Task4

task4_1

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

A1: 函数 replace 的功能是替换字符。

A2: line24,圆括号里循环条件可以改写成 *str != '\0' 。 

 

task4_2

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

 

Task5

task5_1

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

 

task5_2

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

A:这两种算法实现中交换的是指针变量的值。

 

Task6

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

 

Task7

 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 
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     while (*str) {
31         if (*str == 'z')
32             *str = 'a';
33         else if (*str == 'Z')
34             *str == 'A';
35         else if ((*str >= 'a' && *str <= 'y') || (*str >= 'A' && *str <= 'Y'))
36             (*str)++;
37         str++;
38     }
39         
40 }
41 
42 /*函数定义
43 功能:对s指向的字符串进行解码处理
44 解码规则:
45 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换
46 其它非字母字符,保持不变
47 */
48 void decoder(char* str) {
49     while (*str) {
50         if (*str == 'a')
51             *str = 'z';
52         else if (*str == 'A')
53             *str == 'z';
54         else if ((*str >= 'b' && *str <= 'z') || (*str >= 'B' && *str <= 'Z'))
55             (*str)--;
56         str++;
57     }
58 
59 }

 

标签:int,s1,编程,C语言,char,str,printf,include,指针
From: https://www.cnblogs.com/nuistjcy/p/18202925

相关文章

  • 实验5 C语言指针应用编程
    task1_1.c1#include<stdio.h>2#defineN534voidinput(intx[],intn);5voidoutput(intx[],intn);6voidfind_min_max(intx[],intn,int*pmin,int*pmax);78intmain(){9inta[N];10intmin,max;1112print......
  • 蟒蛇书(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语言文件读写文件分类:二进制文件:把数据的补码直接写入文件,这种文件叫二进制文件。​优点:读写和写入时不需要进行转换,所以读写速度快,数据安全性高。​缺点:不能使用文本编译器打开,无法阅读。文本文件:把数据转换成字符串写入文件,也就是把字符的二进制写入文件,这种文件......