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#学习笔记——常量、字段以及事件

    一 常量与字段 (一) 常量 常量总是被视为静态成员,而不是实例成员.定义常量将导致创建元数据.代码引用一个常量时,编译器会在定义常量的程序集的元数据中查找该符号,提取常量的值,并将值嵌入IL中.由于 ...

  2. maven 怎么在MyEclipse中打开Navigator视图

    方法一:1.点击菜单window2.选择show view菜单项3.选择other菜单项4.点击general,在其中可以找到 方法二:1.点击菜单window2.选择show view菜单项3.选择 ...

  3. 10.2、android输入系统_必备Linux编程知识_双向通信(scoketpair)

    2. 双向通信(socketpair) 输入系统肯定涉及进程通讯:进程A读取/分发输入事件,APP处理输入事件,进程A给APP发送输入事件,APP处理完事件回复信息给进程A,APP关闭的时候也要发信息 ...

  4. [Nuxt] Display Vuex Data Differently in Each Page of Nuxt and Vue.js

    You often use the same data in different ways across pages. This lesson walks you through setting up ...

  5. 使用github pages创建博客

      参考:http://wenku.baidu.com/link?url=hi0nlkIp17HnQQpCkUr3KacZOOVGMOYKYbWzjX_HKJZNZpiRxfGPLuwvUydOVxe ...

  6. swift开发网络篇 - post 请求

    /** 所有网络请求,统一使用异步请求! 在今后的开发中,如果使用简单的get/head请求,可以用NSURLConnction异步方法 GET查/POST增/PUT改/DELETE删/HEAD GE ...

  7. [原]MFC中DIALOG(对话框)程序响应加速键(快捷键)

    [原]MFC中DIALOG(对话框)程序响应加速键(快捷键) 2014-8-6阅读266 评论0 新建一个对话框程序,项目名为Test,删除默认确定,取消和静态文本框控件.添加一个按钮,Caption ...

  8. [MobX] MobX fundamentals: deriving computed values and managing side effects with reactions

    Derivations form the backbone of MobX and come in two flavors: computed values are values that can b ...

  9. HQL和SQL的区别

    1.hql与sql的区别 sql 面向数据库表查询 hql 面向对象查询 hql : from 后面跟的 类名+类对象 where 后 用 对象的属性做条件 sql: from 后面跟的是表名    ...

  10. 【32.22%】【codeforces 602B】Approximating a Constant Range

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...