《明解C语言》第三版 (入门篇) 第八章练习答案

练习8-1

#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));
	return 0;
}

练习8-2

#define max(x,y) ((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));  //是先求出a,b一组和c,d一组中的较大值,再比较两个较大值求最大值
    printf("最大值是%d", max1);
    putchar('\n');
    max2 = max(max(max(a, b), c), d);  //依次比较a和b的较大值,再用较大值依次比较c,d,而求出最大值
    printf("最大值是%d", max2);
	return 0;
}

练习8-3

#include <stdio.h>
#define swap(int,a,b) {int temp;temp = a;x = y;y = temp;}
int main()
{
    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]);
    }
    
    return 0;
}

练习8-5

#include <stdio.h>

enum sex {boy, girl, Invaild};
enum season {Spring, summer, fall, winter, Invaild};

int main() {
    enum sex s;
    enum season se;

    printf("请输入性别(男孩/女孩):");
    scanf("%d", &s);

    printf("请输入季节(春/夏/秋/冬):");
    scanf("%d", &se);

    if (s == boy && (se == spring || se == summer)) {
        printf("男孩喜欢%s\n", (se == spring) ? "春天" : "夏天");
    }
    else if (s == girl && (se == spring || se == summer)) {
        printf("女孩喜欢%s\n", (se == spring) ? "春天" : "夏天");
    }
    else if (s == girl && se == winter) {
        printf("女孩喜欢%s\n", "冬天");
    }
    else {
        printf("无效输入\n");
    }

    return 0;
}

简单一点的

enum season{Spring, summer, fall, winter, Invaild};

enum season select(void) {
    int tmp;
    do {
        printf("0...Spring   1...summer   2...fall   3...winter   4...结束");
        scanf("%d", &tmp);
    } while (tmp< Spring || tmp> Invaild);
    return tmp;
}

int main(void) {
    enum season selected;
    do {
        switch (selected=select())
        {
        case Spring:printf("Spring\n");break;
        case summer:printf("Summer\n");break;
        case fall  :printf("Fall\n");break;
        case winter:printf("Winter\n");break;
        }
    } while (selected != Invaild);
    return 0;    
}

练习8-6

#include <stdio.h>

int factorial(int n) {
    int i ;
    int sum = 1;
    for (i = 1; i <= n; i++) {
        sum*=i;
    }
    return sum;
}
int main() {
    int x;
    printf("请输入一个数:");
    scanf("%d", &x);
    printf("它的阶乘是:%d", factorial(x));
}

练习8-7

不认识这个知识点了,网上抄的。

#include <stdio.h>

int factorial(int n)
{
    if (n > 0)
        return n * factorial(n - 1);
    else
        return 1;
}

int combination(int n, int r) 
{

    if (n > 0)
        return  factorial(n) / (factorial(r) * factorial(n - r));
    else
        return 1;
}
int main() 
{
    int n;
    int r;
    printf("n=");
    scanf("%d", &n);
    printf("r=");
    scanf("%d", &r);
    printf("%d", combination(n, r));
    return 0;
}

练习8-8

对于这个知识点不理解可以去百度看介绍,方便理解程序

int gcd(int a,int b)
{
	int v,sum,n;
	if(a < b)
	{
		v = b;
		b = a;
		a = v;
	}
	sum = a%b;
	n =a*b;
	while(sum != 0)
	{
		a = b;
		b = sum;
		sum = a%b;
	}
	return b;
}
//26/12=2...2
//12/2=6...0
int main()
{
	int x,y;
	scanf("%d%d",&x,&y);
	gcd(x,y);
	printf("最大公约数是:%d  最小公倍数是:%d",gcd(x,y),(x*y)/gcd(x,y));
	return 0;
}

另一个网络方法

#include <stdio.h>
int gcd(int x, int y)
{
    int z;
    if (x != y)
    {
        if (x > y)
            x -= y;
        else
            y -= x;
        z = gcd(x, y);
    }
    else
        z = x;
    return z;
}
int main()
{
    int x, y;
    printf("x:");
    scanf("%d", &x);
    printf("y:");
    scanf("%d", &y);
    printf("gcd(%d,%d) = %d\n", x, y, gcd(x, y));
    return 0;
}

练习8-9

#include <stdio.h>
int main(void)
{
	int i = 0;
	int linenum = 1;
	char ch[128];
	while (ch[i] = getchar())
	{
		if (ch[i] == EOF)
			break;
		if (ch[i] == '\n')
			linenum++;
		i++;
	}
	printf("\nlinenum =%d\n",linenum);
	return 0;
}

练习8-10

#include <stdio.h>

int main(void)
{
    int i, ch,j;
    int cnt[10] = { 0 };

    while ((ch = getchar()) != EOF) {
        if (ch >= '0' && ch <= '9')
            cnt[ch - '0']++;

    }
    puts("数字字符的出现次数");
    for (i = 0; i < 10; i++) {
        printf("'%d':", i);
        for (j = 0; j < cnt[i]; j++) {
            putchar('*');
            
        }putchar('\n');
    }
    return 0;
}
相关推荐
lb36363636363 小时前
介绍一下数组(c基础)(详细版)
c语言
一丝晨光4 小时前
编译器、IDE对C/C++新标准的支持
c语言·开发语言·c++·ide·msvc·visual studio·gcc
执笔者5486 小时前
C语言:函数栈帧的创建与销毁
c语言
_小柏_7 小时前
C/C++基础知识复习(15)
c语言·c++
weixin_399264297 小时前
信捷 PLC C语言 POU 指示灯交替灭0.5秒亮0.5秒(保持型定时器)
c语言·开发语言
Darkwanderor7 小时前
用数组实现小根堆
c语言·数据结构·二叉树·
敲上瘾8 小时前
C++11新特性(二)
java·c语言·开发语言·数据结构·c++·python·aigc
知星小度S9 小时前
数据结构——排序
c语言·数据结构·算法
lb363636363610 小时前
扫雷游戏代码分享(c基础)
c语言·算法·游戏
DdddJMs__13512 小时前
C语言 | Leetcode C语言题解之第542题01矩阵
c语言·leetcode·题解