中国矿业大学计算机学院实验报告
第二篇:实验1 线性表及其应用实验报告
暨南大学本科实验报告专用纸
课程名称 数据结构 成绩评定
实验项目名称 线性表及其应用 指导教师王晓明
实验项目编号实验一实验项目类型 综合性实验地点南海楼601 学生姓名 朱芷漫 学号 2010051875
学院 信息科学技术学院 系 计算机 专业 计算机科学与技术
实验时间 20##年9月7日18:30午~9月7日20:30午温度 ℃湿度
一、实验目的和要求
实验目的:熟练掌握线性表基本操作的实现及应用
实验要求:在上机前写出全部源程序完毕并调试完毕。
二、实验原理和主要内容
1.建立4个元素的顺序表SqList={2,3,4,5},实现顺序表的基本操作;在SqList={2,3,4,5}的元素4与5之间插入一个元素9,实现顺序表插入的基本操作;在SqList={2,3,4,9,5}中删除指定位置(i=3)上的元素,实现顺序表删除的操作。
2.利用顺序表完成一个班级的一个学期的课程的管理:能够增加、删除、修改学生的成绩记录。
三、主要仪器设备
PC机,Windows XP操作平台,Visual C++
四、 调试分析
学生课程管理系统的调试过程中发现一些错误,主要是参数设置的问题,经过修改,错误得到排除。
五、 测试结果
1. 顺序表
2. 学生课程管理系统
附录(源程序)
1.顺序表的操作
#include <iostream.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef int ElemType;
#define LIST_INIT_SIZE 10
#define LISTINCREMENT 2
typedef struct shunxubiao{
ElemType *list;
int size;
int Maxsize;
}SqList;
int InitList_Sq(SqList &L)
{
// 构造一个空的线性表L。
L.list = new ElemType[LIST_INIT_SIZE];
if (!L.list) return OVERFLOW; // 存储分配失败
L.size = 0; // 长度为0
L.Maxsize = LIST_INIT_SIZE; // 初始存储容量
return OK;
} // InitList_Sq
int InsertList_Sq(SqList &L, int i, ElemType e)
{
ElemType *p,*q;
if (i < 1 || i > L.Maxsize+1) return ERROR;
q = &(L.list[i-1]); // q指示插入位置
for (p=&(L.list[L.Maxsize-1]); p >= q; --p)
*(p+1) = *p;
// 插入位置及之后的元素右移
*q = e; // 插入e
++L.size; // 表长增1
return OK;
} // ListInsert_Sq
int LocateElem_Sq(SqList L, ElemType e) {
// 在顺序表中查询数据元素e,若存在,则返回它的位序,否则返回 0
int i = 1; // i 的初值为第 1 元素的位序
ElemType *p = L.list; // p 的初值为第 1 元素的存储位置
while (i <= L.size && *p!=e)
{
++i;
++p;
}
if (i <= L.size) return i;
else return 0;
}
Status InsertList_Sq(SqList &L,ElemType e,ElemType f,ElemType g)
{
int i=LocateElem_Sq(L,e);
int j=LocateElem_Sq(L,f);
if(i==j-1)
{
InsertList_Sq(L,j,g);
return OK;
}
else return ERROR;
}
int GetList_Sq(SqList L,int i)
{
if(i>0 && i<=L.size)
{
return L.list[i];
}
else
return ERROR;
}
Status ListDelete_Sq(SqList &L, int i, ElemType &e)
{
ElemType *p,*q;
if ((i < 1) || (i > L.Maxsize)) return ERROR;
p = &(L.list[i-1]); // p为被删除元素的位置
e = *p; // 被删除元素的值赋给e
q = L.list+L.size-1; // 表尾元素的位置
for (++p; p <= q; ++p)
*(p-1) = *p; // 被删除元素之后的元素左移
--L.size; // 表长减1
return OK;
} // ListDelete_Sq
void Create_Sq(SqList &L)
{
cout<<"创建顺序表"<<endl;
cout<<"请输入元素个数:";
int count;
cin>>count;
for(int i=0;i<count;i++)
{
cout<<"请输入第"<<i+1<<"个数:";
cin>>L.list[i];
++L.size;
}
}
void Print_Sq(SqList &L)
{
cout<<"输出顺序表:"<<endl;
for(int i=0;i<L.size;i++)
cout<<L.list[i]<<" ";
}
void main()
{
SqList myList;
ElemType e,f,g,sc;
InitList_Sq(myList);
Create_Sq(myList);
cout<<"请输入要插入顺序表的元素:"<<endl;
cin>>g;
cout<<"请输入新插入元素在顺序表中哪两个元素之间:"<<endl;
cin>>e>>f;
if(!InsertList_Sq(myList,e,f,g))
cout<<"插入的位置不对!"<<endl;
cout<<"删除一个元素,请输入要删除的位序:"<<endl;
int wx;
cin>>wx;
if(!ListDelete_Sq(myList,wx,sc))
cout<<"删除元素失败!"<<endl;
Print_Sq(myList);
}
2.学生课程管理系统
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
const int LIST_INIT_SIZE=3;
const int LISTINCREMENT=5;
struct student
{
int StuNum;
int CouNum;
int Score;
char *name;
};
class StuCourse
{
student *L;
int length;
int listsize;
public:
StuCourse();
void ListAdd(student e);
void ListDelete(int i);
void ListRewrite(student e);
void ListPrint(int i);
void ListPrintAll();
void copy(student *a,student *b);
};
StuCourse::StuCourse()
{
L=new student[LIST_INIT_SIZE];
if(!L)
{
cout<<"内存不足"<<endl;
}
else
{
length=0;listsize=LIST_INIT_SIZE;
}
}
void StuCourse::ListAdd(student e)
{
student *p;
int t,n,i;
bool flag=true;
if(e.StuNum<1) cout<<"插入位置错误"<<endl;
else
{
for(t=0;t<length&&(L+t)->StuNum<e.StuNum;t++);
if((L+t)->StuNum==e.StuNum) cout<<"此学号已存在"<<endl;
else
{
if(length==listsize)
{
p=new student[listsize+LISTINCREMENT];
if(!p)
{
cout<<"内存不足"<<endl;flag=false;
}
else
{
for(i=0;i<length;i++) copy(p+i,L+i);
delete L;L=p;listsize+=LISTINCREMENT;
}
}
if(flag==true)
{
if(t<length)
for(n=length;n>t;n--) copy(L+n,L+n-1);
copy(L+t,&e);length++;
}
}
}
}
void StuCourse::ListDelete(int i)
{
int t;
for(t=0;t<length&&(L+t)->StuNum!=i;t++);
if(t==length) cout<<"此记录不存在"<<endl;
else
{
while(t<length) copy(L+t,L+t+1);t++;
length--;
}
}
void StuCourse::ListRewrite(student e)
{
int t;
for(t=0;t<length&&(L+t)->StuNum!=e.StuNum;t++);
if((L+t)->StuNum==e.StuNum) copy(L+t,&e);
else cout<<"此记录不存在"<<endl;
}
void StuCourse::ListPrint(int i)
{
int t;
for(t=0;t<length&&(L+t)->StuNum!=i;t++);
if((L+t)->StuNum!=i) cout<<"此记录不存在"<<endl;
else
{
cout<<"学号\t姓名\t\t课程号\t成绩"<<endl;
cout<<(L+t)->StuNum<<'\t'<<(L+t)->name<<'\t'<<(L+t)->CouNum<<"\t\t"<<(L+t)->Score<<endl;
}
}
void StuCourse::ListPrintAll()
{
int i;
cout<<"学号\t姓名\t\t课程号\t成绩"<<endl;
for(i=0;i<length;i++)
cout<<(L+i)->StuNum<<'\t'<<(L+i)->name<<'\t'<<(L+i)->CouNum<<"\t\t"<<(L+i)->Score<<endl;
}
void StuCourse::copy(student *a,student *b)
{
a->StuNum=b->StuNum;
a->CouNum=b->CouNum;
a->Score=b->Score;
a->name=b->name;
}
void write(int a,char *p,int b,int c,student &d)
{
d.StuNum=a;
d.name=p;
d.CouNum=b;
d.Score=c;
}
int main()
{
int i,j,k,n=0;
student e;
char c='a';
char name[50][20];
StuCourse ClassA;
while(c!='q'&&c!='Q')
{
cout<<"\t欢迎使用学生成绩管理系统"<<endl;
cout<<"A.打印成绩单\t\tB.查询成绩"<<endl;
cout<<"C.添加记录\t\tD.删除记录"<<endl;
cout<<"E.修改记录\t\tQ.退出"<<endl;
c=getch();
switch(c)
{
case 'a':
case 'A':ClassA.ListPrintAll();getch();break;
case 'b':
case 'B':
cout<<"请输入要查询的学号:";cin>>i;ClassA.ListPrint(i);getch();break;
case 'c':
case 'C':
cout<<"请输入添加记录:";cin>>i>>name[n]>>j>>k;write(i,name[n],j,k,e);
ClassA.ListAdd(e);n++;getch();break;
case 'd':
case 'D':
cout<<"请删除的学号:";cin>>i;ClassA.ListDelete(i);getch();break;
case 'e':
case 'E':
cout<<"请输入要修改的记录:";cin>>i>>name[n]>>j>>k;write(i,name[n],j,k,e);
ClassA.ListRewrite(e);n++;getch();break;
case 'q':
case 'Q':break;
default:cout<<"错误选择"<<endl;getch();
}
system("cls");
}
return 0;
}