陈凯迪的实验6

一、实验结论

  1.1

    代码:

    
 1 #include <stdio.h>
 2 #define N 4
 3 
 4 int main()
 5 {
 6     int x[N] = {1, 9, 8, 4};
 7     int i;
 8     int *p;
 9     
10     // ��ʽ1��ͨ�����������±�����������Ԫ��
11     for(i=0; i<N; ++i)
12         printf("%d", x[i]);
13     printf("\n");
14     
15     // ��ʽ2��ͨ��ָ����������������Ԫ�� ��д��1�� 
16     for(p=x; p<x+N; ++p)
17         printf("%d", *p);
18     printf("\n"); 
19     
20     // ��ʽ2��ͨ��ָ����������������Ԫ�أ�д��2�� 
21     p = x;
22     for(i=0; i<N; ++i)
23         printf("%d", *(p+i));
24     printf("\n");
25 
26     // ��ʽ2��ͨ��ָ����������������Ԫ�أ�д��3�� 
27     p = x;
28     for(i=0; i<N; ++i)
29         printf("%d", p[i]);
30     printf("\n");
31         
32     return 0;
33 }
View Code

    图片:

    
Picture

  1.2

    代码:

    
 1 #include <stdio.h>
 2 #define N 4
 3 
 4 int main()
 5 {
 6     char x[N] = {'1', '9', '8', '4'};
 7     int i;
 8     char *p;
 9     
10     // ��ʽ1��ͨ�����������±�����������Ԫ��
11     for(i=0; i<N; ++i)
12         printf("%c", x[i]);
13     printf("\n");
14     
15     // ��ʽ2��ͨ��ָ����������������Ԫ�� ��д��1�� 
16     for(p=x; p<x+N; ++p)
17         printf("%c", *p);
18     printf("\n"); 
19     
20     // ��ʽ2��ͨ��ָ����������������Ԫ�أ�д��2�� 
21     p = x;
22     for(i=0; i<N; ++i)
23         printf("%c", *(p+i));
24     printf("\n");
25 
26     // ��ʽ2��ͨ��ָ����������������Ԫ�أ�д��3�� 
27     p = x;
28     for(i=0; i<N; ++i)
29         printf("%c", p[i]);
30     printf("\n");
31         
32     return 0;
33 }
View Code

    图片:

    
Picture

  1.3

    回答:1:2004  2:2008  3:指针所指向的地址存放的值的类型不同,int占4bit而char占8bit。

  

  2.1

    代码:

    
 1 #include <stdio.h>
 2 
 3 int main()
 4 {
 5     int x[2][4] = { {1,9,8,4}, {2,0,2,2}} ;
 6     int i, j;
 7     int *p;            // ָ����������int�������ݵĵ�ַ 
 8     int (*q)[4];    // 
 9     
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     // ʹ��ָ�����p��ӷ��ʶ�ά����Ԫ��
19     for(p = &x[0][0], i = 0; p < &x[0][0] + 8; ++p, ++i)
20     {
21         printf("%d", *p);
22         if( (i+1)%4 == 0)
23             printf("\n");
24     }
25     
26     // ʹ��ָ�����q��ӷ��ʶ�ά����Ԫ�� 
27     for(q=x; q<x+2; ++q)
28     {
29         for(j=0; j<4; ++j)
30             printf("%d", *(*q+j));
31         printf("\n");
32     }
33     
34     return 0;
35 }
View Code

    图片:

    
Picture

  2.2

    代码:

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

    图片:

    
Picture

  2.3

    回答:1:2004、2016  2:2008、2032  3:列指针每次指向下一个、行指针每次指向下一行。

 

  3.1

    代码:

    
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main()
 6 {
 7     char s1[] = "C, I love u.";
 8     char s2[] = "C, I hate u.";
 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     return 0;
29 }
View Code

    图片:

    
Picture

  3.2

    代码:

    
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 80
 4 
 5 int main()
 6 {
 7     char *s1 = "C, I love u.";
 8     char *s2 = "C, I hate u.";
 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     return 0;
29 }
View Code

    图片:

    
Picture

    3.3

    回答:1、  1)13、数组的长度、字符串的长度  2)不能  3)是。

       2、  1)字符串的地址、指针的长度(CPU总线的宽度)、字符串的长度  2)不能  3)交换两个指针指向的地址、内存存储没有交换。

 

  4.1

    代码:

    
 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 
 5 int check_id(char *str);   // �������� 
 6 
 7 int main()
 8 {
 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     return 0;
23 }
24 
25 // ��������
26 // ����: ���ָ��strָ�������֤���봮��ʽ���Ƿ�Ϸ���
27 // ��ʽ�Ϸ�������1�����򣬷���0 
28 int check_id(char *str)
29 {
30     char *p;
31     int i=0;
32     p =str;
33     while(*p){
34         if(*p>='0'&&*p<='9'||*p=='X')
35             i++;
36         p++;
37     }
38     if(i==18)
39         return 1;
40     else
41          return 0;     
42 }
View Code

    图片:

    
Picture

 

  5

    代码:

    
 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define N 80
 5 int is_palindrome(char *s);      // �������� 
 6 
 7 int main()
 8 {
 9     char str[N];
10     int flag;
11 
12     printf("Enter a string:\n");
13     gets(str);
14 
15     flag = is_palindrome(str);   // �������� 
16 
17     if (flag)
18         printf("YES\n");
19     else
20         printf("NO\n");
21 
22     return 0;
23 }
24 
25 // ��������
26 // ���ܣ��ж�sָ����ַ����Ƿ��ǻ��Ĵ�
27 // ����ǣ�����1�����򣬷���0 
28 int is_palindrome(char *s)
29 {
30     char *p,*q;
31     q=s+strlen(s)-1;
32     p=s;
33     while(p<q){
34         if(*p!=*q)
35             break;
36         p++;
37         q--;
38     }
39     if(p>=q)
40         return 1;
41     else 
42         return 0;
43 }
View Code

    图片:

    
Picture

 

  6

    代码:

    
 1 #include <stdio.h>
 2 #define N 80
 3 void encoder(char *s);  // �������� 
 4 void decoder(char *s);  // ��������
 5 
 6 int main()
 7 {
 8     char words[N];
 9     
10     printf("English: ");
11     gets(words);
12     
13     printf("encoding: ");
14     encoder(words);  // ��������
15     printf("%s\n", words);
16     
17     printf("decoding: ");
18     decoder(words);  // ��������
19     printf("%s\n", words);
20     
21     return 0;
22 }
23 
24 
25 
26 void encoder(char *s)
27 {
28     char *p;
29     p=s;
30     while(*p){
31         if(*p>=65&&*p<=89||*p>=97&&*p<=121)
32             *p+=1;
33         else if(*p==90||*p==122)
34             *p-=25;
35         p++;
36     }
37 }
38 
39 
40 
41 void decoder(char *s)
42 {
43     char *p;
44     p=s;
45     while(*p){
46         if(*p>=66&&*p<=90||*p>=98&&*p<=122)
47             *p-=1;
48         else if(*p==65||*p==97)
49             *p+=25;
50         p++;
51     }
52 }
View Code

    图片:

    
Picture

 

 二、实验总结

  增加了关于指针的知识,了解了sizeof(指针)的真正含义,加深了关于列指针和行指针的概念。

  另:端午快乐:)

端午快乐鸭

 

 

 

posted on 2022-06-07 16:43  CKDDOUBI  阅读(43)  评论(2编辑  收藏  举报

导航