watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">

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}; //一定要初始化。不然初始值不为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;
}
执行结果


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;
}

执行结果

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">


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;
}
执行结果


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

C++ Primer章课后编程问题的更多相关文章

  1. C++ Primer第九章课后编程问题

    1. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ3V1Z2xlMjAxMA==/font/5a6L5L2T/fontsize/400/fill/I0J ...

  2. java科学和艺术语言 第六章 课后编程

    家转载,为保留作者成果.转载请注明出处,http://blog.csdn.net/netluoriver,有些文件在资源中也能够下载!假设你没有积分,能够联系我索要. 1. package Sixth ...

  3. Kenneth A.Lambert著的数据结构(用python语言描述)的第一章课后编程答案

    第6题:工资部门将每个支付周期的雇员信息的列表保存到一个文本文件, 每一行的格式:<last name><hourly wage><hours worked> 编写 ...

  4. 《C++面向对象程序设计》第6章课后编程题2拓展

    设计一个程序,其中有3个类CBank.BBank.GBank,分别为中国银行类,工商银行类和农业银行类.每个类都包含一个私有数据成员balance用于存放储户在该行的存款数,另有一个友元函数Total ...

  5. Java程序设计(2021春)——第一章课后题(选择题+编程题)答案与详解

    Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第一章课后题(选择题+编程题)答案与详解 第一章选择题 1.1 Java与面向对象程 ...

  6. Java程序设计(2021春)——第二章课后题(选择题+编程题)答案与详解

    Java程序设计(2021春)--第二章课后题(选择题+编程题)答案与详解 目录 Java程序设计(2021春)--第二章课后题(选择题+编程题)答案与详解 第二章选择题 2.1 面向对象方法的特性 ...

  7. C++第三章课后作业答案及解析---指针的使用

    今天继续完成上周没有完成的习题---C++第三章课后作业,本章题涉及指针的使用,有指向对象的指针做函数参数,对象的引用以及友元类的使用方法等 它们具体的使用方法在下面的题目中会有具体的解析(解析标注在 ...

  8. 《深入浅出Node.js》第7章 网络编程

    @by Ruth92(转载请注明出处) 第7章 网络编程 Node 只需要几行代码即可构建服务器,无需额外的容器. Node 提供了以下4个模块(适用于服务器端和客户端): net -> TCP ...

  9. 20145330《Java学习笔记》第一章课后练习8知识总结以及IDEA初次尝试

    20145330<Java学习笔记>第一章课后练习8知识总结以及IDEA初次尝试 题目: 如果C:\workspace\Hello\src中有Main.java如下: package cc ...

随机推荐

  1. [转]C#连接操作mysql实例

    本文转自:http://hi.baidu.com/zhqngweng/item/c4d2520cb7216877bfe97edf 第三方组件:Mysql.Data.dll说明:去官方网站下载Mysql ...

  2. 加固linux

    http://linoxide.com/linux-command/password-aging-secure-linux-access/

  3. Oracle中暂时表空间的清理

    作者:iamlaosong Oracle暂时表空间主要用来做查询和存放一些缓冲区数据. 暂时表空间消耗的主要原因是须要对查询的中间结果进行排序.暂时表空间的主要作用: 索引create或rebuild ...

  4. Oracle Web链接客户端

    TreeSoft数据库管理系统 http://www.treesoft.cn        

  5. POJ 1018 Communication System 贪心+枚举

    看题传送门:http://poj.org/problem?id=1018 题目大意: 某公司要建立一套通信系统,该通信系统需要n种设备,而每种设备分别可以有m个厂家提供生产,而每个厂家生产的同种设备都 ...

  6. spyder在虚拟机上运行内核一直崩溃

    可能的原因是因为虚拟机设置的内存过小了

  7. Eclipse RCP 中创建自己定义首选项,并能读取首选项中的值

    Eclipse RCP的插件中若想自定义首选项须要扩展扩展点: org.eclipse.core.runtime.preferences //该扩展点用于初始化首选项中的值 org.eclipse.u ...

  8. [Angular] Using useExisting provider

    Unlike 'useClass', 'useExisting' doesn't create a new instance when you register your service inside ...

  9. iOS开发Quartz2D之八:图形上下文状态栈

    #import "DrawView.h" @implementation DrawView - (void)drawRect:(CGRect)rect { // Drawing c ...

  10. ios开发事件处理之 :二:事件的产生与传递

    1.事件是怎么样产生与传递的? 当发生一个触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中.(队列是先进先出,而栈是先进后出) UIApplication会从事件队列中 ...