实验6 C语言结构体,枚举应用编程(附实验5 C语言指针应用编程)

发布时间 2023-12-11 21:06:27作者: Regec

实验6

一,实验目的

二,实验准备

三,实验内容

1,实验任务1

task1.c

  1 #include <stdio.h>
  2 #include <string.h> 
  3 #define N 3        // 运行程序输入测试时,可以把这个数组改小一些输入测试 
  4 
  5 typedef struct student {
  6     int id;             // 学号 
  7     char name[20];         // 姓名 
  8     char subject[20];     // 考试科目
  9     double perf;         // 平时成绩 
 10     double mid;         // 期中成绩 
 11     double final;         // 期末成绩
 12     double total;         // 总评成绩 
 13     char level[10];     // 成绩等级
 14 } STU;
 15 
 16 void input(STU [], int);            // 录入学生信息
 17 void output(STU [], int);            // 输出学生信息
 18 void calc(STU [], int);                // 计算总评和等级 
 19 int fail(STU [], STU [], int);        // 统计不及格学生信息
 20 void sort(STU [], int);                // 排序 
 21 
 22 int main() {
 23     STU st[N], fst[N];   // 数组st记录学生信息,fst记录不及格学生信息 
 24     int k;  // 用于记录不及格学生个数 
 25     
 26     printf("录入学生成绩信息:\n");
 27     input(st, N);
 28     
 29     printf("\n成绩处理...\n");
 30     calc(st, N);
 31     
 32     k = fail(st, fst, N);
 33     sort(st, N);
 34     printf("\n学生成绩排名情况:\n");
 35     output(st, N);
 36     
 37     printf("\n不及格学生信息:\n");
 38     output(fst, k);
 39     
 40     return 0;
 41 } 
 42 
 43 void input(STU s[], int n) {
 44     int i;
 45     
 46     for(i = 0; i < n; i++)
 47         scanf("%d %s %s %lf %lf %lf", &s[i].id, s[i].name, s[i].subject,
 48                                       &s[i].perf, &s[i].mid, &s[i].final);
 49 }
 50 
 51 void output(STU s[], int n) {
 52        int i;
 53    
 54       printf("-----------------\n");
 55       printf("学号   姓名     科目   平时   期中   期末   总评   等级\n");
 56        for(i = 0; i<n; i++)
 57           printf("%d   %-6s   %-4s   %-4.0f   %-4.0f   %-4.0f   %-4.1f   %s\n",s[i].id,s[i].name,s[i].subject,s[i].perf,s[i].mid,s[i].final,s[i].total,s[i].level);
 58 }
 59 
 60 
 61 void calc(STU s[],int n) {
 62     int i;
 63 
 64     for(i = 0; i < n; i++) {    
 65         s[i].total = s[i].perf * 0.2 + 
 66                      s[i].mid * 0.2 +
 67                      s[i].final * 0.6;
 68         
 69         if(s[i].total >= 90)
 70           strcpy(s[i].level, "");
 71         else if(s[i].total >= 80 && s[i].total < 90)
 72           strcpy(s[i].level, "");
 73         else if(s[i].total >= 70 && s[i].total < 80)
 74           strcpy(s[i].level, ""); 
 75         else if(s[i].total >= 60 && s[i].total < 70)
 76           strcpy(s[i].level, "及格");
 77         else
 78           strcpy(s[i].level, "不及格");         
 79     }
 80 }
 81 
 82 int fail(STU s[], STU t[], int n) {
 83       int i, cnt = 0;
 84       
 85       for(i = 0; i < n; i++)
 86           if(s[i].total < 60)
 87             t[cnt++] = s[i];
 88             
 89     return cnt;
 90 }
 91 
 92 void sort(STU s[], int n) {
 93     int i, j;
 94     STU t;
 95     
 96     for(i = 0; i < n-1; i++)
 97       for(j = 0; j < n-1-i; j++)
 98         if(s[j].total < s[j+1].total) {
 99             t = s[j];
100             s[j] = s[j+1];
101             s[j+1] = t;
102         }
103 }

2,实验任务2

task2.c

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 10
 4 #define M 80
 5 
 6 typedef struct {
 7     char name[M];       // 书名
 8     char author[M];     // 作者
 9 } Book;
10 
11 int main() {
12     Book x[N] = { {"《一九八四》", "乔治.奥威尔"},
13                   {"《美丽新世界》", "赫胥黎"},
14                   {"《昨日的世界》", "斯蒂芬.茨威格"}, 
15                   {"《万历十五年》", "黄仁宇"},
16                   {"《一只特立独行的猪》", "王小波"},
17                   {"《百年孤独》", "马尔克斯"},
18                   {"《查令十字街84号》", "海莲.汉芙"},
19                   {"《只是孩子》", "帕蒂.史密斯"}, 
20                   {"《刀锋》", "毛姆"},
21                   {"《沉默的大多数》", "王小波"} };
22     Book *ptr;
23     int i;
24     char author[M];
25 
26     // 使用指针遍历结构体数组
27     printf("所有图书信息: \n");
28     for(ptr = x; ptr < x + N; ++ptr)
29         printf("%-30s%-30s\n", ptr->name, ptr->author);
30 
31     // 查找指定作者的图书
32     printf("\n输入作者名: ");
33     gets(author);
34     for(ptr = x; ptr < x + N; ++ptr)
35         if(strcmp(ptr->author, author) == 0) {
36             printf("%-30s%-30s\n", ptr->name, ptr->author);
37         }
38 
39     return 0;
40 }

 

 

3,实验任务3

task3_1.c

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 typedef struct FilmInfo {
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct FilmInfo *next;
11 } Film;
12 
13 
14 void output(Film *head);   // 遍历输出链表信息
15 Film *insert(Film *head, int n);   // 向链表中插入n个结点,返回头指针
16 
17 
18 int main() {
19     int n;          // 结点数
20     Film *head;     // 头指针变量,存放链表中第一个节点的地址
21 
22     head = NULL;
23     printf("输入影片数目: ");
24     scanf("%d", &n);
25 
26     // 向链表中插入n部影片信息
27     head = insert(head, n);
28 
29     // 遍历输出链表中所有影片信息
30     printf("\n所有影片信息如下: \n");
31     output(head);
32 
33     return 0;
34 }
35 
36 // 向链表中插入n个结点,从表头插入,返回头指针变量
37 Film *insert(Film *head, int n) {
38     int i;
39     Film *p;
40 
41     for(i = 1; i <= n; ++i) {
42         p = (Film *)malloc(sizeof(Film));
43         printf("请输入第%d部影片信息: ", i);
44         scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
45         
46         // 把结点从表头插入到链表中
47         p->next = head;
48         head = p;   // 更新头指针变量
49     }
50 
51     return head;
52 }
53 
54 // 遍历输出链表信息
55 void output(Film *head) {
56     Film *p;
57 
58     p = head;
59     while(p != NULL) {
60         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
61         p = p -> next;
62     }
63 }

 

 

task3_2.c

 

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #define N 80
 4 
 5 typedef struct FilmInfo {
 6     char name[N];
 7     char director[N];
 8     char region[N];
 9     int year;
10     struct FilmInfo *next;
11 } Film;
12 
13 
14 void output(Film *head);   // 遍历输出链表信息
15 Film *insert(Film *head, int n);   // 向链表中插入n个结点,返回头指针
16 
17 
18 int main() {
19     int n;          // 结点数
20     Film *head;     // 头指针变量,存放链表中第一个节点的地址
21     Film *p;        // 存放新申请的Film节点内存空间地址
22 
23     // 创建头结点
24     p = (Film *)malloc(sizeof(Film));
25     p->next = NULL;
26     head = p;       // 头指针变量存放头节点的地址
27 
28     printf("输入影片数目: ");
29     scanf("%d", &n);
30 
31     // 向链表中插入n部影片信息
32     head = insert(head, n);
33 
34     // 遍历输出链表中所有影片信息
35     printf("\n所有影片信息如下: \n");
36     output(head);
37 
38     return 0;
39 }
40 
41 // 向链表中插入n个结点,从表头插入,返回头指针变量
42 Film *insert(Film *head, int n) {
43     int i;
44     Film *p;
45 
46     for(i = 1; i <= n; ++i) {
47         p = (Film *)malloc(sizeof(Film));
48         printf("请输入第%d部影片信息: ", i);
49         scanf("%s %s %s %d", p->name, p->director, p->region, &p->year);
50         
51         // 把结点从表头插入到链表中
52         p->next = head->next;
53         head->next = p;
54     }
55 
56     return head;
57 }
58 
59 // 遍历输出链表信息
60 void output(Film *head) {
61     Film *p;
62 
63     p = head->next;
64     while(p != NULL) {
65         printf("%-20s %-20s %-20s %d\n", p->name, p->director, p->region, p->year);
66         p = p -> next;
67     }
68 }

 

 

4,实验任务4

task4.c

 

 1 #include <stdio.h>
 2 #define N 10
 3 
 4 typedef struct {
 5     char isbn[20];          // isbn号
 6     char name[80];          // 书名
 7     char author[80];        // 作者
 8     double sales_price;     // 售价
 9     int  sales_count;       // 销售册数
10 } Book;
11 
12 void output(Book x[], int n);
13 void sort(Book x[], int n);
14 double sales_amount(Book x[], int n);
15 
16 int main() {
17     Book x[N] = {{"978-7-229-14156-1", "源泉", "安.兰德", 84, 59},
18                  {"978-7-5133-5261-1", "李白来到旧金山", "谭夏阳", 48, 16},
19                  {"978-7-5617-4347-8", "陌生人日记", "周怡芳", 72.6, 27},
20                  {"978-7-5722-5475-8", "芯片简史", "汪波", 74.9, 49},
21                  {"978-7-5046-9568-0", "数据化决策", "道格拉斯·W·哈伯德", 49, 42},
22                  {"978-7-5133-4388-6", "美好时代的背后", "凯瑟琳.布", 34.5, 39},
23                  {"978-7-1155-0509-5", "无穷的开始:世界进步的本源", "戴维·多伊奇", 37.5, 55},
24                  {"978-7-5321-5691-7", "何为良好生活", "陈嘉映", 29.5 , 31},
25                  {"978-7-5133-5109-6", "你好外星人", "英国未来出版集团", 118, 42},
26                  {"978-7-2011-4617-1", "世界尽头的咖啡馆", "约翰·史崔勒基", 22.5, 44}};
27     
28     printf("图书销量排名: \n");
29     sort(x, N);
30     output(x, N);
31 
32     printf("\n图书销售总额: %.2f\n", sales_amount(x, N));
33     
34     return 0;
35 }
36 
37 // 待补足:函数output()实现
38 // ×××
39 void output(Book x[], int n)
40 {
41     int i;
42     printf("ISBN号               书名                           作者                      售价   销售册数\n");
43     for(i=0;i<n;++i)
44     {
45         printf("%-20s %-30s %-25s %-8.1lf %-4d\n",x[i].isbn,x[i].name,x[i].author,x[i].sales_price,x[i].sales_count);
46     }
47 }
48 // 待补足:函数sort()实现
49 // ×××
50 void sort(Book x[], int n)
51 {
52     int i,j;
53     Book t;
54     for(i=0;i<n-1;++i)
55     {
56         for(j=0;j<n-1-i;++j)
57         {
58             if(x[j].sales_count<x[j+1].sales_count)
59             {
60                 t=x[j];
61                 x[j]=x[j+1];
62                 x[j+1]=t;
63             }
64         }
65     }
66 }
67 // 待补足:函数sales_count()实现
68 // ×××
69 double sales_amount(Book x[], int n)
70 {
71     int i;
72     double s=0;
73     for(i=0;i<n;++i)
74     {
75         s+=x[i].sales_count*x[i].sales_price;
76     }
77 
78     return s;
79 }

 

 

5,实验任务5

task5.c

 

  1 #include <stdio.h>
  2 
  3 typedef struct {
  4     int year;
  5     int month;
  6     int day;
  7 } Date;
  8 
  9 // 函数声明
 10 void input(Date *pd);                   // 输入日期给pd指向的Date变量
 11 int day_of_year(Date d);                // 返回日期d是这一年的第多少天
 12 int compare_dates(Date d1, Date d2);    // 比较两个日期: 
 13                                         // 如果d1在d2之前,返回-1;
 14                                         // 如果d1在d2之后,返回1
 15                                         // 如果d1和d2相同,返回0
 16 
 17 void test1() {
 18     Date d;
 19     int i;
 20 
 21     printf("输入日期:(以形如2023-12-11这样的形式输入)\n");
 22     for(i = 0; i < 3; ++i) {
 23         input(&d);
 24         printf("%d-%02d-%02d是这一年中第%d天\n\n", d.year, d.month, d.day, day_of_year(d));
 25     }
 26 }
 27 
 28 void test2() {
 29     Date Alice_birth, Bob_birth;
 30     int i;
 31     int ans;
 32 
 33     printf("输入Alice和Bob出生日期:(以形如2023-12-11这样的形式输入)\n");
 34     for(i = 0; i < 3; ++i) {
 35         input(&Alice_birth);
 36         input(&Bob_birth);
 37         ans = compare_dates(Alice_birth, Bob_birth);
 38         
 39         if(ans == 0)
 40             printf("Alice和Bob一样大\n\n");
 41         else if(ans == -1)
 42             printf("Alice比Bob大\n\n");
 43         else
 44             printf("Alice比Bob小\n\n");
 45     }
 46 }
 47 
 48 int main() {
 49     printf("测试1: 输入日期, 打印输出这是一年中第多少天\n");
 50     test1();
 51 
 52     printf("\n测试2: 两个人年龄大小关系\n");
 53     test2();
 54 }
 55 
 56 // 补足函数input实现
 57 // 功能: 输入日期给pd指向的Date变量
 58 void input(Date *pd) {
 59     scanf("%d-%d-%d",&pd->year,&pd->month,&pd->day);
 60 }
 61 
 62 // 补足函数day_of_year实现
 63 // 功能:返回日期d是这一年的第多少天
 64 int day_of_year(Date d) {
 65     int i,s=0;
 66     for(i=1;i<d.month;++i)
 67     {
 68         if(d.year%4==0)
 69         {
 70             if(i==2)
 71                 s+=29;
 72             else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
 73                 s+=31;
 74             else
 75                 s+=30;
 76         }
 77         if(d.year%4!=0)
 78         {
 79             if(i==2)
 80                 s+=28;
 81             else if(i==1||i==3||i==5||i==7||i==8||i==10||i==12)
 82                 s+=31;
 83             else
 84                 s+=30;
 85         }
 86     }
 87     s+=d.day;
 88 
 89     return s;
 90 }
 91 
 92 // 补足函数compare_dates实现
 93 // 功能:比较两个日期: 
 94 // 如果d1在d2之前,返回-1;
 95 // 如果d1在d2之后,返回1
 96 // 如果d1和d2相同,返回0
 97 int compare_dates(Date d1, Date d2) {
 98     if(d1.year<d2.year)
 99         return -1;
100     else if(d1.year>d2.year)
101         return 1;
102     else
103         if(day_of_year(d1)<day_of_year(d2))
104             return -1;
105         else if(day_of_year(d1)<day_of_year(d2))
106             return 1;
107         else
108             return 0;
109 }

 

 

 

6,实验任务6

task6.c

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 enum Role {admin, student, teacher};
 5 
 6 typedef struct {
 7     char username[20];  // 用户名
 8     char password[20];  // 密码
 9     enum Role type;     // 账户类型
10 } Account;
11 
12 
13 // 函数声明
14 void output(Account x[], int n);    // 输出账户数组x中n个账户信息,其中,密码用*替代显示
15 
16 int main() {
17     Account x[] = {{"A1001", "123456", student},
18                     {"A1002", "123abcdef", student},
19                     {"A1009", "xyz12121", student}, 
20                     {"X1009", "9213071x", admin},
21                     {"C11553", "129dfg32k", teacher},
22                     {"X3005", "921kfmg917", student}};
23     int n;
24     n = sizeof(x)/sizeof(Account);
25     output(x, n);
26 
27     return 0;
28 }
29 
30 // 待补足的函数output()实现
31 // 功能:遍历输出账户数组x中n个账户信息
32 //      显示时,密码字段以与原密码相同字段长度的*替代显示
33 void output(Account x[], int n) {
34     int i,j;
35     for(i=0;i<n;++i)
36     {
37         for(j=0;j<strlen(x[i].password);j++)
38         {
39             x[i].password[j]='*';
40         }
41         printf("%-20s %-20s",x[i].username,x[i].password);
42         switch(x[i].type)
43         {
44         case 0:printf("admin\n");break;
45         case 1:printf("student\n");break;
46         case 2:printf("teacher\n");break;
47         default:;break;
48         }
49     }
50 }

 

7,实验任务7

task7.c

 

 1 #include <stdio.h>
  2 #include <string.h>
  3 
  4 typedef struct {
  5     char name[20];      // 姓名
  6     char phone[12];     // 手机号
  7     int  vip;           // 是否为紧急联系人,是取1;否则取0
  8 } Contact; 
  9 
 10 
 11 // 函数声明
 12 void set_vip_contact(Contact x[], int n, char name[]);  // 设置紧急联系人
 13 void output(Contact x[], int n);    // 输出x中联系人信息
 14 void display(Contact x[], int n);   // 按联系人姓名字典序升序显示信息,紧急联系人最先显示
 15 
 16 
 17 #define N 10
 18 int main() {
 19     Contact list[N] = {{"刘一", "15510846604", 0},
 20                        {"陈二", "18038747351", 0},
 21                        {"张三", "18853253914", 0},
 22                        {"李四", "13230584477", 0},
 23                        {"王五", "15547571923", 0},
 24                        {"赵六", "18856659351", 0},
 25                        {"周七", "17705843215", 0},
 26                        {"孙八", "15552933732", 0},
 27                        {"吴九", "18077702405", 0},
 28                        {"郑十", "18820725036", 0}};
 29     int vip_cnt, i;
 30     char name[20];
 31 
 32     printf("显示原始通讯录信息: \n"); 
 33     output(list, N);
 34 
 35     printf("\n输入要设置的紧急联系人个数: ");
 36     scanf("%d", &vip_cnt);
 37     printf("输入%d个紧急联系人姓名:\n", vip_cnt);
 38     for(i = 0; i < vip_cnt; ++i) {
 39         scanf("%s", name);
 40         set_vip_contact(list, N, name);
 41     }
 42 
 43     printf("\n显示通讯录列表:(按姓名字典序升序排列,紧急联系人最先显示)\n");
 44     display(list, N);
 45 
 46     return 0;
 47 }
 48 
 49 // 补足函数set_vip_contact实现
 50 // 功能:将联系人数组x中,联系人姓名与name一样的人,设置为紧急联系人(即成员vip值设为1)
 51 void set_vip_contact(Contact x[], int n, char name[]) {
 52     int i;
 53     for(i=0;i<n;i++)
 54     {
 55         if(strcmp(x[i].name,name)==0)
 56             x[i].vip=1;
 57     }
 58 }
 59 
 60 // 补足函数display实现
 61 // 功能: 显示联系人数组x中的联系人信息
 62 //      按姓名字典序升序显示, 紧急联系人显示在最前面
 63 void display(Contact x[], int n) {
 64     int i,j;
 65     Contact t;
 66     for(i=0;i<n-1;++i)
 67     {
 68         for(j=0;j<n-i-1;++j)
 69         {
 70             if(x[j+1].vip==1&&x[j].vip==0)
 71             {
 72                 t=x[j];
 73                 x[j]=x[j+1];
 74                 x[j+1]=t;
 75             }
 76             else if(x[j].vip==1&&x[j+1].vip==0)
 77             {
 78                 x[j]=x[j];
 79                 x[j+1]=x[j+1];
 80             }
 81             else
 82             {
 83                 if(strcmp(x[j].name,x[j+1].name)>0)
 84                 {
 85                     t=x[j];
 86                     x[j]=x[j+1];
 87                     x[j+1]=t;
 88                 }
 89             }
 90         }
 91     }
 92     output(x, N);
 93 }
 94 
 95 void output(Contact x[], int n) {
 96     int i;
 97 
 98     for(i = 0; i < n; ++i) {
 99         printf("%-10s%-15s", x[i].name, x[i].phone);
100         if(x[i].vip)
101             printf("%5s", "*");
102         printf("\n");
103     }
104 }

 

 

 

 

 

 

 

实验5

一,实验目的

二,实验准备

三,实验内容

1,实验任务1

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 }
复制代码

find_min_max实现功能:返回录入数据中的最小值和最大值
pmin,pmax指向第一个数据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 }
复制代码

find_max功能是返回录入数据中的最大值

可以

2.实验任务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 }
复制代码

 

数组s1大小24个字节,sizeof(s1)计算s1实际所占字节数大小,strlen(s1)计算'\0'之前的字符个数
不能,s[]不完整
交换

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 }
复制代码

s1指向的是字符串存放的内存区域,sizeof(s1)计算s1指向的地址占用的字节,strlen(s1)统计的是s1对应字符串'0'之前的字符个数

能,这里s1指向的是字符串存放的内存区域,区域内存放的字符串为"Learning makes me happy"

交换的是s1,s2指向的内存区域,"Learning makes me happy"和"Learning makes me sleepy"在内存存储单元中没有交换

 3,实验任务3

task3.c

 

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

 

4,实验任务4

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 }
复制代码

 

replace功能,将i替换成*
可以

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         // blank3
32 }
复制代码

5,实验任务5

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 }
复制代码

 

 交换的是指针变量的值

 

6,实验任务6

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     // 补足函数实现
29     // ...
30     int s=0;
31     if(strlen(str) !=18)
32     {
33         return 0;
34     }
35     while(*str)
36     {
37         if((*str>'0'&&*str<'9')||*str=='X')
38             s=0;
39         else
40             s++;
41         str++;
42     }
43     if(s==0)
44         return 1;
45     else
46         return 0;
47 }
复制代码

7,实验任务7

task7.c

 

复制代码
 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     // 补足函数实现
31     // ×××
32     while(*str)
33     {
34         if((*str>='A'&&*str<='Z')||(*str>='a'&&*str<='z'))
35         {
36             if(*str=='z'||*str=='Z')
37             {
38                 *str=*str-25;
39             }
40             else
41             {
42                 *str=*str+1;
43             }
44         }
45         str++;
46     }
47 }
48 
49 /*函数定义
50 功能:对s指向的字符串进行解码处理
51 解码规则:
52 对于a~z或A~Z之间的字母字符,用其前面的字符替换; 其中,a用z替换,A用Z替换
53 其它非字母字符,保持不变
54 */
55 void decoder(char *str) {
56     // 补足函数实现
57     // ×××
58     while(*str)
59     {
60         if((*str>='A'&&*str<='Z')||(*str>='a'&&*str<='z'))
61         {
62             if(*str=='a'||*str=='A')
63             {
64                 *str=*str+25;
65             }
66             else
67             {
68                 *str=*str-1;
69             }
70         }
71         str++;
72     }
73 }
复制代码