#include<fstream>
#include<cstring>
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
using namespace std;
#define OK 1
#define ERROR 0
#define OVERFLOW -1
#define List_INIT_SPACE 100 //存储空间初始分配量
#define List_INC_SPACE 10 //存储空间分配增量
int node = 1;
typedef struct Student
{
int age; //年龄
int student_id; //学号
int course_id; //题目编号
char student_name[40]; //姓名
char sex[10]; //性别
char Class[40]; //班级
char specialty[40]; //专业
char course_name[40]; //题目名称
char keyword[40]; //关键词
char technology[40];
}Student;
typedef struct STent_list
{
struct Student *stu; //存储空间基址
int length; //当前长度
int listsize; //当前分配的存储容量(以sizeof(Student)为单位
}STent_list;
int STent_listInit(STent_list &S)
{
//在内存中分配空间
S.stu = (Student*)malloc(List_INC_SPACE*sizeof(Student));
if(!S.stu) exit(OVERFLOW); //存储空间分配失败
//构造一个空的线性表
S.length = 0;
S.listsize = List_INC_SPACE; //初始存储容量
return OK;
}//函数STent_listInit结束
void write(STent_list &S, int n)
{
fstream myfile;
myfile.open("student.txt",ios::out|ios::binary);
//fstream myfile("student.txt",ios::out|ios::binary);
if(! myfile)
{
cout<<"该文件不能打开!"<<endl;
abort(); //异常终止函数
}
int count = n;
myfile<<count<<endl<<endl;
for(int i = 0; i <= count; i++)
{
myfile<<(S.stu[i]).student_id<<" "<<(S.stu[i]).student_name<<" "<<(S.stu[i]).sex<<" "<<(S.stu[i]).age<<" "<<(S.stu[i]).Class<<" "<<(S.stu[i]).specialty<<" "<<S.stu[i].course_id<<" "<<S.stu[i].course_name<<" "<<S.stu[i].keyword<<" "<<S.stu[i].technology<<" "<<endl;
}
myfile.close();
}
/**************************************/
//函数名: read(STent_list &S)
//参数: (传入) STent_list S顺序表
//返回值: int型,返回1表示创建成功,0表示失败
//功能: 读取顺序表中的内容
/*************************************/
int read(STent_list &S)
{
fstream myfile;
myfile.open("student.txt",ios::in|ios::binary);
//fstream myfile("student.txt",ios::out|ios::binary);
if(! myfile)
{
cout<<"该文件不能打开"<<endl;
abort();
}
int count;
myfile.seekg(0);
myfile>>count;
for(int i = 0; i <= count; i++)
{
myfile>>S.stu[i].student_id>>S.stu[i].student_name>>S.stu[i].sex>>S.stu[i].age>>S.stu[i].Class>>S.stu[i].specialty>>S.stu[i].course_id>>S.stu[i].course_name>>S.stu[i].keyword>>S.stu[i].technology;
}
myfile.close();
return count;
}
/*****************************************/
//函数名:add(STent_list &S)
//参数: (传入)STent_list S,顺序表
//返回值: 空
//功能: 添加学生信息
/*****************************************/
void add(STent_list &S)
{
int n = read(S);
int i = 0;
char sign = 'F';
cout<<endl<<"请输入增加的学生的相关信息:"<<endl;
while(sign != 'N')
{
loop:
cout<<"学号:";
cin>>S.stu[i].student_id;
cout<<endl;
int c = 0;
while(c < i)
{
c++;
if(S.stu[i].student_id == S.stu[i-c].student_id)
{
cout<<"你输入的学号已经存在!请重新输入" <<endl;
goto loop;
}
}
cout<<"姓名:";
cin>>S.stu[i].student_name;
cout<<endl;
cout<<"性别:";
cin>>S.stu[i].sex;
cout<<endl;
cout<<"年龄:";
cin>>S.stu[i].age;
cout<<endl;
cout<<"班级:";
cin>>S.stu[i].Class;
cout<<endl;
cout<<"专业:";
cin>>S.stu[i].specialty;
cout<<endl;
cout<<"题目编号:";
cin>>S.stu[i].course_id;
cout<<endl;
cout<<"题目名称:";
cin>>S.stu[i].course_name;
cout<<endl;
cout<<"关键词:";
cin>>S.stu[i].keyword;
cout<<endl;
cout<<"实现技术:";
cin>>S.stu[i].technology;
cout<<endl;
cout<<"提示:是否继续写入学生信息?(Y/N):";
cin>>sign;
i++;
}
write(S, i);
}
/*****************************************************/
//函数名: search_student_id(STent_list &S)
//参数: (传入)STent_list S,顺序表
//返回值: 空
//功能: 根据学号查找学生信息
/*****************************************************/
void search_student_id(STent_list &S)
{
int n = read(S);
int s;
int i = 0;
cout<<endl<<"查找学生信息:"<<endl;
cout<<"请输入需要查找学生的学号:"<<endl;
cin>>s;
while((S.stu[i].student_id - s) != 0 && i < n) i++;
if(i == n)
{
cout<<"提示: 对不起,无法找到该学生的信息! "<<endl;
}
else
{
cout<<"***************************"<<endl;
cout<<"学号:"<<S.stu[i].student_id<<endl;
cout<<"姓名:"<<S.stu[i].student_name<<endl;
cout<<"性别: "<<S.stu[i].sex<<endl;
cout<<"年龄:"<<S.stu[i].age<<endl;
cout<<"班级:"<<S.stu[i].Class<<endl;
cout<<"专业:"<<S.stu[i].specialty<<endl;
cout<<"题目编号:"<<S.stu[i].course_id<<endl;
cout<<"题目名称:"<<S.stu[i].course_name<<endl;
cout<<"关键词:"<<S.stu[i].keyword<<endl;
cout<<"实现技术:"<<S.stu[i].technology<<endl;
}
}
/*******************************************************/
//函数名: search_student_name(STent_list &S)
//参数: (传入)STent_list S,顺序表
//返回值: 空
//功能: 根据学生姓名查找学生信息
/*******************************************************/
void search_student_name(STent_list &S)
{
int n = read(S);
char a[100];
cout<<"请输入需要查找的姓名:"<<endl;
cin>>a;
for(int i = 0; i < n; i++)
if(strcmp(S.stu[i].student_name, a) == 0)
{
cout<<"****************************"<<endl;
cout<<"学号:"<<S.stu[i].student_id<<endl;
cout<<"姓名:"<<S.stu[i].student_name<<endl;
cout<<"性别: "<<S.stu[i].sex<<endl;
cout<<"年龄:"<<S.stu[i].age<<endl;
cout<<"班级:"<<S.stu[i].Class<<endl;
cout<<"专业:"<<S.stu[i].specialty<<endl;
cout<<"题目编号:"<<S.stu[i].course_id<<endl;
cout<<"题目名称:"<<S.stu[i].course_name<<endl;
cout<<"关键词:"<<S.stu[i].keyword<<endl;
cout<<"实现技术:"<<S.stu[i].technology<<endl;
}
}
/*******************************************************/
//函数名: search_course_id(STent_list &S)
//参数: (传入)STent_list S,顺序表
//返回值: 空
//功能: 根据学生课程设计的编号查找
/*******************************************************/
void search_course_id(STent_list &S)
{
int n = read(S);
int b;
int i = 0;
cout<<"请输入需要查找的题目编号:"<<endl;
cin>>b;
while((S.stu[i].course_id - b) != 0 && i < n) i++;
if(i == n)
{
cout<<"提示:对不起,无法找到该信息!"<<endl;
}
else
{
for(i = 0; i < n; i++)
if(S.stu[i].course_id - b == 0)
{
cout<<"******************************"<<endl;
cout<<"学号:"<<S.stu[i].student_id<<endl;
cout<<"姓名:"<<S.stu[i].student_name<<endl;
cout<<"性别: "<<S.stu[i].sex<<endl;
cout<<"年龄:"<<S.stu[i].age<<endl;
cout<<"班级:"<<S.stu[i].Class<<endl;
cout<<"专业:"<<S.stu[i].specialty<<endl;
cout<<"题目编号:"<<S.stu[i].course_id<<endl;
cout<<"题目名称:"<<S.stu[i].course_name<<endl;
cout<<"关键词:"<<S.stu[i].keyword<<endl;
cout<<"实现技术:"<<S.stu[i].technology<<endl;
}
}
}慕的地6079101
慕粉1642151114
慕粉1642151114