标签 南航 下的文章

在南航四年的时间里面,有件事情是值得吹逼的,就是我的C++考了100分。在这里分享C++课程设计文件给大家,仅供大家参考。

点击这里下载课程设计报告

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cstring>
using namespace std;

class Book
{
    char bookNo[20], bookName[20];
    int Num;
public:
    Book(char *nop =NULL, char *nap =NULL, int n =0)
    {
        if(nop)strcpy(bookNo, nop);
        if(nap)strcpy(bookName, nap);
        Num = n;
    }
    void Show()
    {
        cout << left << setw(20) << bookNo << left << setw(20) << bookName << left << setw(20) << Num << endl;
    }
    void Print(ofstream &out)
    {
        out << left << setw(20) << bookNo << left << setw(20) << bookName << left << setw(20) << Num << endl;
    }
    void setInfo(char *nop, char *nap, int n)
    {
        strcpy(bookNo, nop);
        strcpy(bookName, nap);
        Num = n;
    }
    void setNum(int n)
    {
        Num = n;
    }
    int getNum()
    {
        return Num;
    }
    friend class bookList;
};
class bookList
{
    Book *Books;
    int curLen, maxLen;
public:
    bookList(int len =20)
    {
        maxLen = len;
        curLen = 0;
        Books = new Book[maxLen];
    }
    ~bookList()
    {
        delete []Books;
    }
    void appendBook(char *nop, char *nap, int n)
    {
        if(curLen == maxLen)
        {
            Book *newBooks = new Book[maxLen+10];
            for(int i=0; i<curLen; i++)
                *(newBooks+i)=*(Books+i);        //简单的浅拷贝
            delete []Books;
            Books = newBooks;
            maxLen += 10;
        }
        getBooks(curLen++).setInfo(nop, nap, n);
    }
    int isIn(char *bkNo)
    {
        for (int i=0; i<curLen; i++)
        {
            if(strcmp(getBooks(i).bookNo,bkNo)==0) return i;
        }
        return -1;
    }
    Book & getBooks(int i)
    {
        return *(Books+i);
    }
    void deleteBook(char *bkNo)
    {
            for (int i=isIn(bkNo); i<curLen-1; i++)
             *(Books+i) = *(Books +i +1);    //curLen 控制长度,最后一个不用管,有新数据时会将其覆盖
            curLen--;
    }
    void sortBooks()
    {
        int min;
        Book temp;

        for (int i=0; i<curLen-1; i++)
        {
            min = i;
            for (int j=i+1; j<curLen; j++)
                if(strcmp(getBooks(min).bookNo, getBooks(j).bookNo)==1) min = j;
            if (min != i)
            {
                temp = getBooks(min);
                getBooks(min) = getBooks(i);
                getBooks(i) = temp;
            }
        }
        cout << "按书号的升序排列了图书" << endl;
    }
    void showAll()
    {
        for (int i =0; i<curLen; i++)
            getBooks(i).Show();
    }
    int curBookLen()
    {
        return curLen;
    }
    void setCurBookLen(int len)
    {
        curLen = len;
    }
    void initBookList()
    {
        readFromFile("bookdata.txt");
    }
    void readFromFile(char *filename)
    {
        int len;
        char bkNo[20],bkName[20];
        int Num;
        ifstream infile(filename);
        infile >> len;
        for (int i=0; i<len; i++)
        {
            infile >> bkNo >> bkName >> Num;
            appendBook(bkNo, bkName, Num);
        }
        infile.close();
        cout << "从文件插入了" << len << "条记录" << endl;
    }
    void writeToFile(char *filename)
    {
        ofstream outfile(filename);
        outfile << curLen << endl;
        for (int i=0; i<curLen; i++)
            getBooks(i).Print(outfile);
        outfile.close();
        cout << "向文件输出了" << curLen << "条记录" << endl;
    }
};
void appendRecord(bookList &booksObj)
{
    char bkNo[20], bkName[20];
    int Num;

    cout << "    请输入新增书籍的书号、书名和数量:  ";
    cin >> bkNo >> bkName >> Num;
    booksObj.appendBook(bkNo, bkName, Num);
}

void delRecord(bookList &booksObj)
{
    char bkNo[20];
    cout << "    请输入您要删除的书号:  ";
    cin >> bkNo;
    if (booksObj.isIn(bkNo) >-1) booksObj.deleteBook(bkNo);
    else cout << "您要删除的书不存在" << endl;
}

void showAll(bookList &booksObj)
{
    booksObj.showAll();
}

void query(bookList &booksObj)
{
    char bkNo[20];
    int i;

    cout << "请输入书号:  ";
    cin >> bkNo;
    if ((i=booksObj.isIn(bkNo))>-1)
    {
        cout << "该书为";
        booksObj.getBooks(booksObj.isIn(bkNo)).Show();
    }
    else
        cout << "没有该图书!" << endl;
}

void addBooks(bookList &booksObj)
{
    int i,Num, number;
    char bkNo[20];

    cout << "请输入您要入库的书的书号和数量:  ";
    cin  >> bkNo >> number;
    if((i=booksObj.isIn(bkNo)) > -1)
    {
        Num = booksObj.getBooks(i).getNum();
        booksObj.getBooks(i).setNum(Num+number);
    }
    else
    {
        cout << "该书不存在,请增加一条记录" << endl;
        appendRecord(booksObj);
    }
}

void sellBooks(bookList &booksObj)
{
    int i, Num, sell;
    char bkNo[20];

    cout << "请输入您要销售书的书号、数量:  " << endl;
    cin >> bkNo >> sell;

    if((i=booksObj.isIn(bkNo)) > -1)
    {
        Num = booksObj.getBooks(i).getNum();
        if(Num<sell) cout << "库存不足" << endl;
        else
        {
            booksObj.getBooks(i).setNum(Num-sell);
            cout << "销售了" << sell << "本书" << endl;
        }
    }
    else     cout << "该书不存在,请增加一条记录" << endl;
}

void appendFromTxtFile(bookList &booksObj)
{
    char filename[20];

    cout << "请输入文件名: ";
    cin >> filename;
    booksObj.readFromFile(filename);
}

void writeToTxtFile(bookList booksObj)
{
    char filename[20];

    cout << "请输入文件名:  ";
    cin >> filename;
    booksObj.writeToFile(filename);
}

void sortByNo(bookList &booksObj)
{
    booksObj.sortBooks();
}

void deleteAll(bookList &booksObj)
{
    booksObj.setCurBookLen(0);
}

char Quit(bookList &booksObj)
{
    char flag;

    cout << "Are you sure to quit? input y or n  :";
    cin >> flag;
    if(flag == 'y') booksObj.writeToFile("bookdata.txt");
    return flag;
}

int menu_select()
{
    char *menu[11] = {
        "1.    追加图书记录",
        "2.    删除图书记录",
        "3.    查询所有图书记录",
        "4.    查询图书",
        "5.    图书入库",
        "6.    图书销售",
        "7.    批量导入图书",
        "8.    导出图书记录",
        "9.    按书号排序",
        "10.   删除全部图书记录",
        "0.    退出"
    };
    int choice;
    do{
        system("cls");
        cout << "                                图书库存管理系统" << endl << endl;
        cout << "                       =================================" << endl;
        for(int i=0; i<11; i++)
            cout << "                             " << menu[i] << endl;
        cout << "                       =================================" << endl;
        cout << "请输入选择: ";
        cin >> choice;
    }while(choice<0 || choice>10);
    return choice;
}

int main()
{
    bookList booksObj;
    int sel;
    booksObj.initBookList();

    for (;;)
    {
        switch(sel=menu_select())
        {
            case 1:appendRecord(booksObj);system("pause");break;
            case 2:delRecord(booksObj);system("pause");break;
            case 3:showAll(booksObj);system("pause");break;
            case 4:query(booksObj);system("pause");break;
            case 5:addBooks(booksObj);system("pause");break;
            case 6:sellBooks(booksObj);system("pause");break;
            case 7:appendFromTxtFile(booksObj);system("pause");break;
            case 8:writeToTxtFile(booksObj);system("pause");break;
            case 9:sortByNo(booksObj);system("pause");break;
            case 10:deleteAll(booksObj);system("pause");break;
            case 0:
                if(Quit(booksObj) != 'y') continue;
        }
        if(sel == 0) break;
    }
    return 0;
}