找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 198|回复: 4

[求助]如何按姓氏的首字母顺序排列?

[复制链接]
发表于 2009-11-6 01:38:32 | 显示全部楼层 |阅读模式 IP:江苏扬州
Whitewater High School has contracted you to write a program that will
read a file of student names and test scores in the formation FIRST
NAME LAST NAME: SCORE1 SCORE2 SCORE3, where each of the scores is an
integer. Then your program is to print a report, sorted by last name
with each of the last names fully capitalized (that is, if you read in
Wilkinson you print WILKINSON) followed by a comma, then the first
name, then a colon, then the scores, then the average for the student,
and lastly a letter grade (based upon 90 percent or greater being an
"A," 80 percent or greater being a "B," and so forth). At the end of
the report the averages for each test are to be printed also. Needless
to say, the report must be written to a file and must be "pretty" (in
proper columns and such). (HINT: Read in the whole line as a string,
switch the first and last name, sort the names, then compute the math)
Data file: data.txt
You must turn in print outs of your report and the documented program
on Wednesday, May 30, 2007.
The data file content is:

Mary Jones: 89 90 100
Tom Brown: 100 99 100
John Smith:66 80 98
Englebert Humberdink: 85 87 88
Tom Jones: 76 78 89
Paul McCartney: 88 88 99
Olivia Newton: 77 66 98
Susan Barlow: 87 98 89
Robert Payne: 88 44 76
George Franklin: 77 88 99
Margaret Ibach: 87 89 90
Maggie Chang: 99 99 100
Blaire Bates: 89 87 78
John Lennon: 88 77 99
Silvia Stalone: 66 55 88
Pepper Johnson: 90 80 99
Alfred Newman: 80 80 90
Hugh Walker: 56 90 70
Maureen Ferguson: 100 90 70
Cho Zhang: 99 99 99
<!--[if !supportLineBreakNewLine]-->
这是我在LA上课时老师的题,大意是给你一个文件里边是25名学生的3次考试成绩,要你将这个文件按姓氏的首字母顺序重排并且算出平均值和给个评价(A,B.....):
我的问题是按姓氏的首字母顺序排列,这里涉及了1.提取出首字母2。比较它们的值3。排列。
第2个是说如何将一个array中的每个index的string都改成大写字母形式,如:
array firstname[10]
[0]Adsds
[1]Dwwh
......
我是这么做的,可是不对,为什么?

void ChangeLastname(string lastname[],string LASTNAME[])
{
for(int i=0;i<25;i++)
LASTNAME[i]=toupper(lastname[i]);
}

大家一定说详细些,或者直接给个例子,我是新手啊!!!!!

<!--[endif]-->
发表于 2009-11-6 01:38:33 | 显示全部楼层 IP:江苏扬州
呜呜呜,大家一定要帮忙啊!这是我的final project之一,能不能拿A就看它了。
PS:我正在LA学习编程,没感觉比国内好多少!唉~~~~~~
回复

使用道具 举报

发表于 2009-11-6 01:38:34 | 显示全部楼层 IP:江苏扬州
  1. #include <fstream>
  2. #include <sstream>
  3. #include <string>
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <cassert>
  8. using namespace std;
  9. typedef vector<string>::iterator iter;
  10. int main(){
  11. vector<string> svec;
  12. //read file
  13. ifstream fin("data.dat");
  14. assert(fin);
  15. string strLine;
  16. while (getline(fin,strLine))
  17. svec.push_back(strLine);
  18. //rebulid name and process the scores
  19. string firstName,lastName;
  20. int score1,score2,score3,sum1 = 0,sum2 = 0,sum3 = 0;
  21. for (iter p = svec.begin();p != svec.end();++p){
  22. istringstream strm_in(*p);
  23. strm_in >> firstName >> lastName
  24. >> score1 >> score2 >> score3;
  25. ostringstream strm_out;
  26. strm_out << score1 << " " << score2 << " " << score3;
  27. //reset position of name
  28. lastName.erase(--lastName.end()); //remove the ':' at end
  29. //make last name to uppercase
  30. string::iterator q = lastName.begin();
  31. while (q != lastName.end()){
  32. *q = toupper(*q);
  33. ++q;
  34. }
  35. //compute average for each student
  36. int avg = (score1 + score2 + score3) / 3;
  37. strm_out << "\t" << avg;
  38. if (avg >= 90) strm_out << "\tA";
  39. else if (avg >= 80) strm_out << "\tB";
  40. else if (avg >= 70) strm_out << "\tC";
  41. else strm_out << "\tD";
  42. //compute sum
  43. sum1 += score1;
  44. sum2 += score2;
  45. sum3 += score3;
  46. //relink the data
  47. *p = lastName + " , " + firstName + " : " + strm_out.str();
  48. }
  49. //sort the data base on last name
  50. sort(svec.begin(),svec.end());
  51. //print result
  52. for (iter p = svec.begin();p != svec.end();++p)
  53. cout << *p << endl;
  54. cout << sum1 / svec.size() << endl;
  55. cout << sum2 / svec.size() << endl;
  56. cout << sum3 / svec.size() << endl;
  57. }
复制代码

不知道是不是符合题目意思,你凑合看看先。
环境:vc++2005
回复

使用道具 举报

发表于 2009-11-6 01:38:35 | 显示全部楼层 IP:江苏扬州
斑竹是超级无敌编程王!小弟佩服得五体投地!!!!
回复

使用道具 举报

发表于 2009-11-6 01:38:36 | 显示全部楼层 IP:江苏扬州
我觉得无限循环哥哥
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-10-1 17:32 , Processed in 0.104694 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表