Accelerated C++ 习题解答 第3章

发布时间:2016-12-6 20:13:23 编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"Accelerated C++ 习题解答 第3章",主要涉及到Accelerated C++ 习题解答 第3章方面的内容,对于Accelerated C++ 习题解答 第3章感兴趣的同学可以参考一下。

部分习题参考了网上已有的解答 Ex.3-0 #include<iostream> #include<string> #include<algorithm> #include<ios> #include<iomanip> #include<vector> #include<conio.h> using std::cin; using std::cout; using std::endl; using std::string; using std::vector; using std::sort; using std::streamsize; using std::setprecision; int main() { cout <<"please enter your first 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, followed by end offile: "; vector<double> homework; double x; while (cin >> x) homework.push_back(x); typedef vector<double>::size_typevec_sz; vec_sz size =homework.size(); if (size == 0) { cout << endl<<"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; getch(); return 0; } Ex.3-1 中值是一个可将数值集合划分为相等的上下两部分的数值。对于有限的数集,通过把所有的值高低排序后找出正中间的一个作为中位数。中间点取决于所有的数值。 由此可知,如果任何一个我们已经读到的值被丢弃,则会改变原先中间点的位置,这样就得不出准确的中值。   ,Ex.3-2 #include<algorithm> #include<iostream> #include<vector> #include<conio.h> using std::cin; using std::cout; using std::endl; using std::vector; using std::sort; int main() { vector<int> 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<int>::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; //注意这里确定第3部分范围的时候的技巧 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; getch(); return 0; } Ex.3-3 #include<iostream> #include<string> #include<vector> #include<conio.h> using std::cin; using std::cout; using std::endl; using std::vector; using std::string; int main() { vector<string>words; vector<int> counts; typedef vector<string>::size_type vec_sz; cout <<"enter the words: "; string str; while (cin >> str) { bool found =false; for (vec_sz i = 0; i < words.size(); ++i) { if (str == words[i]) { ++counts[i]; found =true; } } if (!found) { words.push_back(str); counts.push_back(1); } } for (vec_sz i = 0; i < words.size(); ++i) { cout <<words[i] <<" appears "<< counts[i] <<" times"<< endl; } getch(); return 0; } Ex.3-4 #include<iostream> #include<string> #include<conio.h> using std::cin; using std::cout; using std::endl; using std::string; int main() { typedef string::size_type str_sz; string longest; str_sz longest_size =0; string shortest; str_sz shortest_size= 0; cout <<"Enter words: "; string s; while (cin >> s) { if (longest_size == 0 || s.size() > longest_size){ longest = s; longest_size =s.size(); } if (shortest_size == 0 || s.size() < shortest_size){ shortest = s; shortest_size =s.size(); } } cout <<"Longest word is " << longest<<" (" <<longest_size <<")" <<endl; cout <<"Shortest word is " << shortest<<" (" <<shortest_size <<")"<< endl; getch(); return 0; } Ex.3-5 #include<iostream> #include<string> #include<vector> #include<iomanip> #include<ios> #include<conio.h> using std::cin; using std::cout; using std::endl; using std::setprecision; using std::string; using std::streamsize; using std::vector; #define NUM_HOMEWORK 2 int main() { vector<string>names; vector<double> final_grades; bool done =false; while (!done) { cout <<"Please enter your first name: "; string name; cin >> name; cout <<"Hello, " << name <<"!" << endl; names.push_back(name); cout <<"Please enter your midterm and final exam grades:"; double midterm, final; cin >>midterm >> final; cout <<"Enter both your homework grades, " "followed byend-of-file: "; int count = 0; double sum = 0; double x; 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<string>::size_type i = 0; i <names.size(); ++i) { streamsize prec =cout.precision(); cout <<names[i] <<"'s final grade is "<< setprecision(3) << final_grades[i] << setprecision(prec) << endl; } getch(); return 0; }   Ex.3-6 #include<iomanip> #include<ios> #include<iostream> #include<string> #include<conio.h> using std::cin; using std::cout; using std::endl; using std::setprecision; using std::string; using std::streamsize; int main() { cout <<"Please enter your first 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, " "followed by end-of-file: "; int count = 0; double sum = 0; double x; while (cin >> x) { ++count; sum += x; } double homework_grade = (count > 0) ? sum / count: 0.0; streamsize prec =cout.precision(); cout <<"Your final grade is " <<setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 *homework_grade << setprecision(prec) << endl; getch(); return 0; }

上一篇:Android Sliding Menu using Navigation Drawer
下一篇:线程生命周期图示

相关文章

相关评论