附加作业3

附加作业

稍微规范了一点吧。。。

Scan.h

/* 
 * File:   Scan.h
 * Author: fewdan
 *
 * Created on 2016年2月24日, 下午7:59
 */

#ifndef SCAN_H
#define	SCAN_H
#include <iostream>
#include <string>
#include <queue>
using namespace std;

class Scan
{
public:
    queue<string> ToStringQueue (string input);
private:
    /* 键盘输入的字符串 */
    queue<string>s;
    /* 当前处理数字长度 */
    int count;
    /* 输入的字符串中是否有不满足的条件的数字 */
    bool failed;
};

#endif	/* SCAN_H */

Scan.cpp

/* 
 * File:   Scan.cpp
 * Author: fewdan
 * 
 * Created on 2016年2月24日, 下午7:59
 */

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

/*************************************************
  Description:    将键盘输入的待处理字符串,处理成规定的字符串队列
  Input:          待处理字符串
  Output:         
  Return:         字符串队列
  Others:         
*************************************************/
queue<string> Scan::ToStringQueue(string input)
{
    failed = 0;
    count = 0;
    string temp = "";
    for (int i = 0 ; i < input.size() ; i++)
    {
        /* get number */
        if (input[i] >= '0' && input[i] <= '9')
        {
            temp += input[i];
            count++;
            /* 数字长度大于10 */
            if (count > 10)
            {
                failed = 1;
                break;
            }
        }
        else
        {
            /* 处理小数 */
            if (input[i] == '.')//decimal
            {
                temp += '.';
                continue;
            }
            /* 数字+运算符
               number and operator  */
            if (temp != "")
            {
                /* 压入数字 */
                count = 0;
                s.push(temp);
                /* 压入运算符 */
                temp = input[i];
                s.push(temp);
                temp = "";
                /* 清空临时变量 */
            }
            else
            {
                /* continual operator */
                temp = input[i];
                s.push(temp);
                temp = "";
            }
        }
    }
    /* last number */
    if (temp != "")
        s.push(temp);
    /* 数字不符合要求 */
    if (failed)
    {
        /* 清空队列 */
        while (s.size())
            s.pop();
        /* 压入错误信息 */
        s.push("Error!!!!!!!!!!!!!!!!!!");
    }
    return s;
}

Print.h

/* 
 * File:   Print.h
 * Author: fewdan
 *
 * Created on 2016年2月24日, 下午8:06
 */

#ifndef PRINT_H
#define	PRINT_H
#include <iostream>
#include <string>
#include <queue>
using namespace std;

class Print {
public:
    void Myprint (queue<string>s);
};

#endif	/* PRINT_H */

Print.cpp

/* 
 * File:   Print.cpp
 * Author: fewdan
 * 
 * Created on 2016年2月24日, 下午8:06
 */

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

/*************************************************
  Description:    将得到的字符串队列输出
  Input:          字符串数列
  Output:         输出字符串数列
  Return:         
  Others:         
*************************************************/
void Print::Myprint(queue<string>s)
{
    while (s.size())
    {
        cout<<s.front()<<endl;
        s.pop();
    }
}

main.cpp

/* 
 * File:   main.cpp
 * Author: fewdan
 *
 * Created on 2016年2月24日, 下午7:58
 */

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

int main ()
{
    /* 输入类 */
    Scan IN;
    /* 输出类 */
    Print OUT;
    /* 临时变量,输入字符串 */
    string tempin;
    /* 中间变量,传递字符串队列 */
    queue<string>tempqueue;
    cin>>tempin;
    tempqueue=IN.ToStringQueue(tempin);
    OUT.Myprint(tempqueue);
    return 0;
}
原文地址:https://www.cnblogs.com/Coolaaa/p/5321470.html