string类型的"0.9144"如何转换成float类型的0.9144?

zhzh1126 2013-08-04 04:11:12
string类型的"0.9144"如何转换成float类型的0.9144?
...全文
161 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
N3verL4nd 2013-08-04
  • 打赏
  • 举报
回复
Stringstream
N3verL4nd 2013-08-04
  • 打赏
  • 举报
回复
Stringstream
startservice 2013-08-04
  • 打赏
  • 举报
回复
	float f;
	string str = "0.9144";
	f = atof(str.data());
jiandingzhe 2013-08-04
  • 打赏
  • 举报
回复
你可以用C++标准库的istringstream,boost的lexical_cast。
qq120848369 2013-08-04
  • 打赏
  • 举报
回复
NAME strtod, strtof, strtold - convert ASCII string to floating-point number SYNOPSIS #include <stdlib.h> double strtod(const char *nptr, char **endptr); float strtof(const char *nptr, char **endptr); long double strtold(const char *nptr, char **endptr);
net_assassin 2013-08-04
  • 打赏
  • 举报
回复
我写的代码:望指教
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
#include <sstream>
#include <map>

using namespace std;

ifstream input("input.txt");
ofstream output("output.txt");

map<string,string> divisionMap;

double deal(string str1,string str2)
{
	if (str2 == "feet")
	{
		str2 = "foot";
	}
	map<string,string>::iterator it = divisionMap.find(str2);
	if (it == divisionMap.end())
	{
		it = divisionMap.find(str2.substr(0,str2.length()-1));
		if (it == divisionMap.end())
		{
			it = divisionMap.find(str2.substr(0,str2.length()-2));
			if (it == divisionMap.end())
			{
				return 0;
			}
		}
	}
	
	return atof(str1.c_str())*atof((it->second).c_str());
}

int main()
{
	double sum = 0.0;
	string strline = " ";
	string strword1 = " ";
	string strword2 = " ";
	string division,meters;

	while (getline(input,strline) && strline != "")//建立 转换关系映射
	{
		istringstream line(strline);
		while(line>>division>>division>>meters>>meters)
		{
			divisionMap.insert(make_pair(division,meters));
		}
	}

	output<<"wang_chp@sina.cn"<<endl<<endl;
	bool minus = false;
	while(getline(input,strline) && strline != " ") //处理表达式
	{
		istringstream line(strline);
		while(line>>strword1)
		{
			if (strword1 == "+")
			{
				continue;
			}
			if (strword1 == "-")
			{
				minus = true;
				continue;
			}
			line>>strword2;
			
			if (minus)
			{
				sum += -deal(strword1,strword2);
				minus = false;
			}else{
				sum += deal(strword1,strword2);
			}
		}
	    output<<setiosflags(ios::fixed)<<setprecision(2)<<sum<<" m"<<endl;
		sum = 0;
	}
	input.close();
	output.close();
	return 0;
}
net_assassin 2013-08-04
  • 打赏
  • 举报
回复
lz是不是在做渣打银行codemarathon的那个测试题啊 呵呵 atof
zhctj159 2013-08-04
  • 打赏
  • 举报
回复
atof <math.h> and <stdlib.h> double atof( const char *string );
水平不流 2013-08-04
  • 打赏
  • 举报
回复
atof函数可以实现.
super_admi 2013-08-04
  • 打赏
  • 举报
回复
用函数atof?

64,648

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧