accelerated c++ unit3

#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;

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值