第三次作业附加

这里是github的代码的传送门

代码改了挺久的....不知道有没有符合要求...

先附上已经改好的寒假作业之三

里面的代码基本上都已经规范化了..

这里附上github中修改完成的代码:

main.cpp

#include <iostream>
#include<cstdlib>
#include"Scan.h"
#include"Print.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main()
{
	Scan get_scanf;
	Print put_printf;
	
	/*调用 GetString() 函数,利用其返回值将输入的字符串传递给 ToStringQueue() 函数*/
	get_scanf.ToStringQueue(get_scanf.GetString());	
	put_printf.que = get_scanf.que;
	put_printf.putqueue();    //调用函数输出分好的数字与字符 
	system("pause");
}

 
    

Scan.h

/************************************************************
  FileName: 	Scan.h
  Author:       031502248
  Date:         2016/2/16
  History:        
  <author>     <time>   <version>      <desc>
  03150248     16/3/23     2.0       修改代码规范  
***********************************************************/
#ifndef SCAN_H
#define SCAN_H
#include<queue>
#include<string>
using namespace std;
class Scan
{
	public:
		Scan();
		~Scan();
		string GetString();
		void ToStringQueue(string input); 
		queue<string> que;
	private:
		string s;
		
};

#endif


Scan.cpp

#include "Scan.h"
#include<cstring>
#include<string>
#include<iostream>
#include<queue>
using namespace std;
Scan::Scan() 
{
}

Scan::~Scan() 
{
}

/*************************************************************
  functionname:    GetString
  Description:     用于输入一个string类和返回这个string类,目
                   的是为了避免外界直接接触类中的string变量 
  Input:           NULL
  Return:          string s:类中的私有string类型的变量 
  Others:          NULL
**************************************************************/
string Scan::GetString() 
{
	cin >> s;
	return s;
}

/*************************************************************
  functionname:    ToStringQueue
  Description:     将输入的字符串,逐个的字符扫描这,
                   将数字和符号提取出来分别存入队列 
  Input:           string input:输入字符串
  Return:         no return 
  Others:         NULL
**************************************************************/
void Scan::ToStringQueue(string input) 
{
	
	/*temp作为中间变量,暂时存储将要存入队列中的元素,重复利用temp,或者用temp[100]与int类型变量coun,利用数组分别存储数字与字符,再存入队列 */
	string temp = "";
	bool flag = false;           //判断是否存在超过十位的数
	bool flag_dot = false;       //判断是否存在小数点 
	int n = input.size();        //n表示字符串的长度
	for (int i = 0; i<n; i++)    //遍历字符串中的字符 
	{ 
		if (flag)    
		{ 
		    break;
		}
		if (input[i] >= '0' && input[i] <= '9' || input[i] == '.')    
		{
			if (input[i] == '.')
			{
				flag_dot = true;
			} 
			
			/*如果前面一位字符也是数字,或者当前字符是第一个字符,则先不将temp传入队列*/
			if (i == 0 || (input[i-1] >= '0' && input[i-1] <= '9' || input[i-1] == '.'))
			{
			}
			else if (temp.size())    //为了防止出现空串而错误将空串存入队列 
		    {
					Scan::que.push(temp);    
					flag_dot = false;
					temp = "";
			}
			temp += input[i];
			if (temp.size() > 10)    //判断是否存在超过10位的数字 
			{
				if (temp.size() == 11 && flag_dot == true)    //排除包含小数点而实际只有10位数的情况 
				{
				}
				else
				{
				    flag = true;
				} 
			}
		}
	    else    //当前字符是符号 
		{
			if (temp.size()>10) 
			{ 
				if (temp.size() == 11 && flag_dot)
				{
                } 
				else 
				{
					flag = true;
				} 
			}
			if (temp.size())     
			{ 
				Scan::que.push(temp);
				flag_dot = false;
				temp = "";
			}
			temp += input[i];
		}

	}
	
	/*遍历完成时,temp中依然有字符未存入队列,所以还需判断存在超过10位的数和将temp内容存入队列*/
	if (temp.size() > 10)    
	{ 
		if (temp.size() == 11 && flag_dot)
		{
		} 
		else
		{
			flag = true;
	    } 
	}
	if (temp.size()) 
	{
		Scan::que.push(temp);
		temp = ""; 
	}
	if (flag)    //判断如果存在超过十位的数,则只输出警告内容
	{ 
		while (!que.empty())    
		{
			que.pop();
		}    
		que.push("error:You can enter the number of digits can not be more than 10");
	}
	return ; 
}


Print.h

/************************************************************
  FileName: 	Print.h
  Author:       031502248
  Date:         2016/2/16
  History:        
  <author>     <time>   <version>      <desc>
  03150248     16/3/23     2.0       修改代码规范  
***********************************************************/
#ifndef PRINT_H
#define PRINT_H
#include "Scan.h"
class Print :Scan
{
	public:
		Print();
		~Print();
		queue<string> que;
		void putqueue(); 
	protected:
};

#endif


Print.cpp

#include "Print.h"
#include<iostream>
Print::Print()
{

}

Print::~Print()
{
}

/*************************************************************
  functionname:    putqueue
  Description:     输出类中私有成员变量中的queue类中的元素 
  Input:           NULL
  Return:          no return 
  Others:          NULL
**************************************************************/
void Print::putqueue()
{
	while (que.size())   //或者用que.empty(),只要队列不为空,就一直循环下去
	{
		std::cout << que.front() << endl; 
		que.pop(); 
	}
	return ; 
}


Print.h

/************************************************************
  FileName: 	Print.h
  Author:       031502248
  Date:         2016/2/16
  History:        
  <author>     <time>   <version>      <desc>
  03150248     16/3/23     2.0       修改代码规范  
***********************************************************/
#ifndef PRINT_H
#define PRINT_H
#include "Scan.h"
class Print :Scan
{
	public:
		Print();
		~Print();
		queue<string> que;
		void putqueue(); 
	protected:
};

#endif


Print.cpp

#include "Print.h"
#include<iostream>
Print::Print()
{

}

Print::~Print()
{
}

/*************************************************************
  functionname:    putqueue
  Description:     输出类中私有成员变量中的queue类中的元素 
  Input:           NULL
  Return:          no return 
  Others:          NULL
**************************************************************/
void Print::putqueue()
{
	while (!que.empty())   //或者用que.empty(),只要队列不为空,就一直循环下去
	{
		std::cout << que.front() << endl;
		que.pop(); 
	}
	return ; 
}


posted @ 2016-03-23 23:45  洛丶航  阅读(139)  评论(0编辑  收藏  举报