#include<iostream>
#include<string>
#include<iomanip> //define setprecision
#include<ios> //define streamsize
#include<vector>
#include<algorithm>
using std::sort;
using std::vector;
using std::setprecision;
using std::streamsize;
using std::cout;
using std::cin;
using std::endl;
using std::string;
int main()
{
cout << "please enter your first name" << endl;
string name;
cin >> name;
cout << "Please enter your midterm and final exam grades :";
double midterm , final;
cin >> midterm >> final ;
cout <<" enter your homework grades :"
"follow by end-of-file;";
vector<double> homework;
double x;
while(cin>>x)
{
homework.push_back(x);
}
if( homework.size() == 0)
{
cout << endl << "You must enter your grades. "
"Please try again. " << endl;
return 1;
}
sort(homework.begin(), homework.end());
vector<double>::size_type mid = homework.size()/2;
double median;
median = homework.size() %2 == 0 ? ((homework[mid]+ homework[mid-1])/2) : homework[mid];
streamsize pre = cout.precision();
cout << " Your final grad is " << setprecision(3)
<< 0.2 * midterm +0.4 * final + 0.4 * median<<setprecision(pre)
<<endl;
return 0;
}
3-1:
3-2:
int main()
{
cout << "Please enter numbers";
vector<int> vec;
int x;
while( cin >> x )
{
vec.push_back(x);
}
sort(vec.begin(),vec.end());
for( vector<int>::size_type i = 0; i != vec.size() ; i++)
{
cout << vec[i] / 4.0 << endl;
}
return 0;
}
3-3:
int main()
{
cout << "Please enter words: ";
vector<string> str;
vector<int> vec;
string s;
while( cin >> s)
{
int flag = 0;
for(vector<string>::size_type i = 0; i !=str.size(); i ++)
{
if( str[i] == s)
{
vec[i]++;
flag = 1;
}
}
if(flag == 0)
{
str.push_back(s);
vec.push_back(1);
}
}
for( vector<string>::size_type i = 0; i != str.size(); i ++)
{
cout << "*"<< str[i] <<" " << vec[i] << endl;
}
return 0;
}
3-4:
int main()
{
cout <<"Please enter words: ";
vector<string> str;
string s;
string::size_type max = 0, min = 0;
cin >> s;
max = min = s.size();
while( cin >> s)
{
if(s.size() > max) max = s.size();
if(s.size() < min ) min = s.size();
}
cout << max <<", " <<min << endl;
return 0;
}
3-5:
int main()
{
cout << "Please enter ... : ";
vector<string> name;
vector<int> grade;
int x, y , z;
string s;
while( cin >> s >> x >> y >> z)
{
name.push_back(s);
grade.push_back( (x+y+z)/3 );
}
for( vector<string>::size_type i = 0 ; i != name.size() ; i++)
{
cout << name[i] <<", " << grade[i] << endl;
}
return 0;
}
3-6:
输出前先判断 count是否为0;