实验四
task1.c
1 void test1() { 2 int a[N] = { 1, 9, 8, 4 }; 3 int i; 4 5 // 输出数组a占用的内存字节数 6 printf("sizeof(a) = %d\n", sizeof(a)); 7 8 // 输出int类型数组a中每个元素的地址、值 9 for (i = 0; i < N; ++i) 10 printf("%p: %d\n", &a[i], a[i]); 11 12 // 输出数组名a对应的值 13 printf("a = %p\n", a); 14 } 15 16 void test2() { 17 char b[N] = { '1', '9', '8', '4' }; 18 int i; 19 20 // 输出数组b占用的内存字节数 21 printf("sizeof(b) = %d\n", sizeof(b)); 22 23 // 输出char类型数组b中每个元素的地址、值 24 for (i = 0; i < N; ++i) 25 printf("%p: %c\n", &b[i], b[i]); 26 27 // 输出数组名b对应的值 28 printf("b = %p\n", b); 29 } 30 31 int main() { 32 printf("测试1: int类型一维数组\n"); 33 test1(); 34 35 printf("\n测试2: char类型一维数组\n"); 36 test2(); 37 38 return 0;

int型数组a是按行连续存放,且每个元素占用四个内存字节单元,数组名a对应的值和&a[0],&a[0][0]是一样的;
char型数组b在内存中也是按行连续存放的,每个元素占用四个内存字节单元,数组名b对应的值和&b[0],b[0][0]是一样的;
a[0]和a[1]的值相差16,b[0]和b[1]的值相差4,相邻的两行的元素的地址相差的值为一行元素占用的内存字节单元。
task2.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 9 // 输出int类型二维数组a占用的内存字节数 10 printf("sizeof(a) = %d\n", sizeof(a)); 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 printf("\n"); 17 18 // 输出int类型二维数组名a, 以及,a[0], a[1]的值 19 printf("a = %p\n", a); 20 printf("a[0] = %p\n", a[0]); 21 printf("a[1] = %p\n", a[1]); 22 printf("\n"); 23 } 24 25 void test2() { 26 char b[N][M] = { {'1', '9', '8', '4'}, {'2', '0', '4', '9'} }; 27 int i, j; 28 29 // 输出char类型二维数组b占用的内存字节数 30 printf("sizeof(b) = %d\n", sizeof(b)); 31 32 // 输出char类型二维数组b中每个元素的地址、值 33 for (i = 0; i < N; ++i) 34 for (j = 0; j < M; ++j) 35 printf("%p: %c\n", &b[i][j], b[i][j]); 36 printf("\n"); 37 38 // 输出char类型二维数组名b, 以及,b[0], b[1]的值 39 printf("b = %p\n", b); 40 printf("b[0] = %p\n", b[0]); 41 printf("b[1] = %p\n", b[1]); 42 } 43 44 int main() { 45 printf("测试1: int型两维数组"); 46 test1(); 47 48 printf("\n测试2: char型两维数组"); 49 test2(); 50 51 return 0; 52 }

数组作为函数参数时,实际上传递的是数组首元素的地址,而不是整个数组,一维数组可以直接作为函数参数,不需要加[],但二维数组却只能表示指向第一个元素,所以要加[].
总结:一维字符数组可以直接用数组名访问首元素,二维字符数组需要用数组名加[]访问某个元素。
task3.1.c
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; // 用作单词标志,一个新单词开始,值为1;单词结束,值为0 22 int cnt = 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 cnt++; 30 } 31 } 32 33 return cnt; 34 }

task3.2.c
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_s(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; // 单词长度置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; // end保存的是单词结束的下一个坐标位置 34 } 35 36 // 遍历到文本结束时,终止循环 37 if (line[i] == '\0') 38 break; 39 } 40 41 // 输出最长单词 42 printf("最长单词: "); 43 for (i = end - max_len; i < end; ++i) 44 printf("%c", line[i]); 45 printf("\n\n"); 46 } 47 48 return 0; 49 }

task4.c
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); // 函数调用: 把x转换成二进制输出 11 dec_to_n(x, 8); // 函数调用: 把x转换成八进制输出 12 dec_to_n(x, 16); // 函数调用: 把x转换成十六进制输出 13 14 printf("\n输入一个十进制整数: "); 15 } 16 17 return 0; 18 } 19 20 // 函数定义 21 // 功能: 把十进制数x转换成n进制,打印输出 22 // 补足函数实现 23 // ××× 24 void dec_to_n(int x, int n) { 25 char ans[N]; 26 char map[N] = "0123456789ABCDEF"; 27 int p,q; 28 int cnt, i; 29 cnt = 0; 30 while (1) { 31 p = x / n; 32 q = x % n; 33 ans[cnt++] = map[q]; 34 if (p == 0) 35 break; 36 x = p; 37 } 38 for (i = cnt - 1;i >= 0;--i) 39 printf("%c", ans[i]); 40 printf("\n"); 41 }

task5.c
1 #include <stdio.h> 2 #include<stdlib.h> 3 #define N 5 4 5 // 函数声明 6 void input(int x[], int n); 7 void output(int x[], int n); 8 double average(int x[], int n); 9 void bubble_sort(int x[], int n); 10 11 int main() { 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 system("pause" 29 30 return 0; 31 } 32 33 // 函数定义 34 // 输入n个整数保存到整型数组x中 35 void input(int x[], int n) { 36 int i; 37 38 for (i = 0; i < n; ++i) 39 scanf("%d", &x[i]); 40 } 41 42 // 输出整型数组x中n个元素 43 void output(int x[], int n) { 44 int j; 45 46 for (j= 0; j< n; ++j) 47 printf("%d ", x[j]); 48 printf("\n"); 49 } 50 51 double average(int x[], int n) { 52 double s = 0.0; 53 int k; 54 for (k = 0;k < n;k++) 55 s = s + x[k]; 56 s /= n; 57 return s; 58 } 59 void bubble_sort(int x[], int n) { 60 int i, j, t; 61 for (i = 1;i <= n;i++) { 62 for (j = 0;j < n;j++) 63 { 64 if (x[j] < x[j + 1]) 65 { 66 t = x[j]; 67 x[j] = x[j + 1]; 68 x[j + 1] = t; 69 } 70 } 71 } 72 for (i = 0;i < n;i++) 73 printf("%d", x[i]); 74 }

task6.c
1 #include <stdio.h> 2 #include<stdlib.h> 3 #include <string.h> 4 5 #define N 5 6 #define M 20 7 8 // 函数声明 9 void output(char str[][M], int n); 10 void bubble_sort(char str[][M], int n); 11 12 int main() { 13 char name[][M] = { "Bob", "Bill", "Joseph", "Taylor", "George" }; 14 int i; 15 16 printf("输出初始名单:\n"); 17 output(name, N); 18 19 printf("\n排序中...\n"); 20 bubble_sort(name, N); // 函数调用 21 22 printf("\n按字典序输出名单:\n"); 23 output(name, N); 24 system("pause"); 25 return 0; 26 } 27 28 // 函数定义 29 // 功能:按行输出二维数组中的字符串 30 void output(char str[][M], int n) { 31 int i; 32 33 for (i = 0; i < n; ++i) 34 printf("%s\n", str[i]); 35 } 36 void bubble_sort(char str[][M], int n) { 37 int i, j; 38 char R[M]; 39 for (i = 0;i < (n - 1);i++) { 40 for (j = 0;j < (n - i - 1);j++) { 41 if (strcmp(str[j], str[j + 1]) > 0) 42 { 43 strcpy(R, str[j]); 44 strcpy(str[j], str[j + 1]); 45 strcpy(str[j + 1], R); 46 } 47 } 48 } 49 }

task7.c
1 #include<stdio.h> 2 #define N 100 3 int is_repeated(char x[]); 4 int main() 5 { 6 char num[N]; 7 while(scanf("%s",num)!=EOF) 8 { 9 if(is_repeated(num)) 10 printf("YES\n"); 11 else 12 printf("NO\n"); 13 } 14 return 0; 15 } 16 int is_repeated(char x[]){ 17 int count[10]={0}; 18 int i,j; 19 for(i=0;x[i]!='\0';++i){ 20 j=x[i]-'0'; 21 count[j]++; 22 if(count[j]>1) 23 return 1; 24 } 25 return 0; 26 }

task8. 1 #include <stdio. 2 #define N 100
3
#include<stdio.h>
#include<stdlib.h>
#define N 100
#define M 4 4 5 void output(int x[][N], int n); 6 void rotate_to_right(int x[][N], int n); 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("原始矩阵:\n"); 14 output(t, M); 15 16 rotate_to_right(t, M); 17 18 printf("变换后的矩阵\n"); 19 output(t, M); 20 system("pause"); 21 return 0; 22 } 23 void output(int x[][N], int n) { 24 int i, j; 25 26 for (i = 0; i < n; ++i) { 27 for (j = 0; j < n; ++j) 28 printf("%4d", x[i][j]); 29 30 printf("\n"); 31 } 32 } 33 void rotate_to_right(int x[][N],int n){ 34 int i,j; 35 int R[N]; 36 for(i=0;i<n;i++) 37 R[i]=x[i][n-1]; 38 for(i=0;i<n;i++){ 39 for(j=n-1;j>=0;j--){
40x[i][j+1]=x[i][j]; 41 } 42 for(i=0;i<n;i++) 43 x[i][0]=R[i]; 44 } 45 }

浙公网安备 33010602011771号