数据结构-学生管理系统-线性表-顺序表

1.内容

项目名称:学生信息管理系统项目内容:设计一个学生信息管理系统,实现对学生基本信息的添加、删除、修改和查询等操作,其中每个学生信息包含学号,姓名和绩点。要求系统完成以下主要功能:
(1)显示:显示当前所有学生信息记录;
(2)录入:从键盘输入一条学生信息记录,插入到表中指定的位置;
(3)查找:根据学号或者记录的位置查找学生的各项信息;
(4)删除:删除指定位置的学生信息记录;
(5)更新:更新指定位置的学生信息记录;
(6)统计:统计表中学生人数。
(7) 排序:按照学号或者绩点进行排序
(8)清空:清空表中所有记录

2.顺序表的实现


#include<iostream>
#include"SqList.hpp"
#include<cstring>
using namespace std;

int main() {
 SqList<student>* list = new SqList<student>();
 while (1) {
  system("cls");
  show_list();
  int choise;
  cin >> choise;
  if (!(choise < 10 && choise>0))
   continue;
  switch (choise) {
  case 1: {
   show_student(*list);
   system("pause");
   break;
  }
  case 2: {
   set_massage(*list);
   system("pause");
   break;
  }
  case 3: {
   find(*list);
   system("pause");
   break;
  }
  case 4: {
   delete_him(*list);
   system("pause");
   break;
  }
  case 5: {
   change(*list);
   system("pause");
   break;
  }
  case 6: {
   sum_student(*list);
   system("pause");
   break;
  }
  case 7: {
   sort(*list);
   system("pause");
   break;
  }
  case 8: {
   clear_all(*list);
   system("pause");
   break;
  }
  case 9: {
   return 0;
   break;
  }
  }
 }
 system("pause");
 return 0;
}

结构体定义:


typedef struct student {
 student() {
 }
 string name;
 string number;
 double performance;
public:
 void set_student() {
  cout << "输入姓名:" << endl;
  cin >> this->name;
  cout << "输入学号:" << endl;
  cin >> this->number;
  cout << "输入绩点:" << endl;
  cin >> this->performance;
 }
}student;

成员函数的实现:


void print_student(const student& a) {
 cout << "姓名:" << a.name << " 学号:" << a.number << " 绩点:" << a.performance << endl;
}
void show_list() {
 cout << "1.显示:显示当前所有学生信息记录;" << endl;
 cout << "2.录入:从键盘输入一条学生信息记录,插入到表中指定的位置;" << endl;
 cout << "3.查找:根据学号或者记录的位置查找学生的各项信息;" << endl;
 cout << "4.删除:删除指定位置的学生信息记录;" << endl;
 cout << "5.更新:更新指定位置的学生信息记录;" << endl;
 cout << "6.统计:统计表中学生人数。" << endl;
 cout << "7. 排序:按照学号或者绩点进行排序." << endl;
 cout << "8.清空:清空表中所有记录." << endl;
 cout << "9.退出。" << endl;
}
//显示:显示当前所有学生信息记录;
void show_student(SqList<student>& a) {
 if (a.IsEmpty())
  cout << "没有学生" << endl;
 else {
  a.Traverse(print_student);
 }
}
//录入:从键盘输入一条学生信息记录,插入到表中指定的位置;
void set_massage(SqList<student>& a) {
 system("cls");
 int sign = 0;
 cout << "1.在尾部插入" << endl;
 cout << "2.输入插入位置" << endl;
 cin >> sign;
 if (sign == 1) {
  student m;
  m.set_student();
  a.InsertElem(m);
 }
 else if (sign == 2) {
  cout << "输入位置:(在1-" << a.GetLength() << "之间)" << endl;
  int i = 0;
  cin >> i;
  if (i<1 && i>a.GetLength()){
   cout << "输入错误" << endl;
  }
  else {
   student m;
   m.set_student();
   a.InsertElem(i, m);
  }
 }
 else {
  cout << "输入错误" << endl;
 }
 
}
//查找:根据学号或者记录的位置查找学生的各项信息;
void find(SqList<student>& a) {
 system("cls");
 int sign = 0;
 student s;
 cout << "1.位置查找" << endl;
 cout << "2.学号查找" << endl;
 cout << "3.姓名查找" << endl;
 cin >> sign;
 if (sign == 1) {
  int m = 0;
  cout << "输入位置:(在1-" << a.GetLength() << "之间)" << endl;
  cin >> m;
  if (m > 0 && m <= a.GetLength()) {
   a.GetElem(m, s);
  }
  else {
   cout << "位置查找错误" << endl;
  }
  print_student(s);
 }
 else if (sign == 2) {
  string m;
  int ok = 0;
  cout << "输入学号:" << endl;
  cin >> m;
  for (int i = 1; i <= a.GetLength(); i++) {
   a.GetElem(i, s);
   if (s.number == m) {
    print_student(s);
    ok = 1;
    break;
   }
  }
  if (ok == 0)
   cout << "没找到" << endl;
 }
 else if (sign == 3) {
  string m;
  int ok = 0;
  cout << "输入姓名:" << endl;
  cin >> m;
  for (int i = 1; i <= a.GetLength(); i++) {
   a.GetElem(i, s);
   if (s.name == m) {
    print_student(s);
    ok = 1;
    break;
   }
  }
  if (ok == 0)
   cout << "没找到" << endl;
 }
 else {
  cout << "输入错误,请按序号输入" << endl;
 }
}
//删除:删除指定位置的学生信息记录;
void delete_him(SqList<student>& a) {
 int sign = 0;
 student s;
 cout << "输入位置:(在1-" << a.GetLength() << "之间)" << endl;
 cin >> sign;
 if (sign > 0 && sign <= a.GetLength()) {
  a.DeleteElem(sign, s);
  cout << "删除成功" << endl;
  cout << "信息:";
  print_student(s);
 }
 else {
  cout << "位置查找错误" << endl;
 }
}
//更新:更新指定位置的学生信息记录;
void change(SqList<student>& a) {
 system("cls");
 int sign = 0;
 student s;
 cout << "输入位置:(在1-" << a.GetLength() << "之间)" << endl;
 cin >> sign;
 if (sign > 0 && sign < a.GetLength()) {
  a.GetElem(sign, s);
  cout << "原学生信息为:" << endl;
  print_student(s);
  cout << "更改为:" << endl;
  s.set_student();
  a.SetElem(sign, s);
  cout << "更新成功" << endl;
 }
 else {
  cout << "输入错误" << endl;
 }
}
//统计:统计表中学生人数。
void sum_student(SqList<student>& a) {
 system("cls");
 cout << "学生人数为:" << a.GetLength() << endl;
}
//排序:按照学号或者绩点进行排序
void sort(SqList<student>& a) {
 system("cls");
 if (a.GetLength() < 2) {
  cout << "人数过少,无法排序" << endl;
  return;
 }
 int sign = 0;
 cout << "1.按学号排列" << endl;
 cout << "2.按绩点排列" << endl;
 cin >> sign;
 if (sign < 1 && sign>2) {
  cout << "输入错误" << endl;
 }
 else {
  for (int i = 0; i < a.GetLength(); i++) {
   for (int j = 1; j < a.GetLength() - i; j++) {
    student s1, s2;
    a.GetElem(j, s1);
    a.GetElem(j + 1, s2);
    if (sign == 2) {
     if (s1.performance > s2.performance)
      a.change_two(j);
    }
    else {
     if (s1.number > s2.number) {
      a.change_two(j);
     }
    }
   }
  }
 }
 cout << "排序完成" << endl;
}
//清空:清空表中所有记录
void clear_all(SqList<student>& a) {
 for (int i = a.GetLength(); i > 0; i--) {
  student s;
  a.DeleteElem(i, s);
 }
 cout << "清空完成" << endl;
}

头文件SqList.hpp:

#pragma once
#ifndef __SQ_LIST_H__
#endif
#define __SQ_LIST_H__
// ANSI C++标准库头文件
#include <cstring>     // 标准串操作
#include <iostream>     // 标准流操作
using namespace std;
// 宏定义
#define DEFAULT_SIZE 1000   // 缺省元素个数
#define DEFAULT_INFINITY 1000000 // 缺省无穷大
template <class ElemType>
class SqList
{
protected:
 // 顺序表的数据成员
 int length;     // 顺序表的当前长度 
 int maxLength;    // 顺序表的最大容量
 ElemType* data;   // 元素存储空间的首地址 
public:
// 顺序表的函数成 构造一个空表 
 SqList(int size = DEFAULT_SIZE) {
  this->length = 0;
  this->maxLength = size;
  data = new ElemType[DEFAULT_SIZE];
 }
// 根据数组v的内容构造顺序表 
 template <class ElemType>
 SqList(ElemType v[], int n, int size = DEFAULT_SIZE) {
  this->data = new ElemType(DEFAULT_SIZE);
  this->length = n;
  this->maxLength = size;
  for (int i = 0; i < n; i++) {
   ElemType* a = new ElemType;
   *a = v[i];
   data[i] = *a;
  }
 }
 // 析构函数
 virtual ~SqList() {
 }
 // 取顺序表长度 
 int GetLength() const {
  return this->length;
 }
 // 判断顺序表是否为空
 bool IsEmpty() const {
  return this->length == 0;
 }
 // 将顺序表清空
 void Clear() {
  delete[] data;
  this->length = 0;
 }
 // 遍历顺序表
 template <class ElemType>
 void Traverse(void (*Visit)(const ElemType&)) const {
  for (int i = 0; i < this->length; i++) {
   (*Visit)(data[i]);
  }
  if (this->length == 0)
   cout << "没有成员" << endl;
 }
 // 元素定位,求指定元素在顺序表中的位置
 template <class ElemType>
 int LocateElem(const ElemType& e) {
  int sign = 0;
  for (int i = 0; i < this->length; i++) {
   if (this->data[i] == e) {
    sign = 1;
    return i+1;
   }
  }
  if (sign == 0)
   cout << "没有找到" << endl;
  return -1;
 }
 // 取顺序表中第i个元素的值
 template <class ElemType>
 int GetElem(int i, ElemType& e) const {
  
  if ((i - 1 <= this->length-1)&&i>=0)
   e = data[i - 1];
  else
   cout << "没有找到" << endl;
  return 0;
 }
 // 修改顺序表中第i个元素的值
 template <class ElemType>
 int SetElem(int i, const ElemType& e) {
  if ((i  <= this->length) && i >= 0)
   this->data[i-1] = e;
  else
   cout << "没有找到" << endl;
  return 0;
 }
 // 删除顺序表中第i个元素  
 template <class ElemType>
 int DeleteElem(int i, ElemType& e) {
  e = this->data[i - 1];
  if ((i <= this->length) && i >= 0) {
   for (int j = i - 1; j < this->length - 1; j++)
    this->data[j] = this->data[j + 1];
   this->length--;
  }
  else
   cout << "没有找到" << endl;
  return 0;
 }
 // 在顺序表第i个位置插入元素
 template <class ElemType>
 int InsertElem(int i, const ElemType& e) {
  if (((i-1 <= this->length) && i > 0)) {
   for (int j = this->length; j > i - 1; j--) {
    this->data[j] = this->data[j - 1];
   }
   this->data[i - 1] = e;
  }
  else
   cout << "没有找到" << endl;
  this->length++;
  return 0;
}
void change_two(int m) {
  ElemType a;
  a=this->data[m -1];
  this->data[m-1]= this->data[m + 1-1];
  this->data[m + 1-1]=a;
 }
 // 在顺序表表尾插入元素
 template <class ElemType>
 int InsertElem(const ElemType& e) {
  this->data[this->length] = e;
  this->length++;
  return 0;
 }
 // 复制构造函数
 template <class ElemType>
 SqList(const SqList<ElemType>& sa) {
  for (int i = 0; i < sa.GetLength(); i++) {
   this->data[i] = sa.data[i];
  }
  this->length = sa.GetLength();
 }
 // 赋值语句重载
template <class ElemType>
SqList<ElemType>& operator =(const SqList<ElemType>& sa) {
 SqList(sa);
 return *this;
}
};
  • 16
    点赞
  • 67
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值