IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> C++知识库 -> C语言程序设计 现代设计方法——第二章编程题答案 -> 正文阅读

[C++知识库]C语言程序设计 现代设计方法——第二章编程题答案

project01

#include <stdio.h>

int main(void)
{
	printf("            *\n");
	printf("           *\n");
	printf("          *\n");
	printf("         *\n");
	printf("  *     *\n");
	printf("   *   *\n");
	printf("     *\n");

	return 0;
}
#include <stdio.h>

int main(void) {
    
    printf("       *\n");
    printf("      * \n");
    printf("     *  \n");
    printf("*   *   \n");
    printf(" * *    \n");
    printf("  *     \n");

    return 0;
}

project02

#include <stdio.h>
#define SCALLE_FACTOR (4.0f / 3.0f)
#define PI 3.141592653f

int main(void)
{
	int radius = 10;
	float volume;

	volume = SCALLE_FACTOR * radius * radius * radius * PI;

	printf("Volume: %.2f\n", volume);

	return 0;
}
#include <stdio.h>

int main(void) {

    printf("Sphere volume: %.2f cubic meters\n", 4.0f/3.0f * 3.14f * 1000);
    return 0;
}

project03

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define SCALLE_FACTOR (4.0f / 3.0f)
#define PI 3.141592653f

int main(void)
{
	int radius;
	float volume;

	printf("Enter the radius: ");
	scanf("%d", &radius);

	volume = SCALLE_FACTOR * radius * radius * radius * PI;

	printf("Volume: %.2f\n", volume);

	return 0;
}

存在问题:
为什么输入与输出分开显示在两行,但输出的语句与输入的语句之间却没有换行??

#include <stdio.h>

int main(void) {

    int r = 0;

    printf("Enter radius: ");
    scanf("%d", &r);

    printf("\nSphere volume: %.2f cubic meters\n",
           4.0f/3.0f * 3.14f * r * r * r);
    return 0;
}

project04

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define RATE 0.05

int main(void)
{
	int amount;
	float tax_added;

	printf("Enter a amount: ");
	scanf("%d", &amount);

	tax_added = (float)(amount * (1 + RATE));

	printf("With tax added: $%.2f\n", tax_added);

	return 0;
}

存在问题:
1也是常量,应该用宏定义
涉及到强制类型转换,好还是不好?
多定义了一个变量,增加了存储空间

#include <stdio.h>

int main(void) {

    float money = 0.0f;
    printf("Enter an amount: ");
    scanf("%f", &money);
    printf("With tax added: $%.2f\n", money * 1.05f);

    return 0;
}

project05

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void) 
{

    int x;

    printf("Enter x: ");
    scanf("%d", &x);

    printf("3x^5+2x^4-5x^3-x^2+7x-6 = %d\n",
        3*x*x*x*x*x + 2*x*x*x*x + 5*x*x*x - x*x + 7*x - 6);

    return 0;
}
#include <stdio.h>

int main(void) {

    int x = 0;

    printf("Enter value for x: ");
    scanf("%d", &x);
    printf("Result: %d\n",
           (3 * x * x * x * x * x) + (2 * x * x * x * x) - (5 * x * x * x)
           - (x * x) + (7 * x) - 6);

    return 0;
}

project06

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{

    int x;

    printf("Enter x: ");
    scanf("%d", &x);

    printf("3x^5+2x^4-5x^3-x^2+7x-6 = %d\n",
        ((((3*x + 2)*x - 5)*x - 1)*x + 7)*x - 6);

    return 0;
}
#include <stdio.h>

int main(void) {

    int x = 0;

    printf("Enter value for x: ");
    scanf("%d", &x);
    printf("Result: %d\n",
           ((((3 * x + 2) * x - 5) * x - 1) * x + 7) * x - 6);

    return 0;
}

project07

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{

    int money;
    int dollar_20, dollar_10, dollar_5, dollar_1;

    printf("Enter a dollar amount: ");
    scanf("%d", &money);

    dollar_20 = money / 20;
    dollar_10 = (money - dollar_20 * 20) / 10;
    dollar_5 = (money - dollar_20 * 20 - dollar_10 * 10) / 5;
    dollar_1 = money - dollar_20 * 20 - dollar_10 * 10 - dollar_5 * 5;

    printf("$20 bills: %d\n", dollar_20);
    printf("$10 bills: %d\n", dollar_10);
    printf(" $5 bills: %d\n", dollar_5);
    printf(" $1 bills: %d\n", dollar_1);

    return 0;
}

存在问题:
把存储输入数据的变量看成是一个不变的量了

#include <stdio.h>

int main(void) {

    int money = 0;

    printf("Enter a dollar amount: ");
    scanf("%d", &money);

    printf("$20 bills: %d\n", money/20);
    money -= 20 * (money/20);

    printf("$10 bills: %d\n", money/10);
    money -= 10 * (money/10);

    printf(" $5 bills: %d\n", money/5);
    money -= 5 * (money/5);

    printf(" $1 bills: %d\n", money);

    return 0;
}

project08

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

int main(void)
{

    float amountOfLoan, interestRate, monthlyPayment;
    float afterFirstPayment, afterSecondPayment, afterThridPayment;

    printf("Enter amount of loan:");
    scanf("%f", &amountOfLoan);    
    printf("Enter interest rate:");
    scanf("%f", &interestRate);    
    printf("Enter monthly payment:");
    scanf("%f", &monthlyPayment);

    afterFirstPayment = amountOfLoan + amountOfLoan * (interestRate / 100 / 12) - monthlyPayment;
    afterSecondPayment = afterFirstPayment + afterFirstPayment * (interestRate / 100 / 12) - monthlyPayment;
    afterThridPayment = afterSecondPayment + afterSecondPayment * (interestRate / 100 / 12) - monthlyPayment;

    printf("Balance remaining after first payment: $%0.2f\n", afterFirstPayment);
    printf("Balance remaining after second payment: $%0.2f\n", afterSecondPayment);
    printf("Balance remaining after third payment: $%0.2f\n", afterThridPayment);

    return 0;
}

存在问题:
同第7题同样的错误

#include <stdio.h>

int main(void) {

    float loan = 0.0f,
          rate = 0.0f,
          payment = 0.0f;

    printf("Enter amount of loan: ");
    scanf("%f", &loan);

    printf("Enter interest rate: ");
    scanf("%f", &rate);

    printf("Enter monthly payment: ");
    scanf("%f", &payment);


    loan = loan - payment + (loan * rate / 100.0 / 12.0);
    printf("Balance remaining after first payment: $%.2f\n", loan);

    
    loan = loan - payment + (loan * rate / 100.0 / 12.0);
    printf("Balance remaining after second payment: $%.2f\n", loan); 

    loan = loan - payment + (loan * rate / 100.0 / 12.0);
    printf("Balance remaining after third payment: $%.2f\n", loan);

    return 0;
}

  C++知识库 最新文章
【C++】友元、嵌套类、异常、RTTI、类型转换
通讯录的思路与实现(C语言)
C++PrimerPlus 第七章 函数-C++的编程模块(
Problem C: 算法9-9~9-12:平衡二叉树的基本
MSVC C++ UTF-8编程
C++进阶 多态原理
简单string类c++实现
我的年度总结
【C语言】以深厚地基筑伟岸高楼-基础篇(六
c语言常见错误合集
上一篇文章      下一篇文章      查看所有文章
加:2022-04-18 17:22:53  更:2022-04-18 17:25:08 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/23 23:50:13-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码