小學期的歷練

6.29-7.6這段日子,被稱爲完美的小學期,爲什麼呢?在這幾天裏經過代碼的積累,對面向對象程序設計有啦更進一步的瞭解。ios

接下來講說個人這段時間的感悟吧編程

心得體會數組

經過本次小學期的磨練,讓我對面向對象程設計有了更深一步的瞭解,最深的是學會用類的封裝來解決問題,逐步拆解,各個擊破。如下是個人感悟函數

從本次的實驗中,在對系統類的實驗編程中收穫到了用類來封裝一個學生管理系統,更深的體會到了,學生類很近似於結構體,他是一個單獨的學生個體,經過主函數來調用類中的函數來對學生進行調用。不一樣的函數執行的是一個學生個體的信息改動。而且在此過程當中熟練地運用了重載輸入輸出流來實現文件的導入和導出。在學生成績管理程序中,在對於排序的解決方面,我採用了快排的思想,寫了一個比較函數,實現了降序排序。學習

在對數學類的編程中,收穫到類與類之間的關係,直線類繼承點類,面類繼承點類,經過函數來實現直線和平面的構造,進而計算距離,單位法向量,判斷點線關係,線線關係,點面關係,線面關係和麪面關係。在判斷三角形的類別時,將點類的對象內嵌到三角形類中,經過邊之間的關係來判斷三角形的類別,再此過程當中,我遇到一些麻煩,就是當判斷兩條邊相等時不能直接==(由於數據時double類型,需使用fabs(l1-l2)<1e-3)用這樣的形式來判斷兩條邊相等。this

在對遊戲類的編程中,收穫到了隨機數字的使用,以及設定時間的使用,利用sround(unsigned int)time(0));來獲取隨機數,並用rand()%1000+1來控制隨機數字的範圍。spa

然而對於石頭剪子布採起分析用戶出拳的機率,來進行出拳,time_t 來定義時間變量,經過nowtime=time(0);來獲取當前時間,已達到限制時間的使用。在棋盤類中,在計算機的下棋方式的設定中我採起的是①要贏②不能贏,但也不能輸③以上狀況都不知足時隨機選擇下棋地方。在寫判斷輸贏的函數,經過下棋的每行每列以及對角線和反對角線的計算,來判斷輸贏和平局。對於選擇棋子的解決利用兩個字符變量一個表明計算機的,另外一個表明人的,實現了棋子的選擇,對於先手和後手的問題,我採用條件分支語句來分隔他們,調整用戶和計算機的下棋順序。設計

在對鏈表的編程中,①先從整數鏈表提及用類來封裝鏈表的各類操做以及數據,指針域和頭指針。操做依然和以前的鏈表操做同樣,在主函數中對函數進行操做便可。②用鏈表來作簡單的通信錄,其實和整數鏈表差很少,無非是多了點信息,不一樣的是多了文件的導入和導出,依然採起重載輸入輸出流的方式,只不過參數應爲類對象的指針。③最後是約瑟夫環問題,他較爲不一樣的是他是環形鏈表,不是線性的。對於建立環形鏈表,經過頭指針依次鏈接節點,到最後一節點時,讓該節點的指針域指向頭節點的指針域,就構成了環形鏈表。其次是找到從第幾我的開始報數,利用循環計數,找到該節點,而後開始報數,報到該數時,動態分配一個節點,讓該節點的數據等於要刪除的節點的數據,並讓新的頭指針指向該節點,而後如此循環,到最後會出現只有一個節點,而且他的指針域所指向的地址是它自己,就讓他的指針域爲空接在新的鏈表後面,並結束循環。最後輸出新的鏈表。指針

總結:對於這次的小學期任務作歸納就是:這半年來學習的面向對象程序設計實際上是跟着老師的節奏走,老師寫了類,咱們對類的功能進行補充,主函數也不用寫。而小學期的這幾道題,徹底交給咱們本身去編程,類和功能都須要咱們本身去進行封裝,極大地鍛鍊了個人面向對象的封裝的能力以及主函數的編程能力。惟一不足的是:並無用私有類型去封裝數據,怕麻煩(須要改進),並無很熟練的運用繼承的關係。所以,暑假要繼續提升個人面向對象的繼承關係的運用結合用私有類型去封裝數據。code

話很少說,先上幾個優秀的代碼!!

這是一個學生信息管理系統!!

#include<iostream>
#include<fstream>
#include<string>
#include<cstdlib>
using namespace std;
int con=0;
class Student
{
public:
    string name,sex,xuehao,banji,xueyuan,zhuanye;
    int age;
    void show();
    void deletes();
    void creat();
    void add();
    int find1(string l);
    int find2(string l);
    void xiugai();
    friend istream &operator>>(istream &is,Student &a);
    friend ostream &operator<<(ostream &os,Student &a);
};
void Student::show()
{
    cout<<"學號:"<<xuehao<<" 姓名:"<<name<<" 性別:"<<sex<<" 年齡:"<<age<<" 班級:"<<banji<<" 學院:"<<xueyuan<<" 專業:"<<zhuanye<<endl;
}
void Student::deletes()//消除學生信息,讓學號和年齡爲零其餘爲空
{
    this->xuehao=" ";
    this->name=" ";
    this->sex=" ";
    this->banji=" ";
    this->xueyuan=" ";
    this->zhuanye=" ";
    this->age=0;
}
void Student::creat()
{
            cout<<"請輸入學號"<<endl;
            cin>>xuehao;
            cout<<"請輸入姓名"<<endl;
            cin>>name;
            cout<<"請輸入性別"<<endl;
            cin>>sex;
            cout<<"請輸入年齡"<<endl;
            cin>>age;
            cout<<"請輸入班級"<<endl;
            cin>>banji;
            cout<<"請輸入學院"<<endl;
            cin>>xueyuan;
            cout<<"請輸入專業"<<endl;
            cin>>zhuanye;
}
void Student::add()
{
            cout<<"請輸入添加的學號"<<endl;
            cin>>xuehao;
            cout<<"請輸入添加的姓名"<<endl;
            cin>>name;
            cout<<"請輸入添加的性別"<<endl;
            cin>>sex;
            cout<<"請輸入添加的年齡"<<endl;
            cin>>age;
            cout<<"請輸入添加的班級"<<endl;
            cin>>banji;
            cout<<"請輸入添加的學院"<<endl;
            cin>>xueyuan;
            cout<<"請輸入添加的專業"<<endl;
            cin>>zhuanye;
}
int Student::find1(string l)
{
    if(xuehao==l)
        return 1;
    else
        return 0;
}
int Student::find2(string l)
{
    if(banji==l)
        return 1;
    else
        return 0;
}
void Student::xiugai()
{
    int c;
    cout<<"請選擇修改的信息"<<endl;
    cout<<"1.學號 2.姓名 3.性別 4.年齡 5.班級 6.學院 7.專業"<<endl;
    cin>>c;
    switch(c)
    {
        case 1:
            cout<<"請輸入學號"<<endl;
            cin>>xuehao;
            break;
        case 2:
            cout<<"請輸入姓名"<<endl;
            cin>>name;
            break;
        case 3:
            cout<<"請輸入性別"<<endl;
            cin>>sex;
            break;
        case 4:
            cout<<"請輸入年齡"<<endl;
            int x;
            cin>>age;
            break;
        case 5:
            cout<<"請輸入班級"<<endl;
            cin>>banji;
            break;
        case 6:
            cout<<"請輸入學院"<<endl;
            cin>>xueyuan;
            break;
        case 7:
            cout << "請輸入專業" << endl;
            cin>>zhuanye;
            break;
    }
}
istream &operator>>(istream &is,Student &a)
{
    is>>a.xuehao>>a.name>>a.sex>>a.age>>a.banji>>a.xueyuan>>a.zhuanye;
    return is;
}
ostream &operator<<(ostream &os,Student &a)
{
    os<<a.xuehao<<" "<<a.name<<" "<<a.sex<<" "<<a.age<<" "<<a.banji<<" "<<a.xueyuan<<" "<<a.zhuanye;
    return os;
}
int main()
{
    int con=0,x;
    Student a[100];//構建學生類數組
    cout<<"請選擇下面的一個項目:"<<endl;
    cout<<"1.建立 2.添加 3.查詢 4.修改 5.刪除 6.顯示 7.文件導入 8.文件導出 9.退出"<<endl;
    while(1)
    {
        cin>>x;
        if(x==1)//建立學生基本信息
        {
            int n;
            cout<<"請輸入建立學生的個數"<<endl;
            cin>>n;
            for(int i=0;i<n;i++)
            {
                a[con++].creat();
            }
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==2)//添加學生的基本信息
        {
            a[con++].add();
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==3)//查詢學生
        {
            int z,k=0;
            int h;
            string m;
            cout<<"請選擇查詢方式:1.學號 2.班級"<<endl;
            cin>>z;
            if(z==1)//按學號進行查詢
            {
                cout<<"請輸入學號"<<endl;
                cin>>m;
                for(int i=0;i<con;i++)
                {
                    if(a[i].find1(m))
                    {
                        a[i].show();
                        k=1;
                    }
                }
                if(k==0)
                    cout<<"沒有該學生"<<endl;
            }
            if(z==2)//按班級進行查詢
            {
                cout<<"請輸入班級"<<endl;
                cin>>m;
                for(int i=0;i<con;i++)
                {
                    if(a[i].find2(m))
                    {
                        a[i].show();
                        k=1;
                    }
                }
                if(k==0)
                    cout<<"沒有該學生"<<endl;
            }
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==4)//修改學生信息
        {
            string m;
            int l,k=0;
            cout<<"請輸入要修改的學號"<<endl;
            cin>>m;
            for(int i=0;i<con;i++)
            {
                if(a[i].xuehao==m)
                {
                    a[i].xiugai();
                    k=1;
                }
            }
            if(k==0)
                cout<<"沒有該學生"<<endl;
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==5)//刪除學生信息
        {
            string n;
            cout<<"請輸入要刪除的姓名"<<endl;
            cin>>n;
            for(int i=0;i<con;i++)
            {
                if(a[i].name==n)
                {
                    a[i].deletes();
                }
            }
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==6)//展現當前學生的基本信息
        {
            for(int i=0;i<con;i++)
            {
                if(a[i].xuehao==" ")
                    continue;
                a[i].show();
            }
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==7)//導入文件信息
        {
            char l[100];
            cout<<"輸入要導入的文件名"<<endl;
            cin>>l;
            cout<<"輸入要導入幾條學生信息"<<endl;
            int sum;
            cin>>sum;
            ifstream infile(l,ios::in);
            if(!infile)
            {
                cout << "open error!" << endl;
                exit(0);
            }
            for(int i=0;i<sum;i++)
               {
                    infile>>a[con];
                    con++;
               }
               infile.close();
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==8)//導出到文件裏
        {
            char l[100];
            cout<<"輸入要導出的文件名"<<endl;
            cin>>l;
            ofstream out(l, ios::out);
            if(!out)
            {
                cout << "open error!" << endl;
                exit(0);
            }
            for(int i=0;i<con;i++)
                out<<a[i]<<endl;
            out.close();
            cout<<"請輸入下一步操做"<<endl;
        }
        if(x==9)//結束整個程序
            break;
    }
    return 0;
}

數學類:判斷三角形的類別

#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;
class Point//構造點類
{
public:
    double x,y;
    Point(double x1,double y1)
    {
        x=x1;
        y=y1;
    }
    Point(Point &t)
    {
        x=t.x;
        y=t.y;
    }
};
class triangle//三角形類
{
public:
    Point a,b,c;//內嵌對象
    triangle(Point &a1,Point &a2,Point &a3);
    void Judge();
    double bian(Point a,Point b);
};
triangle::triangle(Point &a1,Point &a2,Point &a3):a(a1),b(a2),c(a3)
{
}
double triangle::bian(Point a,Point b)//計算兩點間的距離
{
    double l;
    l=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    return l;
}
void triangle::Judge()//判斷三角形的形狀
{
    double l1,l2,l3,l,t,m;
    l1=bian(a,b);
    l2=bian(a,c);
    l3=bian(b,c);
    if(l1+l2>l3&&l2+l3>l1&&l1+l3>l2)
    {
        if(fabs(l1-l2)<0.001&&fabs(l1-l3)<0.001)
            cout<<"等邊三角形"<<endl;
        else if(l1==l2||l1==l3||l2==l3)
        {
            if(fabs((l1*l1)+(l2*l2)-(l3*l3))<0.001||fabs((l2*l2)+(l3*l3)-(l1*l1))<0.001||fabs((l1*l1)+(l3*l3)-(l2*l2))<0.001)
                cout<<"等腰直角三角形"<<endl;
            else
                cout<<"等腰三角形"<<endl;
        }
        else if(fabs((l1*l1)+(l2*l2)-(l3*l3))<0.001||fabs((l2*l2)+(l3*l3)-(l1*l1))<0.001||fabs((l1*l1)+(l3*l3)-(l2*l2))<0.001)
            cout<<"直角三角形"<<endl;
        else
            cout<<"通常三角形"<<endl;
    }
    else
        cout<<"不能構成三角形"<<endl;
}
int main()
{
    double x1,y1,x2,y2,x3,y3;
    cout<<"請輸入三個點的座標:x y"<<endl;
    cin>>x1>>y1>>x2>>y2>>x3>>y3;
    Point a(x1,y1),b(x2,y2),c(x3,y3);
    triangle T(a,b,c);
    T.Judge();
    return 0;
}

遊戲類:人機下棋

#include<iostream>
#include<cstring>
#include<time.h>
#include <stdlib.h>
using namespace std;
int n;
char a;
class chess
{
public:
    char a,b;
    int n;
    int  v[50][50];
    char map[50][50];
    void chushi();
    int shuying();
    void show();
    void user();
    void computer();
    void play();
};
void chess::chushi()
{
    int i,j;
    memset(v,0,sizeof(v));
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
            cout<<"* ";
        cout<<endl;
    }
}
void chess::show()
{
    int i,j;
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            {
                if(v[i][j]==1)
                    cout<<a<<" ";
                if(v[i][j]==-1)
                    cout<<b<<" ";
                if(v[i][j]==0)
                    cout<<"*"<<" ";
            }
        cout<<endl;
    }
}
int chess::shuying()//根據每行每列或對角線和反對角線是否相加等於n或-n來進行輸贏的判斷和平局的判斷
{
    int i,t=0,j;
    for(i=1;i<=n;i++)
    {
        t=0;
        for(j=1;j<=n;j++)
            t+=v[i][j];
        if(t==-n) return -1;
        if(t==n) return 1;
    }
    t=0;
    for(i=1;i<=n;i++)
    {
        t=0;
        for(j=1;j<=n;j++)
            t+=v[j][i];
        if(t==-n) return -1;
        if(t==n) return 1;
    }

    j=0;t=0;
    for(i=1;i<=n;i++)
    {
        j+=v[i][i];
        t+=v[i][n+1-i];
    }
    if(t==-n||j==-n) return -1;
    if(t==n||j==n) return 1;

    for(i=1;i<=n;i++)
    {
        for(j=1;j<=n;j++)
        {
            if(v[i][j]==0)
                return 0;
        }
    }
    return 3;
}
void chess::user()
{
    int x,y;
    cout<<"請輸入座標(x,y)"<<endl;
    cin>>x>>y;
    v[x][y]=1;
}
void chess::computer()
{
    int i,j,t=0,k;
    //先判斷本身下一步棋可否贏,能贏就下,先對行在對列,而後是對角線和反對角線
    for(i=1;i<=n;i++)
    {
        t=0;
        for(j=1;j<=n;j++)
            t+=v[i][j];
        if(t==-(n-1))
        {
            for(k=1;k<=n;k++)
            {
                if(v[i][k]==0)
                {
                    v[i][k]=-1;
                    return ;
                }
            }
        }
    }
    t=0;
    for(i=1;i<=n;i++)
    {
        t=0;
        for(j=1;j<=n;j++)
            t+=v[j][i];
        if(t==-(n-1))
        {
            for(k=1;k<=n;k++)
            {
                if(v[k][i]==0)
                {
                    v[k][i]=-1;
                    return ;
                }
            }
        }
    }
    j=0;t=0;
    for(i=1;i<=n;i++)
    {
        j+=v[i][i];
        t+v[i][n+1-i];
    }
    if(t==-(n-1))
    {
        for(k=1;k<=n;k++)
        {
            if(v[k][n+1-k]==0)
            {
                v[k][n+1-k]=-1;
                return ;
            }
        }
    }
    if(j==-(n-1))
    {
        for(k=1;k<=n;k++)
        {
            if(v[k][k]==0)
            {
                v[k][k]=-1;
                return ;
            }
        }
    }
//對本身下一步棋不能輸進行選擇
    t=0;
    for(i=1;i<=n;i++)
    {
        t=0;
        for(j=1;j<=n;j++)
        t+=v[i][j];
        if(t==n-1)
        {
            for(k=1;k<=n;k++)
            {
                if(v[i][k]==0)
                {
                    v[i][k]=-1;
                    return ;
                }
            }
        }
    }
    t=0;
    for(i=1;i<=n;i++)
    {
        t=0;
        for(j=1;j<=n;j++)
        t+=v[j][i];
        if(t==n-1)
        {
            for(k=1;k<=n;k++)
            {
                if(v[k][i]==0)
                {
                    v[k][i]=-1;
                    return ;
                }
            }
        }
    }
    j=0;t=0;
    for(i=1;i<=n;i++)
    {
        j+=v[i][i];
        t+=v[i][n+1-i];
    }
    if(t==n-1)
    {
        for(k=1;k<=n;k++)
        {
            if(v[k][n+1-k]==0)
            {
                v[k][n+1-k]=-1;
                return ;
            }
        }
    }
    if(j==n-1)
    {
        for(k=1;k<=n;k++)
        {
            if(v[k][k]==0)
            {
                v[k][k]=-1;
                return ;
            }
        }
    }
//若是以上都不知足,則隨機選擇沒有下過的地方進行下棋
    while(1)
    {
        srand(time(0));
        i=rand()%n+1;
        j=rand()%n+1;
        if(v[i][j]==0)
        {
            v[i][j]=-1;
            break;
        }
    }
    return ;
}
void chess::play()
{
    cout<<"請輸入棋盤大小(n*n)"<<endl;
    cin>>n;
    chushi();
    int x;
    cout<<"請選擇 1.先下 2.後下"<<endl;
    cin>>x;
    cout<<"請選擇棋子 X O(大寫):";
    cin>>a;
    if(a=='X')//選棋
        b='O';
    else
        b='X';
    if(x==1)
    {
        while(!shuying())//判斷輸贏
        {
            user();
            show();
            cout<<"計算機下"<<endl;
            if (shuying() == 1) { cout << ""<< endl; break; }
            if (shuying() == -1) { cout << "" << endl; break; }
            if (shuying() == 3) { cout << "" << endl; break; }
            computer();
            show();
            if (shuying() == 1) { cout << "" << endl; break; }
            if (shuying() == -1) { cout << "" << endl; break; }
            if (shuying() == 3) { cout << "" << endl; break; }
        }
    }
    else
    {
        while(!shuying())
        {
            computer();
            show();
            cout<<"人下"<<endl;
            if (shuying() == 1) { cout << ""<< endl; break; }
            if (shuying() == -1) { cout << "" << endl; break; }
            if (shuying() == 3) { cout << "" << endl; break; }
            user();
            show();
            if (shuying() == 1) { cout << ""<< endl; break; }
            if (shuying() == -1) { cout << "" << endl; break; }
            if (shuying() == 3) { cout << "" << endl; break; }
        }
    }
}

int main()
{
    chess qi;
    qi.play();
    return 0;
}

鏈表類:約瑟夫退圈問題

#include<iostream>
using namespace std;
class people
{
public:
    string num;
    people *next;
    people *head;
    people *newhead;
public:
    void creat();
    void print();
    void play();
};
void people::creat()
{
    int n,i;
    people *p=NULL;
    people *q=NULL;
    cout<<"請選擇建立多少我的圍成一個圈"<<endl;
    cin>>n;
    for(i=1;i<=n;i++)
    {
        q=new people;
        cout<<"請輸入第"<<i<<"我的的編號"<<endl;
        cin>>q->num;
        q->next=NULL;
        if(i==1)
            head->next=q;
        else
            p->next=q;
        p=q;
    }
    p->next=head->next;
}
void people::play()
{
    int i,rand,m,k=0;
    cout<<"請輸入從第幾我的開始報數"<<endl;
    cin>>rand;
    cout<<"報多少個數"<<endl;
    cin>>m;
    people *s=NULL;
    people *t=NULL;
    people *p=head;
    people *q=p->next;
    for(i=1;i<rand;i++)
    {
        p=q;
        q=q->next;
    }
    while(1)
    {
        for(i=1;i<m;i++)
        {
            p=q;
            q=q->next;
        }
        t=new people;
        t->num=q->num;
        t->next=NULL;
        if(k==0)
        {
            newhead=t;
            k++;
        }
        else
        {
            s->next=t;
        }
        s=t;
        p->next=q->next;
        q=q->next;

        if(p==q)
        {
            p->next=NULL;
            s->next=p;
            break;
        }
    }
}
void people::print()
{
    int i=0,j;
    people *p=newhead;
    while(p!=NULL)
    {
        cout<<p->num<<" ";
        p=p->next;
    }
    cout<<endl;
}
int main()
{
    people m;
    m.creat();
    m.play();
    m.print();
    return 0;
}
相關文章
相關標籤/搜索