栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

4、基于对象的编程风格-C++

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

4、基于对象的编程风格-C++





Stack.h
#ifndef STACK_H
#define STACK_H
#include
#include
#include
using namespace std;

class Stack
{
public:
    Stack();

public:
    bool push(const string &elem);
    bool pop(string &elem);
    bool peek(string &elem);
    bool empty() const { return _stack.empty();}

    bool full() const { return _stack.size()==_stack.max_size();}
    int size() const { return _stack.size();}

    bool find(const string &elem) const;
    int count(const string &elem) const;
private:
    vector _stack;
};

#endif // STACK_H

Stack.cpp
#include "Stack.h"

Stack::Stack()
{

}

bool Stack::push(const string &elem)
{
    if(full()) return false;
    _stack.push_back(elem);
    return true;
}

bool Stack::pop(string &elem)
{
    if(empty()) return false;
    elem=_stack.back();
    _stack.pop_back();
    return true;
}

bool Stack::peek(string &elem)
{
    if(empty()) return false;
    elem=_stack.back();
    return true;
}
bool Stack::find(const string &elem) const{
    vector::const_iterator end_it=_stack.end();
    return ::find(_stack.begin(),end_it,elem)!=end_it;
}
int Stack::count(const string &elem) const{
    return ::count(_stack.begin(),_stack.end(),elem);
}

userProfile.h
#ifndef USERPROFILE_H
#define USERPROFILE_H
#include
#include
#include
using namespace std;//本类无法通过Borland C++和GCC,可以通过Visual C++

class UserProfile
{
public:
    enum uLevel{Beginner,Intermediate,Advanced,Guru};
    UserProfile(string login,uLevel=Beginner);
    UserProfile();

    bool operator==(const UserProfile&);
    bool operator!=(const UserProfile& rhs);

    //读取数据
    string login()const {return _login; }
    string user_name() const{ return _user_name;}
    int login_count()const { return _times_logged;}
    int guess_count()const { return _guesses;}
    int guess_correct()const { return _correct_guesses;}
    double guess_average() const;
    string level()const;
    //写数据
    void reset_login(const string&val ) { _login=val ; }
    void user_name(const string&val){  _user_name=val;}

    void reset_level(const string&);
    void reset_level(uLevel newlevel){  _user_level=newlevel;}

    void reset_login_count(int val) { _times_logged=val;}
    void reset_guess_count(int val) {  _guesses=val;}
    void reset_guess_correct(int val) {  _correct_guesses=val;}

    void bump_login_count(int cnt=1) { _times_logged+=cnt;}
    void bump_guess_count(int cnt=1) {  _guesses+=cnt;}
    void bump_guess_correct(int cnt=1) {  _correct_guesses+=cnt;}

private:
    string _login;
    string _user_name;
    int    _times_logged;
    int    _guesses;
    int    _correct_guesses;
     uLevel _user_level;

     static map _level_map;
     static void init_level_map();
     static string guest_login();
};
UserProfile::inline
//istream & operator>>(istream &is,UserProfile &rhs){
//    string login,level;
//    is>>login>>level;

//    int lcount,gcount,gcorrect;
//    is>>lcount>>gcount>>gcorrect;

//    rhs.reset_login(login);
//    rhs.reset_level(level);

//    rhs.reset_login_count(lcount);
//    rhs.reset_guess_count(gcount);
//    rhs.reset_guess_correct(gcorrect);
//    return is;
//}
//ostream& operator<<(ostream& os,const UserProfile &rhs)
//{//输出格式:stanl Beginner 12 100 10 10%
//    os< 
userProfile.cpp 
#include "userProfile.h"
#include

UserProfile::UserProfile()
    :_login("guess"),_user_level(Beginner),_times_logged(1),_guesses(0),_correct_guesses(0)
{
    static int id=0;
    char buffer[16];
    //将整数转ASCII字符串
    _itoa(id++,buffer,10);

    _login+=buffer;
}
double UserProfile::guess_average() const{
    return _guesses?double(_correct_guesses)/double(_guesses)*100:0.0;
}

UserProfile::UserProfile(string login,uLevel level)
    :_login(login),_user_level(level),_times_logged(1),_guesses(0),_correct_guesses(0)
{

}
bool UserProfile::operator==(const UserProfile& rhs){
    if(_login==rhs._login&&_user_name==rhs._user_name)
        return true;
    return false;
}
bool UserProfile::operator!=(const UserProfile& rhs){
    return !(*this==rhs);
}
string UserProfile::level()const{
    static string _level_table[]={
        "Beginner","Intermediate","Advanced","Guru"
    };
    return _level_table[_user_level];
}



map UserProfile::_level_map;

void UserProfile::init_level_map(){
    _level_map["Beginner"]=Beginner;
    _level_map["Intermediate"]=Intermediate;
    _level_map["Advanced"]=Advanced;
    _level_map["Guru"]=Guru;

}

void UserProfile::reset_level(const string& level){
    map::iterator it;
    if(_level_map.empty())
        init_level_map();
    //确保level的确代表一个可识别的用户等级
    _user_level=((it=_level_map.find(level))!=_level_map.end())?it->second:Beginner;

}



Matrix.h
#ifndef MATRIX_H
#define MATRIX_H
#include

using namespace std;
typedef float elemType;

class Matrix
{

    friend Matrix operator+(const Matrix&,const Matrix&);
    friend Matrix operator*(const Matrix&,const Matrix&);
public:
    Matrix(const elemType*);
    Matrix(elemType=0.,elemType=0.,elemType=0.,elemType=0.,
           elemType=0.,elemType=0.,elemType=0.,elemType=0.,
           elemType=0.,elemType=0.,elemType=0.,elemType=0.,
           elemType=0.,elemType=0.,elemType=0.,elemType=0.);

    int rows() const {return 4;}
    int cols() const {return 4;}

    ostream &print(ostream&) const;
    void operator+=(const Matrix&);
    elemType operator()(int row,int column) const
    {return  _matrix[row][column];}

    elemType &operator()(int row,int column)
    {return  _matrix[row][column];}

private:
    elemType _matrix[4][4];
};
inline ostream &operator<<(ostream& os,const Matrix &m){
    return m.print(os);
}

#endif // MATRIX_H

Matrix.cpp
#include "Matrix.h"

Matrix operator+(const Matrix& m1,const Matrix& m2){
    Matrix result(m1);
    result +=m2;
    return result;
}
Matrix operator*(const Matrix& m1,const Matrix& m2){
    Matrix result;//(m1)
    for (int ix=0;ix
        for (int jx=0;jx
            result(ix,jx)=0;
            for (int kx=0;kx
                result(ix,jx)+=m1(ix,kx)+m2(kx,jx);
            }
        }

    }
    return result;
}

void Matrix:: operator+=(const Matrix&m){

    for (int ix=0;ix<4;ix++) {
        for (int jx=0;jx<4;jx++) {
            _matrix[ix][jx]=m._matrix[ix][jx];

        }

    }
}

ostream &Matrix::print(ostream& os) const{
    int cnt=0;
    for (int ix=0;ix<4;++ix) {
        for (int jx=0;jx<4;++jx,++cnt) {
            if(cnt&&!(cnt%8))os<
    int array_index=0;

    for (int ix=0;ix<4;ix++) {
        for (int jx=0;jx<4;jx++) {
            _matrix[ix][jx]=array[array_index++];

        }

    }

}
Matrix::Matrix(elemType a11,elemType a12,elemType a13,elemType a14,
       elemType a21,elemType a22,elemType a23,elemType a24,
       elemType a31,elemType a32,elemType a33,elemType a34,
               elemType a41,elemType a42,elemType a43,elemType a44){
    _matrix[0][0]=a11;
    _matrix[0][1]=a12;
    _matrix[0][2]=a13;
    _matrix[0][3]=a14;

    _matrix[1][0]=a21;
    _matrix[1][1]=a22;
    _matrix[1][2]=a23;
    _matrix[1][3]=a24;

    _matrix[2][0]=a31;
    _matrix[2][1]=a32;
    _matrix[2][2]=a33;
    _matrix[2][3]=a34;

    _matrix[3][0]=a41;
    _matrix[3][1]=a42;
    _matrix[3][2]=a43;
    _matrix[3][3]=a44;
}



Matrix::Matrix()
{

}

globalwarppper.h
#ifndef GLOBALWARPPPER_H
#define GLOBALWARPPPER_H
#include
using namespace std;
class globalWarpper{
public:
    static int tests_passed()       { return  _tests_passed;    }
    static int tests_run()          { return  _tests_run;       }
    static int version_number()     { return  _version_number;  }
    static string version_stamp()   { return  _version_stamp;   }
    static string program_name()    { return  _program_name;    }

    static void tests_passed(int nval)
    { _tests_passed=nval;     }
    static void tests_run(int nval)
    { _tests_run=nval;       }
    static void version_number(int nval)
    { _version_number=nval;  }
    static void version_stamp(const string & nstamp)
    {  _version_stamp=nstamp;   }
    static void program_name(const string & npn)
    {  _program_name=npn;    }
private:

    static string _program_name;
    static string _version_stamp;
    static int    _version_number;
    static int    _tests_run;
    static int    _tests_passed;
};// This is available in all editors.

string globalWarpper::_program_name;
string globalWarpper::_version_stamp;
int    globalWarpper::_version_number;
int    globalWarpper::_tests_run;
int    globalWarpper::_tests_passed;
#endif // GLOBALWARPPPER_H

main.cpp
#include 
#include"Stack.h"
#include"userProfile.h"
#include"Matrix.h"
using namespace std;

int test_1(){
    Stack st;
    string str;
    while (cin>>str&&!st.full()) {
        st.push(str);
        if(cin.get()=='n')
                 break;
    }
    if(st.empty()){
        cout<<'n'<<"Oops:no strings were read -- bailing out n";
        return 0;
    }
    st.peek(str);
    if(st.size()==1&&str.empty()){
        cout<<'n'<<"Oops:no strings were read -- bailing out n";
        return 0;
    }
    cout<<'n'<<"Read in "<< st.size()<<" strings!n"
       <<"the strings, in reverse order:n";

    while (st.size()) {
        if(st.pop(str))
            cout<
    Stack st;
    string str;
    while (cin>>str&&!st.full()) {
        st.push(str);
        if(cin.get()=='n')
                 break;
    }
//    if(st.empty()){
//        cout<<'n'<<"Oops:no strings were read -- bailing out n";
//        return 0;
//    }
    cout<<'n'<<"read in "<>str;

    bool found= st.find(str);
    int count= found?st.count(str):0;

    cout<
//        cout<<'n'<<"Oops:no strings were read -- bailing out n";
//        return 0;
//    }
//    cout<<'n'<<"Read in "<< st.size()<<" strings!n"
//       <<"the strings, in reverse order:n";

//    while (st.size()) {
//        if(st.pop(str))
//            cout<>(istream &is,UserProfile &rhs){
//    string login,level;
//    is>>login>>level;

//    int lcount,gcount,gcorrect;
//    is>>lcount>>gcount>>gcorrect;

//    rhs.reset_login(login);
//    rhs.reset_level(level);

//    rhs.reset_login_count(lcount);
//    rhs.reset_guess_count(gcount);
//    rhs.reset_guess_correct(gcorrect);

//}

//UserProfile::inline
istream & operator>>(istream &is,UserProfile &rhs){
    string login,level;
    is>>login>>level;

    int lcount,gcount,gcorrect;
    is>>lcount>>gcount>>gcorrect;

    rhs.reset_login(login);
    rhs.reset_level(level);

    rhs.reset_login_count(lcount);
    rhs.reset_guess_count(gcount);
    rhs.reset_guess_correct(gcorrect);
    return is;
}
ostream& operator<<(ostream& os,const UserProfile &rhs)
{//输出格式:stanl Beginner 12 100 10 10%
    os<
    UserProfile anon;
    cout << anon;

    UserProfile anon_too;
    cout << anon_too;

    UserProfile anna( "AnnaL", UserProfile::Guru );
    cout << anna;

    anna.bump_guess_count( 27 );
    anna.bump_guess_correct( 25 );
    anna.bump_login_count();

    cout << anna;

    cout << "OK: enter user profile data by hand to see if we can read itn";
    cin >> anon;
    cout << anon;


}

void test_5(){
    Matrix m;
    cout<1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,0.,0.,1.,0.,0.,0.};

    Matrix identity(ar);
    cout<1.,2.,3.,4.,1.,5.,6.,7.,1.,8.,9.,10.,1.,11.,12.,13.};

    Matrix m3(ar2);
    cout<
//    test_4();
    test_5();
    cout << "Hello World!" << endl;
    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/883813.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号