天天看點

面向對象程式設計 第三次作業

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 */