实验四

任务一截图

 

任务二

 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:lianggeyiweicharshuzu\n");
12     test1();
13     
14     printf("\n2:erweichar\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("jiaohuanqian:\n");
25     puts(views1);
26     puts(views2);
27     
28     swap_str(views1,views2);
29     
30     printf("jiaohuanhou:\n");
31     puts(views1);
32     puts(views2);    
33     
34 }
35 
36 void test2(){
37     char views[2][N]={"hey,c,i,hate,u",
38                       "hey,c,i,love,u"};
39                       
40     printf("jiaohuanqian:\n");
41     puts(views[0]);
42     puts(views[1]);
43     
44     swap_str(views[0],views[1]);
45     
46     printf("jiaohuanhou:\n");
47     puts(views[0]);
48     puts(views[1]);    
49     
50     
51 }
52 
53 void swap_str(char s1[N],char s2[N]){
54     char tmp[N];
55     
56     strcpy(tmp,s1);
57     strcpy(s1,s2);
58     strcpy(s2,tmp);
59 }

截图

任务三

 

 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 }
 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             
19             while(line[i] == ' ') {
20                 word_len = 0; 
21                 i++;
22             }
23 
24             
25             while(line[i] != '\0' && line[i] != ' ') {
26                 word_len++;
27                 i++;
28             }
29         
30      
31             if(max_len < word_len) {
32                 max_len = word_len;
33                 end = i;  
34             }
35 
36             if(line[i] == '\0')
37                 break;
38         }
39 
40   
41         printf("最长单词: ");
42         for(i = end - max_len; i < end; ++i)
43             printf("%c", line[i]);
44         printf("\n\n");
45     }
46 
47     return 0;
48 }

截图

 

 

 

 

 

 

 

 

任务四

 1 #include<stdio.h>
 2 #define N 100
 3 void dec_to_n(int x,int n);
 4 
 5 int main(){
 6     int x;
 7     
 8     printf("输入一个十进制整数:");
 9     while(scanf("%d",&x)!=EOF){
10         dec_to_n(x,2);
11         dec_to_n(x,8);
12         dec_to_n(x,16);
13         
14         printf("\n输入一个十进制整数: ");
15         
16     }
17     return 0;
18 }
19 
20 
21 
22 void dec_to_n(int x,int n){
23     char ans[N];
24     char map[17] = {"0123456789ABCDEF"};
25     int r;
26     int cnt=0,i;
27     do{
28         r=x%n;
29         ans[cnt++]=map[r];
30         x=x/n;
31     }
32     while(x!=0);
33     for(i=cnt-1;i>=0;--i)
34     printf("%c",ans[i]);
35     
36     printf("\n");
37 }

任务四截图

任务五

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

 

任务六

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

任务六截图

任务八

 1 #include<stdio.h>
 2 #define N 100
 3 #define M 4
 4 void output(int x[][N],int n);
 5 void rotate_to_right(int x[][N],int n);
 6 
 7 int main(){
 8     int t[][N]={{21,12,13,24},
 9                 {25,16,47,38},
10                 {29,11,32,54},
11                 {42,21,33,10}};
12         
13         printf("yuanshijuzhen:\n");
14         output(t,M);
15         
16         printf("bianhuanhoujuzhen:\n");
17         output(t,M);
18         
19         return 0;        
20 }
21 
22 void output(int x[][N],int n){
23     int i,j;
24     
25     for(i=0;i<n;i++){
26         for(j=0;j<n;j++)
27         printf("%4d",x[i][j]);
28         
29         printf("\n");
30     }
31 }
32 
33 void rotate_to_right(int x[][N],int n){
34     int i,j,a;
35     
36     for(i=0;i<M;i++)
37     {
38     a=x[i][M-1];
39     }
40     
41     for(j=M-1;j>=1;j--){
42     x[i][j]=x[i][j-1];}
43     
44     x[i][0]=a;
45 }

截图

 

posted @ 2023-11-19 21:45  蔡文灿  阅读(12)  评论(0)    收藏  举报