实验4
Task1_1
1 #include <stdio.h> 2 #define N 4 3 void test1() { 4 int a[N] = {1, 9, 8, 4}; 5 int i; 6 // 输出数组a占用的内存字节数 7 printf("sizeof(a) = %d\n", sizeof(a)); 8 // 输出int类型数组a中每个元素的地址、值 9 for (i = 0; i < N; ++i) 10 printf("%p: %d\n", &a[i], a[i]); 11 // 输出数组名a对应的值 12 printf("a = %p\n", a); 13 } 14 void test2() { 15 char b[N] = {'1', '9', '8', '4'}; 16 int i; 17 // 输出数组b占用的内存字节数 18 printf("sizeof(b) = %d\n", sizeof(b)); 19 // 输出char类型数组b中每个元素的地址、值 20 for (i = 0; i < N; ++i) 21 printf("%p: %c\n", &b[i], b[i]); 22 // 输出数组名b对应的值 23 printf("b = %p\n", b); 24 } i 25 nt main() { 26 printf("测试1: int类型一维数组\n"); 27 test1(); 28 printf("\n测试2: char类型一维数组\n"); 29 test2(); 30 return 0; 31 }
Rs1_1(1)连续存放 4字节 一样
(2)连续存放 1字节 一样
Task1_2
1 #include <stdio.h> 2 #define N 2 3 #define M 4 4 void test1() { 5 int a[N][M] = {{1, 9, 8, 4}, {2, 0, 4, 9}}; 6 int i, j; 7 // 输出int类型二维数组a占用的内存字节数 8 printf("sizeof(a) = %d\n", sizeof(a)); 9 // 输出int类型二维数组a中每个元素的地址、值 10 for (i = 0; i < N; ++i) 11 for (j = 0; j < M; ++j) 12 printf("%p: %d\n", &a[i][j], a[i][j]); 13 printf("\n"); 14 // 输出int类型二维数组名a, 以及,a[0], a[1]的值 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 void test2() { 21 char b[N][M] = {{'1', '9', '8', '4'}, {'2', '0', '4', '9'}}; 22 int i, j; 23 // 输出char类型二维数组b占用的内存字节数 24 printf("sizeof(b) = %d\n", sizeof(b)); 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 printf("\n"); 30 // 输出char类型二维数组名b, 以及,b[0], b[1]的值 31 printf("b = %p\n", b); 32 printf("b[0] = %p\n", b[0]); 33 printf("b[1] = %p\n", b[1]); 34 } 35 int main() { 36 printf("测试1: int型两维数组"); 37 test1(); 38 printf("\n测试2: char型两维数组"); 39 test2(); 40 return 0; 41 }
Rs1_2 (1)是 4 是
(2)是 1 是
(3)16 等于每行所占字节数
Task2
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 void swap_str(char s1[N], char s2[N]); 5 void test1(); 6 void test2(); 7 int main() { 8 printf("测试1: 用两个一维char数组,实现两个字符串交换\n"); 9 test1(); 10 printf("\n测试2: 用二维char数组,实现两个字符串交换\n"); 11 test2(); 12 return 0; 13 } 14 void test1() { 15 char views1[N] = "hey, C, I hate u."; 16 char views2[N] = "hey, C, I love u."; 17 printf("交换前: \n"); 18 puts(views1); 19 puts(views2); 20 swap_str(views1, views2); 21 printf("交换后: \n"); 22 puts(views1); 23 puts(views2); 24 } 25 void test2() { 26 char views[2][N] = {"hey, C, I hate u.", 27 "hey, C, I love u."}; 28 printf("交换前: \n"); 29 puts(views[0]); 30 puts(views[1]); 31 swap_str(views[0], views[1]); 32 printf("交换后: \n"); 33 puts(views[0]); 34 puts(views[1]); 35 } 36 void swap_str(char s1[N], char s2[N]) { 37 char tmp[N]; 38 strcpy(tmp, s1); 39 strcpy(s1, s2); 40 strcpy(s2, tmp); 41 }
Rs2
猜测一维字符数组名相当于指针,这也是为什么函数调用后可以带回两个值的原因
puts(一维数组名);可以输出该一维数组
gets同理
Task3
Rs已测试
Task4
1 #include <stdio.h> 2 #define N 100 3 void dec_to_n(int x, int n); // 函数声明 4 int main() { 5 int x; 6 printf("输入一个十进制整数: "); 7 while(scanf("%d", &x) != EOF) { 8 dec_to_n(x, 2); // 函数调用: 把x转换成二进制输出 9 dec_to_n(x, 8); // 函数调用: 把x转换成八进制输出 10 dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出 11 printf("\n输入一个十进制整数: "); 12 } 13 return 0; 14 } 15 // 函数定义 16 // 功能: 把十进制数x转换成n进制,打印输出 17 // 补足函数实现 18 // ××× 19 void dec_to_n(int x, int n){ 20 int a[N]={0}; 21 int s=x,cnt=0,r,Rs; 22 while (s!=0){ 23 a[cnt++]=s%n; 24 s/=n; 25 } 26 for(r=0;r<cnt;r++){ 27 if(a[cnt-1-r]<10) 28 printf("%d",a[cnt-1-r]); 29 if(a[cnt-1-r]>10) 30 printf("%c",(char)a[cnt-1-r]+55); 31 } 32 printf("\n"); 33 }
Rs4

Task5_1
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 // 输入n个整数保存到整型数组x中 25 void input(int x[], int n) { 26 int i; 27 for(i = 0; i < n; ++i) 28 scanf("%d", &x[i]); 29 } 30 // 输出整型数组x中n个元素 31 void output(int x[], int n) { 32 int i; 33 for(i = 0; i < n; ++i) 34 printf("%d ", x[i]); 35 printf("\n"); 36 } 37 // 计算整型数组x中n个元素均值,并返回 38 // 补足函数average()实现 39 double average(int x[], int n){ 40 double Rs=0; 41 int i; 42 for(i = 0; i < n; ++i) 43 Rs+=x[i]; 44 Rs/=n; 45 return Rs; 46 47 } 48 // 对整型数组x中的n个元素降序排序 49 // 补足函数bubble_sort()实现 50 void bubble_sort(int x[], int n){ 51 int stl=0; 52 int cnt =0; 53 int i; 54 for(i = 0; i < n-1; ++i){ 55 for(i =0 ; i<n-1-cnt; i++){ 56 if(x[i]<x[i+1]){ 57 stl=x[i+1]; 58 x[i+1]=x[i]; 59 x[i]=stl; 60 } 61 } 62 cnt++; 63 } 64 }
Rs5_1

Task6
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 // 函数定义 20 // 功能:按行输出二维数组中的字符串 21 void output(char str[][M], int n) { 22 int i; 23 for(i = 0; i < n; ++i) 24 printf("%s\n", str[i]); 25 } 26 // 函数定义 27 // 功能:使用冒泡排序算法对二维数组str中的n个字符串按字典序排序 28 // 补足函数bubble_sort()实现 29 void bubble_sort(char str[][M], int n){ 30 char stl[M]; 31 int j; 32 for(j=0;j<N-1;j++){ 33 for(j=0;j<N-1;j++){ 34 if (strcmp(str[j],str[j+1])>0){ 35 strcpy(stl,str[j+1]); 36 strcpy(str[j+1],str[j]); 37 strcpy(str[j],stl); 38 } 39 40 } 41 } 42 }
Rs6

Task7
1 #include <stdio.h> 2 int main(){ 3 char a[100],i,j; 4 gets(a); 5 int map[10]={0}; 6 for(i=0;i<100;i++){ 7 for(j=0;j<10;j++){ 8 if((int)a[i]==j+32) map[j]++; 9 } 10 } 11 i=0; 12 for(j=0;j<10;j++){ 13 if(map[j]>1) { 14 i++; 15 } 16 } 17 if(i>1) printf("YES"); 18 else printf("NO"); 19 return 0; 20 }
Rs7

Task8
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 int main() { 7 int t[][N] = {{21, 12, 13, 24}, 8 {25, 16, 47, 38}, 9 {29, 11, 32, 54}, 10 {42, 21, 33, 10}}; 11 printf("原始矩阵:\n"); 12 output(t, M); // 函数调用 13 rotate_to_right(t, M); // 函数调用 14 printf("变换后矩阵:\n"); 15 output(t, M); // 函数调用 16 return 0; 17 } 18 // 函数定义 19 // 功能: 输出一个n*n的矩阵x 20 void output(int x[][N], int n) { 21 int i, j; 22 for (i = 0; i < n; ++i) { 23 for (j = 0; j < n; ++j) 24 printf("%4d", x[i][j]); 25 printf("\n"); 26 } 27 } 28 // 待补足3:函数rotate_to_right()定义 29 // 功能: 把一个n*n的矩阵x,每一列向右移, 最右边被移出去的一列绕回左边 30 void rotate_to_right(int x[][N], int n){ 31 int i,j; 32 int stl; 33 for(i=0;i<n;i++){ 34 for(j=n-2;j>=0;j--){ 35 stl=x[i][j]; 36 x[i][j]=x[i][j+1]; 37 x[i][j+1]=stl; 38 } 39 } 40 }
Rs8


Task
Rs
Task
Rs
浙公网安备 33010602011771号