第三次作业

代码

遇到的一些状况

1.要求函数return一个队列,以前从未这么试过,查阅资料后得知,定义的时候直接

queue<string> ToStringQueue(string input)

2.考虑超过10位数字报错。难点在于对于这个的理解不是很明白。例如0.123456789算几位数呢?最终在我的程序中,0.123456789算合法数字,0.1234567891就算错误。例如1.00000000000这样我也视作不合法。

3.一些特殊情况是否判断。例如 "1." 或者 ".1" 这2个数字算合法吗?

4.第一次做的时候将两个类直接写进main.cpp里面,经过重新阅读题目要求之后直接拆开,写到一个项目里面。

代码一览

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);
    /*void Print( )
    {//test1
        while (s.size())
        {
            cout<<s.front()<<endl;
            s.pop();
        }
    }*/
    /*bool isfailed ()
    {
        return failed;
    }*/
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;

queue<string> Scan::ToStringQueue(string input)
{
    failed=0;
    count=0;
    string temp="";
    for (int i=0;i<input.size();i++)
    {
        if (input[i]>='0'&&input[i]<='9')
        {
          temp+=input[i];//get number
          count++;
          if (count>10)
          {
            failed=1;
            break;
          }
        }
        else
        {
            if (input[i]=='.')//decimal
            {
                temp+='.';
                continue;
            }
            if (temp!="")//number and operator
            {
              count=0;
              s.push(temp);
              temp=input[i];
              s.push(temp);
              temp="";
            }
            else//continual operator
            {
              temp=input[i];
              s.push(temp);
              temp="";
            }
        }
    }
    if (temp!="")//last number
        s.push(temp);
    if (failed)
    {//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;

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/5215152.html