|
以前写的一个,没写完
程序代码:
/*
Name:
Copyright:
Author:
Date: 20-11-07 22:16
Description:
1、add()记录每个学生的各科成绩
2、place()根据总分排出名次
3、aver()求平均分
4、search()根据姓名查询成绩
5、print_result()打印出成绩单
*/
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <algorithm>
#include <utility>
using namespace std;
typedef multimap <string, pair <string, float> > multi_map;
typedef multimap <string, pair <string, float> > ::iterator map_iter;
const int subject_num = 6;
/*=========函数声明===============*/
multi_map add();
map_iter search(multi_map name_score, string name, map_iter &beg);
float aver(multi_map name_score, map_iter beg, map_iter end);
/*========1、add()记录每个学生的各科成绩=============*/
multi_map add()
{
cout << "添加一个记录: " << endl;
cout << "姓名: " << flush;
string name;
cin >> name;
cout << "学号: " << flush;
int stu_num;
cin >> stu_num;
//添加成绩
multimap <string, pair <string, float> > name_score;
float temp;
string subject[] = { "语文 ", "数学 ", "英语 ", "物理 ", "化学 ", "生物 "};
for (int ix = 0; ix != subject_num; ++ix)
{
cout << subject[ix] << ": " << flush;
cin >> temp;
name_score.insert(make_pair(name, make_pair(subject[ix], temp)));
if (ix == subject_num-1)
cout << "输入完成! " << endl;
}
return name_score;
}
/*============search()根据姓名查询成绩=============*/
/*它接受三个参数:name_score为存放信息的容器
name为要查询的姓名
&beg为额外返回的一个name_score.lower_bound(name)产生的迭代器
end函数本身返回一个name_score.upper_bound(name)产生的迭代器
end和&beg用于初始化函数aver()的形参
*/
map_iter
search(multi_map name_score, string name, map_iter &beg)
{
beg = name_score.lower_bound(name);
map_iter end = name_score.upper_bound(name);
if(beg != name_score.end())
{
cout << "以下是为您查询到的内容: " << endl;
while(beg != end)
{
cout << beg-> second.first << flush
<<beg-> second.second << endl;
++beg;
}
}
else
cout << "您要查找的数据不存在! " << endl;
return end;
}
/*============aver()求平均分=============*/
float aver(multi_map name_score, string name, map_iter beg, map_iter end) //
{
float sum = 0;
beg = name_score.lower_bound(name);
end = name_score.upper_bound(name);
while(end != beg)
{
sum += beg-> second.second;
++beg;
}
float average = sum/subject_num;
return average;
}
/*=============主函数===========*/
int main()
{
multi_map add_it;
add_it = add();
cout << "输入要查找的姓名: " << flush;
string name;
cin >> name;
map_iter beg, end;
end = search(add_it, name, beg);
cout << "平均分: " << aver(add_it, name, beg, end) <<endl;
system( "pause ");
return 0;
}
[[italic] 本帖最后由 随心 于 2008-1-9 20:50 编辑 [/italic]] |
|