2021数据结构课程设计源代码(申请优秀)

2021数据结构课程设计源代码(申请优秀)

实验一

code1.cpp

// code1.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#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); // 带参构造函数,根据一个book数组来初始换链表
    ~SeqList(); // 析构函数
    
    void Add();  // 增加一本书的信息在链表开头
    void Adds();  // 根据指定图书个数,逐个输入图书信息
    void Insert(int idx,book& obj);  // 根据置顶的待入库的新图书的位置和信息,将新的图书插入到图书表中指定位置
    void Print();  // 逐个现实图书表中所有图书的相关信息
    void Delete(int idx);  // 删除对应下标的数据
    int Find(string no);  // 找到最贵书号为no 的书籍下标,如果没有找到,则返回-1
    void rmDup();  // remove duplicate 图书信息表的图书去重
    void Sort(int l,int r);  // 利用快速排序按照图书价格降序排序
    book BiSearch(int l, int r,double price);  // 二分查找
    int Size();  // 统计表中图书的个数
    void findExp();  // find the most expensive book,如果有多个,则将所有最贵的书籍信息输出
};

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

// code2.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#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];//{ {0,1,2},{3,4,5},{6,7,8} };

class node {
public:
    vector<vector<int>> grid;
    int f;
    int g;
    string s;
    int r, c;  // 标记当前0的位置,初始化为(0,0)
    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 = "";
    }
};

/***由于使用了STL容器中的unordered_set,也就是哈希表,在存储的时候,我们将关键字设计为字符串,
因此就要设计出函数来将九宫格转换成字符串来存储,下边是将九宫格转换成字符串的函数*/
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]; // {{1,2,3},{4,5,6},{7,8,0}};

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;
}

// Run program: Ctrl + F5 or Debug > Start Without Debugging menu
// Debug program: F5 or Debug > Start Debugging menu

// Tips for Getting Started: 
//   1. Use the Solution Explorer window to add/manage files
//   2. Use the Team Explorer window to connect to source control
//   3. Use the Output window to see build output and other messages
//   4. Use the Error List window to view errors
//   5. Go to Project > Add New Item to create new code files, or Project > Add Existing Item to add existing code files to the project
//   6. In the future, to open this project again, go to File > Open > Project and select the .sln file

实验三

code3.cpp

// code3.cpp : This file contains the 'main' function. Program execution begins and ends there.
//

#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;
    }
    /*
    node& operator=(const node& obj) {
        this->str = "";
        for (int i = 0; i < (int)obj.str.size(); i++)
            str.push_back(obj.str[i]);
        cnt = obj.cnt;
        return *this;
    }*/
};

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);  // 插入一个节点,根据一个字符串初始化,次数为首次出现的1次
    void Print();  // 打印顺序表中所有的信息
    void Delete(int idx);  // 删除下标为idx 的节点信息
    node Get(int idx);  // 获得idx位置的node信息
    int Size();  // 或许顺序表长度
    int Find(string& obj);  // 根据字符串来查找该字符串所在的节点,没有查找到则返回-1
    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);
	}
};

// if find return the step of search, otherwise return 0
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;
};

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值