| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3796 人关注过本帖
标题:[求助]如何按姓氏的首字母顺序排列?
只看楼主 加入收藏
tx1988
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2007-5-29
收藏
 问题点数:0 回复次数:4 
[求助]如何按姓氏的首字母顺序排列?

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

这是我在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]);
}

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

搜索更多相关主题的帖子: 顺序 字母 排列 姓氏 
2007-05-29 06:37
tx1988
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2007-5-29
收藏
得分:0 
回复:(tx1988)[求助]如何按姓氏的首字母顺序排列?...
呜呜呜,大家一定要帮忙啊!这是我的final project之一,能不能拿A就看它了。
PS:我正在LA学习编程,没感觉比国内好多少!唉~~~~~~
2007-05-29 07:36
aipb2007
Rank: 8Rank: 8
来 自:CQU
等 级:贵宾
威 望:40
帖 子:2879
专家分:7
注 册:2007-3-18
收藏
得分:0 

[CODE]#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
using namespace std;
typedef vector<string>::iterator iter;
int main(){
vector<string> svec;
//read file
ifstream fin("data.dat");
assert(fin);
string strLine;
while (getline(fin,strLine))
svec.push_back(strLine);
//rebulid name and process the scores
string firstName,lastName;
int score1,score2,score3,sum1 = 0,sum2 = 0,sum3 = 0;
for (iter p = svec.begin();p != svec.end();++p){
istringstream strm_in(*p);
strm_in >> firstName >> lastName
>> score1 >> score2 >> score3;
ostringstream strm_out;
strm_out << score1 << " " << score2 << " " << score3;
//reset position of name
lastName.erase(--lastName.end()); //remove the ':' at end
//make last name to uppercase
string::iterator q = lastName.begin();
while (q != lastName.end()){
*q = toupper(*q);
++q;
}
//compute average for each student
int avg = (score1 + score2 + score3) / 3;
strm_out << "\t" << avg;
if (avg >= 90) strm_out << "\tA";
else if (avg >= 80) strm_out << "\tB";
else if (avg >= 70) strm_out << "\tC";
else strm_out << "\tD";
//compute sum
sum1 += score1;
sum2 += score2;
sum3 += score3;
//relink the data
*p = lastName + " , " + firstName + " : " + strm_out.str();
}
//sort the data base on last name
sort(svec.begin(),svec.end());
//print result
for (iter p = svec.begin();p != svec.end();++p)
cout << *p << endl;
cout << sum1 / svec.size() << endl;
cout << sum2 / svec.size() << endl;
cout << sum3 / svec.size() << endl;
}[/CODE]

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

[此贴子已经被作者于2007-5-29 10:28:13编辑过]


Fight  to win  or  die...
2007-05-29 10:24
tx1988
Rank: 1
等 级:新手上路
帖 子:10
专家分:0
注 册:2007-5-29
收藏
得分:0 
回复:(aipb2007)[CODE]#include #i...
斑竹是超级无敌编程王!小弟佩服得五体投地!!!!
2007-05-29 11:41
孤魂居士
Rank: 2
来 自:老A(中国地大)
等 级:论坛游民
威 望:4
帖 子:1142
专家分:18
注 册:2007-5-21
收藏
得分:0 
我觉得无限循环哥哥  

准备用3年做个高级软件工程师 10年也做不成。准备用10年做成高级软件工程师 3年就成了QQ 群 45771086
欢迎版主...欢迎JAVA爱好者...
一起从深夜 到凌晨...
2007-05-29 15:28
快速回复:[求助]如何按姓氏的首字母顺序排列?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023875 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved