Sorry, I forgot to update my blog yesterday, make up today.
Score Enquiry System
Score: 21
Math teacher small y want to write a score query system, including the following instructions:
- Insert [name] [score], inserting a message into the system, indicating that the student whose name is named is the math score of score.
- Find [name], which represents the math score for a student named name.
Note that some students may be in order to brush a number of elective courses, the query when the maximum score can be. The student's name is made up of lowercase letters. The score is a 0 ... An integer of 100.
The teacher found you and wanted you to help him complete the system.
Input format
Enter several lines, each of which is the form of insert [name] [score] or find [name], or a line end that indicates the end of the input. The number of input rows is not greater than 1000, and the length of each student name is not greater than 20 characters.
Output format
For each query, the output queries the student's highest score, if the student does not exist in the system, output? 1.
Sample input
Insert Zhangsan 90
Insert Lisi 78
Insert Xiaoming 86
Find Xiaoming
Find Jack
End
Sample output
86
-1
Author notes: I have limited ability to write with pure C has a bug, the whole Std wrote a, for reference only.
By the right, the code function adds a function to query all the records, and the deletion is in line with test instructions.
1#include <stdio.h>2#include <vector>3#include <string>4#include <iostream>5#include <math.h>6 using namespacestd;7 structinfo{8 stringname;9 intscore;Ten }stu; One /* A define the Find function: - function: Find a record of the same name in the container by name; - parameters: Container of struct type, name of string type; the return value type: A pointer to a piece of data. - */ -Vector<info>::iterator Find (Vector<info>&stu,stringname) { -Vector<info>::iterator it;//declaring iterators + for(It=stu.begin (); It!=stu.end (); it++){ - if(it->name==name) + Break; A } at returnIt//returns a pointer to a piece of data - } - intMain () { - stringSign,name; - intscore; -Vector<info> inf;//a container that declares a struct type, used to hold data incin>>sign;//input operator -Vector<info>::iterator it;//declaring iterators to while(sign!="End"){ + if(sign=="Insert"){ -cin>>stu.name>>stu.score;//input theit = find (Inf,stu.name);//calling Functions * if(it==Inf.end ()) { $ Inf.push_back (stu);Panax Notoginseng}Else if(It->score<stu.score) {//If you have just entered a result that is greater than your existing score -Inf.insert (It,stu);//storing records with high scores the}Else{//not found, new record saved +Inf.insert (it+1, Stu); A } the } + Else if(sign=="Find"){ -cin>>stu.name;//input $It=find (Inf,stu.name);//calling Functions $cout<<it->score<<Endl; - } - Else if(sign==" All"){ the for(It=inf.begin (); It!=inf.end (); it++){ -cout<<it->name<<" "<<it->score<<Endl;Wuyi } the } -cin>>sign;//perform the operation once and go to the next Wu } - return 0; About}
This topic learns a technique for dealing with access and output: use while to make judgments.
In the title while (sign!= "end") {...}.
C language · Score Enquiry System