C++把string转换成double,求整数a/b精确到小数点后任意位
// cppTest.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <cassert>
#include <iomanip>
// 求a/b,输出结果小数点后的前n位
void Div(int a, int b, int n)
{
using namespace std;
assert(n >= 0);
if (a < b)
cout << "0.";
else
cout << a/b << ".";
for (int i = 0; i < n; i++)
{//输出前n位
a *= 10;
while (a < b)
{
a *= 10;
cout << "0";
}
cout << a/b;
a = a%b;
}
cout << endl;
}
inline bool IsNum(const char c)
{
return c >= '0' && c <= '9';
}
inline bool IsAlpha(const char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 从字符串str中获得一个浮点数
// start表示这个浮点数在客串中的起始位置。end表示浮点数结束的下一个位置
// dot表示小数点所在的位置
bool CheckString(const char* str, int& start, int& dot, int& end)
{
if (str == NULL)//字符串为空
return false;
char* p = const_cast<char*>(str);
while(!IsNum(*p++));
p--;
start = p - str;
if (str[start - 1] == '.')// -.14f
{
start--; p--;
}
for (; IsNum(*p); p++);
if (*p == '.')
{
dot = p - str;
while(IsNum(*++p));
end = p - str;
return true;
}
dot = p - str;
end = p - str;
return true;
}
double Pow(const int a, const int b)
{
double rslt = 1;
for (int i = 0; i < b; i++)
rslt *= a;
return rslt;
}
// 把一个字符串转化成double型的浮点数
double Atof(const char* str)
{
int s, d, e;
if (!CheckString(str, s, d, e))
{
std::cout << "please check your string." << std::endl;
return 0;
}
double rslt = 0.0;
double pow = Pow(10, e - 1 - d);
// 计算小数部分
for (int i = e - 1; i > d; i--)
{
rslt += (str[i] - '0') / pow;
pow /= 10;
}
pow = 1;
// 计算整数部分
for (int i = d - 1; i >= s; i--)
{
rslt += (str[i] - '0') * pow;
pow *= 10;
}
if (s > 0 && str[s - 1] == '-')
rslt = -rslt;
return rslt;
}
void SerializePositive(int n)
{
using namespace std;
int tt, t, a;
bool ntag = true;
cout << "=====================\n";
cout << " " << n << endl;
for (int i = 1; ; i++)
{
tt = 2 * n / (i + 1);
t = tt - i;
if (t <= 0)
break;
if ((2 * n % (i + 1)) != 0 || (t % 2) != 0)
continue;
ntag = false;
a = t / 2;
for (int j = 0; j < i; j++)
cout << (a + j) << "+";
cout << (a + i);
cout << endl;
}
if (ntag)
cout << " NONE" << endl;
// cout << "====================" << endl;
}
#include <list>
int _tmain(int argc, _TCHAR* argv[])
{
int a = 8, b = 107;
Div(a, b, 100);
using namespace std;
cout << endl << setprecision(20) << Atof("kk-3.1415926535897932f22df") << endl;
for (int i = 100; i < 130; i++)
SerializePositive(i);
/*
int ab[] = {1, -2, -1, -3, -8, 8, 10, 8, -2, 10};
list<int> fff(ab, ab + 10);
list<int>::iterator iter_begin = fff.begin();
list<int>::iterator iter_end = fff.end();
list<int>::iterator iter, curIter;
for (iter = iter_begin; iter != iter_end; ++iter)
{
cout << *iter << " ";
}
cout << endl;
cout << fff.size() << endl;
for (iter = iter_begin; iter != iter_end; )
{
curIter = iter++;
if (*curIter < 0)
{
fff.erase(curIter);
}
}
cout << fff.size() << endl;
for (iter = iter_begin; iter != iter_end; ++iter)
{
cout << *iter << " ";
}
cout << endl;
*/
return 0;
}
//
#include "stdafx.h"
#include <cassert>
#include <iomanip>
// 求a/b,输出结果小数点后的前n位
void Div(int a, int b, int n)
{
using namespace std;
assert(n >= 0);
if (a < b)
cout << "0.";
else
cout << a/b << ".";
for (int i = 0; i < n; i++)
{//输出前n位
a *= 10;
while (a < b)
{
a *= 10;
cout << "0";
}
cout << a/b;
a = a%b;
}
cout << endl;
}
inline bool IsNum(const char c)
{
return c >= '0' && c <= '9';
}
inline bool IsAlpha(const char c)
{
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
}
// 从字符串str中获得一个浮点数
// start表示这个浮点数在客串中的起始位置。end表示浮点数结束的下一个位置
// dot表示小数点所在的位置
bool CheckString(const char* str, int& start, int& dot, int& end)
{
if (str == NULL)//字符串为空
return false;
char* p = const_cast<char*>(str);
while(!IsNum(*p++));
p--;
start = p - str;
if (str[start - 1] == '.')// -.14f
{
start--; p--;
}
for (; IsNum(*p); p++);
if (*p == '.')
{
dot = p - str;
while(IsNum(*++p));
end = p - str;
return true;
}
dot = p - str;
end = p - str;
return true;
}
double Pow(const int a, const int b)
{
double rslt = 1;
for (int i = 0; i < b; i++)
rslt *= a;
return rslt;
}
// 把一个字符串转化成double型的浮点数
double Atof(const char* str)
{
int s, d, e;
if (!CheckString(str, s, d, e))
{
std::cout << "please check your string." << std::endl;
return 0;
}
double rslt = 0.0;
double pow = Pow(10, e - 1 - d);
// 计算小数部分
for (int i = e - 1; i > d; i--)
{
rslt += (str[i] - '0') / pow;
pow /= 10;
}
pow = 1;
// 计算整数部分
for (int i = d - 1; i >= s; i--)
{
rslt += (str[i] - '0') * pow;
pow *= 10;
}
if (s > 0 && str[s - 1] == '-')
rslt = -rslt;
return rslt;
}
void SerializePositive(int n)
{
using namespace std;
int tt, t, a;
bool ntag = true;
cout << "=====================\n";
cout << " " << n << endl;
for (int i = 1; ; i++)
{
tt = 2 * n / (i + 1);
t = tt - i;
if (t <= 0)
break;
if ((2 * n % (i + 1)) != 0 || (t % 2) != 0)
continue;
ntag = false;
a = t / 2;
for (int j = 0; j < i; j++)
cout << (a + j) << "+";
cout << (a + i);
cout << endl;
}
if (ntag)
cout << " NONE" << endl;
// cout << "====================" << endl;
}
#include <list>
int _tmain(int argc, _TCHAR* argv[])
{
int a = 8, b = 107;
Div(a, b, 100);
using namespace std;
cout << endl << setprecision(20) << Atof("kk-3.1415926535897932f22df") << endl;
for (int i = 100; i < 130; i++)
SerializePositive(i);
/*
int ab[] = {1, -2, -1, -3, -8, 8, 10, 8, -2, 10};
list<int> fff(ab, ab + 10);
list<int>::iterator iter_begin = fff.begin();
list<int>::iterator iter_end = fff.end();
list<int>::iterator iter, curIter;
for (iter = iter_begin; iter != iter_end; ++iter)
{
cout << *iter << " ";
}
cout << endl;
cout << fff.size() << endl;
for (iter = iter_begin; iter != iter_end; )
{
curIter = iter++;
if (*curIter < 0)
{
fff.erase(curIter);
}
}
cout << fff.size() << endl;
for (iter = iter_begin; iter != iter_end; ++iter)
{
cout << *iter << " ";
}
cout << endl;
*/
return 0;
}
posted on 2007-10-03 20:56 preternatural 阅读(6918) 评论(0) 编辑 收藏 举报