瑞辅 发布的文章

Nokia mobile can be used not only make phone calls, but also to smash walnuts. It's versatile.

We can get paint and dyes from pigment, but Nobel believes he can made it into explosive. Though his parents didn't know what came over to him, they are supportive to him. Every time he don't eat anymore, they help him dispose of the food.

One day, when he mixed the chemicals in two flasks, the house vanished. He was so excited because it substantiated his idea. His parent suppressed their sadness and said "you are so~ good".

After the invention, he made an itinerary of Europe to tell others about it. And now it's prevalent to respect Nobel as a great scientist.

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#define M 16        //M name_length
#define N 12        //N no_length
#define K 3            //K score_length

using namespace std;

class Student
{
    char num[N], name[M];
    int score[K];
    double gpa[K], agpa;
public:
    //there neednt be a constructor
    void setnum(char nu[]);        //set...
    void setname(char na[]);
    void setscore(int sc[]);
    void setgpa(double g[]);
    void setagpa(double ag);
    void computegpa();
    void computeagpa();

    char *getnum();            //get...
    char *getname();
    void getscore(int sc[]);
    void getgpa(double g[]);
    void getagpa(double &ag);

    void disp();        //display...
};

class UserDatabase
{
    int nElem, Maxu;
    Student *user;
public:
    UserDatabase(int n=30);
    ~UserDatabase();
    void add_record(char *nu,char *na, int sc[]);    //add record
    void modify_record(Student *p, int sc[]);        //modify record
    void delete_record(Student *p);                //delete record 不知道书上的第二个参数是干什么用的        
    int query(char *nu);                        //我这里把返回值改成了int,如果存在的话,返回下标,不存在的话返回-1,感觉这样比较简单
    Student *getStudent(int i);            //这个函数是我新加的用来解决上面那个函数修改的问题

    void sorta_num();        //sort by num ascend
    void sorta_name();        //name asc
    void sorta_agpa();        //agpa asc
    void sort_num();        //num descend
    void sort_name();        //name desc
    void sort_agpa();        //agpa desc

    void disp();    //display record
    void clear();    //delete all
};

void Student::setnum(char nu[])
{
    strcpy(num, nu);
}    
void Student::setname(char na[])
{
    strcpy(name, na);
}
void Student::setscore(int sc[])
{
    for (int i=0; i<K; i++)
        score[i] = sc[i];
}
void Student::setgpa(double g[])
{
    for (int i=0; i<K; i++)
        gpa[i] = g[i];
}
void Student::setagpa(double ag)
{
    agpa = ag;
}
void Student::computegpa()
{
    for (int i=0; i<K; i++)
        if (score[i]<60) gpa[i] = 0;
        else gpa[i] = score[i]/10.0-5;
}
void Student::computeagpa()
{
    double sum=0;
    for (int i=0; i<K; i++) sum += gpa[i];
    agpa = sum/K;
}
char *Student::getnum()
{
    return num;
}
char *Student::getname()
{
    return name;
}
void Student::getscore(int sc[])
{
    for (int i=0; i<K; i++)
        sc[i] = score[i];
}
void Student::getgpa(double g[])
{
    for (int i=0; i<K; i++)
        g[i] = gpa[i];
}
void Student::getagpa(double &ag)
{
    ag = agpa;
}
void Student::disp()
{
    int i;
    cout << setw(N-5) << left << num << setw(M-5) << left << name;
    for (i=0; i<K; i++)
        cout << setw(7) << left << score[i];
    for (i=0; i<K; i++)
        cout << setw(8) << left << fixed << setprecision(1) << gpa[i];
    cout << left << agpa << endl;
    cout.unsetf(ios::fixed);
}
UserDatabase::UserDatabase(int n)
{
    char nu[N], na[M];
    int sc[K], i;
    double g[K], ag;

    ifstream infile;
    infile.open("user.txt");
    if    (!infile)
    {
        cout << "user.txt文件不存在" << endl;
        exit(0);
    }
    nElem = 0;
    Maxu = n;
    if (n)
        user = new Student[n];
    else
        user = NULL;
    while (infile >> nu)
    {
        infile >> na;
        for (i=0; i<K; i++)
            infile >> sc[i];
        for (i=0; i<K; i++)
            infile >> g[i];
        infile >> ag;
        add_record(nu, na, sc);
    }
        infile.close();
}
UserDatabase::~UserDatabase()
{
    int i, j, sc[K];
    char nu[N], na[M];
    double g[K], ag;
    ofstream outfile;
    outfile.open("user.txt");
    for (i=0; i<nElem; i++)
    {
        strcpy(nu,user[i].getnum());
        strcpy(na,user[i].getname());
        user[i].getscore(sc);
        user[i].getgpa(g);
        user[i].getagpa(ag);
        outfile << nu << '\t' << na << '\t';
        for (j=0; j<K; j++)
            outfile << sc[j] << '\t';
        for (j=0; j<K; j++)
            outfile << g[j] << '\t';
        outfile << '\t' << ag << endl;
    }
    outfile.close();
}
void UserDatabase::add_record(char *nu,char *na, int sc[])
{
    int j;
    if (nElem == Maxu)
    {
        Student *newptr = new Student[Maxu +10];
        for(int i=0; i<nElem; i++)
            newptr[i] = user[i];
        delete []user;
        Maxu +=10;
        user = newptr;
    }
    for (j=nElem-1; j>=0&&(strcmp(user[j].getnum(),nu)==1); j--)
            user[j+1] = user[j];
    user[j+1].setnum(nu);
    user[j+1].setname(na);
    user[j+1].setscore(sc);
    user[j+1].computegpa();
    user[j+1].computeagpa();
    nElem++;
}
void UserDatabase::modify_record(Student *p, int sc[])
{
    p->setscore(sc);
    p->computegpa();
    p->computeagpa();
}
void UserDatabase::delete_record(Student *p)
{
    for (nElem--; p<user+nElem; p++)
        *p = *(p+1);
}
int UserDatabase::query(char *nu)
{
    for (int i=0; i<nElem; i++)
        if (strcmp(user[i].getnum(),nu)==0)
            return i;
    return -1;
}
Student *UserDatabase::getStudent(int i)
{
    return (user+i);
}
void UserDatabase::sorta_num()
{
    int min;
    Student temp;
    for (int i=0; i<nElem-1; i++)
    {
        min = i;
        for (int j=i+1; j<nElem; j++)
            if(strcmp(user[min].getnum(),user[j].getnum())==1)
                min = j;
        if (min!=i)
        {
            temp = user[min];
            user[min] = user[i];
            user[i] = temp;
        }
    }
}
void UserDatabase::sorta_name()
{
    int i, j;
    Student p;
    for (i=1; i<nElem; i++)
    {
        p = user[i];
        for (j=i-1; j>=0&&(strcmp(user[j].getname(),p.getname())==1); j--)
            user[j+1] = user[j];
        user[j+1] = p;
    }
}
void UserDatabase::sorta_agpa()
{
    double agpa1, agpa2;
    Student temp;
    for (int i=1; i<nElem; i++)
        for (int j=0; j<nElem-i; j++)
        {
            user[j].getagpa(agpa1);
            user[j+1].getagpa(agpa2);
            if (agpa1>agpa2)
            {
                temp = user[j];
                user[j] = user[j+1];
                user[j+1] = temp;
            }
        }    
}
void UserDatabase::sort_num()
{
    int max;
    Student temp;
    for (int i=0; i<nElem-1; i++)
    {
        max = i;
        for (int j=i+1; j<nElem; j++)
            if(strcmp(user[max].getnum(),user[j].getnum())==-1)
                max = j;
        if (max!=i)
        {
            temp = user[max];
            user[max] = user[i];
            user[i] = temp;
        }
    }
}
void UserDatabase::sort_name()
{
    Student temp;
    sorta_name();
    for (int i=0; i<nElem-i-1; i++)
    {
        temp = user[i];
        user[i] = user[nElem-i-1];
        user[nElem-i-1] = temp;
    }
}
void UserDatabase::sort_agpa()
{
    double agpa1, agpa2;
    Student temp;
    for (int i=1; i<nElem; i++)
        for (int j=0; j<nElem-i; j++)
        {
            user[j].getagpa(agpa1);
            user[j+1].getagpa(agpa2);
            if (agpa1<agpa2)
            {
                temp = user[j];
                user[j] = user[j+1];
                user[j+1] = temp;
            }
        }    
}
void show_head(int);        //声明一下这个函数
void UserDatabase::disp()
{
    int i, j;
    for (i=0; i<nElem; i++)
    {
        if (i%10==0)
        {
            if(i) system("pause");
            show_head(6);
            
            cout << setw(N-5) << left << "学号" << setw(M-5) << left << "姓名";
            cout << "成绩1  ";
            cout << "成绩2  ";
            cout << "成绩3  ";
            cout << "绩点1   ";
            cout << "绩点2   ";
            cout << "绩点3  ";
            cout << "平均绩点";
            cout << endl;
        }
        user[i].disp();
    }
        
}
void UserDatabase::clear()
{
    nElem = 0;
}

void show_head(int i)
{
    system("cls");
    cout << "=======================================================================" << endl;
    cout << "                             ";
    switch (i)
    {
        case 1:    cout << "增加记录" << endl;        break;
        case 2: cout << "修改记录" << endl;        break;
        case 3: cout << "删除记录" << endl;        break;
        case 4: cout << "     查找" << endl;        break;
        case 5: cout << "     排序" << endl;        break;
        case 6: cout << "显示记录" << endl;    break;
        case 7: cout << "全删" << endl;        break;
        case 0: cout << "退出" << endl;        break;
        case 998: cout << "学生成绩管理系统" << endl; break;
    }
    cout << "=======================================================================" << endl;
}
int menu_select(bool flag=true)    //传的值是一个bool值,true用来显示主菜单,false 显示排序的菜单
{
    char *menu_main[]={
        "     1  -   增加记录",
        "     2  -   修改记录",
        "     3  -   删除记录",
        "     4  -   查找",
        "     5  -   排序",
        "     6  -   显示记录",
        "     7  -   全删",
        "     0  -   退出                    www.rvfu.me",
        NULL
    };
    char *menu_sort[]={
        "1 - 按学号(升序)",
        "2 - 按姓名(升序)",
        "3 - 按平均绩点(升序)",
        "4 - 按学号(降序)",
        "5 - 按姓名(降序)",
        "6 - 按平均绩点(降序)",
        NULL
    };
    int i, choice;
    do{
        system("cls");
        if (!flag)
            show_head(5);
        else show_head(998);
            if(flag)
                for (i=0; menu_main[i]; i++)
                    cout << "\t\t\t" << menu_main[i] << endl;
            else 
                for (i=0; menu_sort[i]; i++)
                    cout << "\t\t\t" << menu_sort[i] << endl;
                cout << "=======================================================================" << endl;
        cout << "输入选择: ";
        cin >> choice;
    }while(   ( flag&&(choice<0||choice>7) )  ||  ( !flag&&(choice<1||choice>6) )    );
    return choice;
}
void add_record(UserDatabase &usrdb)
{
    char num[N], name[M];
    int score[K];

    cout << "输入学号: ";
    cin >> num;
    if (usrdb.query(num)!=-1)
        cout << endl << "数据重复,插入失败" << endl << endl;
    else
    {
        cout << "输入姓名: ";
        cin >> name;
        cout << "输入成绩: ";
        for (int i=0; i<K; i++)
            cin >> score[i];
        usrdb.add_record(num,name,score);
        cout << endl << "插入成功" << endl << endl;
    }
}
void modify_record(UserDatabase &usrdb)
{
    Student *stuptr;
    char num[N];
    int score[K], i;
    cout << "输入学号: ";
    cin >> num;
    if ((i=usrdb.query(num))==-1)
        cout << endl << "数据不存在" << endl << endl;
    else
    {
        cout << endl << "原始记录: ";
        stuptr = usrdb.getStudent(i);
        stuptr->disp();
        cout << endl << "输入新成绩: ";
        for (int i=0; i<K; i++)
            cin >> score[i];
        usrdb.modify_record(stuptr,score);
        cout << endl << "修改成功" << endl << endl;
    }
}
void delete_record(UserDatabase &usrdb)
{
    Student *stuptr;
    char num[N], flag;
    int i;
    cout << "输入学号: ";
    cin >> num;
    if ((i=usrdb.query(num))==-1)
        cout << endl << "数据不存在" << endl << endl;
    else
    {
        cout << endl << "该条数据为: ";
        stuptr = usrdb.getStudent(i);
        stuptr->disp();
        cout << endl << "确认删除吗? y/n ";
        cin >> flag;
        if (flag=='y'|| flag=='Y')
        {
            usrdb.delete_record(stuptr);
            cout << endl <<"删除了该条数据" << endl << endl;
        }
        else
            cout << endl <<"取消了删除操作" << endl << endl;
    }
}
void query(UserDatabase &usrdb)
{
    Student *stuptr;
    char num[N];
    int i;
    cout << "输入学号: ";
    cin >> num;
    if ((i=usrdb.query(num))==-1)
        cout << endl << "数据不存在" << endl << endl;
    else
    {
        cout << endl << "该条数据为: ";
        stuptr = usrdb.getStudent(i);
        stuptr->disp();
        cout << endl;
    }
}
void sort(UserDatabase &usrdb)
{
    switch (menu_select(false))
    {
        case 1:usrdb.sorta_num();    break;
        case 2:usrdb.sorta_name();    break;
        case 3:usrdb.sorta_agpa();    break;
        case 4:usrdb.sort_num();    break;
        case 5:usrdb.sort_name();    break;
        case 6:usrdb.sort_agpa();    break;
    }
    cout << endl << ":)已经按指定要求排序" << endl << endl;
}
void disp(UserDatabase &usrdb)
{
    usrdb.disp();
}
void clear(UserDatabase &usrdb)
{
    char flag;
    cout << "确认删除吗? y/n ";
    cin >> flag;
    if (flag=='y'|| flag=='Y')
    {
        usrdb.clear();
        cout << endl << "完成操作" << endl << endl;
    }
    else
        cout << endl << "取消了操作" << endl << endl;
}
int main()
{
    UserDatabase usrdb;
    int select;
    char flag;

    while("欢迎大家来关注我的个人公众号瑞辅笔记,我的网站主页是suruifu.com")
    {
        switch(select=menu_select())
        {
            
            case 1: show_head(1);    add_record(usrdb);        system("pause"); break;
            case 2: show_head(2);    modify_record(usrdb);    system("pause"); break;
            case 3: show_head(3);    delete_record(usrdb);    system("pause"); break;
            case 4: show_head(4);    query(usrdb);            system("pause"); break;
            case 5:                    sort(usrdb);            system("pause"); break;
            case 6: show_head(6);    disp(usrdb);            system("pause"); break;
            case 7: show_head(7);    clear(usrdb);            system("pause"); break;
            case 0: show_head(0);    cout << "Are you sure ? y/n : ";
                    cin >> flag;
                    if (flag!='y'&& flag!='Y') continue;
        }
        if (select==0) break;
    }
    return 0;

When Xia was a little boy, he was so vulnerable and his place was always in a jumble that nobody played with him.

One day after he saw the steam vanished in the air he find it amazing. After asking the teacher way, the teacher goaded him and he started his thought about scientific research. Several years later, he went to NUAA. NUAA is renowned for its canteen for its hygiene and yummy food.

When in his tenth year in NUAA, he got some money. It's so timely that with it Xia invented an AC which can boil water. Every time in his course, he starts with preaching his invention and then get down to business. For us, its just like a convention.

在南航四年的时间里面,有件事情是值得吹逼的,就是我的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;
}