#pragma once
#include<iostream>
#include<fstream>
#define FILENAME "stulist.txt"
using namespace std;
struct StuNode
{
StuNode(int num, int math, int eng, int yuwen, int sum);
int num; // sutdent number
int math, eng, yuwen; // module name
int sum; // total marks
StuNode* nextstu; // node link
};
class SInfo
{
StuNode* StuListHead;
public:
bool m_FileIsEmpty;
//学生数组指针
StuNode** m_StuArry;
//记录学生人数
int m_StuNum;
//初始化学生
void init_Stu();
//统计文件中人数
int get_StuNum();
SInfo(); //Constructor 构造函数 初始化数据 显示菜单
~SInfo(); //Destructor 析构
void StuInsert(int snum, int smath, int seng, int syuwen); //Insert student information 3、附加的学生信息
};
StuNode::StuNode(int num, int math, int eng, int yuwen, int sum)
{
this->eng = eng;
this->math = math;
this->num = num;
this->sum = sum;
this->yuwen = yuwen;
}
SInfo::SInfo()
{
//初始化属性
//文件不存在
ifstream ifs;
ifs.open(FILENAME, ios::in);//读文件
//1.文件不存在的情况
if (!ifs.is_open())
{
//cout << "文件不存在" << endl;
//初始化属性
//初始化记录人数
this->m_StuNum = 0;
//初始化数组指针
this->m_StuArry = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//2.文件存在 数据为空
char ch;
ifs >> ch;
if (ifs.eof())
{
//文件为空
//cout << "文件为空!" << endl;
//初始化属性
//初始化记录人数
this->m_StuNum = 0;
//初始化数组指针
this->m_StuArry = NULL;
//初始化文件是否为空
this->m_FileIsEmpty = true;
ifs.close();
return;
}
//3、文件存在,并记录属性
int num = this->get_StuNum();
this->m_StuNum = num;
//开辟空间
this->m_StuArry = new StuNode * [this->m_StuNum];
//将文件中的数据,存到数组中
this->init_Stu();
}
//insert student information 3、附加的学生信息
void SInfo::StuInsert(int snum, int smath, int seng, int syuwen)
{
//合函数
int ssum = 0;
//添加
//计算添加新的空间大小
int newSize = this->m_StuNum + 1;//新空间大小 = 原来记录人数 + 新增人数
//开辟新空间
StuNode** newSpace = new StuNode * [newSize];
//将原来空间下数据,拷贝到新空间下
if (this->m_StuArry != NULL)
{
for (int i = 0; i < this->m_StuNum; i++)
{
newSpace[i] = this->m_StuArry[i];
}
}
cout << "Please enter student information:" << endl;
cout << "Num >> Math >> English >> Chinese " << endl;
cin >> snum >> smath >> seng >> syuwen;
StuNode* student = NULL;
//判断是否输入正确
if (cin.good())
{
ssum = smath + seng + syuwen;
student = new StuNode(snum, smath, seng, syuwen, ssum);
newSpace[this->m_StuNum + 1] = student;
//释放原有空间
delete[] this->m_StuArry;
//更改新空间的指向
this->m_StuArry = newSpace;
//更新新的职工人数
this->m_StuNum = newSize;
this->m_FileIsEmpty = false;
for (int i = 0; i < this->m_StuNum; i++)
{
cout << this->m_StuArry[i]->num << " "
<< this->m_StuArry[i]->math << " "
<< this->m_StuArry[i]->eng << " "
<< this->m_StuArry[i]->yuwen << " "
<< this->m_StuArry[i]->sum << " "
<< endl;
}
cout << endl;
cout << "The addition was successful" << endl;
cout << endl;
}
else
{
cout << endl;
cout << "Wrong data type entered,will return to the menu" << endl;
cin.clear();
while (cin.get() != '\n')
continue;
}
system("pause");
system("cls");
}
//统计文件中的人数
int SInfo::get_StuNum()
{
ifstream ifs;
ifs.open(FILENAME, ::ios::in);//打开文件 读
int num; // sutdent number
int math, eng, yuwen; // module name
int sum; // total marks
int Num = 0;
while (ifs >> num && ifs >> math && ifs >> eng && ifs >> yuwen && ifs >> sum)
{
//统计人数变量
Num++;
}
return Num;
}
//初始化学生
void SInfo::init_Stu()
{
ifstream ifs;
ifs.open(FILENAME, ios::in);
int num; // sutdent number
int math, eng, yuwen; // module name
int sum; // total marks
int index = 0;
while (ifs >> num && ifs >> math && ifs >> eng && ifs >> yuwen && ifs >> sum)
{
StuNode* student = NULL;
student = new StuNode(num, math, eng, yuwen, sum);
this->m_StuArry[index] = student;
index++;
}
//关闭文件
ifs.close();
}
SInfo::~SInfo()
{
}
int main()
{
SInfo wm;
wm.StuInsert(0,0,0,0);
system("pause");
return 0;
}

c++在vs2019中运行代码163行显示访问权限错误
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- 技术专家团-小桥流水 2021-10-26 20:52关注
newSpace[this->m_StuNum + 1] = student 这里应该是:
newSpace[this->m_StuNum ] = student本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报