1、
代码
#include<iostream>
int main()
{using namespace std;int num1;int num2;int total=0;cout << "请输入開始数字\n";cin >> num1;cout << "请输入结束数字\n";cin >> num2;for (num1; num1<=num2; num1++)total = num1 + total;cout << num1 << " 和 " << num2 << "之间的整数和为 " << total <<endl;return 0;
}
执行结果
2、
代码
#include<iostream>
int main()
{using namespace std;double total = 0.0;double in;cout << "请输入数字:";cin >> in;while (in != 0){total += in;cout << "全部输入数的和为:" << total << "\n";cout << "请输入下一个数字:";cin >> in;}cout << "执行结束";return 0;
}
执行结果
3、
代码
#include<iostream>
int main()
{using namespace std;double daphne=100;double cleo=100;int year=1;while (daphne >= cleo){daphne += 100*0.1;cleo += cleo*0.05;cout << "第" << year << "年,daphne投资价值为 " << daphne << "cleo投资价值为 " << cleo <<endl;year++;}cout << "第 " << year << "年,时cleo超过daphne的投资价值"<< endl;return 0;
}
执行结果
4、
代码
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{using namespace std;int sales[12];int total;for (int i=1; i<=12; i++){cout << "请输入" << months[i-1] << "销售数量:";cin >> sales[i-1];}for (int j=0; j<12; j++){total = total+sales[j];}cout << "总销售为:" << total <<endl;return 0;
}
执行结果
5、
代码
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{using namespace std;int sales[3][12];int total[3] = {0};   //一定要初始化。不然初始值不为0int sum;for (int a=1;a<=3;a++){for (int i=1; i<=12; i++){cout << "请输入第"<< a << "年" << months[i-1] << "销售数量:";cin >> sales[a-1][i-1];}}for (int b=0; b<3; b++){for (int j=0; j<12; j++){total[b] = total[b]+sales[b][j];}sum = sum + total[b];cout << "第" << b+1 << "年总销量为" << total[b] <<endl;}cout << "总销售为:" << sum <<endl;return 0;
}
执行结果
6、
代码
<pre name="code" class="cpp">//一定要加while (cin.get() != '\n');
#include<iostream>
using namespace std;
const int LEN = 60;
struct Car
{char brand[LEN];int year;
};
int main()
{int num;cout << "How many cars do you wish to catalog?";cin >> num;while (cin.get() != '\n');Car *ps = new Car[num];for (int i=0;i<num;i++){cout << "Car #" << (i+1) << ":\n";cout << "Please enter the make:";cin.getline(ps[i].brand, LEN);cout << "Please enter the year made:";cin >> ps[i].year;while(cin.get() != '\n');}cout << "Here is your collection:\n";for (int i=0; i<num; i++){cout << ps[i].year << " " << ps[i].brand <<endl;}delete [] ps;return 0;
}
执行结果
7、
代码
#include<iostream>
#include<cstring>
const int STR_LEN=60;
int main()
{using namespace std;char words[STR_LEN];int count=0;cout << "Enter words(to stop,type the word done):\n";while (cin >> words && strcmp("done", words))++count;cout << "You entered a total of " << count << " words .\n" <<endl;return 0;
}
执行结果
8、
代码
#include<iostream>
#include<string>
int main()
{using namespace std;string words;int count=0;cout << "Enter words {to stop,type the word done}:\n";while (cin >> words && words != "done")++count;cout << "You entered a total of " << count << " words .\n";return 0;
}
执行结果
9、
代码
#include<iostream>
int main()
{using namespace std;int row;cout << "Enter number of row:";cin >> row;for (int i=0; i<row; i++){for (int j=row-i; j>1; j--){cout << ".";}for (int k=0; k<=i; k++){cout << "*";}cout << "\n";}return 0;
}
执行结果

版权声明:本文博主原创文章。博客,未经同意不得转载。

转载于:https://www.cnblogs.com/gcczhongduan/p/4888896.html

C++ Primer章课后编程问题相关推荐

  1. C++ Primer Plus课后编程练习第6章参考代码

    (C++ Primer Plus课后编程练习第6章参考代码) 声明: 作者入门小白,将学习过程中的代码做一些分享,仅供大家参考,欢迎大家交流指正.全部编译运行过,水平有限,不喜勿喷. 环境: Wind ...

  2. 【C Primer Plus第二章课后编程题】

    [C Primer Plus第二章课后编程题] 2.1 2.2 2.3 2.4 2.5 2.6 2.7 2.8 2.1 #include <stdio.h> int main (void) ...

  3. C++ Primer Plus第四章课后编程

    C++ Primer Plus第四章课后编程 4.12 复习题* 4.13 编程练习* 三句话,希望读者可以先看* 4.12 复习题* #include<iostream> #includ ...

  4. C primer plus 第四章课后编程练习答案笔记解释整理

    第四章的编程练习: 1.编写一个程序,提示用户输入名和姓,然后以"名,姓"的格式打印. 编程分析: 程序功能是读取用户输入的字符串,并且重新格式化输出.应该针对名和姓分别定义对应的 ...

  5. C++ Primer Plus(第六版) 第4章 课后编程题

    1.输出下列的信息:名字包含多个字母:程序中将下调整成绩,向上调整一个字母 What is your first name? Betty Sue What is your last name? Yew ...

  6. C++ Primer Plus第二章课后编程答案

    1. #include<iostream>using namespace std;int main() {cout << "我的名字是:帅哥" <&l ...

  7. 尚学堂Java学习第四章课后编程题作业参考答案

    我只是一个学习java 的初学者哈哈哈,代码写的都很烂,望大佬们指点指点. #尚学堂第三章:java面向对象基础 ##课后作业编程题一: 题目:1. 编写 Java 程序用于显示人的姓名和年龄.定义 ...

  8. Web前端开发精品课HTML CSS JavaScript基础教程第十四章课后编程题答案

    编程题: 下面有一段代码,请在这段代码的基础上使用正确的选择器以及这两章学到的字体样式.文本样式来实现图14-13所示的效果. <!DOCTYPE html> <html> & ...

  9. Web前端开发精品课HTML CSS JavaScript基础教程第六章课后编程题答案

    编程题: 利用这样一章学到的知识,制作如图6-10所示的表格效果,并且要求代码语义化. 用VS2013新建ASP.NET空网站,添加新项,建立HTML文件,向其加入以下代码: <!DOCTYPE ...

最新文章

  1. 0x80070659系统策略禁止这个安装 vc_教你一招,解决所有系统丢失DLL文件的问题!...
  2. Gridview 隐藏列
  3. python nums函数获取结果记录集有多少行记录_PHP mysqli_num_rows():获取查询结果的行数...
  4. 地址html后面传参,JS中获取地址栏url后面的参数的三种方法
  5. postgresql 动态添加过滤条件_XsqlFilterResult----动态生成sql语句的类,过滤一些为空的查询条件...
  6. maven配置testng_TestNG Maven Surefire插件配置
  7. 应用程序的可视化图形引擎库控件Vectordraw Developer Framework
  8. 常见的HTTP状态码大全
  9. 工程系列 计算机中级职称,工程类中级职称有哪些
  10. STM32F7 I2S接口 WM8978 边播放 边录音
  11. win7 计算机定时关机脚本,电脑设置定时关机的方法 win7系统怎么设置定时关机...
  12. devops运维平台汇总
  13. 快速幂与快速矩阵幂(以大数下的斐波那契数列为例)
  14. RabbitMq 消息接收确认(可靠消费)
  15. 127.0.0.1 拒绝了我们的连接请求--访问本地IP时显示拒绝访问
  16. 各种奇葩3389连接不上的解决方法
  17. Java中英文字母的大小写转换ideaeclipse大小写快捷键
  18. 戴森全新V12 Detect Slim轻量无绳吸尘器中国首发
  19. RocketDock 重启之后又回到默认样式的问题
  20. Python 生产者消费者模型

热门文章

  1. 虚拟机安装黑群晖_【群晖系统】HEI群辉DSM 6.2.1 系统安装图文教程 (19年2月)
  2. 人工智能在fpga的具体应用_人工智能带动了FPGA的发展
  3. h5 网站滚动到某个位置
  4. java用scanner 和random的一个小案例
  5. C++(STL):31 ---关联式容器map源码剖析
  6. linux常用解压和压缩文件的命令
  7. mysql hp ux_hp ux apa 切换
  8. 密码学在区块链隐私保护中的应用学习
  9. 以太坊智能合约 编译脚本
  10. Android Gradle 多渠道打包、动态配置AppName