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;
    }
  • 相关阅读:
    递归遍历树形json
    关于作用域理解的一道题
    微信站 返回上一页并刷新
    Vuex有哪些作用
    两段代码实现vue路由懒加载
    Vuex目录结构推荐
    售后打电话说现场设备出问题了,嵌入式工程师最想干什么?
    Qt编译出现cc1plus.exe: out of memory allocating 65536 bytes问题
    OpenCV计算机视觉编程攻略(第三版)源码
    C++ Json工具--Jsoncpp用法简介
  • 原文地址:https://www.cnblogs.com/davidgu/p/4640721.html
Copyright © 2011-2022 走看看