-
Notifications
You must be signed in to change notification settings - Fork 0
/
4_use batch data.cpp
79 lines (67 loc) · 1.9 KB
/
4_use batch data.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
//
// Created by Daisy on 2020/10/15.
//
#include <iostream>
#include "algorithm"
#include "iomanip"
#include "string"
#include "vector"
#include "ios"
using std::cout; using std::endl;
using std::cin; using std::sort;
using std::string; using std::vector;
using std::streamsize; using std::setprecision;
int calGrades(){
cout << "please enter your first name: ";
string name;
cin >> name;
cout << "Hello" << name << "!" << endl;
cout << "please enter your midterm and final grades:";
double midterm, final;
cin >> midterm >> final;
cout << "enter all of 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 << "you should enter your grades" << endl;
return 1;
}
sort(homework.begin(),homework.end());
vec_sz mid = size/2; //result in integers
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.2*median
<< setprecision(prec)<<endl;
return 0;
}
int splitinto43_2(){
cout << "please enter your numbers: ";
vector<double> nums;
double x;
while (cin>>x){
nums.push_back(x);
}
sort(nums.begin(),nums.end());
typedef vector<double>::size_type vec_sz;
vec_sz numsize = nums.size();
vec_sz fouth = numsize - 1 - numsize/4;
int begin = numsize - 1;
for(int i=0;i!= 4; ++i){
for (int cnt = begin; cnt != fouth; --cnt) {
cout << nums[cnt] << " ";
}
cout << endl;
begin = fouth;
fouth = i < 2? fouth - numsize/4: -1;
}
return 0;
}