输入字符串,其中整数应为 - C++
Input String where Integer should be - C++
我是初学者,在学习Stroustrup的原则和实践时被困在这样一个简单的问题上。
仅使用基本元素
#include "std_lib_facilities.h"
int main()
{
double highest = 0;
double lowest = 100;
int i=0;
double sum = 0;
vector <double> inputlist;
double input;
string unit;
cout<<"Type in a number followed by it's unit n";
while(cin>>input>>unit){
inputlist.push_back(input);
sum += inputlist[i];
if (input >= lowest && input <= highest){
cout<<input<<" n";
++i;
}
else if (input < lowest){
lowest = input;
cout<<"nLowest Number so far n"<<lowest;
++i;
}
else if (input > highest){
highest = input;
cout<<"nHighest number so far n"<< highest;
++i;
}
else
cout<<"Lowest is: n"<<lowest<<"nn Highest is: n"<<highest<<" nn and the total is: n"<<sum;
if (unit == "ft", "m", "in","cm")
cout<<unit<<"n";
else
cout<<"cannot recognize unit";
}
keep_window_open();
return 0;
}
我需要程序在输入字符"|"时向用户显示总和以及最高值和最低值。问题是:我需要在应该输入整数值的地方输入这个。
注意:我对转换了解不多,但尝试了一些,但它们不起作用。
如果我理解正确,您想从std::cin
中读取int
,但是:
int i;
if (std::cin >> i) {
...
不符合您的需求,因为可能有'|'
符号作为终止读取的信号。
您可以执行以下操作:逐字读取输入(std::string
)并使用临时std::istringstream
分别解析这些单词:
std::string word;
if (std::cin >> word) {
if (word == "|")
...
// else:
std::istringstream is(word);
int i;
if (is >> i) {
// integer successfully retrieved from stream
}
}
只是#include <sstream>
读取带有字符串的值。 如果不匹配 | 使用以下函数将其转换为双精度:
double toDouble(string s)
{
int sign = 1, i=0;
if (s[0]=='-')
sign = -1, i=1;
double result = 0, result2 = 0;
for (; i < s.size(); i++)
if (s[i] == '.')
break;
else
result = result * 10 + (s[i] - '0');
for (i = s.size()-1 ; i>=0 ; i--)
if (s[i] == '.')
break;
else
result2 = result2 / 10 + (s[i] - '0');
if (i>=0)
result += result2/10;
return result * sign;
}
用英寸对米求和没有多大意义。因此,应考虑将单位转换为比例因子。您可以使用地图来获取比例因子。即使这有点过调,您也可以使用正则表达式来解析用户输入。如果正则表达式不匹配,您可以测试诸如"|"之类的内容。在新的 c++ 标准 (http://en.wikipedia.org/wiki/C%2B%2B11) 中,为此目的定义了正则表达式库。可惜的是,g++ 正则表达式库有问题。但是您可以使用增强 (http://www.boost.org/doc/libs/1_54_0/libs/regex/doc/html/boost_regex/)。下面是一个示例:
#include <iostream>
#include <vector>
#include <map>
#include <boost/regex.hpp> //< Pittyingly std::regex is buggy.
using namespace std; ///< Avoid this in larger projects!
using namespace boost;
int main() {
const string strReFloat("([-+]?[[:digit:]]*\.?[[:digit:]]+(?:[eE][-+]?[[:digit:]]+)?)");
const string strReUnit("([[:alpha:]]+)");
const string strReMaybeBlanks("[[:blank:]]*");
const string strReFloatWithUnit(strReMaybeBlanks+strReFloat+strReMaybeBlanks+strReUnit+strReMaybeBlanks);
const regex reFloatWithUnit(strReFloatWithUnit);
const map<const string,double> unitVal= {
{"m", 1.0},
{"in", 0.0254},
{"ft", 0.3048},
{"cm", 0.01}
};
double highest = 0;
double lowest = 100;
int i=0;
double sum = 0;
vector <double> inputlist;
double input;
double unitToMeter;
string unit;
string str;
while( (cout<<"nType in a number followed by it's unit n", getline(cin,str), str != "") ){
smatch parts;
if( regex_match(str,parts,reFloatWithUnit) ) {
unit = parts[2].str();
auto found = unitVal.find(unit);
if( found != unitVal.end() ) {
cout<<unit<<"n";
input = found->second * atof(parts[1].str().c_str());
} else {
cout << "Unit "" << unit << "" not recognized. Using meters.n";
}
inputlist.push_back(input);
sum += inputlist[i];
if (input >= lowest && input <= highest){
cout<<input<<" n";
++i;
}
else if (input < lowest){
lowest = input;
cout<<"nLowest Number so far n"<<lowest;
++i;
}
else if (input > highest){
highest = input;
cout<<"nHighest number so far n"<< highest;
++i;
}
} else if( str == "|" ) {
cout << "sum:" << sum << "n";
} else {
cout << "Input not recognized.n";
}
}
return 0;
}
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- C++:如何循环通过向量中的整数元素
- 我可以信任表示整数的浮点或双精度来保持精度吗
- 序列化,没有库的整数,得到奇怪的结果
- 在一定长度后从数组中打印时缺少整数
- std::当在256字节边界上写入整数时,流的奇怪行为
- 内联程序集printf将整数解释为地址
- 是否基于数组B整数打印数组A中的整数
- 如何在C++中将整数转换为其数字数组