设为首页 加入收藏

TOP

Accelerated C++ 学习笔记及题解----第三章
2015-07-20 17:41:38 来源: 作者: 【 】 浏览:3
Tags:Accelerated 学习 笔记 题解 ---- 第三章

本章主要内容

1.类型定义typedef

2.局部变量

3.格式化输出初涉

4.向量vector及基本方法


本章主要程序代码

1.计算平均成绩

#include 
  
   
#include 
   
     #include 
    
      using namespace std; int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; // the number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (cin >> x) { ++count; sum += x; } // write the result streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count << setprecision(prec) << endl; return 0; } 
    
   
  
结果:

\

2.计算成绩中值<??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PC9wPgo8cHJlIGNsYXNzPQ=="brush:java;">#include #include #include #include #include using namespace std; int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for and read the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; vector homework; double x; // invariant: `homework' contains all the homework grades read so far while (cin >> x) homework.push_back(x); // check that the student entered some homework grades #ifdef _MSC_VER typedef std::vector ::size_type vec_sz; #else typedef vector ::size_type vec_sz; #endif vec_sz size = homework.size(); if (size == 0) { cout << endl << "You must enter your grades. " "Please try again." << endl; return 1; } // sort the grades sort(homework.begin(), homework.end()); // compute the median homework grade vec_sz mid = size/2; double median; median = size % 2 == 0 ? (homework[mid] + homework[mid-1]) / 2 : homework[mid]; // compute and write the final grade 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; } 结果:

\

习题答案

3.2

#include 
  
   
#include 
   
     #include 
    
      using namespace std; int main() { vector
     
       integers; cout << "Integers: "; int x; while (cin >> x) integers.push_back(x); if (integers.size() == 0) { cout << endl << "No integers!" << endl; return 1; } sort(integers.rbegin(), integers.rend()); typedef vector
      
       ::size_type vec_sz; cout << "1st quartile" << endl; for (vec_sz i = 0; i < integers.size() / 4; ++i) cout << integers[i] << endl; cout << "2nd quartile" << endl; for (vec_sz i = integers.size() / 4; i < integers.size() / 2; ++i) cout << integers[i] << endl; cout << "3rd quartile" << endl; for (vec_sz i = integers.size() / 2; i < integers.size() * 3 / 4; ++i) cout << integers[i] << endl; cout << "4th quartile" << endl; for (vec_sz i = integers.size() * 3 / 4; i < integers.size(); ++i) cout << integers[i] << endl; return 0; } 
      
     
    
   
  
结果:

\

3.3

#include 
  
   
#include 
   
     #include 
    
      using namespace std; int main() { typedef vector
     
      ::size_type vec_sz; vector
      
        words; vector
       
         counts; cout << "Words: "; string s; while (cin >> s) { bool found = false; for (vec_sz i = 0; i < words.size(); ++i) { if (s == words[i]) { ++counts[i]; found = true; } } if (!found) { words.push_back(s); counts.push_back(1); } } for (vec_sz i = 0; i < words.size(); ++i) cout << words[i] << " appeared " << counts[i] << " times" << endl; return 0; } 
       
      
     
    
   
  
结果:

\

3.4

#include 
  
   
#include 
   
     using namespace std; int main() { typedef string::size_type str_sz; string longest; str_sz longest_length = 0; string shortest; str_sz shortest_length = 0; cout << "Words: "; string s; while (cin >> s) { if (longest_length == 0 || s.size() > longest_length) { longest = s; longest_length = s.size(); } if (shortest_length == 0 || s.size() < shortest_length) { shortest = s; shortest_length = s.size(); } } cout << "Longest: " << longest << endl; cout << "Shortest: " << shortest << endl; return 0; } 
   
  

\

3.5

#include 
  
   
#include 
   
     #include 
    
      #include 
     
       using namespace std; #define NUM_HOMEWORK 2 using std::vector; int main() { vector
      
        names; vector
       
         final_grades; bool done = false; while (!done) { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; names.push_back(name); // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter both your homework grades, " "followed by end-of-file: "; // he number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (count < NUM_HOMEWORK) { ++count; cin >> x; sum += x; } double final_grade = 0.2 * midterm + 0.4 * final + 0.4 * sum / count; final_grades.push_back(final_grade); cout << "More? (Y/N) "; string s; cin >> s; if (s != "Y") done = true; } for (vector
        
         ::size_type i = 0; i < names.size(); ++i) { // write the result streamsize prec = cout.precision(); cout << names[i] << "'s final grade is " << setprecision(3) << final_grades[i] << setprecision(prec) << endl; } return 0; } 
        
       
      
     
    
   
  

3.6

#include 
  
   
#ifndef __GNUC__
#include 
   
     #endif #include 
    
      #include 
     
       using namespace std; int main() { // ask for and read the student's name cout << "Please enter your first name: "; string name; cin >> name; cout << "Hello, " << name << "!" << endl; // ask for and read the midterm and final grades cout << "Please enter your midterm and final exam grades: "; double midterm, final; cin >> midterm >> final; // ask for the homework grades cout << "Enter all your homework grades, " "followed by end-of-file: "; // the number and sum of grades read so far int count = 0; double sum = 0; // a variable into which to read double x; // invariant: // we have read `count' grades so far, and // `sum' is the sum of the first `count' grades while (cin >> x) { ++count; sum += x; } double homework_grade = (count > 0) ? sum / count : 0.0; // write the result streamsize prec = cout.precision(); cout << "Your final grade is " << setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * homework_grade << setprecision(prec) << endl; return 0; } 
     
    
   
  


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据结构与算法问题 [NOIP2001]求.. 下一篇HDOJ 5011 Game

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·MySQL 安装及连接-腾 (2025-12-25 06:20:28)
·MySQL的下载、安装、 (2025-12-25 06:20:26)
·MySQL 中文网:探索 (2025-12-25 06:20:23)
·Shell脚本:Linux Sh (2025-12-25 05:50:11)
·VMware虚拟机安装Lin (2025-12-25 05:50:08)