C++ 输出加框 (期中+期末+作业)中值输出成绩

输入:

1.姓名

2.期中、期末、不定次数的作业成绩

输出:

1.加框

2.取作业的中值

#include <iostream>
#include <string>
#include <algorithm>
#include <ios>
#include <vector>
#include <iomanip>
using namespace std;
int main()
{
    //输入姓名
    cout<<"pleace inter your name:";
    string name;
    cin>>name;
    cout<<"Hello,"<<name<<"!"<<endl;
    
    //输入期中期末成绩
    cout<<"pleace inter your midterm and final exam grades:";
    double midterm,final;
    cin>>midterm>>final;
    
    //输入家庭作业成绩
    cout<<"enter all your homework grades,"
    "flolowed 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<<"you must enter your grades."
        "pleace 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();
    const double grade=0.2*midterm+0.4*final+0.4*median;
    const string greeting="Hello,"+name+"!"+"your grade is: ";
    const string spaces(greeting.size()+sizeof(double),' ');
    const string second="*"+spaces+"*";
    const string first(second.size(),'*');
    
    cout<<endl;
    cout<<first<<endl;
    cout<<second<<endl;
    cout<<"*"<<greeting<<setiosflags(ios::fixed)<<setprecision(5)<<grade<<"*"<<endl;
    cout<<second<<endl;
    cout<<first<<endl;
    return 0;
}








示例:

特点:

1.向量的使用

2.输入输出函数

3.setiosflags(ios::fixed)强制输出精度

优化:利用头文件,将主函数进行简洁

主函数:

#include <iostream>
#include <string>
#include <algorithm>
#include <ios>
#include <vector>
#include <iomanip>
#include "grades.hpp"
using namespace std;
int main()
{
    string name;
    double midterm,final;
    vector<double> homework;
    
    input(name,midterm,final,homework);
    
    double fg=grade(midterm,final,homework);
    
    output(name,fg);
    
    return 0;
}

grade头文件

//
//  grades.hpp
//  zhou
//
//  Created by 旭先生 on 2018/7/27.
//  Copyright © 2018年 旭先生. All rights reserved.
//

#ifndef grades_hpp
#define grades_hpp
#include <iostream>
#include <string>
#include <algorithm>
#include <ios>
#include <vector>
#include <iomanip>
using namespace std;

void input(string &,double &,double &,vector<double> &);

double grade(double &,double &,vector<double> &);

void output(string &,double &);

#endif /* grades_hpp */

grade cpp文件

//
//  grades.cpp
//  zhou
//
//  Created by 旭先生 on 2018/7/27.
//  Copyright © 2018年 旭先生. All rights reserved.
//
#include <iostream>
#include <string>
#include <algorithm>
#include <ios>
#include <vector>
#include <iomanip>
#include "grades.hpp"
using namespace std;
typedef vector<double>::size_type vec_sz;

void input(string &name,double &midterm,double &final,vector<double> &homework)
{
    //输入姓名
    cout<<"pleace inter your name:";
    cin>>name;
    cout<<"Hello,"<<name<<"!"<<endl;
    
    //输入期中期末成绩
    cout<<"pleace inter your midterm and final exam grades:";
    cin>>midterm>>final;
    
    //输入家庭作业成绩
    cout<<"enter all your homework grades,"
    "flolowed by end-of-file:";
    double x;
    
    while (cin>>x) {
        homework.push_back(x);
    }
    
}

double grade(double &midterm,double &final,vector<double> &homework)
{
    //排序
    sort(homework.begin(), homework.end());
    
    //计算中值
    vec_sz size=homework.size();
    vec_sz mid=size/2;
    double median;
    median=size % 2==0?(homework[mid]+homework[mid-1])/2:homework[mid];
    
    //计算输出
    //streamsize prec=cout.precision();
    const double grade=0.2*midterm+0.4*final+0.4*median;
    return  grade;
}

void output(string &name,double &fg)
{
    const string greeting="Hello,"+name+"!"+"your grade is: ";
    const string spaces(greeting.size()+sizeof(double),' ');
    const string second="*"+spaces+"*";
    const string first(second.size(),'*');
    
    cout<<endl;
    cout<<first<<endl;
    cout<<second<<endl;
    cout<<"*"<<greeting<<setiosflags(ios::fixed)<<setprecision(5)<<fg<<"*"<<endl;
    cout<<second<<endl;
    cout<<first<<endl;
}

猜你喜欢

转载自blog.csdn.net/qq_41995348/article/details/81219988