Accelerated C++学习笔记5―(组织程序和数据)(四)

2014-11-24 13:11:03 · 作者: · 浏览: 18
maxlen = max(maxlen, record.name.size()); students.push_back(record); } // 按字母顺序排列学生记录 sort(students.begin(), students.end(), compare); //输出姓名和成绩 for (std::vector ::size_type i = 0; i != students.size(); ++i) { //输出姓名,把姓名填充至maxlen + 1 个字符长度 cout << setw(maxlen+1) << students[i].name; //计算并输出成绩 try { double final_grade = grade(students[i]); streamsize prec = cout.precision(); cout << setprecision(3) << final_grade << setprecision(prec); } catch(domain_error e) { cout << e.what(); } cout << endl; } return 0; }
6、编写一个程序来计算从1-100的整数(int)值的平方。程序的输出分为两列;第一列是整数值。第二列是整数值的平方
// lesson4_3.cpp : 定义控制台应用程序的入口点。
//功能:编写一个程序来计算从1-100的整数(int)值的平方。程序的输出分为两列;第一列是整数值。第二列是整数值的平方
//时间:2014.5.12

#include "stdafx.h"
#include 
        
          #include 
         
           //定义控制器setprecision 这个控制器可以让我们指明输出所包含的有效位数 #include 
          
            using namespace std; int _tmain(int argc, _TCHAR* argv[]) { for(int i = 1; i <= 100; ++i) { cout << setw(3) << i << setw(6) << i * i << endl; } return 0; } 
          
         
        
运行结果: \
程序说明:setw(n) 返回一个类型为streamsize的值,如果把这个函数用于输出流s,那么它的作用跟调用s.width(n)的一样。
7、改进程序让它更好的适应性,当i增长的时不需要修正setw的参数
// lesson4_4.cpp : 定义控制台应用程序的入口点。
//功能:让它更好的适应性,当i增长的时不需要修正setw的参数
//时间:2014.5.12

#include "stdafx.h"
#include 
        
          #include 
         
           #include 
          
            using namespace std; int get_width(double n) { return log10(n) + 1; } int _tmain(int argc, _TCHAR* argv[]) { double max = 100.0; for(int i = 1; i <= max; ++i) { cout << setw(get_width(max)) << i << setw(get_width(max * max) + 1) << i * i << endl; } return 0; } 
          
         
        

注意:VS2008中定义的那个get_width只能用float,否则报错。
8、编写一个函数来从输入流读单词,把读到的单词存储在一个向量中,利用这个函数编写一个程序来计算输入的单词的数目以及每一个单词所出现的次数。
// lesson4_5.cpp : 定义控制台应用程序的入口点。
//功能:编写一个函数来从输入流读单词,把读到的单词存储在一个向量中,利用这个函数编写一个程序来计算输入的单词的数目以及每一个单词所出现的次数。
//时间:2014.5.13

#include "stdafx.h"
#include 
        
          #include 
         
           #include 
          
            #include 
           
             using namespace std; //从输入流中将单词读入到一个vector
            
              istream& read_words(istream& in, vector
             
              & words) { if(in) { //清除原先的内容 words.clear(); //读单词 string word; while (in >> word) words.push_back(word); //清除流以使输入动作对下一个单词有效 in.clear(); } return in; } int _tmain(int argc, _TCHAR* argv[]) { typedef vector
              
               ::size_type vec_sz; vector
               
                 words; read_words(cin, words); cout << "Num of words: " << words.size() << endl; sort(words.begin(), words.end()); string prev_word = ""; int count = 0; for(vec_sz i = 0;i < words.size(); ++i) { if (words[i] != prev_word) { if (prev_word != "") { cout << prev_word << " appeared " << count << "times" << endl; } prev_word = words[i]; count = 1; } else ++count; } cout << prev_word << " appeared " << count << "times" << endl; return 0; } 
               
              
             
            
           
          
         
        

运行结果: \

注意:这里我在实现的时候,报错error c3872: “0x3000”: 此字符不允许在标识符中使用,解决方法:
这个语句的后面空白部分,都删除掉,免得有不可见的全角空格。可能是不小心按到空格键了。
9、编写一个程序来计算存储在一个vector 类型的向量中的数据的平均值
// lesson4_7.cpp : 定义控制台应用程序的入口点。
//功能:编写一个程序来计算存储在一个vector
         
          类型的向量中的数据的平均值 #include "stdafx.h" #include 
          
            #include 
           
             #include 
            
              using namespace std; int _tmain(int argc, _TCHAR* argv[]) { vector
             
               nums; double num; while (cin >> num) nums.push_back(num); cout << accumulate(nums.begin(), nums.end(), 0.0) / nums.size() << endl; //如果不要求存储在一个vector
              
               类型的向量 /*int count = 0; double sum = 0; double x; //把数据读入变量x中 while (cin >> x) { ++count; sum += x; } cout << "The avg: " << sum / count << endl; */ return 0; } 
              
             
            
           
          
         

程序说明:本程序使用了accumulate函数
10、小结 1)#include< 系统头文件>;#include"用户定义的头文件名称"(这里需要注意不应该包含using声明,但是给标准库的名称应该明确加上一个前缀using) 2)通过学习本章懂得了如何使用函数和数据结构来组织运算,还是需要一个方法来把程序划分为我们可以独立编译且在编译后能组合在一起的文件。 ――To_捭阖_youth