16周任务三 score 学生类的文件操作

/* (程序头部注释开始)
* 程序的版权和版本声明部分
* Copyright (c) 2011, 烟台大学计算机学院学生 
* All rights reserved.
* 文件名称:   score                          
* 作    者:   姜雅明                              
* 完成日期:   2012    年   06    月    05    日
* 版 本 号:   1.0       

* 对任务及求解方法的描述部分
* 输入描述: 
* 问题描述: 
* 程序输出: 
* 程序头部的注释结束
*/

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

class Student
{
private:
	string name;
	double cpp;
	double math;
	double english;
	double all_score;
	double ave_score;
public:
	Student(){};
	Student(string name, double cpp, double math, double english, double all_score, double ave_score);

	void set_value(string name, double cpp, double math, double english);

	string get_name(){return name;}
	double get_cpp(){return cpp;}
	double get_math(){return math;}
	double get_english(){return english;}
	double get_all_score(){return all_score;}
	double get_ave_score(){return ave_score;}

	void set_name(string name){this->name = name;}
	void set_cpp(double cpp){this->cpp = cpp;}
	void set_math(double math){this->math = math;}
	void set_english(double english){this->english = english;}
	void set_all_score(double all_score){this->all_score = all_score;}
	void set_ave_score(double ave_score){this->ave_score = ave_score;}
};

Student::Student(string name, double cpp, double math, double english, double all_score, double ave_score)
{
	this->name = name;
	this->cpp = cpp;
	this->math = math;
	this->english = english;
	this->all_score = all_score;
	this->ave_score = ave_score;
}

void Student::set_value(std::string name, double cpp, double math, double english)
{
	this->name = name;
	this->cpp = cpp;
	this->math = math;
	this->english = english;
	all_score = cpp + math + english;
	ave_score = all_score / 3;
}

int main()
{
	Student stu[100];

	string name;
	double cpp, math, english;

	//读入文件
	ifstream infile("score.dat",ios::in);

	if(!infile)
	{
		cerr << "open error!\n";

		exit(1);
	}
	for(int i = 0; i < 100; ++i)
	{
		infile >> name >> cpp >> math >> english;

		stu[i].set_value(name, cpp, math, english);

	}
	infile.close();

	//各科最高分
	Student max_score("no", 0, 0, 0, 0, 0);

	for(int i = 0; i < 100; ++i)
	{
		if(stu[i].get_cpp() > max_score.get_cpp()) {max_score.set_cpp(stu[i].get_cpp());}

		if(stu[i].get_math() > max_score.get_math()) {max_score.set_math(stu[i].get_math());}

		if(stu[i].get_english() > max_score.get_english()) {max_score.set_english(stu[i].get_english());}

		if(stu[i].get_all_score() > max_score.get_all_score()) {max_score.set_all_score(stu[i].get_all_score());}
	}

	cout << "cpp最高分为:" << max_score.get_cpp() << endl
		<< "数学最高分为:" << max_score.get_math() << endl
		<< "英语最高分为:" << max_score.get_english() << endl
		<< "总分最高分为:" << max_score.get_all_score() << endl;

	//按总分排序
	Student k;

	for(int i = 0; i < 100; i++)
	{
		for(int j = 0; j < 100 - i; j++)
		{
			if(stu[j].get_all_score() < stu[j + 1].get_all_score())
			{
				k = stu[j];

				stu[j] = stu[j + 1];

				stu[j + 1] = k;
			}
		}
	}

	//输出到文件
	ofstream outfile("ordered_score.txt",ios::out);

	for(int i = 0; i < 100; i++)
	{
		outfile << stu[i].get_name() << "\t"

			<< stu[i].get_cpp() << "\t"

			<< stu[i].get_math() << "\t"

			<< stu[i].get_english() << "\t"

			<< stu[i].get_all_score() << "\t"

			<< stu[i].get_ave_score() << "\t\n";
	}

	system("pause");

	return 0;

}

开始向在类中把文件读入和写入文件的,但是没成功。看过老师的有所启发,变成这样了。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是一个简单的C语言学生管理系统文件操作代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_NAME_LEN 20 #define MAX_STUDENT_NUM 100 typedef struct { char name[MAX_NAME_LEN]; int age; int score; } Student; int readStudents(Student students[]) { FILE *fp; int i = 0; fp = fopen("students.txt", "r"); if (fp == NULL) { printf("Can't open file: students.txt\n"); exit(1); } while (fscanf(fp, "%s %d %d", students[i].name, &students[i].age, &students[i].score) != EOF) { i++; } fclose(fp); return i; } void writeStudents(Student students[], int num) { FILE *fp; int i; fp = fopen("students.txt", "w"); if (fp == NULL) { printf("Can't open file: students.txt\n"); exit(1); } for (i = 0; i < num; i++) { fprintf(fp, "%s %d %d\n", students[i].name, students[i].age, students[i].score); } fclose(fp); } void printStudents(Student students[], int num) { int i; printf("Name\tAge\tScore\n"); for (i = 0; i < num; i++) { printf("%s\t%d\t%d\n", students[i].name, students[i].age, students[i].score); } } int main() { Student students[MAX_STUDENT_NUM]; int num; num = readStudents(students); printStudents(students, num); // Add new student strcpy(students[num].name, "Tom"); students[num].age = 20; students[num].score = 90; num++; writeStudents(students, num); return 0; } ``` 该代码实现了以下功能: 1. 从文件中读取学生信息到数组中 2. 将学生信息打印到屏幕上 3. 向数组中添加一个新的学生信息 4. 将更新后的学生信息写入文件中 其中,readStudents函数从文件中读取学生信息到数组中,返回学生数量;writeStudents函数将学生信息写入文件中;printStudents函数将学生信息打印到屏幕上。在主函数中,先调用readStudents函数读取学生信息,再调用printStudents函数打印学生信息,然后添加一个新学生信息,最后调用writeStudents函数将更新后的学生信息写入文件中。 这是一个简单的实现,真实的学生管理系统可能需要更复杂的功能和数据结构。但是,文件操作是学生管理系统中必不可少的一部分,通过文件操作可以方便地保存和读取学生信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值