⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 task_filework.cpp

📁 学生成绩管理。 Win32(dos)字符界面。可实现读入、简单计算、统计、输出等操作
💻 CPP
字号:
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include "filework.h"
#include "student.h"
#include "deeds.h"

ifstream fin;
ofstream fout;

void fileIn(student *&headOfStudent,courselist *&headOfCourse)
{
	char s[100];
	cin.ignore(100,'\n');
	cout<<char(7);
	cout<<"\n警告:从文件读入数据将丢失该操作前的所有输入信息!\n"
		<<"如果要继续,请按回车键:";
	cin.getline(s,100);
	if (s[0])
	{
		cout<<char(7);
		cout<<"\n从文件读入操作被终止!"<<endl;
		fin.close();
		return;
	}
	if (headOfStudent) 
		Free_Student(headOfStudent);
	if (headOfCourse)
		Free_Courselist(headOfCourse);
	cout<<"\n请输入您的数据文件的名称,如 Student_Input.txt "<<endl;
	cin>>s;
	fin.open(s);
	if (!fin)
	{
		cout<<char(7);
		cout<<"\n打开文件失败!请确认文件名是否正确,文件是否放在该程序目录下!"<<endl;
		fin.close();
		return;
	}
	cin.ignore(100,'\n');
	cout<<"\n请确认输入文件的格式满足以下要求:\n"
		<<" 1. 文件第一行是一个正整数n,后面为n个课程名及其学分,相互以空格隔开\n"
		<<" 2. 文件第二行至文件尾,为若干学生信息\n"
		<<" 3. 学生信息的格式为:\n"
		<<"     (a)学生姓名 学号 各科成绩\n"
		<<"     (b)各个输入之间用空格隔开,一个学生的信息独占一行\n"
		<<"     (c)学生姓名为少于五个字的汉字,中间不能包含空格\n"
		<<"     (d)学号为标准的8位学号,如:00648127 \n"
		<<"     (e)学生各科成绩的输入顺序与第一行课程顺序对应,如果该学生未选这门课,则输入 -1\n"
		<<"\n若都已确认,请按回车键:";
	cin.getline(s,100);
	if (s[0])
	{
		cout<<char(7);
		cout<<"\n文件格式不符合要求,文件读入失败!"<<endl;
		fin.close();
		return;
	}

	short coursenumber,i;
	fin>>coursenumber;
	if (cin.fail() || (coursenumber<0))
	{
		cout<<char(7);
		cout<<"\n文件格式不符合要求,文件读入失败!"<<endl;
		fin.close();
		return;
	}
	cout<<"\n已读入:\n"<<coursenumber<<' ';
	courselist *temp,*endv;
	if (coursenumber)
	{
		temp=new courselist;
		if (temp==NULL)
		{
			cout<<char(7)<<endl;
			cout<<"\n申请内存失败!不能读入学生信息!"<<endl;
			fin.close();
			return;
		}
		fin>>temp->courseName>>temp->credits;
		if (cin.fail())
		{
			cout<<char(7)<<endl;
			cout<<"\n课程学分输入有误!文件读入失败!"<<endl;
			fin.close();
			return;
		}
		cout<<temp->courseName<<' '<<temp->credits<<' ';
		temp->next=NULL;
	}
	headOfCourse=temp;
	endv=temp;
	for (i=1;i<coursenumber;i++)
	{
		temp=new courselist;
		if (temp==NULL)
		{
			cout<<char(7)<<endl;
			cout<<"\n申请内存失败!不能读入学生信息!"<<endl;
			fin.close();
			return;
		}
		fin>>temp->courseName>>temp->credits;
		if (cin.fail())
		{
			cout<<char(7)<<endl;
			cout<<"\n课程学分输入有误!文件读入失败!"<<endl;
			fin.close();
			return;
		}
		cout<<temp->courseName<<' '<<temp->credits<<' ';
		temp->next=NULL;
		endv->next=temp;
		endv=temp;
	}
	cout<<endl;
	student *tem;
	courseOfStudent *course,*endvv;
	while (!fin.eof())
	{
		tem=new student;
		temp=headOfCourse;
		if (tem==NULL)
		{
			cout<<char(7)<<endl;
			cout<<"\n申请内存失败!不能读入学生信息!"<<endl;
			fin.close();
			return;
		}
		tem->studentName[0]=0;
		tem->studentNumber[0]=0;
		fin>>tem->studentName>>tem->studentNumber;
		if (tem->studentName[0]==0)
		{
			fin.close();
			return;
		}
		cout<<tem->studentName<<' '<<tem->studentNumber<<' ';
		tem->courses=NULL;
		tem->pNameLc=NULL;
		tem->pNameRc=NULL;
		tem->pNumberLc=NULL;
		tem->pNumberRc=NULL;
		tem->pScoreLc=NULL;
		tem->pScoreRc=NULL;
		for (i=0;i<coursenumber;i++)
		{
			course=new courseOfStudent;
			if (course==NULL)
			{
				cout<<char(7)<<endl;
				cout<<"\n申请内存失败!不能读入学生信息!"<<endl;
				fin.close();
				return;
			}
			strcpy(course->courseName,temp->courseName);
			fin>>course->score;
			if (cin.fail())
			{
				cout<<char(7)<<endl;
				cout<<"\n学生"<<tem->studentName<<"的课程成绩输入有误!文件读入失败!"<<endl;
				fin.close();
				return;
			}
			cout<<course->score<<' ';
			course->next=NULL;
			if (tem->courses==NULL)
			{
				tem->courses=course;
				endvv=course;
			}
			else
			{
				endvv->next=course;
				endvv=course;
			}
			temp=temp->next;
		}
		StudentAdd_Name(headOfStudent,tem);
		StudentAdd_Number(headOfStudent,tem);
		cout<<endl;
	}
}

void fileOut(student *&headOfStudent,courselist *&headOfCourse,char filename[])
{
	filename[0]=0;
	char s[100];
	cin.ignore(100,'\n');
	cout<<"\n所有学生信息将被保存到文件中!继续请按回车,放弃请按其他键并回车";
	cin.getline(s,100);
	if (s[0]) 
	{
		cout<<char(7);
		cout<<"\n学生信息保存到文件被终止!"<<endl;
		return;
	}
	cout<<"\n请输入您想要将学生信息保存到的文件名,如 Student_Output.txt \n"
		<<"请输入:";
	cin>>s;
	fout.open(s);
	if (!fout)
	{
		cout<<char(7);
		cout<<"\n创建文件失败!学生信息不能保存到 "<<s<<" 中!"<<endl;
		return;
	}
    int coursesnumber=0;
	courselist *temp=headOfCourse;
	while (temp)
	{
		coursesnumber++;
		temp=temp->next;
	}
	fout<<setw(25)<<setiosflags(ios::left)<<coursesnumber;
	temp=headOfCourse;
	while (temp)
	{
		fout<<temp->courseName<<' '<<temp->credits<<"   ";
		temp=temp->next;
	}
	fout<<endl;

	if (headOfStudent)
		Fout(headOfStudent);

	fout.close();

	strcpy(filename,s);
}

void Fout(student *&head)
{
	if (head->pNameLc)
		Fout(head->pNameLc);

	fout<<setw(15)<<setiosflags(ios::left)<<head->studentName
		<<setw(10)<<head->studentNumber;
	courseOfStudent *p=head->courses;
	while (p)
	{
		fout<<setw(8)<<setiosflags(ios::fixed)<<setprecision(2)<<p->score;
		p=p->next;
	}
	fout<<endl;

	if (head->pNameRc)
		Fout(head->pNameRc);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -