1 #include <stdio.h> 2 #define N 4 3 int main() 4 { 5 int a[N] = { 1, 9, 8, 4 }; 6 char b[N] = { '1', '9', '8', '4' }; 7 int i; 8 printf("sizeof(int) = %d\n", sizeof(int)); 9 printf("sizeof(char) = %d\n", sizeof(char)); 10 printf("\n"); 11 // 输出一维int数组a中每个元素的地址、值 12 for (i = 0; i < N; ++i) 13 printf("%p: %d\n", &a[i], a[i]); 14 printf("\n"); 15 // 输出一维char数组b中每个元素的地址、值 16 for (i = 0; i < N; ++i) 17 printf("%p: %c\n", &b[i], b[i]); 18 printf("\n"); 19 // 输出数组名a和b对应的值 20 printf("a = %p\n", a); 21 printf("b = %p\n", b); 22 return 0; 23 } 24 25 26 27 /* 28 ① int型数组a,在内存中是连续存放的,每个元素占用4个内存字节单元。 29 ② char型数组b,在内存中是连续存放的,每个元素占用1个内存字节单元? 30 ③ 数组名a对应的值,和&a[0]是一样的,数组名b对应的值,和&b[0]是一样的。 31 */
1 #include <stdio.h> 2 #define N 2 3 #define M 4 4 int main() { 5 int a[N][M] = { {1, 9, 8, 4}, {2, 0, 2, 2} }; 6 char b[N][M] = { {'1', '9', '8', '4'}, {'2', '0', '2', '2'} }; 7 int i, j; 8 // 输出二维数组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 // 输出二维数组a中每个元素的地址和值 14 for (i = 0; i < N; ++i) 15 for (j = 0; j < M; ++j) 16 printf("%p: %c\n", &b[i][j], b[i][j]); 17 return 0; 18 } 19 20 21 22 /* 23 ① int型二维数组a,在内存中是"按行连续存放"的,每个元素占用4个内存字节单 24 元。 25 ② char型二维数组b,在内存中是"按行连续存放"的,每个元素占用1个内存字节 26 单元。 27 */
1 #include <stdio.h> 2 #define N 13 3 int days_of_year(int year, int month, int day); 4 int main() { 5 int year, month, day; 6 int days; 7 while (scanf_s("%d%d%d", &year, &month, &day) != EOF) { 8 days = days_of_year(year, month, day); 9 printf("%4d-%02d-%02d是这一年的第%d天.\n\n", year, month, day, days); 10 } 11 return 0; 12 } 13 int days_of_year(int year, int month, int day) 14 { 15 int i, j,s; 16 j = 0; 17 if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) 18 j = 1; 19 int k[N] = { 0,31,28 + j,31,30,31,30,31,31,30,31,30,31 }; 20 s = 0; 21 for (i = 1; i < month; i++) 22 { 23 s += k[i]; 24 } 25 s += day; 26 return s; 27 28 }
1 #include <stdio.h> 2 #define N 5 3 void input(int x[], int n); 4 void output(int x[], int n); 5 double average(int x[], int n); 6 void bubble_sort(int x[], int n); 7 int main() { 8 int scores[N]; 9 double ave; 10 printf("录入%d个分数:\n", N); 11 input(scores, N); 12 printf("\n输出课程分数: \n"); 13 output(scores, N); 14 printf("\n课程分数处理: 计算均分、排序...\n"); 15 ave = average(scores, N); 16 bubble_sort(scores, N); 17 printf("\n输出课程均分: %.2f\n", ave); 18 printf("\n输出课程分数(高->低):\n"); 19 output(scores, N); 20 return 0; 21 } 22 void input(int x[], int n) { 23 int i; 24 for (i = 0; i < n; ++i) 25 scanf_s("%d", &x[i]); 26 } 27 void output(int x[], int n) { 28 int i; 29 for (i = 0; i < n; ++i) 30 printf("%d ", x[i]); 31 printf("\n"); 32 } 33 double average(int x[], int n) 34 { 35 int i; 36 double j; 37 j = 0; 38 for (i = 0; i < N; i++) 39 { 40 j += x[i]; 41 } 42 j /= N; 43 return j; 44 } 45 void bubble_sort(int x[], int n) 46 { 47 int i, j, k; 48 49 for (i = 0; i < N - 1; i++) 50 { 51 for (j = 0; j < N - i - 1; j++) 52 { 53 if (x[j] < x[j + 1]) 54 { 55 k = x[j]; 56 x[j] = x[j + 1]; 57 x[j + 1] = k; 58 } 59 } 60 61 } 62 63 64 }
1 #include <stdio.h> 2 #include<stdlib.h> 3 #define N 100 4 void dec2n(int x, int n); // 函数声明 5 int main() { 6 int x; 7 printf("输入一个十进制整数: "); 8 while (scanf_s("%d", &x) != EOF) { 9 dec2n(x, 2); // 函数调用: 把x转换成二进制输出 10 dec2n(x, 8); // 函数调用: 把x转换成八进制输出 11 dec2n(x, 16); // 函数调用: 把x转换成十六进制输出 12 printf("\n输入一个十进制整数: "); 13 } 14 return 0; 15 } 16 void dec2n(int x, int n) 17 { 18 int k[N]; 19 int i, j; 20 char s[N]; 21 for (i = 0; i < N; i++) 22 { 23 k[i] = x % n; 24 x /= n; 25 if (x == 0) 26 break; 27 } 28 if (n == 2 || n == 8) 29 { 30 for (j = i; j >= 0; j--) 31 { 32 printf("%d", k[j]); 33 } 34 printf("\n"); 35 } 36 if (n == 16) 37 { 38 for (j = 0; j <= i; j++) 39 { 40 if (k[j] < 10) 41 s[j] =k[j]+ 48; 42 else 43 s[j] =k[j]+ 55; 44 } 45 for (j = i; j >= 0; j--) 46 { 47 printf("%c", s[j]); 48 } 49 printf("\n"); 50 } 51 //方法2: 52 if (n == 16) 53 { 54 char standard[17] = { "0123456789ABCDEF" }; 55 for (j = i; j >= 0; j--) 56 { 57 printf("%c", standard[k[j]]); 58 } 59 printf("\n"); 60 } 61 }
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 int main() 5 { 6 char views1[N] = "hey, C, I have not love u yet."; 7 char views2[N] = "hey, C, how can I love u?"; 8 char t[N]; 9 printf("交换前: \n"); 10 printf("views1: %s\n", views1); 11 printf("views2: %s\n", views2); 12 strcpy_s(t, views1); 13 strcpy_s(views1, views2); 14 strcpy_s(views2, t); 15 printf("交换后: \n"); 16 printf("views1: %s\n", views1); 17 printf("views2: %s\n", views2); 18 return 0; 19 }
1 #include <stdio.h> 2 #include <string.h> 3 #define N 80 4 int main() { 5 char views[2][N] = { "hey, C, I have not love u yet.", 6 "hey, C, how can I love u?" }; 7 char t[N]; 8 printf("交换前: \n"); 9 printf("views1: %s\n", views[0]); 10 printf("views2: %s\n", views[1]); 11 // 交换 12 strcpy_s(t, views[0]); 13 strcpy_s(views[0], views[1]); 14 strcpy_s(views[1], t); 15 printf("交换后: \n"); 16 printf("views1: %s\n", views[0]); 17 printf("views2: %s\n", views[1]); 18 return 0; 19 }
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 char temp[M]; 10 int i,j; 11 for (j = 0; j < N; j++) 12 { 13 14 for (i = 0; i < N-j-1; i++) 15 { 16 if (strcmp(str[i], str[i + 1])>0) 17 { 18 strcpy_s(temp, str[i]); 19 strcpy_s(str[i], str[i + 1]); 20 strcpy_s(str[i + 1], temp); 21 } 22 } 23 } 24 25 26 27 28 } 29 30 int main() { 31 char name[][M] = { "Bob", "Bill", "Joseph", "Taylor", "George" }; 32 int i; 33 34 printf("输出初始名单:\n"); 35 for (i = 0; i < N; i++) 36 printf("%s\n", name[i]); 37 38 printf("\n排序中...\n"); 39 bubble_sort(name, N); // 函数调用 40 41 printf("\n按字典序输出名单:\n"); 42 for (i = 0; i < N; i++) 43 printf("%s\n", name[i]); 44 45 return 0; 46 }