• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

cxj114-514

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

实验五

1.实验任务1

task1_1.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 4
 4 
 5 int main()
 6 {
 7     int x[N] = {1,9,8,4};
 8     int i;
 9     int *p;
10     for(i = 0;i < N;i++)
11         printf("%d",x[i]);
12     printf("\n");
13 
14     for(p = x; p < x + N;++p)
15         printf("%d",*p);
16     printf("\n");
17 
18     p = x;
19     for(i = 0;i < N;i++)
20         printf("%d",*(p+i));
21     printf("\n");
22 
23     p = x;
24     for(i = 0;i < N;i++)
25         printf("%d",p[i]);
26     printf("\n");
27 
28     system("pause");
29     return 0;
30 }

task1_2.c

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 int main()
 5 {
 6     int x[2][4] = {{1,9,8,4},{2,0,4,9}};
 7     int i,j;
 8     int *p;
 9     int(*q)[4];
10 
11     for(i = 0;i < 2;++i)
12     {
13         for(j = 0;j < 4;++j)
14             printf("%d",x[i][j]);
15         printf("\n");
16     }
17 
18     for(p = &x[0][0],i = 0;p < &x[0][0] + 8;++p,++i)
19     {
20         printf("%d",*p);
21         if((i+1)%4==0)
22             printf("\n");
23     }
24 
25     for(q = x;q < x + 2;++q)
26     {
27         for(j = 0;j < 4;++j)
28             printf("%d",*(*q+j));
29         printf("\n");
30     }
31 
32     system("pause");
33     return 0;
34 }

 

2.实验任务2

task2_1.c

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

问题1:大小是24个字节。sizeof(s1)计算的是字符串数组所占空间的大小,strlen(s1)统计的是字符串的字符长度。

问题2:不能。给一维数组定义时要说明长度分配内存。

问题3:交换了。

task2_2.c

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

问题1:存放的是字符串。sizeof(s1)计算的是指针变量s1所占的内存空间大小,strlen(s1)统计的是存放字符串的长度。

问题2:能。task2_1是定义一个字符型数组,把字符串储存进数组里面,task2_2是定义一个指针变量,先占用内存,再把字符串储存进去。

问题3:交换的是指针变量的值,在内存单元中没有交换。

3.实验任务3

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void str_cpy(char *target,const char *source);
 5 void str_cat(char *str1,char *str2);
 6 
 7 int main()
 8 {
 9     char s1[80], s2[20] = "1984";
10 
11     str_cpy(s1,s2);
12     puts(s1);
13 
14     str_cat(s1," Animal Farm");
15     puts(s1);
16 
17     system("pause");
18     return 0;
19 }
20 
21 void str_cpy(char *target,const char *source)
22 {
23     while(*target++ = *source++)
24         ;
25 }
26 
27 void str_cat(char *str1,char *str2)
28 {
29     while(*str1)
30         str1++;
31 
32     while(*str1++ = *str2++)
33         ;
34 }

 

4.实验任务4

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 4 
 5 int func(char *);
 6 
 7 int main()
 8 {
 9     char str[80];
10 
11     while(gets(str)!=NULL)
12     {
13         if(func(str))
14             printf("yes\n");
15         else
16             printf("no\n");
17     }
18 
19     system("pause");
20     return 0;
21 }
22 
23 int func(char *str)
24 {
25     char *begin, *end;
26 
27     begin = end = str;
28 
29     while(*end)
30         end++;
31 
32     end--;
33 
34     while(begin < end)
35     {
36         if(*begin!= *end)
37             return 0;
38         else
39         {
40             begin++;
41             end--;
42         }
43     }
44     return 1;
45 }

 

5.实验任务5

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 80
 4 
 5 void func(char *);
 6 
 7 int main()
 8 {
 9     char s[N];
10 
11     while(scanf("%s",s)!=EOF)
12     {
13         func(s);
14         puts(s);
15     }
16 
17     system("pause");
18     return 0;
19     
20 }
21 
22 void func(char *str)
23 {
24     int i;
25     char *p1,*p2,*p;
26 
27     p1 = str;
28     while(*p1 == '*')
29         p1++;
30     p2 = str;
31     while(*p2)
32         p2++;
33     p2--;
34     
35     while(*p2 == '*')
36         p2--;
37 
38     p = str;
39     i = 0;
40     while(p < p1)
41     {
42         str[i] = *p;
43         p++;
44         i++;
45     }
46 
47     while(p <= p2)
48     {
49         if(*p!='*')
50         {
51             str[i] = *p;
52             i++;
53         }
54         p++;
55     }
56 
57     while(*p!='\0')
58     {
59         str[i] = *p;
60         p++;
61         i++;
62     }
63 
64     str[i] = '\0';
65 }

 

6.实验任务6

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

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

交换的是指针变量的值。

7.实验任务7

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

 

8.实验任务8

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

 

 

posted on 2023-05-04 23:54  信专某黑化学子  阅读(30)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3