实验四
1.实验任务1
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 4 4 5 int main() 6 { 7 int a[N] = {2, 0, 2, 3}; 8 char b[N] = {'2','0','2','3'}; 9 int i; 10 11 printf("sizeof(int) = %d\n",sizeof(int)); 12 printf("sizeof(char) = %d\n",sizeof(char)); 13 printf("\n"); 14 15 for(i = 0;i < N;i++) 16 printf("%p: %d\n",&a[i],a[i]); 17 18 printf("\n"); 19 20 21 for(i = 0;i < N;i++) 22 printf("%p: %c\n",&b[i],b[i]); 23 24 printf("\n"); 25 26 printf("a = %p\n",a); 27 printf("b = %p\n",b); 28 29 system("pause"); 30 return 0; 31 }
- 是连续存放的,占用4个内存字节单元。
- 是连续存放的,占用1个内存字节单元。
- 是一样的。
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 2 4 #define M 3 5 6 int main() 7 { 8 int a[N][M] = {{1,2,3},{4,5,6}}; 9 char b[N][M] = {{'1','2','3'},{'4','5','6'}}; 10 int i,j; 11 12 //输出int型二维数组a中每个元素的地址、值// 13 for(i = 0;i < N;i++) 14 for(j = 0;j < M;j++) 15 printf("%p: %d\n",&a[i][j],a[i][j]); 16 17 printf("\n"); 18 19 //输出int型二位数组名a,以及a[0],a[1]的值// 20 printf("a = %p\n",a); 21 printf("a[0] = %p\n",a[0]); 22 printf("a[1] = %p\n",a[1]); 23 printf("\n"); 24 25 //输出char型二维数组b中每个元素的地址、值// 26 for(i = 0;i < N;i++) 27 for(j = 0;j < M;j++) 28 printf("%p: %c\n",&b[i][j],b[i][j]); 29 30 printf("\n"); 31 32 //输出char型二维数组名b,以及b[0],b[1]的值// 33 printf("b = %p\n",b); 34 printf("b[0] = %p\n",b[0]); 35 printf("b[1] = %p\n",b[1]); 36 printf("\n"); 37 38 system("pause"); 39 return 0; 40 }
- 是。4个内存字节单元。
- 是。
- 是。1个内存字节单元。
- 是。
- a[0]是第一行第一个元素的地址,a[1]是第二行第一个元素的地址。b[]同理。
2.实验任务2
1 #include<stdio.h> 2 #include<stdlib.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 { 12 printf("测试1:用两个一维数组,实现两个字符串交换\n"); 13 test1(); 14 15 printf("\n测试2:用二维数组,实现两个字符串交换\n"); 16 test2(); 17 18 system("pause"); 19 return 0; 20 } 21 22 void test1() 23 { 24 char views1[N] = "hey,C,I hate u."; 25 char views2[N] = "hey,C,I love u."; 26 27 printf("交换前:\n"); 28 puts(views1); 29 puts(views2); 30 31 swap_str(views1,views2); 32 33 printf("交换后:\n"); 34 puts(views1); 35 puts(views2); 36 } 37 38 void test2() 39 { 40 char views[2][N] = {"hey,C,I hate u.","hey,C,I love u."}; 41 42 printf("交换前:\n"); 43 puts(views[0]); 44 puts(views[1]); 45 46 swap_str(views[0],views[1]); 47 48 printf("交换后:\n"); 49 puts(views[0]); 50 puts(views[1]); 51 } 52 53 void swap_str(char s1[N],char s2[N]) 54 { 55 char tmp[N]; 56 57 strcpy(tmp,s1); 58 strcpy(s1,s2); 59 strcpy(s2,tmp); 60 }
test1中是一维数组,不加[],实参是地址数,而test2中是二维数组,二维数组加一个[]也代表地址数,都可以实行交换。puts同理。
3.实验任务3
1 #include<stdio.h> 2 #include<stdlib.h> 3 4 #define N 80 5 6 int count(char x[]); 7 8 int main() 9 { 10 char words[N+1]; 11 int n; 12 13 while(gets(words)!= NULL) 14 { 15 n = count(words); 16 printf("单词数:%d\n\n",n); 17 } 18 19 system("pause"); 20 return 0; 21 } 22 23 int count(char x[]) 24 { 25 int i; 26 int word_flag = 0; 27 int number = 0; 28 29 for(i = 0;x[i]!='\0';i++) 30 { 31 if(x[i] == ' ') 32 word_flag = 0; 33 else if(word_flag == 0) 34 { 35 word_flag = 1; 36 number++; 37 } 38 } 39 40 return number; 41 }
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 1000 4 5 int main() 6 { 7 char line[N]; 8 int word_len; 9 int max_len; 10 int end; 11 int i; 12 13 while(gets(line)!= NULL) 14 { 15 word_len = 0; 16 max_len = 0; 17 end = 0; 18 19 i = 0; 20 while(1) 21 { 22 23 while(line[i] == ' ') 24 { 25 word_len = 0; 26 i++; 27 } 28 29 while(line[i]!='\0'&&line[i]!= ' ') 30 { 31 word_len++; 32 i++; 33 } 34 35 if(max_len < word_len) 36 { 37 max_len = word_len; 38 end = i; 39 } 40 41 if(line[i] == '\0')\ 42 break; 43 } 44 45 printf("最长单词:"); 46 for(i = end - max_len;i < end ;i++) 47 printf("%c",line[i]); 48 49 printf("\n\n"); 50 } 51 52 system("pause"); 53 return 0; 54 }
4.实验任务4
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 5 4 5 void input(int x[],int n); 6 void output(int x[],int n); 7 double average(int x[],int n); 8 void bubble_sort(int x[],int n); 9 10 int main() 11 { 12 int scores[N]; 13 double ave; 14 15 printf("录入%d个分数:\n",N); 16 input(scores,N); 17 18 printf("\n输出课程分数:\n"); 19 output(scores,N); 20 21 printf("\n课程分数处理:计算均分、排序...\n"); 22 ave = average(scores,N); 23 bubble_sort(scores,N); 24 25 printf("\n输出课程均分:%.2f\n",ave); 26 printf("\n输出课程分数(高->低):\n"); 27 output(scores,N); 28 29 system("pause"); 30 return 0; 31 } 32 33 void input(int x[],int n) 34 { 35 int i; 36 37 for(i = 0;i < N;i++) 38 scanf("%d",&x[i]); 39 } 40 41 void output(int x[],int n) 42 { 43 int i; 44 45 for(i = 0;i < n;i++) 46 printf("%d ",x[i]); 47 48 printf("\n"); 49 } 50 51 double average(int x[],int n) 52 { 53 int i; 54 double s = 0; 55 for(i = 0;i < N;i++) 56 s = s + x[i]; 57 58 return s / N; 59 60 } 61 62 void bubble_sort(int x[],int n) 63 { 64 int i,j,t; 65 for(i = 0;i < N-1;i++) 66 for(j = 0;j < N-1-i;j++) 67 if(x[j]<x[j+1]) 68 { 69 t = x[j]; 70 x[j] = x[j+1]; 71 x[j+1] = t; 72 } 73 }
5.实验任务5
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #define N 100 5 void dec2n(int x,int n); 6 7 int main() 8 { 9 int x; 10 11 printf("输入一个十进制整数:"); 12 while(scanf("%d",&x)!= EOF) 13 { 14 dec2n(x,2); 15 dec2n(x,8); 16 dec2n(x,16); 17 18 printf("\n输入一个十进制整数:"); 19 } 20 21 system("pause"); 22 return 0; 23 } 24 25 void dec2n(int x,int n) 26 { 27 int i = 0,remain,k; 28 char a[N],b[N]; 29 char c[N] = {"0123456789ABCDEF"}; 30 for(;x!=0;) 31 { 32 remain = (int)x % n; 33 x = x / n; 34 a[i] = c[remain]; 35 i++; 36 } 37 for(;i>=0;i--) 38 printf("%c",a[i-1]); 39 printf("\n"); 40 }
6.实验任务6
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define N 100 4 #define M 4 5 6 void output(int x[][N],int n); 7 void rotate_to_right(int x[][N],int n); 8 9 int main() 10 { 11 int t[][N] = {{21,12,13,24}, 12 {25,16,47,38}, 13 {29,11,32,54}, 14 {42,21,33,10}}; 15 printf("原始矩阵:\n"); 16 output(t,M); 17 18 rotate_to_right(t,M); 19 printf("变换后矩阵:\n"); 20 output(t,M); 21 22 system("pause"); 23 return 0; 24 } 25 26 void output(int x[][N],int n) 27 { 28 int i,j; 29 30 for(i = 0;i < n;++i) 31 {for(j = 0;j < n;++j) 32 printf("%4d",x[i][j]); 33 34 printf("\n"); 35 } 36 } 37 38 void rotate_to_right(int x[][N],int n) 39 { 40 int i,j; 41 int s; 42 for(i = 0;i < n;i++) 43 { 44 s = x[i][3]; 45 for(j = 4;j!=0;j--) 46 47 48 x[i][j] = x[i][j-1]; 49 50 x[i][0] = s; 51 } 52 53 }
7.实验任务7
1 #include <stdio.h> 2 #define N 80 3 4 void replace(char x[], 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 system("pause"); 18 return 0; 19 } 20 21 void replace(char x[], char old_char, char new_char) { 22 int i; 23 24 for (i = 0; x[i] != '\0'; ++i) 25 if (x[i] == old_char) 26 x[i] = new_char; 27 }
- 实现的功能是把指定字符替换成另一种字符。
- '\0'是字符串的结束符,当一个字符数组到最后一个字符后,就是字符串的结束符,这一句话就已经替换完了,就可以结束了。
8.实验任务8
1 #include <stdio.h> 2 #include <string.h> 3 4 #define N 5 5 #define M 20 6 7 void bubble_sort(char str[][M], int n); 8 9 int main() { 10 char name[][M] = {"Bob", "Bill", "Joseph", "Taylor", "George"}; 11 int i; 12 13 printf("输出初始名单:\n"); 14 for (i = 0; i < N; i++) 15 printf("%s\n", name[i]); 16 17 printf("\n排序中...\n"); 18 bubble_sort(name, N); 19 20 printf("\n按字典序输出名单:\n"); 21 for (i = 0; i < N; i++) 22 printf("%s\n", name[i]); 23 24 system("pause"); 25 return 0; 26 } 27 28 void bubble_sort(char str[][M], int n) 29 { 30 int i,j; 31 char tmp[M]; 32 for(i = 0;i < N-1;i++) 33 for(j = 0;j < N-1-i;j++) 34 if(strcmp(str[j],str[j+1])>0) 35 { 36 strcpy(tmp,str[j]); 37 strcpy(str[j],str[j+1]); 38 strcpy(str[j+1],tmp);} 39 }