2021数据结构课程设计源代码(申请优秀)
实验一
code1.cpp
#include <iostream>
#include "SeqList.h"
#include <string>
using namespace std;
void menu ( ) {
cout << "_______________图书管理系统_________________" << endl;
cout << "(1)根据指定图书个数,逐个输入图书信息" << endl
<< "(2)逐个显示图书表中所有图书的相关信息" << endl
<< "(3)图书插入到图书表中指定的位置" << endl
<< "(4)根据图书的书号,将该图书从图书表中删除" << endl
<< "(5)能统计表中图书个数" << endl
<< "(6)实现图书信息表的图书去重" << endl
<< "(7)二分查找指定价格的书籍(快速排序之后再进行操作)" << endl
<< "(8)利用快速排序按照图书价格降序排序" << endl
<< "(9)实现最贵图书的查找" << endl;
}
void solve ( ) {
int ope = 0 ;
SeqList list;
do {
menu ( ) ;
char ch;
cout << "请选择想要进行的操作:" ; cin >> ch;
ope = int ( ch- '0' ) ;
if ( ope == 1 ) {
list. Adds ( ) ;
cout << "__您输入的内容__" << endl;
list. Print ( ) ;
}
else if ( ope == 2 ) {
list. Print ( ) ;
}
else if ( ope == 3 ) {
book obj;
int idx;
cout << "输入插入位置" << endl; cin >> idx;
cout << "书号:" ; cin >> obj. no;
cout << "书名:" ; cin >> obj. name;
cout << "价格:" ; cin >> obj. price;
list. Insert ( idx, obj) ;
list. Print ( ) ;
}
else if ( ope == 4 ) {
int idx;
string no;
cout << "输入删除的书号" << endl; cin >> no;
idx = list. Find ( no) ;
if ( idx != - 1 ) {
list. Delete ( idx) ;
cout << "删除成功" << endl;
list. Print ( ) ;
}
else {
cout << "查无此书!!" << endl;
}
}
else if ( ope == 5 ) {
cout << "图书的个数为:" << list. Size ( ) << endl;
}
else if ( ope == 6 ) {
list. rmDup ( ) ;
list. Print ( ) ;
}
else if ( ope == 7 ) {
double p; cout << "请输入图书的价格" ;
cin >> p;
list. BiSearch ( 0 , list. Size ( ) , p) ;
}
else if ( ope == 8 ) {
list. Sort ( 0 , list. Size ( ) - 1 ) ;
list. Print ( ) ;
}
else if ( ope == 9 ) {
list. findExp ( ) ;
}
else {
cout << "没有此操作,请重新输入" << endl;
}
cout<< endl << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
} while ( true ) ;
}
int main ( )
{
solve ( ) ;
return 0 ;
}
SeqList.h
#pragma once
#include <iostream>
#include <cmath>
using namespace std;
struct book {
string no;
string name;
double price;
void Print ( ) {
cout << no << "\t\t" << name << "\t\t" ;
printf ( "%.2f\n" , price) ;
}
bool operator == ( book& obj) {
if ( no != obj. no || name != obj. name || abs ( price - obj. price) >= 0.001 )
return false ;
return true ;
}
} ;
class SeqList
{
book* data;
int len;
int maxsize;
public :
SeqList ( ) ;
SeqList ( book arr[ ] , int n) ;
~ SeqList ( ) ;
void Add ( ) ;
void Adds ( ) ;
void Insert ( int idx, book& obj) ;
void Print ( ) ;
void Delete ( int idx) ;
int Find ( string no) ;
void rmDup ( ) ;
void Sort ( int l, int r) ;
book BiSearch ( int l, int r, double price) ;
int Size ( ) ;
void findExp ( ) ;
} ;
SeqList.cpp
#include "SeqList.h"
SeqList:: SeqList ( ) {
data = new book[ 100 ] ;
len = 0 ;
maxsize = 100 ;
}
SeqList:: SeqList ( book arr[ ] , int n) {
data = new book[ n+ 100 ] ;
len = 0 ;
maxsize = n + 100 ;
for ( int i = 0 ; i < n; i++ ) {
data[ i] = arr[ i] ;
len++ ;
}
}
SeqList:: ~ SeqList ( ) {
delete [ ] data;
}
void SeqList:: Add ( )
{
if ( len == maxsize)
{
book* p = new book[ maxsize + 100 ] ;
for ( int i = 0 ; i < len; i++ )
p[ i] = data[ i] ;
delete [ ] data;
data = p;
}
string s;
cout << "请输入书号:" ;
cin >> s;
if ( ( int ) s. size ( ) > 19 )
throw "书号过长" ;
else
data[ len] . no = s;
cout << "请输入书名:" ;
cin >> s;
if ( ( int ) s. size ( ) > 63 )
throw "书名过长" ;
else
data[ len] . name = s;
cout << "请输入图书价格:" ;
cin >> data[ len] . price;
len++ ;
}
void SeqList:: Adds ( )
{
cout << "请输入图书个数" ;
int n; cin >> n;
for ( int i = 0 ; i < n; i++ ) {
cout << "------------------" << endl;
Add ( ) ;
}
}
void SeqList:: Print ( )
{
cout << "所有图书的信息如下:" << endl;
cout << "书号\t\t书名\t\t价格\n" ;
for ( int i = 0 ; i < len; i++ )
{
data[ i] . Print ( ) ;
}
}
void SeqList:: Insert ( int idx, book& obj)
{
if ( idx > len)
throw "插入位置不合法" ;
if ( idx <= 0 )
throw "插入位置不能为负数或者零" ;
if ( len == maxsize)
{
book* p = new book[ maxsize + 100 ] ;
for ( int i = 0 ; i < len; i++ )
p[ i] = data[ i] ;
delete [ ] data;
data = p;
}
for ( int i = len; i >= idx; i-- ) {
data[ i + 1 ] = data[ i] ;
}
data[ idx] = obj;
len ++ ;
}
int SeqList:: Size ( )
{
return len;
}
void SeqList:: Delete ( int idx)
{
if ( idx < 0 || idx >= len) throw "删除的下标参数不合法" ;
for ( int i = idx + 1 ; i < len; i++ )
data[ i - 1 ] = data[ i] ;
len-- ;
}
int SeqList:: Find ( string no) {
for ( int i = 0 ; i < len; i++ ) {
if ( data[ i] . no == no)
return i;
}
return - 1 ;
}
void SeqList:: rmDup ( )
{
for ( int j = 0 ; j < len; j++ ) {
int i = j + 1 ;
while ( i < len) {
if ( data[ j] == data[ i] )
Delete ( i) ;
else
i++ ;
}
}
cout << "已经去除重复信息!" << endl;
}
void SeqList:: Sort ( int l, int r)
{
if ( l >= r)
return ;
int i = l + 1 , j = r;
while ( i < j)
{
while ( i < j && data[ j] . price < data[ l] . price) j-- ;
while ( i< j && data[ i] . price > data[ l] . price) i++ ;
if ( i >= j)
break ;
book temp = data[ i] ;
data[ i] = data[ j] ;
data[ j] = temp;
}
book temp = data[ j] ;
data[ j] = data[ l] ;
data[ l] = temp;
Sort ( l, j- 1 ) ;
Sort ( j+ 1 , r) ;
}
book SeqList:: BiSearch ( int l, int r, double price)
{
while ( l < r) {
int mid = ( l + r + 1 ) / 2 ;
if ( mid >= price)
l = mid;
else
r = mid - 1 ;
}
if ( abs ( data[ l] . price - price) > 0.001 )
cout << "没有找到相应信息" ;
else {
cout << "为您找到:" << endl;
data[ l] . Print ( ) ;
}
return data[ l] ;
}
void SeqList:: findExp ( )
{
double exp = - 1 ;
for ( int i = 0 ; i < len; i++ ) {
if ( data[ i] . price > exp)
exp = data[ i] . price;
}
cout << "查找最贵书籍:" << endl;
for ( int i = 0 ; i < len; i++ ) {
if ( abs ( data[ i] . price - exp) <= 0.001 )
cout<< data[ i] . name<< endl;
}
}
实验二
code2.cpp
#include <iostream>
#include <vector>
#include <queue>
#include <string>
#include <unordered_set>
using namespace std;
const int N = 3 ;
int dir[ 4 ] [ 2 ] = { { 0 , - 1 } , { 0 , 1 } , { 1 , 0 } , { - 1 , 0 } } ;
int goal[ 3 ] [ 3 ] ;
class node {
public :
vector< vector< int >> grid;
int f;
int g;
string s;
int r, c;
node& operator = ( const node& x) {
for ( int i = 0 ; i < N; i++ ) {
for ( int j = 0 ; j < N; j++ ) {
this - > grid[ i] [ j] = x. grid[ i] [ j] ;
}
}
this - > f = x. f;
this - > g = x. g;
this - > r = x. r;
this - > c = x. c;
this - > s = x. s;
return * this ;
}
node ( ) {
grid. resize ( 3 , vector < int > ( 3 , 0 ) ) ;
f = 0 ;
g = 0 ;
s = "" ;
}
} ;
string getString ( node& x) {
string res = "" ;
for ( int i = 0 ; i < N; i++ ) {
for ( int j = 0 ; j < N; j++ )
{
res. push_back ( ( char ) ( '0' + x. grid[ i] [ j] ) ) ;
}
}
return res;
}
int getH1 ( node& x) {
int h = 0 ;
for ( int i = 0 ; i < N; i++ )
{
for ( int j = 0 ; j < N; j++ ) {
if ( x. grid[ i] [ j] == goal[ i] [ j] )
h++ ;
}
}
return 9 - h;
}
class cmp {
public :
bool operator ( ) ( node& a, node& b) {
return a. f > b. f;
}
} ;
priority_queue< node, vector< node> , cmp> open;
unordered_set< string> close;
unordered_set< string> vis;
int init_state[ 3 ] [ 3 ] ;
void solve ( ) {
cout << "输入初始状态:" << endl;
for ( int i = 0 ; i < 3 ; i++ )
for ( int j = 0 ; j < 3 ; j++ )
cin >> init_state[ i] [ j] ;
cout << "输入目标状态:" << endl;
for ( int i = 0 ; i < 3 ; i++ )
for ( int j = 0 ; j < 3 ; j++ )
cin >> goal[ i] [ j] ;
int cnt = 0 ;
int past = 1 ;
node start;
for ( int i = 0 ; i < N; i++ ) {
for ( int j = 0 ; j < N; j++ ) {
start. grid[ i] [ j] = init_state[ i] [ j] ;
if ( init_state[ i] [ j] == 0 )
{
start. r = i;
start. c = j;
}
}
}
start. g = 0 ;
start. f = start. g + getH1 ( start) ;
start. s = getString ( start) ;
close. emplace ( start. s) ;
open. push ( start) ;
bool jud = false ;
while ( ! jud && ! open. empty ( ) )
{
cnt++ ;
node cur = open. top ( ) ;
cout << "______第" << cnt << "次搜索_____" << endl;
for ( int i = 0 ; i < N; i++ )
{
for ( int j = 0 ; j < N; j++ ) {
cout << cur. grid[ i] [ j] << " " ;
}
cout << endl;
}
cout < "启发函数数值:" << cur. f << endl;
open. pop ( ) ;
close. emplace ( cur. s) ;
for ( int d = 0 ; d < 4 ; d ++ ) {
int dr = cur. r + dir[ d] [ 0 ] ;
int dc = cur. c + dir[ d] [ 1 ] ;
if ( dr < 0 || dc < 0 || dr >= N || dc >= N)
continue ;
node temp;
temp = cur;
temp. grid[ temp. r] [ temp. c] = temp. grid[ dr] [ dc] ;
temp. grid[ dr] [ dc] = 0 ;
temp. s = getString ( temp) ;
if ( close. find ( temp. s) != close. end ( ) || vis. find ( temp. s) != vis. end ( ) )
continue ;
temp. r = dr;
temp. c = dc;
temp. g+ = 1 ;
temp. f = temp. g + getH1 ( temp) ;
open. push ( temp) ;
vis. emplace ( temp. s) ;
past++ ;
if ( getH1 ( temp) == 0 )
{
jud = true ;
break ;
}
}
}
cnt++ ;
cout << "______第" << cnt << "次搜索_____" << endl;
for ( int i = 0 ; i < N; i++ )
{
for ( int j = 0 ; j < N; j++ ) {
cout << open. top ( ) . grid[ i] [ j] << " " ;
}
cout << endl;
}
cout << "共计搜索:" << cnt<< "次" << endl;
cout << "表中的状态共有:" << past<< "种" << endl;
cout << "搜索深度:" << open. top ( ) . g << endl;
}
int main ( )
{
solve ( ) ;
return 0 ;
}
实验三
code3.cpp
#include <iostream>
#include <fstream>
#include "SeqList.h"
#include "BiTree.h"
using namespace std;
void menu ( ) {
cout << "___________________________" << endl;
cout << "1.读取文件" << endl
<< "2.删除出现小于五次的单词并输出剩下单词" << endl
<< "3.计算在线性表和二叉排序树情况下的ASL,并比较" << endl
<< "4.退出系统" << endl;
cout << "选择操作:" ;
}
void solve ( ) {
int ope = 0 ;
SeqList list;
BiTree tree;
do {
system ( "cls" ) ;
menu ( ) ;
cin >> ope;
if ( ope == 1 ) {
read ( list) ;
cout << "读取文件成功!" << endl;
list. Print ( ) ;
}
else if ( ope == 2 ) {
for ( int i = 0 ; i < list. Size ( ) ; i++ ) {
if ( list. Get ( i) . cnt <= 5 )
{
cout << "删除" << list. Get ( i) . str << "\t出现次数:" << list. Get ( i) . cnt << endl;
list. Delete ( i) ;
i-- ;
}
}
cout << "剩余单词:" << endl;
list. Print ( ) ;
}
else if ( ope == 3 ) {
double linear_ASL = 0 , BST_ASL = 0 ;
tree. Create ( list) ;
for ( int i = 0 ; i < list. Size ( ) ; i++ ) {
linear_ASL + = ( i + 1 ) ;
int step = tree. Find ( list. Get ( i) ) ;
BST_ASL + = step;
}
linear_ASL = linear_ASL / double ( list. Size ( ) ) ;
BST_ASL = BST_ASL / double ( list. Size ( ) ) ;
cout << "线性表的ASL:" << linear_ASL<< endl;
cout << "二叉排序树的ASL:" << BST_ASL << endl;
}
else if ( ope != 4 )
{
ope = 0 ;
cout << "没有此操作,请重新输入" << endl;
}
system ( "PAUSE" ) ;
} while ( ope != 4 ) ;
}
int main ( )
{
solve ( ) ;
return 0 ;
}
nodes.h
#pragma once
#include <iostream>
#include <string>
using namespace std;
#define FILENAME "Infile.txt"
#define SAVEFILE "Outfile.txt"
struct node {
string str;
int cnt;
node ( ) {
str = "" ;
cnt = 0 ;
}
} ;
struct BiNode {
string str;
int cnt;
BiNode* lchild, * rchild;
BiNode ( ) {
lchild = nullptr ;
rchild = nullptr ;
str = "" ;
cnt = 0 ;
}
} ;
SeqList.h
#pragma once
#include <iostream>
#include <fstream>
#include "nodes.h"
using namespace std;
const int maxsize = 1000 ;
class SeqList
{
node data[ maxsize] ;
int len;
public :
SeqList ( ) ;
~ SeqList ( ) ;
friend class BiTree ;
void Insert ( int idx, node& obj) ;
void Insert ( int idx, string& str) ;
void Print ( ) ;
void Delete ( int idx) ;
node Get ( int idx) ;
int Size ( ) ;
int Find ( string& obj) ;
friend void read ( SeqList& obj) ;
friend void save ( SeqList& obj) ;
} ;
SeqList.cpp
#include "SeqList.h"
#include "SeqList.h"
SeqList:: SeqList ( ) {
len = 0 ;
}
SeqList:: ~ SeqList ( ) {
}
void SeqList:: Print ( )
{
cout << "单词:\t\t\t出现次数" << endl;
for ( int i = 0 ; i < len; i++ )
{
cout << data[ i] . str << "\t\t" ;
if ( data[ i] . str. size ( ) < 8 )
cout << "\t" ;
cout<< data[ i] . cnt << endl;
}
}
void SeqList:: Insert ( int idx, node& obj)
{
if ( idx > len) throw "插入位置不合法" ;
if ( idx < 0 )
throw "插入位置不能为负数或者零" ;
for ( int i = len; i >= idx; i-- ) {
data[ i + 1 ] = data[ i] ;
}
data[ idx] = obj;
len++ ;
}
void SeqList:: Insert ( int idx, string& str)
{
if ( idx > len)
throw "插入位置不合法" ;
if ( idx < 0 )
throw "插入位置不能为负数或者零" ;
for ( int i = len; i >= idx; i-- ) {
data[ i + 1 ] = data[ i] ;
}
data[ idx] . str = str;
data[ idx] . cnt = 1 ;
len++ ;
}
node SeqList:: Get ( int idx) {
if ( idx > len)
throw "获取位置不合法" ;
if ( idx < 0 )
throw "获取位置不能为负数或者零" ;
return data[ idx] ;
}
int SeqList:: Size ( )
{
return len;
}
void SeqList:: Delete ( int idx)
{
if ( idx < 0 || idx >= len) throw "删除的下标参数不合法" ;
for ( int i = idx + 1 ; i < len; i++ )
data[ i - 1 ] = data[ i] ;
len-- ;
}
int SeqList:: Find ( string& obj) {
int idx = - 1 ;
for ( int i = 0 ; i < len; i++ ) {
if ( data[ i] . str == obj) {
idx = i;
break ;
}
}
return idx;
}
void read ( SeqList& obj) {
ifstream ifs;
ifs. open ( FILENAME, ios:: in) ;
if ( ! ifs. is_open ( ) )
{
cout << "First Use..." << endl;
ifs. close ( ) ;
return ;
}
char c;
ifs >> c;
if ( ifs. eof ( ) ) {
cout << "Empty Data..." << endl;
ifs. close ( ) ;
return ;
}
ifs. close ( ) ;
ifstream ifs1;
ifs1. open ( FILENAME, ios:: in) ;
while ( ! ifs1. eof ( ) ) {
string s;
ifs1 >> s;
int len = s. size ( ) - 1 ;
if ( ( len>= 0 ) && ! ( ( s[ len] >= '0' && s[ len] <= '9' ) || ( s[ len] >= 'A' && s[ len] <= 'Z' ) || ( s[ len] >= 'a' && s[ len] <= 'z' ) ) )
{
char temp = s[ len] ;
s. erase ( s. begin ( ) + len) ;
if ( temp == '"' )
s. erase ( s. begin ( ) ) ;
}
int idx = obj. Find ( s) ;
if ( idx != - 1 ) {
obj. data[ idx] . cnt++ ;
}
else {
int end = obj. Size ( ) ;
obj. Insert ( end, s) ;
}
}
ifs1. close ( ) ;
}
void save ( SeqList& obj) {
ofstream ofs;
ofs. open ( SAVEFILE, ios:: out) ;
int len = obj. Size ( ) ;
for ( int i = 0 ; i < len; i++ ) {
ofs << obj. data[ i] . str << '\t' << obj. data[ i] . cnt << endl;
}
ofs. close ( ) ;
}
BiTree.h
#pragma once
#pragma once
#include <iostream>
#include "SeqList.h"
#include "nodes.h"
#include <string>
using namespace std;
class BiTree
{
BiNode* root;
public :
BiTree ( ) ;
~ BiTree ( ) ;
void Insert ( node& obj, BiNode* & p) ;
void Create ( SeqList& linear) ;
void Destory ( BiNode* p) ;
int Find ( node obj) ;
} ;
BiTree.cpp
#include "BiTree.h"
BiTree:: BiTree ( )
{
root = nullptr ;
} ;
BiTree:: ~ BiTree ( ) {
Destory ( root) ;
} ;
void BiTree:: Destory ( BiNode* p)
{
if ( p == nullptr )
return ;
Destory ( p- > lchild) ;
Destory ( p- > rchild) ;
delete [ ] p;
} ;
void BiTree:: Insert ( node& obj, BiNode* & p)
{
if ( p == nullptr ) {
p = new BiNode ( ) ;
p - > str = obj. str;
p - > cnt = obj. cnt;
return ;
}
if ( obj. cnt <= p- > cnt)
Insert ( obj, p- > lchild) ;
else
Insert ( obj, p- > rchild) ;
} ;
void BiTree:: Create ( SeqList& linear)
{
for ( int i = 0 ; i < linear. Size ( ) ; i++ ) {
Insert ( linear. data[ i] , root) ;
}
} ;
int BiTree:: Find ( node obj)
{
int step = 0 ;
BiNode* p = root;
while ( p != nullptr ) {
step++ ;
if ( p- > str == obj. str)
return step;
if ( obj. cnt <= p- > cnt)
p = p- > lchild;
else
p = p- > rchild;
}
return step;
} ;