贴一个我则研究出来的东东:double 数据类型的计算过程是不精确的。为此,我进一步展开了尝试,写了一个程序:#include <iostream> using namespace std;int main() { double x = 0.000000; for (; x<4.000000; x=x+0.100000); if ( 4.000000*x > 16.000000 ) printf ("4x = %f, x = %f\n", 4*x, x); return 0; }因为 4*x = 16,所以,在通常看来,这个程序它并不应该输出什么东西,可是事实上它却输出了: 4x = 16.000000, x = 4.000000于是,我就在 printf 那儿设置一个断点,进入调试,会发现此时 x = 4.0000000000000018但是,如果写个简单一点的却又是另外一种情况:#include <iostream> using namespace std;int main() { double x = 0.000000;
x += 0.100000; if ( x > 0.100000 ) printf ("x = %f\n", x); return 0; }经调试发现 x = 0.1000000000000001 而且,此时似乎又正常了,不输出任何东西。如果把 x 自加 10 次 0.100000 你会发现结果的 x 不是 1.000000 ,在 Visual C++ 6.0 里的结果是 x = 0.99999999999999989,如果把它和 1 去比较的话,它会显示比 1 小了。 所以,double 类型是不精确的,在进行大小判断时,不能把它与某个确切的值进行比较,而是应该给个范围。
我也没什么意思,我们做程序的NNNNNNN.
虽然结贴了,我还是要再贴几个经典 C++ 程序,希望给楼主以帮助: 1.格式化输出 #include <iostream> #include <iomanip> using namespace std;int main () { int i; cout << "A list of numbers:" << endl; for (i = 1; i <= 1024; i *= 2) { cout.width (7); cout << i << endl; } cout << "A table of numbers:" << endl; for (i = 0; i <= 4; i++) { cout << setw(3) << i << setw(5) << i * i * i << endl; } return 0; }2.获取字符串数据 #include <iostream> #include <strstream> #include <cstring> using namespace std;int main () { char a[1024]; istrstream b(a, 1024); strcpy (a, "45.656"); double k, p; b.seekg(0); // Start from first character. b >> k; k = k + 1; cout << k << endl; strcpy (a, "444.23 56.89"); b.seekg(0); b >> k >> p; cout << k << ", " << p + 1 << endl; return 0; }3.字符串数组操作 #include <iostream> #include <strstream> #include <cstring> #include <cmath> using namespace std;int main () { char a[1024]; ostrstream b(a, 1024); b.seekp(0); // Start from first char. b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl ) // ends is simply the // null character '\0' cout << a << endl; double v = 2; strcpy (a, "A sinus: "); b.seekp(strlen (a)); b << "sin (" << v << ") = " << sin(v) << ends; cout << a << endl; return 0; }4.读文件 #include <iostream> #include <fstream> using namespace std;int main () { fstream f; char c; cout << "What's inside the test.txt file from"; cout << "the C: hard disk root " << endl; cout << endl; f.open("c:\\test.txt", ios::in); while (! f.eof() ) { f.get(c); // Or c = f.get() cout << c; } f.close(); return 0; }5. 写文件 #include <iostream> #include <fstream> using namespace std;int main () { fstream f; f.open("c:\\test.txt", ios::out); f << "This is a text output to a file." << endl; double a = 345; f << "A number: " << a << endl; f.close(); return 0; }
using namespace std;int main()
{
double x = 0.000000; for (; x<4.000000; x=x+0.100000);
if ( 4.000000*x > 16.000000 )
printf ("4x = %f, x = %f\n", 4*x, x); return 0;
}因为 4*x = 16,所以,在通常看来,这个程序它并不应该输出什么东西,可是事实上它却输出了:
4x = 16.000000, x = 4.000000于是,我就在 printf 那儿设置一个断点,进入调试,会发现此时
x = 4.0000000000000018但是,如果写个简单一点的却又是另外一种情况:#include <iostream>
using namespace std;int main()
{
double x = 0.000000;
x += 0.100000;
if ( x > 0.100000 )
printf ("x = %f\n", x); return 0;
}经调试发现 x = 0.1000000000000001 而且,此时似乎又正常了,不输出任何东西。如果把 x 自加 10 次 0.100000 你会发现结果的 x 不是 1.000000 ,在 Visual C++ 6.0 里的结果是 x = 0.99999999999999989,如果把它和 1 去比较的话,它会显示比 1 小了。
所以,double 类型是不精确的,在进行大小判断时,不能把它与某个确切的值进行比较,而是应该给个范围。
1.格式化输出
#include <iostream>
#include <iomanip>
using namespace std;int main ()
{
int i;
cout << "A list of numbers:" << endl;
for (i = 1; i <= 1024; i *= 2)
{
cout.width (7);
cout << i << endl;
}
cout << "A table of numbers:" << endl;
for (i = 0; i <= 4; i++)
{
cout << setw(3) << i << setw(5) << i * i * i << endl;
} return 0;
}2.获取字符串数据
#include <iostream>
#include <strstream>
#include <cstring>
using namespace std;int main ()
{
char a[1024];
istrstream b(a, 1024);
strcpy (a, "45.656");
double k, p;
b.seekg(0); // Start from first character.
b >> k;
k = k + 1;
cout << k << endl;
strcpy (a, "444.23 56.89");
b.seekg(0);
b >> k >> p;
cout << k << ", " << p + 1 << endl;
return 0;
}3.字符串数组操作
#include <iostream>
#include <strstream>
#include <cstring>
#include <cmath>
using namespace std;int main ()
{
char a[1024];
ostrstream b(a, 1024);
b.seekp(0); // Start from first char.
b << "2 + 2 = " << 2 + 2 << ends; // ( ends, not endl )
// ends is simply the
// null character '\0'
cout << a << endl;
double v = 2;
strcpy (a, "A sinus: ");
b.seekp(strlen (a));
b << "sin (" << v << ") = " << sin(v) << ends;
cout << a << endl;
return 0;
}4.读文件
#include <iostream>
#include <fstream>
using namespace std;int main ()
{
fstream f;
char c;
cout << "What's inside the test.txt file from";
cout << "the C: hard disk root " << endl;
cout << endl;
f.open("c:\\test.txt", ios::in);
while (! f.eof() )
{
f.get(c); // Or c = f.get()
cout << c;
}
f.close();
return 0;
}5. 写文件
#include <iostream>
#include <fstream>
using namespace std;int main ()
{
fstream f;
f.open("c:\\test.txt", ios::out);
f << "This is a text output to a file." << endl;
double a = 345;
f << "A number: " << a << endl;
f.close();
return 0;
}