About the C + + version of computing student scores

Source: Internet
Author: User

The requirement is this: Enter the name, enter the midterm and final results, and then a regular performance, according to a certain proportion of the overall results.

With C voice implementation There is nothing to say, but I want to use C + + and use a variety of strange templates or containers to implement (→→ actually I do not understand), the code in accordance with accelerated C + +, and then write their own comments.

On the code:

1#include <algorithm>//stl content in this code to implement a quick sort of vector (in fact, an array of c)2#include <iomanip>//The setprecision header file used in the code below is used here to achieve the output of the exact number of digits. Manip is the abbreviation of manipulator.3#include <ios>//streamsize header file used in the code below, used here to denote length (typedef)4#include <iostream>//output and input stream
5#include <string>6#include <vector>//header files using container vectors7 using namespacestd;8 intMain ()9 {Tencout<<"Please enter your first name:"; One stringname; ACin>>name; -cout<<"Hello,"<<name<<" !"<<Endl; -cout<<"Please enter your midterm and final exam grades:"; the Doublemidteam,final; -Cin>>midteam>>final; -cout<<"Enter all your homework Gradge," - "followed by end-of-file:"; +/*
The previous part of the code to implement some of the input and output and the definition of variables and so on, and show that the output of the performance of the time to use EOF to end the way (End-of-file)
*/
-vector<Double>homework;//The book is about defining a vector homework, in fact my understanding is to define an array. The way vector is used to define vectors is vector< data type > vector name + Doublex; Variable x is used to read data and act as a temp. A while(cin>>x)//continue to execute while loop when input is successful at homework.push_back (x); The number inside the parentheses is pressed into the vector. The format for writing is the vector name. push_back (x); -typedef vector<Double>:: Size_type VEC_SZ;
/* Above this sentence is the focus. Vector Type the following also defines a type, called Size_type, to hold the length of the vector, and this length can be infinitely large,
This will not affect the length of the variable because of the different types of access, but also to separate the system environment.
。 There is also a function called size, which belongs to the vector type and is used to return the length value.
But every time with Size_type to write is too troublesome, so in order to simplify the program, the heap of genius found a language tool called TypeDef.
Through the use of the above code, we use VEC_SZ instead of Vector<double>::size_type, later need to use the time to use the VEC_SZ to replace the good.
Note that the format used here is such a "typedef vector< data type >::size_type alternative name"
*/
-VEC_SZ size =homework.size ();//equal to Vector<double>::size_type = Homework.size (); - if(size==0//if statement is used to determine whether or not to read in peacetime homework results, if not, the direct exit to come again ~ - { -cout<<endl<<"You must enter your Gradge." in "Please try again."<<Endl; - to return 1; + } - sort (Homework.begin (), Homework.end ());//quickly sort vectors (non-descending). The vector class below also provides two member functions begin and end, used to return the Kinsoku element theVEC_SZ mid=size/2; * Doublemedian; $Median = size%2==0? (homework[mid]+homework[mid-1])/2: Homework[mid];
/* The above paragraph is used to find the median value. Note the 36-line notation. The question mark followed by the execution, do not need to write homework=xxxxxxxxxx....*/ -Streamsize Prec =cout.precision (); This part is not understood for the time being, come again later thecout<<"Your final grade is"<<setprecision (3) +<<0.2*midteam+0.4*final+0.4*Median A<<setprecision (PREC) <<Endl; the return 0; +}

About the C + + version of computing student scores

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.