面向對象程序設計 第三次做業

Github鏈接https://github.com/zora02/object-oriented/tree/master/Caculatorhtml

1、題目

題目描述鏈接:http://www.cnblogs.com/fzuoop/p/5187275.htmlios

2、準備過程

  1. 把c++遠征計劃中的離港篇和封裝篇上看完了。(學習計劃並無完成T_T
  2. 發現題目中要求用到queue的知識,就去度娘了有關隊列的使用,大概知道該怎麼用吧。
  3. 原本在電腦裏下了c++ primer plus看一部分,發現根本看不下去,也看不太懂(╥﹏╥)

3、解題遇到的問題

  1. 最開始把兩個類都寫在main.cpp這個文件夾中,運行的時候發現本身沒有處理報錯的要求。一開始我把報錯放在Print 這個類中,發現調用的參數不對。後來問了學霸,他建議我放在Scan類中比較好,我就改過去了,調用參數也比較容易。原本我用break語句跳出,後來學霸建議我用 exit比較好,我又去度娘了exit的用法,又學到同樣東西~~
  2. 將兩個類放到分文件的過程當中,因爲是初學吧,我對這個操做感到很陌生,遇到了類重定義的問題,發現是個人分文件中的語法有問題後,我又去把有關的視頻教程看了一遍,改來改去改了好多遍才改對>_<

4、代碼

main.cppc++

#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.cppgit

#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.hgithub

#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.cppoop

#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 */
相關文章
相關標籤/搜索