第3章习题 使用批量数据 Accelerate C++ 学习笔记8

//0
#include<iostream>
#include<algorithm>
#include<string>
#include<ios>//streamsize
#include<iomanip>//setprecision
#include<vector>

using std::cout;       using std::cin;
using std::endl;       using std::sort;
using std::streamsize; using std::vector;
using std::setprecision;  using std::string;

int main(int argc, char const *argv[])
{
  cout << "Please enetr 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 << "Please enter your homework grades,"//家庭作业成绩
          "followed by end-of-file:";
  vector<double> homework;
  double x;
  while(cin >> x){
    homework.push_back(x);
  }

  //检查homeword是否为空
  typedef vector<double>::size_type vec_sz;
  vec_sz size = homework.size();
  if(size == 0){
    cout << endl << "You must enetr 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;

  return 0;
}  


//2
#include<iostream>
#include<algorithm>
#include<vector>

using std::cin;     using std::cout;
using std::vector;  using std::endl;

int main(int argc, char const *argv[])
{
  cout << "Please enter number:";
  int x;
  vector<int> num;
  while(cin >> x){
    num.push_back(x);
  }
  cout << endl;

typedef vector<int>::size_type vec_sz;
vec_sz size = num.size();
if(size == 0){
  cout << endl << "You must enter number"
                  "Please try again" << endl;
  return 1;
}

sort(num.begin(), num.end());

int count = size / 4;

for (int i = 0; i != size; ++i)
{
  for (int j = 0; j != count; ++j)
  {
    cout << num[size - 1 - i] << " ";
  }
  cout << endl;
}

  return 0;
}
//3
#include<iostream>
#include<string>
#include<vector>

using std::string; using std::endl; 
using std::cout;    using std::cin;
using std::vector;

int main(int argc, char const *argv[])
{
  cout << "Please enter word:";
  vector<string> word;
  vector<int> count;
  typedef vector<string>::size_type vec_sz;

  string s;
  while(cin >> s){
    bool found = false;

    for (int i = 0; i != word.size(); ++i)
    {
      if(s == word[i]){
        ++count[i];
        found = true;
      }
    }

    if(found == false){
      word.push_back(s);
      count.push_back(1);
    }

  }

  cout << endl;
  for (int i = 0; i != word.size(); ++i)
  {
    cout << word[i] << " appear " << count[i] << " times!"<< endl;
  }

  return 0;
}
//4
#include<iostream>
#include<string>
#include<vector>

using std::string;  using std::endl;
using std::cin;     using std::cout;
using std::vector;

int main(int argc, char const *argv[])
{
    cout << "Please enter word:";

    typedef vector<string>::size_type vec_sz;
    vector<string> word;
    vector<vec_sz> length;
    string s;

    while(cin >> s){
        cin >> s;
        bool found = false;
        for (vec_sz i = 0; i != word.size(); ++i)
        {
            if(s == word[i]){
                found = true;
            }
        }

        if(found == false){
            word.push_back(s);
            length.push_back(s.size());
        }
    }

    int max = 0,min = INT_MAX;
    int maxNum = -1, minNum = - 1;
    for (vec_sz i = 0; i != word[i].size(); ++i)
    {
        if(max <= length[i]){
            max = length[i];
            maxNum = i;
        }
        if(min >= length[i]){
            min = length[i];
            minNum = i;
        }
    }

    cout << "Longest string: " << word[maxNum] << "; length is " << max << endl;
    cout << "Shorst string: " << word[minNum] << "; length is " << min << endl;

    return 0;
}
//4 方法2
#include<iostream>
#include<string>

using std::cin; using std::cout;
using std::endl; using std::string;

int main(int argc, char const *argv[])
{
  cout << "Please enter word:";
  
  string s;
  string longestWord;
  string shortestWord;
  int longestNum = 0;
  int shortNum = INT_MAX;

  while(cin >> s){
    string::size_type length = s.size();
    if(longestNum <= length){
      longestNum = length;
      longestWord = s;
    }
    if(shortNum >= length){
      shortNum = length;
      shortestWord = s;
    }
  }

  cout << "The longestWord is " << longestWord <<" ,length is " << longestNum << endl;
  cout << "The shortestWord is " << shortestWord <<" ,length is " << shortNum << endl;
  return 0;
}
//5
#include<iostream>
#include<vector>
#include<string>
#include<ios>
#include<iomanip>

using std::string;  using std::vector;
using std::cout;    using std::cin;
using std::endl;    using std::streamsize;
using std::setprecision;

#define HomewordKNum 3 //作业个数

int main(int argc, char const *argv[])
{
  string s;
  double examMId = 0, examFinal = 0, homewordGrade = 0;
  vector<string> name;
  vector<double> gradeFinal;
  
  bool done = true;//判断是否还需要输入学生的信息
  while(done){
      done = false;
      cout << "Please enter your name:";
      cin >> s;
      name.push_back(s);
      cout << "Please enetr your midexam and finalexam grade:";
      cin >> examMId >> examFinal ;
      cout << "Please enter your homeword garde:";
      for (int i = 0; i != HomewordKNum; ++i)
      {
        double temp;
        cin >> temp;
        homewordGrade += temp; 
      }
     
     // cout << "test" << homewordGrade << endl;
      gradeFinal.push_back(examMId * 0.2 + examFinal * 0.4 + (homewordGrade /HomewordKNum) * 0.4);
      
      cout << "More(Y/N)?";
      string Judge;
      cin >> Judge;
      if(Judge == "Y")
        done = true;
  }

for (int i = 0; i != name.size(); ++i)
{
  streamsize prec = cout.precision();
  cout << name[i] <<" garde is " << setprecision(3)
        << gradeFinal[i] << " !" 
        << setprecision(prec) << endl;
}
  return 0;
}
//6
#include<iostream>
#include<iostream>
#include<string>
#include<iomanip>

using std::cin;  using std::string;
using std::cout;  using std::setprecision;
using std::endl;  using std::streamsize;

int main(int argc, char const *argv[])
{
  cout << "Please enter your first name:";
  string name;
  cin >> name;
  cout <<"Hello, " << name << "!" << endl;

  cout << "Please eneter your midterm and final grades:";
  double midterm ,final;
  cin >> midterm >> final;

  cout << "Enter all your homeword grades, "
          "followed by end-of-file";

  int count = 0;
  double sum =0;

  double x;

  while(cin >> x){
    count++;
    sum += x;
  }

  //自定义平均成绩除零时的运算
  double homegrade = (count > 0) ? sum / count : 0.0;

  streamsize prec = cout.precision();
  cout << "Your final grade is " << setprecision(3)
  << 0.2 * midterm + 0.4 * final + homegrade * 0.4
  << setprecision(prec) << endl;

  return 0;
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 岁月 设计师:pinMode 返回首页