本章节开始引入批量输入,使用cin的输入状态的判断来进行批量输入, cin 强大的输入识别方式简直让我 amazing!不同类型数据的输入,如果使用c 语言,会有好多输入语句并且有不少的判断条件,真为以前自己不愿意接触除了C语言之外的语言的想法感觉羞愧。不过,accelerated c++ 真的是一本比较适合我的书,以前使用c++ primer的时候,简直毁了我对c++的兴趣,我觉得c++ primer可以当一本工具书去查找,如果你有一定的C基础想转去学 c++ 的话,那就强烈推荐accelerated c++!可以让你开门见山的学习C++,费话不多说了,下面是本章节的一些程序:
本章例题全码:
#include <iostream>
#include <ios>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
//请求输入并读入学生姓名
cout << "please enter your firsts name: ";
string name;
cin >> name;
cout << "hello, " << name << "!" << endl;
//请求输入并读入期中和期末成绩
cout << "please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
//请输入家庭作业成绩
cout << "enter all your homework grades";
vector<double> homework;
double x;
//不变式:homework包含所有的家庭作业成绩
while(cin >> x)
homework.push_back(x);
//检查homework是否为空
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
if (size == 0){
cout << "you must enter your grades."
"please try again." << endl;
return 1;
}
//对成绩进行排序
sort(homework.begin(), homework.end());
//计算家庭作业成绩的中值
vec_sz mid = size / 2;
double median;
median = size % 2 == 0?(homework[mid] + homework[mid - 1]) / 2:homework[mid];
//计算并输出总成绩
streamsize prec = cout.precision();
cout << "your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * median
<< setprecision(prec) << endl;
return 0;
}
运行结果:
此例使用setprecision函数来设置输出的格式,并使用了vector向量,此向量可以存储很多种数据类型,并且sort 函数可以对vector的数据进行排序。
另外, cin使用空白(空格、制表符和换行符)来确定字符串的结束位置,cin输入一系列的字符串、数字、它能够分辩出来。
在使用EOF来作为文件的结束符时,需要注意,若作为字符串文件结束符,则在windows 环境下,应输入Ctrl+z; 当作为数字的结束符时,就输入-1结束;
部分课后习题代码:
3.2、先把vector里的数据进行排序,然后再倒序每四个为一行输出
思路:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
cout << "enter a set of integer: ";
int x;
vector<int> grade;
while(cin >> x)
grade.push_back(x);
//序列排序
sort(grade.begin(), grade.end());
//计算向量的数据
typedef vector<int>::size_type vec_sz;
vec_sz size = grade.size();
//倒序打印,每4个数据进行一次换行
for(vec_sz i = size; i > 0; i --)
{
cout << grade[i -1] << " ";
if (!((i - 1)% 4))
{
cout << endl;
}
}
return 0;
}
运行结果:
3.3
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string word;
//存储单词的向量
vector<string> words;
//存储单词出现次数的向量
vector<int> cnt_words;
cout << "enter s sentence,and this program will help you"<< endl <<"count the presence of words:"
"(please end up with Ctrl + z)"<< endl;
while(cin >> word){
bool flag_same = false;
for(int i = 0; i < words.size(); i ++)
{
//当单词出现时
if (word == words[i])
{
flag_same = true;
//相应的单词记录次数增加1次
cnt_words[i] ++;
break;
}
}
//没有找到相同的单词
if (flag_same == false)
{
//把新单词添加到存储单词向量后面
words.push_back(word);
//把新单词出现的次数添加到该向量后面
cnt_words.push_back(1);
}
}
//打印单词及其出现的次数
for (int i = 0; i < words.size(); i ++)
{
cout << words[i] << "\t" << cnt_words[i] << endl;
}
return 0;
}
程序运行结果:
3.4
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string word;
//分别记录最长和最短的单词长度
int max, min;
//定义两个向量分别来存储最长的和最短的单词
vector<string> max_words;
vector<string> min_words;
//读入一系列字符串
cout << "enter a sentence(end up with Ctrl + Z):";
while(cin >> word){
int temp_length = word.length();
//此单词为第一个单词,则存入向量中
if (!max_words.size())
{
max_words.push_back(word);
min_words.push_back(word);
max = temp_length;
min = temp_length;
}
else{
//此单词比最长的单词长
if (temp_length > max)
{
max_words.pop_back();
max_words.push_back(word);
max = temp_length;
}else if (temp_length < min) //此单词比最短的单词短
{
min_words.pop_back();
min_words.push_back(word);
min = temp_length;
}
}
}
cout << "word " << max_words[0] << " is the longest words,and has " << max << " charactors" << endl ;
cout << "word " << min_words[0] << " is the shortest words,and has " << min << " charactors" << endl ;
}