zoukankan      html  css  js  c++  java
  • 一个完整的C++程序SpreadSheet

    1. SpreadsheetCell.h

    #pragma once
    
    #include <string>
    
    class SpreadsheetCell
    {
    public:
        void setValue(double inValue);
        double getValue() const;
    
        void setString(const std::string& inString);
        const std::string& getString() const;
    
    private:
        std::string doubleToString(double inValue) const;
        double stringToDouble(const std::string& inString) const;
    
        double mValue;
        std::string mString;
    };

    2. SpreadsheetCell.cpp

    #include "SpreadsheetCell.h"
    
    #include <iostream>
    #include <sstream>
    using namespace std;
    
    void SpreadsheetCell::setValue(double inValue)
    {
        mValue = inValue;
        mString = doubleToString(mValue);
    }
    
    double SpreadsheetCell::getValue() const
    {
        return mValue;
    }
    
    void SpreadsheetCell::setString(const string& inString)
    {
        mString = inString;
        mValue = stringToDouble(mString);
    }
    
    const string& SpreadsheetCell::getString() const
    {
        return mString;
    }
    
    string SpreadsheetCell::doubleToString(double inValue) const
    {
        ostringstream ostr;
    
        ostr << inValue;
        return ostr.str();
    }
    
    double SpreadsheetCell::stringToDouble(const string& inString) const
    {
        double temp;
    
        istringstream istr(inString);
    
        istr >> temp;
        if (istr.fail() || !istr.eof()) {
            return 0;
        }
        return temp;
    }

    3. SpreadSheetCellInStackTest.cpp

        (在堆栈中创建并使用对象)

    #include <iostream>
    #include "SpreadsheetCell.h"
    #include "SpreadSheetCellInStackTest.h"
    
    using namespace std;
    
    void SpreadSheetCellInStackTest::run()
    {
        SpreadsheetCell myCell, anotherCell;
        myCell.setValue(6);
        anotherCell.setString("3.2");
    
        cout << "cell 1: " << myCell.getValue() << endl;
        cout << "cell 2: " << anotherCell.getValue() << endl;
    }
  • 相关阅读:
    ICQ
    Create小程序
    LRU最近最少使用算法
    感知器
    聚类-K均值
    阈值分类法
    最邻近分类
    设计模式
    高级排序算法
    Socket编程
  • 原文地址:https://www.cnblogs.com/davidgu/p/4640721.html
Copyright © 2011-2022 走看看