从控制台读入double数据的容错处理(附完整可运行代码)

来源:互联网 发布:怎样拍淘宝照片 编辑:程序博客网 时间:2024/05/18 03:56

以下是我自己写的从控制台读入double数据的容错处理的代码(可运行的完整代码在后面, 请大家 多多指点!!)

代码思想:

1 用栈 保存 控制台中的各个符合条件字符(位权大的先入栈, 例如:2.3, '2'先入栈, 接着是'.', 最后是'3')。

2.记录 小数点 的位置。

3. 每个数字位符依次出栈, 将各个字符转换成整数数字, 然后进行权位转换(例如:2.3: 第一个出栈的是'3', 操作是 3*pow(10, -1)即0.3, 第二个出栈的是'.'不做处理,最后一个出栈的是'2',操作是:2*pow(10, 1)即2,   将所得的2 加上0.3 得 2.3。

// 从控制台 接收 double 类型 的 数据 的容错处理 double InputDoubleVariableFromConsole(){stack<char> sLength;int iFlag = 0; // 小数点 的标示位; 1表示点已经存在, 0 表示点不存在int iDotPosition = 0; // 小数点 的位置int iCount = 0; // 记录 从控制台 输入的 字符的个数char cTempChar;// 临时 记录 从控制台 接收 的字符while ((cTempChar = cin.get()) != '\n'){if ((cTempChar >= '0' && cTempChar <= '9') || cTempChar == '.'){iCount++;if (cTempChar == '.'){if ( 0 == iFlag) // 小数点 不存在{iDotPosition = iCount; // 记录 小数点 的位置iFlag = 1; }else// 小数点 已经存在{cout << "Input the length wrong, input again!" << endl;// 刷新 输入缓冲区cin.sync();cin.clear();ClearStack(sLength); // 清空 栈 中 的数据// 重新 置0iFlag = 0; // 小数点 不存在iCount = 0;  // 重置栈中的数据个数为0continue;}}sLength.push(cTempChar); // 压入栈}else{cout << "Input the length wrong, input again!" << endl;// 刷新 输入缓冲区cin.sync();cin.clear();ClearStack(sLength); // 清空 栈 中 的数据// 重新 置0iFlag = 0; // 小数点 不存在iCount = 0;  // 重置栈中的数据个数为0continue;}}/*****************(2012年3月31日10:10:34 添加)********************/// 如果不存在 小数点, 为了后面 计算方便 额外加入一个小数点 if ( 0 == iFlag) // 小数点 不存在   {  iDotPosition = sLength.size() + 1; // 记录 小数点 的位置   sLength.push('.'); // 将小数点 压入栈  iFlag = 1;   }  /*****************(2012年3月31日10:10:34 添加)********************/double iTempLength = 0; // 返回 的 double 数据         while (!sLength.empty()){int iLong = sLength.size(); // 栈的 长度if (iLong > iDotPosition)  // 计算 小数部分。栈的长度大于小数点的位置{iTempLength += (sLength.top() - 48) * pow(10.0, iDotPosition - iLong);}else if (iLong < iDotPosition) // 计算 整数部分。栈的长度小于小数点的位置{// 因为 指数 从 0 开始, 所以 减1iTempLength += (sLength.top() - 48) * pow(10.0, iDotPosition - iLong - 1);}else{}sLength.pop(); // 出栈}return iTempLength;} 


 

完整代码介绍

1.该程序 是 c++ 控制台程序, 共有 三个文件:RectangleCodeTest.cpp, My_head_rectangle.cpp, My_head_rectangle.h。

2.从控制台 接收 矩形 的长和宽, 完成对矩形的面积和周长的计算与输出。

3. 我 把 头文件 和 源文件 贴到下面, 分享一下 我的思路。

4. 请各位 不要手下留情, 帮我看看 我的程序中有哪些可以再进一步的改进, 有哪些不妥之处, 谢谢!  f

附测试完整代码如下:

RectangleCodeTest.cpp 文件

/* Created: 2012年3月29日11:13:24 * * FileName: RectangleCodeText.cpp * * Author: Renzhibo * * Subject: 设计一个矩形类,完成计算矩形面积和显示矩形属性功能。 * * Summary: 1. 注意 头文件 放置的 位置 */#include <iostream>#include <cmath>#include <stack>using std::cin;using std::cout;using std::endl;using std::stack;#include "My_head_rectangle.h"    void ClearStack(stack<char>& s);double InputDoubleVariableFromConsole();int main(){double length = 0, width = 0;My_rectangle myrectangle(length, width);// 输入 矩形 长 和 宽cout << "Please input the length:" << endl;myrectangle.Set_Length(InputDoubleVariableFromConsole());    cout << "Please input the width:" << endl;myrectangle.Set_Width(InputDoubleVariableFromConsole());myrectangle.Get_my_rectangle_area();  // 计算 矩形的 面积myrectangle.Show_my_rectangle_area(); // 显示 矩形 面积myrectangle.Get_my_rectangle_circumference();  // 计算 矩形的 圆周长myrectangle.Show_my_rectangle_circumference(); // 显示 矩形 圆周长myrectangle.Show_my_rectangle_property(); // 显示 矩形的 长宽 属性cin.get();return 0;}// 从控制台 接收 double 类型 的 数据 的容错处理 double InputDoubleVariableFromConsole(){stack<char> sLength;int iFlag = 0; // 小数点 的标示位; 1表示点已经存在, 0 表示点不存在int iDotPosition = 0; // 小数点 的位置int iCount = 0; // 记录 从控制台 输入的 字符的个数char cTempChar;// 临时 记录 从控制台 接收 的字符while ((cTempChar = cin.get()) != '\n'){if ((cTempChar >= '0' && cTempChar <= '9') || cTempChar == '.'){iCount++;if (cTempChar == '.'){if ( 0 == iFlag) // 小数点 不存在{iDotPosition = iCount; // 记录 小数点 的位置iFlag = 1; }else// 小数点 已经存在{cout << "Input the length wrong, input again!" << endl;// 刷新 输入缓冲区cin.sync();cin.clear();ClearStack(sLength); // 清空 栈 中 的数据// 重新 置0iFlag = 0; // 小数点 不存在iCount = 0;  // 重置栈中的数据个数为0continue;}}sLength.push(cTempChar); // 压入栈}else{cout << "Input the length wrong, input again!" << endl;// 刷新 输入缓冲区cin.sync();cin.clear();ClearStack(sLength); // 清空 栈 中 的数据// 重新 置0iFlag = 0; // 小数点 不存在iCount = 0;  // 重置栈中的数据个数为0continue;}}double iTempLength = 0; // 返回 的 double 数据
/*****************(2012年3月31日10:10:34 添加, 为了能够处理输入的整数数据)********************/// 如果不存在 小数点, 为了后面 计算方便 额外加入一个小数点 if ( 0 == iFlag) // 小数点 不存在   {  iDotPosition = sLength.size() + 1; // 记录 小数点 的位置   sLength.push('.'); // 将小数点 压入栈  iFlag = 1;   }  /*****************(2012年3月31日10:10:34 添加)********************/
while (!sLength.empty()){int iLong = sLength.size(); // 栈的 长度if (iLong > iDotPosition)  // 计算 小数部分。栈的长度大于小数点的位置{iTempLength += (sLength.top() - 48) * pow(10, iDotPosition - iLong);}else if (iLong < iDotPosition) // 计算 整数部分。栈的长度小于小数点的位置{// 因为 指数 从 0 开始, 所以 减1iTempLength += (sLength.top() - 48) * pow(10, iDotPosition - iLong - 1);}else{}sLength.pop(); // 出栈}return iTempLength;}void ClearStack(stack<char>& s){while(s.empty()==false)s.pop();return;}

 

My_head_rectangle.h 文件

/* Created: 2012年3月29日11:13:46** FileName: My_head_rectangle.h ** Author: Renzhibo** Subject: ** Summary:  1. 宏定义中 的 变量 的命名 规范 和 变量一样 ,不可以 有点*/#ifndef __MY_HEAD_RECTANGLE_H#define __MY_HEAD_RECTANGLE_Hclass My_rectangle{private:double my_rectangle_length;double my_rectangle_width;double my_rectangle_area;double my_rectangle_circumference;public://constructMy_rectangle(double dLength, double dWidth, double dArea = 0, double dCircumference = 0) : my_rectangle_length(dLength), my_rectangle_width(dWidth), my_rectangle_area(dArea),my_rectangle_circumference(dCircumference){}// methodvoid Set_Length(double dLength);void Set_Width(double dWidth);void Get_my_rectangle_area();void Get_my_rectangle_circumference();void Show_my_rectangle_area();void Show_my_rectangle_circumference();void Show_my_rectangle_property();//destruct~My_rectangle(){}};#endif

 

My_rectangle_head.cpp文件

/* Created: 2012年3月29日11:13:53** FileName: My_head_rectangle.cpp ** Author: Renzhibo** Subject: ** Summary:  */#include <iostream>using std::endl;using std::cout;#include "My_head_rectangle.h"/******************************************************* My_Rectangle类的 设置 长  成员函数 体******************************************************/void My_rectangle::Set_Length(double dLength){this->my_rectangle_length = dLength;return;}/******************************************************* My_Rectangle类的 设置 宽  成员函数 体******************************************************/void My_rectangle::Set_Width(double dWidth){this->my_rectangle_width = dWidth;return;}/******************************************************* My_Rectangle类的 获得 面积 成员函数 体******************************************************/void My_rectangle::Get_my_rectangle_area(){my_rectangle_area = my_rectangle_length * my_rectangle_width;return;}/******************************************************* My_Rectangle类的 显示 矩形面积 的 成员函数 体******************************************************/void My_rectangle::Show_my_rectangle_area(){cout << "The Rectangle's area is:" << my_rectangle_area << endl;return;}/******************************************************* My_Rectangle类的 获得 圆周 成员函数 体******************************************************/void My_rectangle::Get_my_rectangle_circumference(){my_rectangle_circumference = 2 * (my_rectangle_length + my_rectangle_width);return;}/******************************************************* My_Rectangle类的 显示 矩形面积 的 成员函数 体******************************************************/void My_rectangle::Show_my_rectangle_circumference(){cout << "The Rectangle's circumference is:" << my_rectangle_circumference << endl;return;}/******************************************************* My_Rectangle类的 显示矩形的 长 宽  成员函数 体******************************************************/void My_rectangle::Show_my_rectangle_property(){cout << "The Rectangle' length is:" << my_rectangle_length << "\t"<< ",width is:" << my_rectangle_width << endl;return;}


以上 ~~~

请 大家 多给我点 建议, 谢谢!!