《C语言程序设计现代方法 第2版》 第三章课后习题答案

2019-11-23 463点热度 1人点赞 0条评论

前言

本人在通过《C语言程序设计:现代方法(第2版)》系统学习C语言时,发现国内并没有该书完整的课后习题答案,所以就想把自己在学习过程中所做出的答案分享出来,以供大家参考。因为并没有权威的答案来源,所以可能会存在错误的地方,还望大家帮助指出。

 

第三章编程题答案

3.1

[highlight lanaguage="C"]

#include 

int main(void)
{
    int day, month, year;
    
    printf("Enter a data (mm/dd/yy):");
    scanf("%d/%d/%d", &month, &day, &year);
    printf("You entered the data %d%.2d%.2d", year, month, day);
    
    return 0;
}

[/highlight]

3.2

[highlight lanaguage="C"]

#include 

int main(void)
{
    int num, day, month, year;
    float price;
    
    printf("Enter item number:");
    scanf("%d", &num);
    printf("Enter unit price:");
    scanf("%f", &price);
    printf("Enter a data (mm/dd/yy):");
    scanf("%d/%d/%d", &month, &day, &year);
    
    printf("Item\t\tUnit\t\tPurchase\n");
    printf("\t\tPrice\t\tData\n");
    printf("%-d\t\t$%7.2f\t%-.2d\/%.2d\/%d\n", num, price, month, day, year);
    
    return 0;
}

[/highlight]

3.3

[highlight lanaguage="C"]

#include 

int main(void)
{
  int prefix, group, publisher, item, check_digit;

  printf("Enter ISBN: ");
  scanf("%d-%d-%d-%d-%d", &prefix, &group, &publisher, &item, &check_digit);

  printf("GS1 prefix: %d\n", prefix);
  printf("Group identifier: %d\n", group);
  printf("Publisher code: %d\n", publisher);
  printf("Item number: %d\n", item);
  printf("Check digit: %d\n", check_digit);
  
  return 0;
}

[/highlight]

3.4

[highlight lanaguage="C"]

#include 

int main(void)
{
    int num1, num2, num3;
    
    printf("Enter phone number [(xxx) xxx-xxxx]:");
    scanf("(%d) %d-%d", &num1, &num2, &num3);
    printf("You entered %d.%d.%d", num1, num2, num3);
    
    return 0;
}

[/highlight]

3.5

[highlight lanaguage="C"]

/*输入1到16所有整数,以4*4显示,并计算每行、每列、和对角线的和 */

#include 

int main(void)
{
    /* 定义十六个数字的变量,从num1到num16 */
    int num1 = 0, num2 = 0, num3 = 0, num4 = 0, num5 = 0, num6 = 0, num7 = 0, num8 = 0;
    /* 拆成两行定义是因为写在一行里实在是太长了,不方便阅读 */
    int num9 = 0, num10 = 0, num11 = 0, num12 = 0, num13 = 0, num14 = 0, num15 = 0, num16 = 0;
    /* 定义4×4矩阵的每行和的变量,从row1到row4 */
    int row1 = 0, row2 = 0, row3 = 0, row4 = 0;
    /* 定义4×4矩阵的每列和的变量,从col1到col4 */
    int col1 = 0, col2 = 0, col3 = 0, col4 = 0;
    /* 定义两个对角线和的变量dia1和dia2 */
    int dia1 = 0, dia2 = 0;

    printf("Enter the numbers from 1 to 16 in any order: ");
    /* 将十六个数字分别存入到16个变量里 */
    scanf("%d%d%d%d%d%d%d%d", &num1, &num2, &num3, &num4, &num5, &num6, &num7, &num8);
    /* 同样因为太长了,所以拆成两行写 */
    scanf("%d%d%d%d%d%d%d%d", &num9, &num10, &num11, &num12, &num13, &num14, &num15, &num16);

    /* 显示出矩阵,根据题目示例,每个数字应该保证两个字符宽,右对齐。通过制表符来使每行和每列对齐 */
    printf("%2d\t%2d\t%2d\t%2d\t\n%2d\t%2d\t%2d\t%2d\t\n", num1, num2, num3, num4, num5, num6, num7, num8);
    printf("%2d\t%2d\t%2d\t%2d\t\n%2d\t%2d\t%2d\t%2d\t\n\n\n", num9, num10, num11, num12, num13, num14, num15, num16);

    /* 计算每行的和 */
    row1 = num1 + num2 + num3 + num4;
    row2 = num5 + num6 + num7 + num8;
    row3 = num9 + num10 + num11 + num12;
    row4 = num13 + num14 + num15 + num16;

    /* 计算每列的和 */
    col1 = num1 + num5 + num9 + num13;
    col2 = num2 + num6 + num10 + num14;
    col3 = num3 + num7 + num11 + num15;
    col4 = num4 + num8 + num12 + num16;

    /* 计算两个对角线的和 */
    dia1 = num1 + num6 + num11 + num16;
    dia2 = num4 + num7 + num10 + num13;

    /* 显示结果 */
    printf("Row sums: %d  %d  %d  %d  \nColumn sums: %d  %d  %d  %d  \n", row1, row2, row3, row4, col1, col2, col3, col4);
    printf("Diagonal sums: %d  %d  \n", dia1, dia2);

    return 0;
}

[/highlight]

3.6

[highlight lanaguage="C"]

/*同时输入两个分数并计算*/

#include 

int main(void)
{
    /* 定义两个分数的变量,分子分别是x1和x2,分母是y1和y2 */
    int x1 = 0, y1 = 0, x2 = 0, y2 = 0;
    /* 计算出的分子用result_num变量表示,分母用result_denom变量表示 */
    int result_num = 0, result_denom = 0;

    printf("Enter two fractions separated by a plus sign: ");
    /* 读取两个分数 */
    scanf("%d/%d+%d/%d", &x1, &y1, &x2, &y2);

    /* 计算两个分数的和 */
    result_num = x1 * y2 + x2 * y1;
    result_denom = y1 * y2;
    printf("The sum is: %d/%d\n", result_num, result_denom);

    return 0;
}

[/highlight]

close

订阅博客,及时获取文章更新通知

订阅博客,及时获取文章更新通知

古月弧

文章评论