面向对象程序设计 第三次作业

Github连接https://github.com/zora02/object-oriented/tree/master/Caculator

一、题目

题目描述连接:http://www.cnblogs.com/fzuoop/p/5187275.html

二、准备过程

  1. 把c++远征计划中的离港篇和封装篇上看完了。(学习计划并没有完成T_T
  2. 发现题目中要求用到queue的知识,就去度娘了有关队列的使用,大概知道该怎么用吧。
  3. 本来在电脑里下了c++ primer plus看一部分,发现根本看不下去,也看不太懂(╥﹏╥)

三、解题遇到的问题

  1. 最开始把两个类都写在main.cpp这个文件夹中,运行的时候发现自己没有处理报错的要求。一开始我把报错放在Print 这个类中,发现调用的参数不对。后来问了学霸,他建议我放在Scan类中比较好,我就改过去了,调用参数也比较容易。本来我用break语句跳出,后来学霸建议我用 exit比较好,我又去度娘了exit的用法,又学到一样东西~~
  2. 将两个类放到分文件的过程中,由于是初学吧,我对这个操作感到很陌生,遇到了类重定义的问题,发现是我的分文件中的语法有问题后,我又去把有关的视频教程看了一遍,改来改去改了好多遍才改对>_<

四、代码

main.cpp

 #include <iostream>
 #include <string>
 #include <queue>
 #include <stdlib.h>
 #include "Scan.hpp"
 #include "Print.hpp"
using namespace std;


int main() {
    string s;
    cin >> s;
    Print :: PrintQueue(Scan :: ToStringQueue(s));
}

Scan.cpp

 #include "Scan.hpp"
 #include <iostream>
 #include <string>
 #include <queue>
 #include <stdlib.h>
using namespace std;

bool Scan::checkNum(char c)
    {
        return ('0' <= c && c <= '9') || c == '.'; //判断数字或者是小数点
    }
queue<string> Scan::ToStringQueue(string s)
    {
        int len = s.size();
        string tem_s;
        queue<string> q;
        for (int i = 0 ; i < len;)
        {
            tem_s.clear();
            if(!checkNum(s[i])) //判断是符号还是数字
            {
                tem_s += s[i++]; //符号
                
            }
            else
            {
                while(i < len && checkNum(s[i]))
                    tem_s += s[i++]; //数字
            }
            if(tem_s.size() > 10)
            {
                cout << "Error" << endl;
                exit(0); //报错
            }
            q.push(tem_s);
        }
        return q;
        
    };

Scan.h


 #ifndef Scan_hpp
 #define Scan_hpp
 #include <iostream>
 #include <string>
 #include <queue>
 #include <stdlib.h>
using namespace std;

class Scan
{
    public :
    static bool checkNum(char c);

    static queue<string> ToStringQueue(string s);
    
    queue<string> q;
  
};


 #endif /* Scan_hpp */

Print.cpp


 #include "Print.hpp"
 #include <iostream>
 #include <string>
 #include <queue>
 #include <stdlib.h>
using namespace std;


void Print::PrintQueue(queue<string> q)
    {
        while(!q.empty())
            cout << q.front() << endl, q.pop();
    };

Print.h


 #ifndef Print_hpp
 #define Print_hpp
 #include <iostream>
 #include <string>
 #include <queue>
 #include <stdlib.h>
using namespace std;

class Print
{
public:
    static void PrintQueue(queue<string> q);
    
};


 #endif /* Print_hpp */
原文地址:https://www.cnblogs.com/perhap-s/p/5215590.html