c++编程的一个小问题。。

duliang135 2012-10-02 01:14:28
小弟初学c++。有个问题不知道标准答案。麻烦大家帮我解答一下。谢谢。用到Vector+Sort简单点儿就好。。

一共有10个人。提示用户输入1~10的整数。然后输出学号和成绩。第一方阵不包含平均分
请输入想要看的学生成绩数:3
2011123458 45 32 64
2010345782 34 98 100
2009425342 74 64 21
//下面是自动按照3个学号高低排列的,最后一个数字53,73,47是平均成绩。
2009425342 74 64 21 53
2010345782 34 98 100 73
2011123458 45 32 64 47
//下面是按照平均成绩排列的。
2010345782 34 98 100 73
2009425342 74 64 21 53
2011123458 45 32 64 47

下面是书上的要求。。。
Maximum number of students is 10
Round off below the decimal point of average scores
You don’t need to care Tie-breaking
Insert format of student is important
◦StudentID Score1 Score2 Score3

请帮我写全了。因为我刚学。还不是很熟练。、谢谢了
...全文
142 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
hongwenjun 2012-10-02
  • 打赏
  • 举报
回复
VC2010 编译器 使用正则判断数据是否正确
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <algorithm>
#include <regex>
#include <stdio.h>
#include <string.h>


using namespace std;
// StudentID Score1 Score2 Score3 average
struct Student {
char StudentID[16];
int Score1 , Score2 , Score3 ;
double average;
};

void LoadStudentLib(istream& fs , vector<Student>& vs);
void PrintStudentList(vector<Student>& vs);

// 比较函数
bool IDCmp(const Student& a , const Student& b) {return (strcmp(a.StudentID, b.StudentID) < 0);}
bool averageCmp(const Student& a , const Student& b) { return a.average > b.average;}

FILE* CerrLog = fopen("错误数据.log" , "w+");
void PrintCerrLog(); // 输出错误日志文件

int main()
{
vector<Student> verStudent;

// cin 换成文件 就可以直接文件输入了,用cin要手工ctrl+z 结束输入
//LoadStudentLib(cin , verStudent);

ifstream file("Student.txt");
LoadStudentLib(file , verStudent);
file.close();

vector<Student>::iterator first = verStudent.begin();
vector<Student>::iterator last = verStudent.end();

cout << string(78 , '-') << endl;
sort(first , last, IDCmp); // 按学号排序
PrintStudentList(verStudent);

cout << string(78 , '-') << endl;
sort(first , last, averageCmp); // 按平局分排序
PrintStudentList(verStudent);

cout << string(78 , '-') << endl;
PrintCerrLog();
return 0;
}


void LoadStudentLib(istream& fs , vector<Student>& vs)
{
char ID[16] = {0};
int S1 , S2 , S3 ;
double Avg;
Student tmp;
string line;

// 匹配正则公式 \s*\S+\s+(\d+\s+\d+\s+\d+)\s* [ 朱佩誉 79 122 70 ]
string rs = "\\s*\\S+\\s+(\\d+\\s+\\d+\\s+\\d+)\\s*";
regex expression(rs); // 字符串传递给构造函数,建立正则表达式

while (getline(fs, line)) {
if (line[0] == '#' || !regex_match(line, expression)) { // #开头是注解行
fprintf(CerrLog, "无效line-> %s\n" , line.c_str());
continue;
}

sscanf(line.c_str(), "%s %d %d %d" , ID, &S1 , &S2 , &S3);
Avg = (S1 + S2 + S3) / 3.0;
strcpy(tmp.StudentID , ID);
tmp.Score1 = S1; tmp.Score2 = S2;
tmp.Score3 = S3; tmp.average = Avg;
vs.push_back(tmp);
}
}

void PrintStudentList(vector<Student>& vs)
{
vector<Student>::iterator it = vs.begin();
for (; it != vs.end() ; ++it)
printf("%s\t%d\t%d\t%d\t%0.2f\n" , it->StudentID ,
it->Score1 , it->Score2 , it->Score3 , it->average);
}

void PrintCerrLog() // 输出错误日志文件
{
size_t fsize = ftell(CerrLog);
char* pfbuf = new char[fsize + 1];
fseek(CerrLog, 0, SEEK_SET);
fread(pfbuf, 1 , fsize, CerrLog);
printf("%s\n", pfbuf);
delete[] pfbuf;
}

/* 输入文件 Student.txt
#姓名 成绩1 成绩2 成绩3
杨丰艳 81 121 71
董颖 77 118.8 68
#王乃东 86 119 73
朱义城 81 127 72
李超 7B 128 71
李江卫80 122 71
廉功乙 80 121 x 70
邓玉姝 79 120 69
胡鑫莹 90 136 79
王文涛 76 141 73
韩佳彤 81 126 72
董晓明 91 146 82
傅瀛 86 128 75
郝玉靖 85 128 74
高 珊 82 126 73
阿茹罕 80 125 71
马玉玲 79 127 71
朱佩誉 79 122 70
周春苗 77 119 68
#张丽娅 87 130 76
石荣广 81 127 72
沈 泊 78 129 71
何力威 86 135 77
黄 悦 82 133 74
谢飞 75 130 70
张爱梅 85 126 74
李鸥 80 126 71
刘立孟 80 126 71
麻艳伟 78 126 70
刘蒙 66 126 65
申小敏 87 135 77
张慧超 83 132 75
田洋洋 82 129 73
赵华军 77 137 73
吴昉 80 128 72
刘光宗 78 126 70
武冰 77 131 71
王中君 78 126 70
何莹 75 126 69
刘喜成 87 133 77
张良宝 77 137 73
郑勇维 79 128 72
李云晴 78 128 71
李霁 80 121 70
陈金花 75 110 65
朝泽东 88 132 77
李进 85 131 75
王佳 82 126 73
路磊 79 118 69
唐智伟 77 115 67
富瑶 78 109 66
王莹 72 109 63
*/
hongwenjun 2012-10-02
  • 打赏
  • 举报
回复
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <string.h>

using namespace std;
// StudentID Score1 Score2 Score3 average
struct Student {
char StudentID[16];
int Score1 , Score2 , Score3 ;
double average;
};

void LoadStudentLib(istream& fs , vector<Student>& vs);
void PrintStudentList(vector<Student>& vs);

// 比较函数
bool IDCmp(const Student& a , const Student& b) {return (strcmp(a.StudentID, b.StudentID) < 0);}
bool averageCmp(const Student& a , const Student& b) { return a.average > b.average;}

int main()
{
vector<Student> verStudent;

// cin 换成文件 就可以直接文件输入了,用cin要手工ctrl+z 结束输入
LoadStudentLib(cin , verStudent);

vector<Student>::iterator first = verStudent.begin();
vector<Student>::iterator last = verStudent.end();

cout << string(78 , '-') << endl;
sort(first , last, IDCmp); // 按学号排序
PrintStudentList(verStudent);

cout << string(78 , '-') << endl;
sort(first , last, averageCmp); // 按平局分排序
PrintStudentList(verStudent);

return 0;
}



void LoadStudentLib(istream& fs , vector<Student>& vs)
{
char ID[16] = {0};
int S1 , S2 , S3 ;
double Avg;
Student tmp;
string line;
while (getline(fs, line)) {
sscanf(line.c_str(), "%s %d %d %d" , ID, &S1 , &S2 , &S3);
Avg = (S1 + S2 + S3) / 3.0;
strcpy(tmp.StudentID , ID);
tmp.Score1 = S1; tmp.Score2 = S2;
tmp.Score3 = S3; tmp.average = Avg;
vs.push_back(tmp);
}
}

void PrintStudentList(vector<Student>& vs)
{
vector<Student>::iterator it = vs.begin();
for (; it != vs.end() ; ++it)
printf("%s\t%d\t%d\t%d\t%0.2f\n" , it->StudentID ,
it->Score1 , it->Score2 , it->Score3 , it->average);
}

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧