标签:
(测试环境为windows8下vs2013)
3-0编译运行测试本章程序
#include <iomanip> #include <ios> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { cout << "please enter your 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"; vector<double> homework; double x; while (cin >> x){ homework.push_back(x); } typedef vector<double>::size_type vec_sz; vec_sz size = homework.size(); if (size == 0){ cout << endl << "no grade,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 << "you final grade is " << setprecision(3) << 0.2*midterm + 0.4*final + 0.4*median <<setprecision(prec)<< endl; return 0; }
文件结束:Ctrl+Enter+z
prec:重新恢复cout的精度
3-2写一个程序计算并输出一个整数集的四位分数(也就是,最大值的1/4,然后是下一个最大值的1/4,如此而已)
(四分位数:即统计学中,把所有数值由小到大排列并分成四等份,处于三个分割点位置的数值就是四分位数。)
相关算法如下:
#include <iomanip> #include <ios> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { cout << "please input the set of integer :" << endl; vector<double> sets; double x; while (cin >> x){ sets.push_back(x); } typedef vector<double>::size_type vec_sz; vec_sz size = sets.size(); vec_sz mid = size / 2; if (size == 0){ cout << "no number! " << endl; return 1; } sort(sets.begin(), sets.end()); double q1, q2, q3; if (size % 2 == 0){ q2 = (sets[mid] + sets[mid - 1]) / 2; }else{ q2 = sets[mid]; }
if (mid % 2 == 0){ q1 = (sets[mid / 2] + sets[mid / 2 - 1]) / 2; q3 = (sets[size - 1 - mid / 2] + sets[size - mid / 2]) / 2; }else{ q1 = sets[mid / 2]; q3 = sets[size - 1 - mid / 2]; } streamsize prec = cout.precision(); cout << "the first block is: " <<setprecision(3)<< q1 << endl; cout << "the second block is: " << q2 << endl; cout << "the third block is: " << q3 <<setprecision(prec)<< endl; return 0; }
测试结果:
3-3写一个程序,计算输入中每个不同的单词出现了多少次
算法思路:
1.用一个vector<string>存储输入文本中所有的单词
2.用另一个vector<string>存储不同的单词,即重复的就没有了
3.用一个int变量输出单词出现的次数(要存储的话用vector即可)
#include <iomanip> #include <ios> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { cout << "please input the text: " << endl; vector<string> text; string word; while (cin >> word){ text.push_back(word); } if (text.size() == 0){ cout << "no text! please try again: " << endl; return 1; } // put the different word to vector for temp vector<string> temp; int i, j; for (i = 0; i != text.size(); i++){ for (j = 0;; j++){ if (j == temp.size()){ temp.push_back(text[i]); break; }else{ if (temp[j] == text[i]) break; } } } //output the count of word int count; for (i = 0; i != temp.size(); i++){ count = 0; for (j = 0; j != text.size(); j++){ if (temp[i] == text[j]){ count++; } } cout << "the word: " << temp[i] << " present " << count << " times" << endl; } return 0; }
运行结果:
3-4写一个程序,找出输入中最长和最短的字符串
#include <iomanip> #include <ios> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { cout << "please input the text: " << endl; vector<string> text; string word; while (cin >> word){ text.push_back(word); } if (text.size() == 0){ cout << "no text! please try again: " << endl; return 1; } //the strmax use for store the longest string //the max use for store the length of the longest string //the strmin and the min also string strmax, strmin; string::size_type max, min; strmax = strmin = text[0]; max = min = text[0].size(); for (int i = 1; i != text.size(); i++){ if (text[i].size() > max){ max = text[i].size(); strmax = text[i]; }else if (text[i].size() < min){ min = text[i].size(); strmin = text[i]; }else continue; } cout << "the longest word is: " << strmax <<endl << " and the length is: " << max << endl; cout << "the shortest word is " << strmin <<endl <<" and the length is: " << min << endl; return 0; }
测试结果:
3-5写一个程序,可以一次计算多个学生的成绩,这个程序需要同时使用两个vector:
第一个vector要保存学生的姓名,第二个vector要保存根据输入计算出的最终成绩。
假定家庭作业成绩的数目是确定的(本文假定为3,计算的为平均成绩)。
#include <iomanip> #include <ios> #include <iostream> #include <string> #include <algorithm> #include <vector> using namespace std; int main() { vector<string> name; vector<double> grade; string sname; double x; while (cin >> sname){ name.push_back(sname); double sum = 0; int count = 0; while (cin >> x){ sum += x; count++; if (count == 3) break; } grade.push_back(sum / count); } if (name.size() == 0){ cout << "no student ,try again!" << endl; return 1; } streamsize prec = cout.precision(); for (int i = 0; i != name.size(); i++){ cout << "the student: " << name[i] << setprecision(3) << " ‘s grade is :" << grade[i] << setprecision(prec) << endl; } return 0; }
运行结果:
3-6在3.1节程序中,如果学生没有输入任何家庭作业成绩,那么计算平均值时
可能会除以0,在c++中,除以0是不确定的,也就是说系统会进行随意的操作
在你的c++系统中会怎么样呢?重写这个程序,使得程序的行为不依赖于系统
如何对待除以0的情况。
出现情况:
在程序中加入检查代码即可,如本文3-0程序
if (size == 0){ cout << endl << "no grade,please try again!" << endl; return 1; }
再次出现时:
标签:
原文地址:http://www.cnblogs.com/monster-prince/p/5654849.html