|
发表于 2013-5-31 12:53:20
|
显示全部楼层
大概明白你的意思了,上面那个之所以会有之前的结果,是因为精度问题
cout.precision(i); 这个方法保证了输出的精度,那是对同一个数的根号值不同精度的输出。
下面那个文本想保存之前输入的也很简单的,不过不是你这么干,需要把输入的文本保存下来,下次一并输入,代码如下:- #include <string>
- #include <iostream>
- using namespace std;
- int _tmain(int argc, _TCHAR* argv[])
- {
- #if(0)
- double result = sqrt(3.0);
- cout << "对3开根号保留小数点后0~9位,结果如下: \n" << endl;
- for( int i=0; i <= 9 ; i++ )
- {
- cout.precision(i);
- cout << result << endl;
- }
- cout << "当前输出精度为: " << cout.precision() << endl;
- #endif
- #if(1)
- string str = "";
- string perStr = "";
-
- cout << "输入一段文本: \n";
- while ( cin >>str)
- {
- if (0 != perStr.length())
- {
- str = perStr.append(str);
- }
- cout << str << endl;
- perStr = str;
- }
- #endif
- return 0;
- }
复制代码 结果如下:
|
|