练习8-1
#include<stdio.h>
#define diff(x,y)(x-y)
int main() {
int x;
int y;
printf("x=");
scanf("%d", &x);
printf("y=");
scanf("%d", &y);
printf("%d", diff(x, y));
}
练习8-2
#include <stdio.h>
int max (int x,int y)
{
return (x>y?x:y)
}
int main() {
int a, b, c, d;
int max1, max2;
printf("请输入a b c d 的值:\n");
scanf("%d",&a);
scanf("%d", &b);
scanf("%d", &c);
scanf("%d", &d);
max1 = max(max(a, b), max(c, d));
printf("最大值是%d", max1);
putchar('\n');
max2 = max(max(max(a, b), c), d);
printf("最大值是%d", max2);
}
练习8-3
#include <stdio.h>
#define swap(int,a,b) {int temp;temp = a;x = y;y = temp;}
int main(void){
int x = 5;
int y = 10;
swap(int, x, y);
printf("x = %d\ny = %d\n", x, y);
return 0;
}
练习8-4
#include <stdio.h>
#define number 6
void bsort(int n, int v[]) {
int i, j;
for (i = 0; i < n - 1; i++) {
for (j = n - 1; j > i; j--) {
if (v[j - 1] < v[j]) {
int tem = v[j];
v[j] = v[j - 1];
v[j - 1] = tem;
}
}
}
}
int main() {
int i;
int v[number];
for (i = 0; i < number; i++) {
printf("v[%d]=", i);
scanf("%d", &v[i]);
}
putchar('\n');
bsort(number, v);
for (i = 0; i < number; i++)
{
printf("v[%d]=%d\n", i, v[i]);
}
}
练习8-5
#include <stdio.h>
enum season{春,夏,秋,冬,Invaild};
enum season select(void) {
int tmp;
do {
printf("0...春 1...夏 2...秋 3...冬 4...结束");
scanf("%d", &tmp);
} while (tmp< 春 || tmp> Invaild);
return tmp;
}
int main(void) {
enum season selected;
do {
switch (selected=select())
{
case 春:break;
case 夏:break;
case 秋:break;
case 冬:break;
}
} while (selected != Invaild);
return 0;
}