《C语言程序设计(现代方法第二版)》习题集

本文介绍了三个编程任务,包括解析13位ISBN码,将(xxx)xxx-xxxx格式的电话号码转化为xxx.xxx.xxxx格式,以及创建4x4矩阵展示16个数字并计算行、列和对角线的和,探讨了幻方的概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


第三章 格式化输入/输出

3. 图书用国际标准书号(ISBN)进行标识

2007年1月1日之后分配的ISBN包含13位数字(旧的ISBN使用10位数字),分为5组,如978-0-393-97950-3。第一组(GS1前缀)目前为978或979。第二组(组标识)指明语言或者原出版国(如0和1用于讲英语的国家)。第三组(出版商编号)表示出版商(393是W. W.Norton出版社的编号)。第四组(产品编号)是由出版商分配的用于识别具体哪一本书的(97950)。ISBN的末尾是一个校验数字,用于验证前面数字的准确性。编写一个程序来分解用户录入的ISBN信息:
在这里插入图片描述

代码

// Created by YaPotato at 2023/3/4 20:48.
// The filename is identifyISBN.c
//
# include <stdio.h>
# define SIZE 5

void printIBSN(int *IBSN);
void input();

int main(void){
    // example: 978-0-393-97950-3
    /*
     * The distribution ISBN contains 13 number(old used ISBN is ten number) that have a five group.
     * In the time, initiate number is 978 or 979. In addition a group number, which show clearly public country or language.
     * The third group represent publisher. In last but lasted number that distinguish the specific book. Last a group number is test and verify.
     */
    input();

    return 0;
}

void input(){
    int gs1, group, publisher, item, digit;

    printf("Enter ISBN: ");

    scanf("%d-%d-%d-%d-%d", &gs1, &group, &publisher, &item, &digit);
    int ISBN[] ={gs1, group, publisher, item, digit};

    printIBSN(ISBN);
}

void printIBSN(int *IBSN){
    input();
    char const *p[] = {"GS1 prefix", "Group identifier", "Publisher code", "Item number", "Check digit"};
    for (int i = 0; i < SIZE; i++) {
        printf("%s: %d\n", p[i], IBSN[i]);
    }
}



4. 编写一个程序,提示用户以(xxx) xxx-xxxx的格式输入电话号码,并以xxx.xxx.xxxx的格式显示该号码

在这里插入图片描述

代码

// Created by YaPotato at 2023/3/4 21:09.
// The filename is phoneNumber.c
//
# include <stdio.h>

int input(int x1, int x2, int x3);

int main(void){
//    input();
    int x1, x2, x3;
    printf("Enter phone number [(xxx) xxx-xxxx]:");
    scanf("(%d) %d-%d", &x1, &x2, &x3);
    printf("%d.%d.%d", x1, x2, x3);
    return 0;
}



5.编写一个程序,要求用户(按任意次序)输入从1到16的所有整数,然后用4×4矩阵的形式将它们显示出来,再计算出每行、每列和每条对角线上的和:

Enter the numbers from 1 to 16 in any order:
16 3 2 13 5 10 11 8 9 6 7 12 4 15 14 1
16   3   2   13
 5  10  11    8
 9   6   7   12
 4  15  14    1
Row sums: 34 34 34 34
Column sums: 34 34 34 34
Diagonal sums: 34 34

如果行、列和对角线上的和都一样(如本例所示),则称这些数组成一个幻方(magic square)。这里给出的幻方出现于艺术家和数学家Albrecht Dürer在1514年的一幅画中。(注意,矩阵的最后一行中间的两个数给出了该画的创作年代。)


代码

// Created by YaPotato at 2023/3/5 11:26.
// The filename is order.c
//
# include <stdio.h>
# define SIZE 4
# define MAXSIZE 16
# define DIAGONALSIZE 2

enum OPTION{ROWSUMS, COLUMSUMS, DIAGONALSUMS};

void printHand(int p[SIZE][SIZE], int option, char *str);
void forRange(int Isize, int Jsize, int p[SIZE][SIZE]);
void printQ(int p[SIZE][SIZE]);

int main(){
    printf("Enter the number from 1 to 16 in any order:\n");
    // static space;
    int p[MAXSIZE] = {16, 3, 2, 13, 5, 10, 11, 8, 9, 6, 7, 12, 4, 15, 14, 1};
    int q[SIZE][SIZE];
    int *r = (int *)p;

    int const printNumber = 3;
    char *s[] = {"Row Sums", "Colum Sums", "Diagonal Sums"};


    // matrix
    /*
     * i=0;i<4;i++ j=i;j<4;j++
     * i -> 0   j -> 0
     * q[0][0] -> 16
     * q[0][1] -> 3
     * q[0][2] -> 2
     * q[0][3] -> 13
     *
     * q[1][0] -> 5
     * q[1][1] -> 10
     * q[1][2] -> 11
     * q[1][3] -> 8
     * ...
     */
    // Save other
    int l = 0;
    for (int k = 0; k < SIZE; k++) {
        for (int j = 0; j < SIZE; j++) {
            q[k][j] = r[l];
            l++;
        }
    }

    printQ(q);
    for (int i = 0; i < printNumber; i++) {
        printHand(q, i, s[i]);
    }

    return 0;
}

void printQ(int p[SIZE][SIZE]){
    for (int l = 0; l < SIZE; l++) {
        for (int m = 0; m < SIZE; m++) {
            printf("%d ", p[l][m]);
        }
        printf("\n");
    }
}

void forRange(int Isize, int Jsize, int p[SIZE][SIZE]){
    int sum = 0;
    for (int i = 0; i < Isize; i++) {
        for (int j = 0; j < Jsize; j++) {
            sum += p[i][j];
        }
        printf("%d ", sum);
        sum = 0;
    }
    printf("\n");
}

void printHand(int p[SIZE][SIZE], int option, char *str){
    printf("%s: ", str);
    switch (option) {
        case ROWSUMS:
            forRange(SIZE, SIZE, p);
            break;
        case COLUMSUMS:
            forRange(SIZE, SIZE, p);
            break;
        case DIAGONALSUMS:
            forRange(DIAGONALSIZE, SIZE, p);
            break;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值