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

📄 stuinfo.cpp

📁 数据结构习题答案完全版,对本科生一定有帮助
💻 CPP
字号:
#include<iostream.h>
#include<stdio.h>
#include<string.h>

#define LIST_INIT_SIZE 100 // 存储空间初始分配
#define LISTINCREMENT 10 //存储空间分配增量
#define OK 1
#define ERROR 0
#define OVERFLOW -2

typedef struct student{
	char *no;
	char *name;
	float score;
}student;

//学生信息结构体类型

typedef struct{
	student *stu;
	int length;
	int listsize;
}sqlist;

//顺序表类型

typedef int status;

status InitList(sqlist &sl);
status creat(sqlist &sl);
status insertlist(sqlist &sl,student elem,int pos);
status search(sqlist &sl,char *s);
status dele_elem(sqlist &sl,int pos,student *elem);
status dele_elem(sqlist &sl,char *s,student *elem);
status out_list(sqlist sl);

status InitList(sqlist &sl)
{
	sl.stu = new student[LIST_INIT_SIZE];
	if(!sl.stu) 
		return OVERFLOW;
	sl.length=0;
	sl.listsize=LIST_INIT_SIZE;
	return OK;
}

status creat(sqlist &sl)
{
	char flag='y';
	student *s;
	s = new student();
	int i=1;
	while(flag=='y'||flag=='Y')
	{
		if(i>sl.listsize)
		{
			cout<<"数据量过大无法储"<<endl;	
			return OVERFLOW;
		}
		
		s->no=new char[11];
		s->name=new char[11];
		if(!s->no&&s->name)
		{	
			cout<<"创建失败"<<endl;
			return ERROR;
		}
		
		cout<<"请输入学号"<<endl;
		cin>>s->no;
		cout<<"请输入姓名"<<endl;
		cin>>s->name;
		cout<<"请输入分数"<<endl;
		cin>>s->score;
		insertlist(sl,*s,i);
		i++;
		cout<<"是否继续输入信息Y/N"<<endl;
		cin>>flag;
	}
	return OK;
}

status insertlist(sqlist &sl,student elem,int pos)
{
	if(sl.length==LIST_INIT_SIZE) 
	{
		cout<<"OVERFLOW"<<endl;
		return ERROR;
	}

	if(pos<1||pos>sl.length+1)
	{
		cout<<"Infeasible"<<endl;
		return ERROR;
	}
    
	int j;

	for(j=sl.length;j>=pos;j--)
		sl.stu[j]=sl.stu[j-1];
	sl.stu[pos-1]=elem;
	sl.length++;
	return OK;
}

status search(sqlist &sl,char *s)
{
	int i=0;
	while(i<=sl.length-1)
	{
		if((sl.stu+i)->no==s) 
		{
			cout<<"学号\t姓名\t成绩\t"<<endl;
			cout<<(sl.stu+i)->no<<"\t"<<(sl.stu+i)->name<<"\t"<<(sl.stu+i)->score<<"\t"<<endl;
			return OK;
		}

		else i++;

	}

	if(i>sl.length-1) 
	{
		cout<<"该记录不存在"<<endl;
        return ERROR;
}

status dele_elem(sqlist &sl,int pos,student &elem)
{
	if(sl.length==0)
	{
		cout<<"Emptylist"<<endl;
		return ERROR;
	}

    if(pos<1||pos>sl.length)
	{
		cout<<"Illeagal"<<endl;
		return ERROR;
	}
    
    elem=sl.stu[pos-1];
	for(int j=pos-1;j<=sl.length-2;j++)
		sl.stu[pos]=sl.stu[pos+1];
	sl.length--;
	return OK;
}

status dele_elem(sqlist &sl,char *s,student &elem)
{
	int i,flag=0,j;
	for(i=0;i<sl.length;i++)
		if(!strcmp((sl.stu+i)->no,s))
		{
			flag=1;
			break;
		}
	if(flag)
	{
        elem=*(sl.stu+i);
		for(j=i;j<sl.length-1;j++)
			*(sl.stu+j)=*(sl.stu+j+1);
		sl.length--;
	}
	else return ERROR;
	return OK;
}

status out_list(sqlist sl)
{
	int i;
	cout<<"学号\t姓名\t成绩"<<endl;
	for(i=0;i<sl.length;i++)
        cout<<(sl.stu+i)->no<<"\t"<<(sl.stu+i)->name<<"\t"<<(sl.stu+i)->score<<endl;
}

⌨️ 快捷键说明

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