练习10-1
#include<stdio.h>void adjust_point(int *n){if (*n < 0)*n = 0;elseif (*n > 100)*n = 100;return;}int main(void){int x;printf("input integer:");scanf("%d",&x);adjust_point(&x);printf("adjusted integer:%d\n",x);return 0;}
练习10-2
#include<stdio.h>void increment_date(int *y, int *m, int *d){if ((*m == 12) && (*d == 31))//跨年{*y += 1;*m = 1;*d = 1;}elseif (((*m == 1)&& (*d == 31)) || //跨月((*m == 3) && (*d == 31))||((*m == 5) && (*d == 31))||((*m == 7) && (*d == 31))||((*m == 8) && (*d == 31))||((*m == 10) && (*d == 31))||((*m == 12) && (*d == 31))||((*m == 4) && (*d == 30))||((*m == 6) && (*d == 30))||((*m == 9) && (*d == 30))||((*m == 11) && (*d == 30))||(((*y)%4!=0)&&(*m==2)&&(*d==28))||//平年(((*y)%100==0)&&((*y)%400!=0)&& (*m == 2) && (*d == 28))||//平年(((*y)%400==0)&&(*m==2)&&(*d==29))|| //闰年((*y)%4==0)&&((*y)%100!=0)&&(*m==2)&&(*d==29))//闰年{*m += 1;*d = 1;}else//不跨年不跨月{*d += 1;}return;}void decrement_date(int *y, int *m, int *d){if ((*m == 1) && (*d == 1))//跨年{*y -= 1;*m = 12;*d = 31;}elseif (((*m == 2) && (*d == 1)) || //跨月((*m == 4) && (*d == 1)) ||((*m == 6) && (*d == 1)) ||((*m == 8) && (*d == 1)) ||((*m == 9) && (*d == 1)) ||((*m == 11) && (*d == 1))){*m -= 1;*d = 31;}elseif (((*m == 5) && (*d == 1)) || //跨月((*m == 7) && (*d == 1)) ||((*m == 10) && (*d == 1)) ||((*m == 12) && (*d == 1))){*m -= 1;*d = 30;}elseif ((((*y) % 4 != 0) && (*m == 3) && (*d == 1)) ||//跨月 平年 (((*y) % 100 == 0) && ((*y) % 400 != 0) && (*m == 3) && (*d == 1))) //跨月 平年{*m -= 1;*d = 28;}elseif ((((*y) % 400 == 0) && (*m == 3) && (*d == 1)) || //跨月 闰年((*y) % 4 == 0) && ((*y) % 100 != 0) && (*m == 3) && (*d == 1)) //跨月 闰年{*m -= 1;*d = 29;}else //不跨年不跨月{*d -= 1;}return;}int main(void){int y, m, d;printf("input year month date:");scanf("%d %d %d",&y,&m,&d);decrement_date(&y, &m, &d);printf("decrement_date:%d %d %d\n",y,m,d);increment_date(&y, &m, &d);//抵消devrement_dateincrement_date(&y, &m, &d);printf("increment_date:%d %d %d\n",y,m,d);return 0;}
练习10-3
#include<stdio.h>void sort3(int *n1, int *n2, int *n3){int tmp;if (*n1 > *n2){tmp = *n2;*n2 = *n1;*n1 = tmp;}if (*n1 > *n3){tmp = *n3;*n3 = *n1;*n1 = tmp;}if (*n2 > *n3){tmp = *n2;*n2 = *n3;*n3 = tmp;}return;}int main(void){int n1,n2,n3;printf("input n1 n2 n3:");scanf("%d %d %d",&n1,&n2,&n3);sort3(&n1,&n2,&n3);printf("sort:%d %d %d\n",n1,n2,n3);return 0;}
练习10-4
#include<stdio.h>#define NUM 5void set_idx(int *v, int n){int i;for (i = 0; i < n; i++){v[i] = i;}return;}int main(void){int i;int a[NUM];set_idx(a,NUM);for (i = 0; i < NUM; i++){printf("a[%d] = %d\n",i,a[i]);}return 0;}
练习10-5
#include <stdio.h>void ary_set(int v[], int n, int val){int i;for (i = 0; i < n; i++)v[i] = val;}int main(void){int i;int a[] = { 1, 2, 3, 4, 5 };ary_set(&a[2], 5, 99);for (i = 0; i < 5; i++)printf("a[%d] = %d\n", i, a[i]);return 0;}