#include <stdio.h> #define N 4 int main() { int a[N] = { 1, 9, 8, 4 }; char b[N] = { '1', '9', '8', '4' }; int i; printf("sizeof(int) = %d\n", sizeof(int)); printf("sizeof(char) = %d\n", sizeof(char)); printf("\n"); for (i = 0; i < N; ++i) printf("%p: %d\n", &a[i], a[i]); printf("\n"); for (i = 0; i < N; ++i) printf("%p: %c\n", &b[i], b[i]); printf("\n"); printf("a = %p\n", a); printf("b = %p\n", b); return 0; }
#include <stdio.h> #define N 2 #define M 4 int main() { int a[N][M] = { {1, 9, 8, 4}, {2, 0, 2, 2} }; char b[N][M] = { {'1', '9', '8', '4'}, {'2', '0', '2', '2'} }; int i, j; for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) printf("%p: %d\n", &a[i][j], a[i][j]); printf("\n"); for (i = 0; i < N; ++i) for (j = 0; j < M; ++j) printf("%p: %c\n", &b[i][j], b[i][j]); return 0; }
task1_1
1、是;4个字节
2、是;1个字节
3、是;是
task1_2
1、是;4个内存字节
2、是;1个内存字节
#include <stdio.h> #define N 13 int days_of_year(int year, int month, int day); int main() { int year, month, day; int days; while (scanf_s("%d%d%d", &year, &month, &day) != EOF) { days = days_of_year(year, month, day); printf("%4d-%02d-%02d是这一年的第%d天.\n\n", year, month, day, days); } return 0; } int days_of_year(int year, int month, int day) { int days[N] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 }; int sum = 0; if (year % 4 == 0) { for (int i = 1; i < month; i++) { sum += days[i]; } sum += day; sum++; } else { for (int i = 1; i < month; i++) { sum += days[i]; } sum += day; } return sum; }
#include <stdio.h> #define N 5 void input(int x[], int n); void output(int x[], int n); double average(int x[], int n); void bubble_sort(int x[], int n); int main() { int scores[N]; double ave; printf("录入%d个分数:\n", N); input(scores, N); printf("\n输出课程分数: \n"); output(scores, N); printf("\n课程分数处理: 计算均分、排序...\n"); ave = average(scores, N); bubble_sort(scores, N); printf("\n输出课程均分: %.2f\n", ave); printf("\n输出课程分数(高->低):\n"); output(scores, N); return 0; } void input(int x[], int n) { int i; for (i = 0; i < n; ++i) scanf_s("%d", &x[i]); } void output(int x[], int n) { int i; for (i = 0; i < n; ++i) printf("%d ", x[i]); printf("\n"); } double average(int x[], int n) { double aver; int sum = 0; for (int i = 0; i < n; i++) { sum += x[i]; } aver = (double)sum / n; return aver; } void bubble_sort(int x[], int n) { int j; for (int i = 0; i < n-1; i++) { for (j = i + 1; j < n; j++) { if (x[i] < x[j]) { int t; t = x[j]; x[j] = x[i]; x[i] = t; } } } return ; }
#include <stdio.h> #define N 100 void dec2n(int x, int n); int main() { int x; printf("输入一个十进制整数: "); while (scanf_s("%d", &x) != EOF) { dec2n(x, 2); dec2n(x, 8); dec2n(x, 16); printf("\n输入一个十进制整数: "); } return 0; } void dec2n(int x, int n) { int i = 0, j = 0; int a[16] = {0}; while (x) { a[i] = x % n; x /= n; i++; } for (j = i - 1; j >= 0; j--) { if (a[j] >= 10) { printf("%c", 'A' + a[j] - 10); } else printf("%d", a[j]); } printf("\n"); }
#include <stdio.h> #define N 100 void func(int x[][N], int n); void output(int x[][N], int n); int main() { int x[N][N]; int n; printf("Enter n: "); while (scanf_s("%d", &n) != EOF) { func(x, n); output(x, n); printf("\nEnter n: "); } return 0; } void output(int x[][N], int n) { int i, j; for (i = 0; i < n; ++i) { for (j = 0; j < n; ++j) printf("%5d", x[i][j]); printf("\n"); } } void func(int x[][N], int n) { int i, j; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { if (j >= i) x[i][j] = i + 1; else x[i][j] = j + 1; } } }
#include <stdio.h> #include <string.h> #define N 80 int main() { char views1[N] = "hey, C, I have not love u yet."; char views2[N] = "hey, C, how can I love u?"; char t[N]; printf("交换前: \n"); printf("views1: %s\n", views1); printf("views2: %s\n", views2); // 交换 strcpy_s(t, views1); strcpy_s(views1, views2); strcpy_s(views2, t); printf("交换后: \n"); printf("views1: %s\n", views1); printf("views2: %s\n", views2); return 0; }
#include <stdio.h> #include <string.h> #define N 80 int main() { char views[2][N] = { "hey, C, I have not love u yet.", "hey, C, how can I love u?" }; char t[N]; printf("交换前: \n"); printf("views1: %s\n", views[0]); printf("views2: %s\n", views[1]); // 交换 strcpy_s(t, views[0]); strcpy_s(views[0], views[1]); strcpy_s(views[1], t); printf("交换后: \n"); printf("views1: %s\n", views[0]); printf("views2: %s\n", views[1]); return 0; }
#include <stdio.h> #include <string.h> #include<stdlib.h> #define N 5 #define M 20 void bubble_sort(char str[][M], int n); int main() { char name[][M] = { "Bob", "Bill", "Joseph", "Taylor", "George" }; int i; printf("输出初始名单:\n"); for (i = 0; i < N; i++) printf("%s\n", name[i]); printf("\n排序中...\n"); bubble_sort(name, N); printf("\n按字典序输出名单:\n"); for (i = 0; i < N; i++) printf("%s\n", name[i]); return 0; } void bubble_sort(char str[][M], int n) { int i,j; char t[M]; for (i = 0; i < N - 1;i++) { for (j = 0; j < N - 1; j++) { if (strcmp(str[j], str[j + 1]) > 0) { strcpy_s(t, str[j]); strcpy_s(str[j], str[j + 1]); strcpy_s(str[j + 1], t); } } } }