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. regexp模式匹配+location页面跳转+cookie/localstorage本地存储

    学习js的过程中,根据知识点编写一些code进行测试,以便检验. 这段程序使用了以下知识点: 1.regexp,对数据进行模式匹配 2.使用location对象进行页面跳转. 3.cookie/loc ...

  2. 前端开发概述+JS基础细节知识点

    一 前端开发概述 html页面:html css javascript 拿到UI设计图纸:切图-->html+css静态布局-->用JS写一写动态效果-->ajax和后台进行交互,把 ...

  3. Caused by: java.lang.NoSuchMethodException: &lt;init&gt; [class android.content.Context, interface android

     在写自己定义的view时,有时会报下面错误: Caused by: java.lang.NoSuchMethodException: <init> [class android.co ...

  4. bootstrap课程4 bootstrap的css样式有哪些内容需要注意

    bootstrap课程4 bootstrap的css样式有哪些内容需要注意 一.总结 一句话总结: 1.如何选择产品(框架)的版本? 大版本下的最后一个版本,但是同时又要选择稳定的版本,也就是如果做产 ...

  5. vue指令概览

    原文 简书原文:https://www.jianshu.com/p/5fd47b7422fd 大纲 1.什么是vue指令 2.向指令中传入参数 3.指令中带入修饰符 4.指令的缩写 5.常见的vue指 ...

  6. js+html实现遮罩层效果(收藏哦)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script ty ...

  7. jQuery和CSS3炫酷GOOGLE样式的用户登录界面

    这是一款使用jQuery和CSS3打造的GOOGLE样式的用户登录界面特效.该登录界面特效中,右上角的小问号和错误提示小图标使用SVG来制作.username和password输入框採用浮动标签特效. ...

  8. POJ 3132 &amp; ZOJ 2822 Sum of Different Primes(dp)

    题目链接: POJ:id=3132">http://poj.org/problem?id=3132 ZOJ:http://acm.zju.edu.cn/onlinejudge/show ...

  9. ImageButton按压效果失效

    LinearLayout中ImageButton的按压效果不起作用,如图 布局如下: <LinearLayout android:id="@id/ll_add_reply_face&q ...

  10. 【44.10%】【codeforces 723B】Text Document Analysis

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...