C++ Primer Plus笔记: 2023.07.14

这篇具有很好参考价值的文章主要介绍了C++ Primer Plus笔记: 2023.07.14。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

第五章编程题:
(1)
第一种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please input two integers: " << endl ;
	int a, b ;
	cin >> a ;
	cin >> b ;
	cout << "The sum of numbers between them: " ;
	for (int i = (a+1); i<=b; i++)
		a+=i ;
	cout << a << endl ;
	return 0 ;
}

第二种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter two integers: " << endl ;
	int a, b, c;
	cin >> a ;
	cin >> b ;
	c = a ;
	while ( c < b )
	{
		c++ ;
		a += c ;
	}
	cout << "The sum of these numbers between them: " ;
	cout << a << endl ;
	return 0 ;
}

第三种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please input two integers: " << endl ;
	int a, b, c ;
	cin >> a ;
	cin >> b ;
	c = a ;
	do
	{
		a += ++c ;
	} while (c < b) ;

	cout << "The sum of numbers between them: " << endl ;
	cout << a << endl ;
	return 0 ;
}

(2)

#include <iostream>
#include <array>
using namespace std ;

const int ArraySize = 100 ;

int main()
{
	array<long double, ArraySize> factorial ;
	factorial[1] = factorial[0] = 1 ;
	for (int i = 1; i<ArraySize; i++)
		factorial[i] = i * factorial[i-1] ;
	for (int i = 0; i<ArraySize; i++)
		cout << i << "! = " << factorial[i] << endl ;
	cout << "100! = " << 100 * factorial[99] << endl ;
	return 0 ;
}

(3)
第一种解法:

#include <iostream>
using namespace std ;

int main()
{
	int a=0, b ;
	cout << "Please enter numbers casually: " << endl ;
	cin >> b ;
	while ( b != 0)
	{
		cout << "Now the sum = "
			 << (a+=b) << endl ;
		cin >> b ;
	}
	cout << "That's the end! " << endl ;
}

第二种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter numbers casually: " << endl ;
	int a=0, b ;
	do
    {
		cin >> b ;
		a += b ;
		cout << "The sum of them = "
			 << a << endl ;
	} while ( b != 0 ) ;
	cout << "That's the end!" << endl ;
}

第三种解法:

#include <iostream>
using namespace std ;

int main()
{
	cout << "Please enter numbers casually: " << endl ;
	int a = 0, b ;
	for (cin >> b; (b != 0)&&(a+=b);)
	{
		cout << "The sum of them = " << a << endl ;
		cin >> b ;
	}
	cout << "That's the end ! " << endl ;
	return 0 ;
}

(4)
第一种解法:

#include <iostream>
using namespace std ;

int main()
{
	double Daphne, Cleo, i=0 ;
	Daphne = Cleo = 100 ;
	while (!(Cleo > Daphne))
	{
		i++ ;

		Daphne += 100 * 0.10 ;
		Cleo += 0.05 * Cleo ;

		cout << "Now Daphne have " << Daphne << " dallors.\n"
			 << "Cleo have " << Cleo << " dallors.\n" ;
	}
	cout << "After " << i << " years, Cleo's money beyond Daphne's.\n" ;
}

第二种解法:

#include <iostream>
using namespace std ;

int main()
{
	double Daphne, Cleo, i = 0;
	for (Daphne = Cleo = 100;
		 Cleo <= Daphne ;
		 i++, Daphne += 0.10 * 100, Cleo += 0.05 * Cleo) ;
	cout << "After " << i << " years, "
		 << "Cleo's money beyond Daphne's.\n"
		 << "Now Daphne have " << Daphne << " dollars.\n"
		 << "Cleo have " << Cleo << "dollars.\n" ;
}

(5)

#include <iostream>
#include <string>
using namespace std ;

int main()
{
	string month[12] = {"Jenuary", "Febrary", "March", "April",
						"May", "June", "July", "August", "September",
						"October", "November", "December"} ;
	int books[12] = {0}, sum = 0 ;
	cout << "Enter how much books have sold: " << endl ;

	for(int i=1; i<=12; i++)
	{
		cout << month[i-1] << " : " ;
		cin >> books[i-1] ;
		sum += books[i-1] ;
	}

	cout << "This years have sold " << sum << " \"C++ for Fools\".\n" ;
}

(6)

#include <iostream>
#include <string>
using namespace std ;

int main()
{
	string month[12] = {"Jenuary", "Febrary", "March", "April", "May",
			    "June", "July", "August", "September", "October",
			    "November", "December"} ;
	int books[3][12] = {0} ;
	int sum[3] = {0} ;
	
	for (int i=1; i<=3; i++)
	{
		cout << "In year " << i << " :\n" ;
		for (int j=1; j<=12; j++)
		{
			cout << month[j-1] << " : " ;
			cin >> books[i-1][j-1] ;
			sum[i-1] += books[i-1][j-1] ;
		}
		cout << "The sum of this year = " << sum[i-1] << endl ;
	}
	cout << "The sum of 3 years = " << sum[0] + sum[1] + sum[2]
	     << endl ;
	return 0 ;
}

(7)文章来源地址https://www.toymoban.com/news/detail-563878.html

#include <iostream>
#include <string>
using namespace std ;

struct Car{
	string brand ;
	int year ;
} ;

int main()
{
	int c ;
	cout << "How many cars do you wish to catalog? " ;
	cin >> c ;
	Car* k = new Car[c] ;
	
	for (int i=1; i<=c; i++)
	{
		cout << "Car #" << i << ":\n" ;
		cout << "Please enter the make: " ;
		cin.get() ;                      // 这是非常必要的,用来消除换行符
		getline(cin, k[i-1].brand) ;
		cout << "Please enter the year made: " ;
                cin >> k[i-1].year ;
	}
	
	cout << "Here is your collection: " << endl ;

	for (int i=1; i<=c; i++)
 	{
                cout << k[i-1].year << " " << k[i-1].brand << endl ;
        }
	
	delete [] k ;
}

到了这里,关于C++ Primer Plus笔记: 2023.07.14的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • C++ Primer Plus 第8章

    1.内联函数的运行速度比常规函数稍快,但代价是需要占用更多内存。如果程序在10个不同的地方调用同一个内联函数,则程序将包含该函数代码的10个副本。 2.注意,内联函数不能递归。 3.在声明引用时必须将其初始化,否则就会出现类似以下的报错: 4.引用一旦和某个变量

    2024年02月14日
    浏览(25)
  • C++ Primer Plus: 第10章(2)

    第10章编程题: (1) Account.h: Account.cpp: main.cpp: (2) Person.h: Person.cpp: main.cpp: (3) golf.h: golf.cpp: main.cpp: (4) Sales.h: Sales.cpp: main.cpp: (5) Stack.h: Stack.cpp: main.cpp: (6) Move.h: Move.cpp: main.cpp: (7) Plorg.h: Plorg.cpp: main.cpp: (8) List.h: List.cpp: main.cpp:

    2024年02月12日
    浏览(37)
  • 【C++】C++ primer plus第二章练习题

    c++程序的模块叫什么? 函数。 下面的预处理器编译指令是做什么用的? 包含头文件,将iostream文件的内容添加·到代码中 下面的语句是做什么用的? using namespace std; using是预编译器指令,让其使用std命名空间 什么语句可以用来打印短语“Hello, world”,然后开始新的一行? s

    2024年02月06日
    浏览(40)
  • C++ Primer Plus第五章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个要求用户输入两个整数的程序。该程序将计算并输出这两个整数之间包括这两个整数)所有整数的和。这里假设先输入较小的整数。例如,如果用户输入的是2和则程序将出29之间所有整数的和为44 2.使用array对

    2024年02月09日
    浏览(39)
  • C++ primer plus第七章编程练习答案

    1.编写一个程序,不断要求用户输入两个数,直到其中的一个为 0。对于每两个数,程序将使用一个南数来计算它们的调和平均数,并将结果返回给 main(),而后者将报告结果。调和平均数指的是倒数平均值的倒数,计算公式如下: 调和平均数=2.0*xy/(x + y) 2.编写一个程序,要求用

    2024年02月10日
    浏览(32)
  • C++ Primer Plus第二章编程练习答案

    答案仅供参考,实际运行效果取决于运行平台和运行软件 1.编写一个C++程序,它显示您的姓名和地址。 2.编写一个C程序它要求用户输入一个以 long 为单位的距离,然后将它转换为码(- ng等于220码) 3.编写1个C++程序它使用3个用户定义的函数(括mai()),并生成下面的输出Three blind

    2024年02月09日
    浏览(45)
  • 【CPP_Primer_Plus】C++ IDE推荐

    C++编译器推荐 windows 推荐 Resharper++插件 vcpkg 功能介绍 编辑器 Visual Studio 的编辑器具有出色的代码补全功能、语法突出显示、快速信息提示、附带代码修复建议的错误和警告。 IntelliSense 比 IntelliCode(内置于编辑器中的 AI 工具) 调试器 顶部的绿色运行按钮可启动调试程序。

    2024年02月09日
    浏览(30)
  • 《C++ Primer》第14章 重载运算与类型转换(一)

    参考资料: 《C++ Primer》第5版 《C++ Primer 习题集》第5版 重载的运算符是具有 特殊名字的函数 ,其名字有 operator 和要定义的运算符组合而成。和其他函数一样,重载运算符也具有返回类型、参数列表、函数体。 重载运算符函数的 参数数量 和该运算符的 运算对象数量一样多

    2024年01月20日
    浏览(24)
  • 2023-07-14力扣每日一题

    链接: 979. 在二叉树中分配硬币 题意: 一个二叉树,n个节点,节点 权值总和 为n, 每次 可以 相邻节点 间移动 1 权值 求让每个节点都为1的最少次数 解: 给定了一个树的结构体,先整一手DFS/BFS,n不大,随便莽莽 首先每个节点只需要剩下1,而且可以知道 叶子节点 如果不

    2024年02月16日
    浏览(27)
  • 2023-07-14:讲一讲Kafka与RocketMQ中存储设计的异同?

    2023-07-14:讲一讲Kafka与RocketMQ中存储设计的异同? 答案2023-07-14: 在Kafka中,文件的布局采用了Topic/Partition的方式,每个分区对应一个物理文件夹,且在分区文件级别上实现了顺序写入。然而,当一个Kafka集群拥有大量的主题和每个主题拥有数百个分区时,在高并发写入消息的

    2024年02月16日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包