练习10-1
#include <stdio.h> void adjust_point(int*n) { if (*n > 100) *n = 100; if (*n < 0) *n = 0; } int main() { int x; printf("请输入一个数:"); scanf("%d", &x); adjust_point(&x); printf("修改后的值是%d",x); }
练习10-2
void decrement_date(int* y, int* m, int* d) { *d -= 1; if (*d == 0) { *m -= 1; if (*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 10 || *m == 8 ||* m == 0) { *d = 31; } if (*m == 2) { *d = 28; if (*y % 4 == 0) { *d = 29; } } else *d = 30; if (*m == 0) { *m = 12; *y -= 1; } } }
void increment_date(int* y, int* m, int* d) { *d += 1; if ((*m == 1 || *m == 3 || *m == 5 || *m == 7 || *m == 10 || *m == 8 || *m == 12) && (*d == 32)) { *d = 1; *m += 1; } if ((*m == 4 || *m == 6 || *m == 9 || *m == 11 ) && (*d == 31)) { *d = 1; *m += 1; } if (*m == 2) { if (*d == 29&& ((*y & 4) != 0)) { *d = 1; *m += 1; } if (*d == 30 && ((*y & 4) == 0)) { *d = 1; *m += 1; } } if (*m == 13) { *y += 1; *m = 1; } }
练习10-3
#include<stdio.h> void swap(int* px, int*py) { int temp = *px; *px = *py; *py = temp; } void sort3(int*n1, int*n2, int*n3) { if (*n1 > *n2) { swap(n1, n2); } if (*n1 > * n3) { swap(n1, n3); } if (*n2 > * n3) { swap(n2, n3); } } int main(void) { int n1, n2, n3; printf("n1="); scanf("%d", &n1); printf("n2="); scanf("%d", &n2); printf("n3="); scanf("%d", &n3); putchar('\n'); sort3(&n1, &n2, &n3); printf("%d,%d,%d", n1, n2, n3); }
练习10-4
#include <stdio.h> #define number 5 void set_idx(int*v, int n) { int i = 0; for (i = 0; i < n; i++) { *(v + i) = i; } } int main() { int i; int x[number]; set_idx(x, number); for (i = 0; i < number; i++) { printf("x[%d]=%d", i,x[i] ); putchar('\n'); } }
练习10-5
会报错
数组中元素会溢出,因为直接从v【2】开始赋值。