实验任务4

发布时间 2023-11-20 04:02:18作者: 符周彦彧

task1_1.c

 1  1 #include <stdio.h>
 2  2 #define N 4
 3  3 
 4  4 void test1() {
 5  5     int a[N] = {1, 9, 8, 4};          
 6  6     int i;
 7  7     printf("sizeof(a) = %d\n", sizeof(a));
 8  8 
 9  9      for (i = 0; i < N; ++i)
10 10         printf("%p: %d\n", &a[i], a[i]);
11 11 
12 12     
13 13     printf("a = %p\n", a); 
14 14 }
15 15 
16 16 void test2() {        
17 17     char b[N] = {'1', '9', '8', '4'}; 
18 18     int i;
19 19     printf("sizeof(b) = %d\n", sizeof(b));
20 20     for (i = 0; i < N; ++i)
21 21         printf("%p: %c\n", &b[i], b[i]);
22 22  
23 23     printf("b = %p\n", b); 
24 24 }
25 25 
26 26 int main() {
27 27     printf("测试1: int类型一维数组\n");
28 28     test1();
29 29 
30 30     printf("\n测试2: char类型一维数组\n");
31 31     test2();
32 32 
33 33     return 0;
34 34 }

task1_2.c

 1 #include <stdio.h>
 2 #define N 2
 3 #define M 4
 4 
 5 void test1() {
 6     int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}};
 7     int i, j;
 8     printf("sizeof(a) = %d\n", sizeof(a));
 9     for (i = 0; i < N; ++i)
10         for (j = 0; j < M; ++j)
11             printf("%p: %d\n", &a[i][j], a[i][j]);
12     printf("\n");
13 
14  
15     printf("a = %p\n", a);
16     printf("a[0] = %p\n", a[0]);
17     printf("a[1] = %p\n", a[1]);
18     printf("\n");
19 }
20 
21 void test2() {
22     char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}};
23     int i, j;
24     printf("sizeof(b) = %d\n", sizeof(b));
25     for (i = 0; i < N; ++i)
26         for (j = 0; j < M; ++j)
27             printf("%p: %c\n", &b[i][j], b[i][j]);
28     printf("\n");
29     printf("b = %p\n", b);
30     printf("b[0] = %p\n", b[0]);
31     printf("b[1] = %p\n", b[1]);
32 }
33 
34 int main() {
35     printf("测试1: int型两维数组");
36     test1();
37 
38     printf("\n测试2: char型两维数组");
39     test2();
40 
41     return 0;
42 }

task 2

 1 #include <stdio.h>
 2 #include <string.h>
 3 
 4 #define N 80
 5 
 6 void swap_str(char s1[N], char s2[N]);
 7 void test1();
 8 void test2();
 9 
10 int main() {
11     printf("测试1: 用两个一维char数组,实现两个字符串交换\n");
12     test1();
13 
14     printf("\n测试2: 用二维char数组,实现两个字符串交换\n");
15     test2();
16 
17     return 0;
18 }
19 
20 void test1() {
21     char views1[N] = "hey, C, I hate u.";
22     char views2[N] = "hey, C, I love u.";
23 
24     printf("交换前: \n");
25     puts(views1);
26     puts(views2);
27 
28     swap_str(views1, views2);
29 
30     printf("交换后: \n");
31     puts(views1);
32     puts(views2);
33 }
34 
35 void test2() {
36     char views[2][N] = {"hey, C, I hate u.", 
37                         "hey, C, I love u."};
38 
39     printf("交换前: \n");
40     puts(views[0]);
41     puts(views[1]);
42 
43     swap_str(views[0], views[1]);
44 
45     printf("交换后: \n");
46     puts(views[0]);
47     puts(views[1]);
48 }
49 
50 void swap_str(char s1[N], char s2[N]) {
51     char tmp[N];
52 
53     strcpy(tmp, s1);
54     strcpy(s1, s2);
55     strcpy(s2, tmp);
56 }

 

 

task3_1

 1 #include <stdio.h>
 2 
 3 #define N 80
 4 
 5 int count(char x[]);
 6 
 7 int main() {
 8     char words[N+1];
 9     int n;
10 
11     while(gets(words) != NULL) {
12         n = count(words);
13         printf("单词数: %d\n\n", n);
14     }
15 
16     return 0;
17 }
18 
19 int count(char x[]) {
20     int i;
21     int word_flag = 0; 
22     int number = 0; 
23 
24     for(i = 0; x[i] != '\0'; i++) {
25         if(x[i] == ' ')
26             word_flag = 0;
27         else if(word_flag == 0) {
28             word_flag = 1;
29             number++;
30         }
31     }
32 
33     return number;
34 }

task 3_2

 1 #include <stdio.h>
 2 #define N 1000
 3 
 4 int main() {
 5     char line[N];
 6     int word_len;   
 7     int max_len;    
 8     int end;        
 9     int i;
10 
11     while(gets(line) != NULL) {
12         word_len = 0;
13         max_len = 0;
14         end = 0;
15 
16         i = 0;
17         while(1) {
18             while(line[i] == ' ') {
19                 word_len = 0;  
20                 i++;
21             }
22             while(line[i] != '\0' && line[i] != ' ') {
23                 word_len++;
24                 i++;
25             }
26             if(max_len < word_len) {
27                 max_len = word_len;
28                 end = i;  
29             }
30             if(line[i] == '\0')
31                 break;
32         }
33         printf("最长单词: ");
34         for(i = end - max_len; i < end; ++i)
35             printf("%c", line[i]);
36         printf("\n\n");
37     }
38 
39     return 0;
40 }

tssk4

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #define N 100
 4 void dec_to_n(int x, int n); // 函数声明
 5 int main() {
 6     int x;
 7     printf("输入一个十进制整数: ");
 8     while(scanf("%d", &x) != EOF) {
 9         dec_to_n(x, 2);  // 函数调用: 把x转换成二进制输出
10         dec_to_n(x, 8);  // 函数调用: 把x转换成八进制输出
11         dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出
12         printf("\n输入一个十进制整数: ");
13     }
14     system("pause");
15     return 0;
16 }
17 void dec_to_n(int x,int n){
18     char a[]={"0123456789ABCDEF"};
19     char b[N];
20     int r,i=0,j;
21     do{
22     r=x%n;
23     b[i++]=a[r];
24     x=x/n;
25     }while(x!=0);
26     for(j=i-1;j>=0;--j)
27         printf("%c",b[j]);
28  printf("\n");
29 }

task 5

 1 #include <stdio.h>
 2 #define N 5
 3 
 4 void input(int x[], int n);
 5 void output(int x[], int n);
 6 double average(int x[], int n);
 7 void bubble_sort(int x[], int n);
 8 int main() {
 9 int scores[N];
10 double ave;
11 printf("录入%d个分数:\n", N);
12 input(scores, N);
13 printf("\n输出课程分数: \n");
14 output(scores, N);
15 printf("\n课程分数处理: 计算均分、排序...\n");
16 ave = average(scores, N);
17 bubble_sort(scores, N);
18 printf("\n输出课程均分: %.2f\n", ave);
19 printf("\n输出课程分数(高->低):\n");
20 output(scores, N);
21 return 0;
22 }
23 
24 void input(int x[], int n) {
25 int i;
26 for(i = 0; i < n; ++i)
27 scanf("%d", &x[i]);
28 }
29 
30 void output(int x[], int n) {
31 int i;
32 for(i = 0; i < n; ++i)
33 printf("%d ", x[i]);
34 printf("\n");
35 }
36 
37 double average(int x[],int n){
38     int i;
39     double sum=0.0;
40     
41     for(i=0;i<=n-1;i++){
42         sum=sum+x[i]; 
43     }
44     
45     return  sum/n;
46 } 
47 void bubble_sort(int x[], int n){
48     int i,j,t;
49     for(i=0;i<n-1;i++){
50         for(j=0;j<n-1-i;j++){
51             if(x[j]<x[j+1]){
52                 t=x[j];
53                 x[j]=x[j+1];
54                 x[j+1]=t;
55             }
56         }
57     }
58 }

task 6

 1 #include <stdio.h>
 2 #include <string.h>
 3 #define N 5
 4 #define M 20
 5 
 6 void output(char str[][M], int n);
 7 void bubble_sort(char str[][M], int n);
 8 int main() {
 9     char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"};
10     int i;
11     printf("输出初始名单:\n");
12     output(name, N);
13     printf("\n排序中...\n");
14     bubble_sort(name, N); 
15     printf("\n按字典序输出名单:\n");
16     output(name, N);
17     return 0;
18 }
19 void output(char str[][M], int n) {
20     int i;
21     for(i = 0; i < n; ++i)
22         printf("%s\n", str[i]);
23 }
24 void bubble_sort(char str[][M], int n){
25     int i,j;
26     for(i=1;i<5;i++)   
27  {
28     for(j=0;j<5-i;j++)
29     {
30         if(strcmp(str[j],str[j+1])>0) 
31         { 
32              char str1[1000];
33              strcpy(str1,str[j]); 
34              strcpy(str[j],str[j+1]); 
35              strcpy(str[j+1],str1); 
36         }
37      }
38   }
39 }

 

task 7

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 int main() {
 4     char a[101];
 5     int i,j,k=0;
 6     while(gets(a)!=NULL){
 7  
 8     for(i=0;i<101&&a[i]!='\0';++i)
 9     {  if(k==1) break;
10         for(j=i+1;a[j]!='\0';++j)
11      
12         if(a[i]==a[j])
13          
14              k=1;
15          
16      
17     }
18     if(k==1) 
19         printf("YES\n");
20     else 
21         printf("NO\n"); k=0;
22     }
23     system("pause");
24     return 0;
25 }

task 8

 1 #include <stdio.h>
 2 #include<stdlib.h>
 3 #include<string.h>
 4 #define N 100
 5 #define M 4
 6  
 7 
 8 void output(int x[][N], int n);         
 9 void rotate_to_right(int x[][N], int n);
10  
11  
12 int main() {
13     int t[][N] = {{21, 12, 13, 24},
14                   {25, 16, 47, 38},
15                   {29, 11, 32, 54},
16                   {42, 21, 33, 10}};
17  
18     printf("原始矩阵:\n");
19     output(t, M); 
20  
21     rotate_to_right(t, M);
22  
23     printf("变换后矩阵:\n");
24     output(t, M); 
25     system("pause");
26     return 0;
27 }
28  
29 void output(int x[][N], int n) {
30     int i, j;
31  
32     for (i = 0; i < n; ++i) {
33         for (j = 0; j < n; ++j)
34             printf("%4d", x[i][j]);
35  
36         printf("\n");
37     }
38 }
39 void rotate_to_right(int x[][N], int n){
40          
41         int i,t;
42         for(i=0;i<4;++i){
43         t=x[i][0];
44         x[i][0]=x[i][3];
45         x[i][3]=t;
46         t=x[i][1];
47         x[i][1]=x[i][3];
48         x[i][3]=t;
49         t=x[i][3];
50         x[i][3]=x[i][2];
51         x[i][2]=t;
52         }
53  
54  
55 }