C++ Primer章课后编程问题
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
#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;
}
#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;
}
#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;
}
#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;
}
#include<iostream>
const char *months[12] = {"一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"};
int main()
{
using namespace std;
int sales[3][12];
int total[3] = {0}; //一定要初始化。不然初始值不为0
int 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;
}
<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;
}
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
#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;
}
#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;
}
#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;
}
版权声明:本文博主原创文章。博客,未经同意不得转载。
C++ Primer章课后编程问题的更多相关文章
- C++ Primer第九章课后编程问题
1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0J ...
- java科学和艺术语言 第六章 课后编程
家转载,为保留作者成果.转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也能够下载!假设你没有积分,能够联系我索要. 1. package Sixth ...
- Kenneth A.Lambert著的数据结构(用python语言描述)的第一章课后编程答案
第6题:工资部门将每个支付周期的雇员信息的列表保存到一个文本文件, 每一行的格式:<last name><hourly wage><hours worked> 编写 ...
- 《C++面向对象程序设计》第6章课后编程题2拓展
设计一个程序,其中有3个类CBank.BBank.GBank,分别为中国银行类,工商银行类和农业银行类.每个类都包含一个私有数据成员balance用于存放储户在该行的存款数,另有一个友元函数Total ...
- Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...
- Java程序设计(2021春)——第二章课后题(选择题+编程题)答案与详解
Java程序设计(2021春)--第二章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第二章课后题(选择题+编程题)答案与详解 第二章选择题 2.1 面向对象方法的特性 ...
- C++第三章课后作业答案及解析---指针的使用
今天继续完成上周没有完成的习题---C++第三章课后作业,本章题涉及指针的使用,有指向对象的指针做函数参数,对象的引用以及友元类的使用方法等 它们具体的使用方法在下面的题目中会有具体的解析(解析标注在 ...
- 《深入浅出Node.js》第7章 网络编程
@by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...
- 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试
20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...
随机推荐
- Android, IOS 史上最强多语言国际化,不仅第一次会尾随系统,并且会保存用户的语言设置
劲爆消息,我提供源代码了.你能够先看完再下载.也能够先下载再看完, android源代码地址: https://github.com/hebiao6446/------Bluetooth-Androi ...
- 重构——DataTable转泛型
泛型简单介绍 泛型能够最大限度的重用代码.保护类型的安全.提高性能. 泛型最常见的用途是创建集合类 泛型数据类型中使用的信息可在执行时通过反射 ...
- Android滑动到顶部悬停
无图说卵,先上图 jianshu-top.gif 查阅资料后,发现网上大部分都是用这种方法实现的: 多写一个和需要悬浮的部分一模一样的layout,先把浮动区域的可见性设置为gone.当浮动区域滑动到 ...
- amazeui学习笔记--js插件(UI增强3)--折叠面板Collapse
amazeui学习笔记--js插件(UI增强3)--折叠面板Collapse 一.总结 注意点: 1.data-am-collapse:这个东西就是展开折叠事件 2.am-collapse(包括其下属 ...
- 硬件——STM32 , 录音
战舰V3的录音程序解析 上一章,我们实现了一个简单的音乐播放器,本章我们将在上一章的基础上,实现一个简单的录音机,实现WAV录音.本章分为如下几个部: 50.1 WAV简介 50.2 硬件设计 50. ...
- [Nuxt] Load Data from APIs with Nuxt and Vuex
In a server-rendered application, if you attempt to load data before the page renders and the data f ...
- [Nuxt] Setup a "Hello World" Server-Rendered Vue.js Application with the Vue-CLI and Nuxt
Install: npm install -g vue-cli Init project: vue init nuxt/starter . Run: npm run dev Create a inde ...
- google校招在线測试题---2048
先附代码:(简单地说就是给出一个矩阵代表2048游戏的一个状态以及一个方向,输出往这个方向移动之后的矩阵) #include<iostream> #include<fstream&g ...
- POJ 3132 & ZOJ 2822 Sum of Different Primes(dp)
题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...
- ping localhost 没反应解决方式
今天在搭建导师公司的spark的集群的时候须要有一台机器须要ssh 到自己.可是运行以下两条命令后发现没有反应 ping localhost ping 127.0.0.1 之后问了下别人,发现是/et ...