- 博客(41)
- 收藏
- 关注
原创 <C++ Primer_5th>习题_3.36
//编写一段程序,比较两个数组是否相等#include#include#includeusing namespace std;int main(){ const int sz = 5; int a[sz], b[sz], i; //生成随机数种子 srand((unsigned)time(NULL)); //为数组赋值 for (i = 0; i < sz; ++i)
2017-10-03 21:19:08
303
原创 <C++ Primer_5th>习题_3.35
//编写一段程序,利用指针将数组中的元素置为0#includeusing namespace std;int main(){ const int sz = 10; int a[sz], i = 0; //通过for循环为数组赋值 for (i = 0; i < sz; ++i) a[i] = i; cout << "初始状态下数组的内容是: " << endl; /
2017-10-03 21:18:28
293
原创 <C++ Primer_5th>习题_3.31
//编写一个程序,定义一个含有10个int的数组,令每个元素的值就是其所在位置的值#includeusing namespace std;int main(){ const int sz = 10; int a[sz]; //遍历数组元素并赋值 for (int i = 0; i < sz; ++i) { a[i] = i; } //输出数组中的全部元素 cout
2017-10-03 21:17:50
393
原创 <C++ Primer_5th>习题_3.25
//使用迭代器划分分数范围#include#includeusing namespace std;int main(){ //该vector对象记录各分数段的人数,初始值为0 vector v_grade(11); int i_val; auto it = v_grade.begin(); cout << "请输入一组成绩(0~100): " << endl; whil
2017-10-03 21:16:55
339
原创 <C++ Primer_5th>习题_3.24
//读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来。//使用迭代器实现#include#includeusing namespace std;int main(){ vector v_int; int i_val; cout << "请输入一组数字: " << endl; while (cin >> i_val) v_int.push_ba
2017-10-03 21:16:17
295
原创 <C++ Primer_5th>习题_3.23
//编写一段程序,创建一个含有10个整数的vcetor对象,然后使用迭代器将所有元素值都变成原来的2倍。输出vector对象的内容,检验程序是否正确#include#include#include#includeusing namespace std;int main(){ vector v_int; //生成随机数种子 srand((unsigned)time(NULL
2017-10-03 21:15:40
322
原创 <C++ Primer_5th>习题_3.22
//修改之前的那个输出 text第一段程序,首先把text的第一段全部改写成大写形式,然后再输出它#include#include#includeusing namespace std;int main(){ vector text; string s; cout << "请输入一段字符串" << endl; //利用getline读取一句话,直接回车产生一个空串,表示段
2017-10-03 21:14:59
406
原创 <C++ Primer_5th>习题_3.20
//读入一组整数并把它们存入一个vector对象,将每对相邻整数的和输出出来。#include#includeusing namespace std;int main(){ vector v_int; int i_val; cout << "请输入一组数字: " << endl; while (cin >> i_val) v_int.push_back(i_val);
2017-10-03 21:14:01
230
原创 <C++ Primer_5th>习题_3.17
//从cin读入一组词并把它们存入一个vcetor对象,然后设法把所有词都改为大写形式。输出改变后的的结果,每个词占一行。#include#include#includeusing namespace std;int main(){ vector v_string; string s; char cont = 'y'; //提示用户输入 cout << "请输入第一个词
2017-10-03 21:13:21
278
原创 <C++ Primer_5th>习题_3.16
#include#include#include using namespace std;int main( ){ vector v1; vector v2(10); vector v3(10, 42); vector v4{ 10 }; vector v5{ 10,42 }; vector v6{ 10 }; vector v7{ 10,"hi" }; cout
2017-10-03 21:12:08
222
原创 <C++ Primer_5th>习题_3.14
//编写一段程序,用cin读入一组整数并把他们存入一个vector对象#include#includeusing namespace std;int main(){ vector v_int; int i; //记录用户的输入值 char cont = 'y'; cout << "请输入第一个整数值: " << endl; while (cin
2017-09-30 21:19:42
389
原创 <C++ Primer_5th>习题_3.10
//编写一段程序,读入一个包含标点符号的字符串,将标点符号去除后输出字符串剩余的部分//使用范围for实现#include#include#includeusing namespace std;int main( ){ string s; //提示用户输入 cout << "请输入一个字符串,最好含有某些标点符号: " << endl; getline(cin, s)
2017-09-30 21:18:46
272
原创 <C++ Primer_5th>习题_3.6
//编写一段程序,使用范围for语句将字符串内的所用字符用X代替#include#includeusing namespace std;int main( ){ string s; //提示用户输入 cout << "请输入一个字符串,可以包含空格:" << endl; getline(cin, s); //读取整行,遇回车符结束 for (auto &c
2017-09-30 21:17:41
247
原创 <C++ Primer_5th>习题_3.5
//编写一段程序从标准输入中读入多个字符串并将它们连接在一起,输出连接成的大字符串#include#includeusing namespace std;int main(){ char cont='y' ; string s, result ; //提示用户输入第一个字符串 cout << " 请输入第一个字符串: " << endl; //判断第一个字符串是否正确
2017-09-30 21:17:08
275
原创 <C++ Primer_5th>习题_3.4
//编写一段程序读入两个字符串,比较其是否相等并输出结果。如果不等,输出较大的那个字符串。#include#includeusing namespace std;int main( ){ string s1, s2; //提示用户输入字符串 cout << "请输入两个字符串 : " << endl; cin >> s1 >> s2; //比较两个字符串 if (s1
2017-09-30 21:14:19
316
原创 <C++ Primer_5th>习题_3.3
//string类的输入运算符和getline函数分别是如何处理空白字符的。(string和getline函数对含有空白字符串的区别)#include#includeusing namespace std;int main(){ string word, line; //提示用户输入字符串 cout << "请选择读取字符串的方式: 1表示逐词读取, 2表示整行读取 " <<
2017-09-30 21:13:15
292
原创 <C++ Primer_5th>习题_3.2
//编写一段程序从标准输入中一次读取一整行,然后修改该程序使其一次读入一个词。#include#includeusing namespace std;int main( ){ string line; //提示用户输入字符串,直至文件结束或遇到异常输入 cout << " 请输入你的字符串,可以包括空格: " << endl; //使用getline()函数来读取一行字符串
2017-09-30 21:11:46
305
原创 <C++ Primer_5th>第一章用到的头文件<Sales_item.h>
//头文件:Sales_item.h/* * This file contains code from "C++ Primer, Fifth Edition", by Stanley B. * Lippman, Josee Lajoie, and Barbara E. Moo, and is covered under the * copyright and warranty notic
2017-09-30 09:37:28
366
原创 <C++ Primer_5th>习题_1.23
//编写程序,读取多条销售记录,并统计每个ISBN(每本书)有几条销售记录。#include#include "Sales_item.h"int main(){ Sales_item trans1, trans2; int num = 1; std::cout << "请输入若干销售记录: " << std::endl; //判断输入是否正确 if (std::cin >>
2017-09-30 09:30:58
422
原创 <C++ Primer_5th>习题_1.22
// 编写程序,读取多个具有相同的ISBN的销售记录,输出所有记录的和。#include#include"Sales_item.h"int main(){ Sales_item total, trans; std::cout << "请输入几条ISBN相同的销售记录: " << std::endl; //判断是否为相同的ISBN if (std::cin >> total)
2017-09-30 09:29:53
271
原创 <C++ Primer_5th>习题_1.21
//编写程序,读取两个ISBN相同的Sales_item对象,输出它们的和。#include#include"Sales_item.h"int main(){ Sales_item trans1, trans2; std::cout << "请输入两条ISBN相同的销售记录" << std::endl; std::cin >> trans1 >> trans2; //判断两条
2017-09-29 13:01:00
263
原创 <C++ Primer_5th>习题_1.20
//读取一组书籍销售记录,将每条记录打印到标准输出上。#include#include"Sales_item.h"int main(){ Sales_item book; std::cout << "请输入销售记录: " << std::endl; while (std::cin >> book) { std::cout << "ISBN、售出本数、销售额和平均售价为
2017-09-29 13:00:03
399
原创 <C++ Primer_5th>习题_1.16
//编写程序,从cin读取一组数,输出其和。#includeint main(){int sum = 0, value = 0;//提示用户输入一些数,按ctrl+z表示结束std::cout > value) //从输入流读取值,并返回左值{sum += value;}std::cout << sum << std::endl;system("pause");return 0;}//也可使用fo
2017-09-29 12:55:41
240
原创 <C++ Primer_5th>习题_1.11
//编写程序,提示用户输入两个数,打印出这两个整数所指定的范围内的所有整数。#includeint main(){ std::cout << "请输入两个数: "; std::cout << std::endl; int v1, v2; std::cin >> v1 >> v2; if(v1 > v2) while (v1 >= v2) //选择循环语句,依
2017-09-29 12:53:09
231
原创 <C++ Primer_5th>习题_1.10
/*除了++运算符将运算对象的值增加1以外,还有一个递减运算符(--)实现将值减1。编写递减运算符在循环中按递减打印出10到0之间的整数。*/#includeint main(){int sum = 0;int i = 10;while (i >= 0){sum += i;--i;}std::cout << "10到0之间的整数和为: " << sum << std::endl;system("p
2017-09-29 12:48:48
223
原创 <C++ Primer_5th>习题_1.9
//编写程序,使用while循环将50到100的整数相加。#includeint main(){ int sum = 0; int i = 50; while (i <= 100) { sum += i; ++i; } std::cout << "50到100之间的整数之和为: " << sum << std::endl; system("pause"); r
2017-09-29 12:45:40
312
原创 <C++ Primer_5th>习题_1.4
/*我们的程序使用加法运算+来将两个数相加。编写程序使用乘法运算*,来打印两个数的积*/#includeint main(){ std::cout << "Please enter two numbers " << std::endl; int v1, v2; std::cin >> v1 >> v2; std::cout << v1 << "和" << v2 << "的积为"
2017-09-29 12:28:27
222
原创 <C++ Primer_5th>习题_1.3
/******************************************** ** 功能:显示输出“Hello World!”字符串 ** *********************************************/ #include // 编译预处理命令 using namespace std; // 使用标准名空间std /*以下是主函数
2017-09-29 11:26:29
192
转载 好的程序员有必要看的书籍
作为以后要称为千千万万程序员中的一员,今天在优快云中看到了一个帖子,记录了计算机经典书籍,个人感觉的确有必要给自己加点料,充实一下自己,觉得自己知道的东西实在是少之又少,以后要慢慢的来读这些书,是自己能够早点成长起来。。。转载自:http://blog.youkuaiyun.com/wconvey/article/details/9027443 JAVA篇《Java 2 核心技术》、
2017-09-29 10:19:53
337
原创 Accelerated C++<4-7>
//编写一个程序计算存储在一个vector类型的向量中的数据的平均值。#include #include #include using namespace std;int main() { vector nums; double num; while (cin >> num) nums.push_back(nu
2017-04-10 23:58:12
811
原创 Accelerated C++<4-6>
//重新写Student_info结构并使用重写后的结构来直接计算成绩,要求在程序中仅存储总成绩。#include #include #include #include #include #include #ifdef _MSC_VER#include "../minmax.h"#elseusing std::max;#endif
2017-04-10 23:50:59
612
原创 Accelerated C++<4-5>
//编写一个函数以从输入流读单词,将读到的单词存储在一个向量中。利用这个函数编写一个程序以计算输入的单词的数目以及每一个单词所出现的次数。HintReview the implementation of read_hw in §4.1.3. Then, learn how to use separate compilation with your par
2017-04-10 23:43:16
468
原创 Accelerated C++<4-4>
//现在,再次修改你的求平方程序,用它来求double类型而不是int类型的值的平方。使用控制器控制输出,让数值按列排列起来。HintAllow for fractional values, and change how you determine the number of digits in the highest base and square.
2017-04-10 23:31:05
487
原创 Accelerated C++<4-3>
//如果重写上题的程序,让它计算从1到999的整数的平方。但是,我们忘记了更改setw的参数的值。会出现什么问题呢?重写这个程序,让它具有更好的适应性,再次修改你的求平方程序,用它来求double类型而不是int类型的值的平方。使用控制器控制输出,让数值按列排列起来。HintDetermine the length of the longest base
2017-04-10 23:16:21
471
原创 Accelerated C++<4-2>
//Write a program to calculate the squares of int values up to 100. The program should write two columns: The first lists the value; the second contains the square of that value. Use setw to manag
2017-03-27 21:49:30
396
原创 Accelerated C++<4-1>
//We noted in §4.2.3/65 that it is essential that the argument types in a call to max match exactly. Will the following code work? If there is a problem, how would you fix it?int maxlen;
2017-03-27 21:36:49
408
原创 Accelerated C++<3-6>
//如果学生没有参加任何考试,则平均成绩计算可能会出现除零的问题,除零在C++中并未定义,在这种情况下,我们要告知程序会出怎样的结果。HintLook in the code and see which variable could be used as the denominator in division. Test that value against z
2017-03-27 21:18:57
474
原创 Accelerated C++<3-5>
//编写一个程序用于同时跟踪n个学生的成绩。要求程序能够保持两个向量的同步:第一个应保存学生的姓名;第二个保存总成绩,而这个总成绩能够根据读到的输入来计算。应假定家庭作业成绩的个数是固定的。HintAs suggested, use two vectors. Define a maximum number of grades to request for each student.
2017-03-27 15:26:46
543
原创 Accelerated C++<3-4>
//编写一个程序用于报告它的输入中最长以及最短字符串的长度HintFor each word that is input, keep track of the shortest and longest words you’ve encountered.SolutionCreate two variables of type string::size_ty
2017-03-27 11:47:38
599
原创 Accelerated C++<3-3>
//编写一个程序用于计算在它的输入中每个不同的单词所出现的次数。#include#include#include#includeusing namespace std;int main(){ //请求读入单词coutvectorwords;string word; //将读到的单词放入容器中
2017-03-27 10:21:21
579
1
空空如也
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人
RSS订阅