#include <iostream>
using namespace std;
int main()
{
cout.precision(50);
cout<<"Anmol's Square Root Calculator!"<<endl;
cout<<""<<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout<<""<<endl;
cout<<"Only positive numbers work with this algorithm!"<<endl;
cout<<""<<endl;
cout<<"All cycle #1 approximate square roots are guessed\n"<<"using 1 as the first approximate."<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
char playAgain='y';
while (playAgain !='n')
{
int count(25), cycle(1);
double guess, sqrt, num;
cout << "Please enter the number you would like to compute the square root of: ";
cin >> num;
cout<<""<<endl;
do {
if (num <= 0)
{
cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> num;
cout<<""<<endl;
}
} while (num <= 0);
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
for (guess=1; count!=0; count--)
{
guess =(guess + (num/guess))/2;
cout<<""<<endl;
cout<<"Cycle "<<cycle<<" Aproximate: "<<guess<<endl;
sqrt = guess;
cycle++;
}
cout<<""<<endl;
cout << "The square root of "<< num << " is " << sqrt<<"!"<<endl;;
cout <<""<< endl;
do {
cout <<""<<endl;
cout << "Would you like to calculate again? (y/n): ";
cin >> playAgain;
cout <<" "<<endl;
do {
cout <<""<<endl;
if ((playAgain !='y' && playAgain !='n'))
{
cout << "Invalid input. Only y/n: ";
cin >> playAgain;
cout <<" "<<endl;
}
} while (playAgain !='y' && playAgain !='n');
} while (playAgain !='y' && playAgain !='n');
}
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cin.get();
return 0;
}
对于我的数学课,我们正在学习平方根。根据我的老师的说法,只有使用计算器或表格才能找到平方根,这显然是假的,因为计算器需要一种方法来计算sqrt。经过广泛的研究,我使用巴比伦算法制作了这段代码。但我面临一个问题 在这段代码中,我特意将小数的精度设置为15.但是它输出12位小数。但是在这段代码中,它以15位小数输出。
#include <iostream>
using namespace std;
int main()
{
cout.precision(18);
cout<<"Anmol's Square Root Calculator!"<<endl;
cout<<""<<endl;
cout << "This program will compute the square root\n";
cout << "of a number using the Babylonian algorithm!\n";
cout<<""<<endl;
cout<<"Only positive numbers work with this algorithm!"<<endl;
cout<<""<<endl;
cout<<"All cycle #1 approximate square are guessed\n"<<"using 1 as the approximate."<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
char playAgain='y';
while (playAgain !='n')
{
int count(25), cycle(1);
double guess(1), sqrt, x, num;
cout << "Please enter the number you would like to know the square root of: ";
cin >> num;
cout<<""<<endl;
do {
if (num <= 0)
{
cout<<""<<endl;
cout<<"Invalid input. Please re-enter a valid number: ";
cin >> num;
cout<<""<<endl;
}
} while (num <= 0);
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
while (count > 0)
{
x = guess =(guess + (num/guess))/2;
cout<<""<<endl;
cout<<"Cycle "<<cycle<<" Aproximate: "<<guess<<endl;
sqrt = guess;
count-=1;
cycle+=1;
}
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout << "The sqaure root of "<< num << " is " << sqrt<<"!"<<endl;;
cout <<""<< endl;
do {
cout <<""<<endl;
cout << "Would you like to calculate again? (y/n): ";
cin >> playAgain;
cout <<" "<<endl;
do {
cout <<""<<endl;
if ((playAgain !='y' && playAgain !='n'))
{
cout << "Invalid input. Only y/n: ";
cin >> playAgain;
cout <<" "<<endl;
}
} while (playAgain !='y' && playAgain !='n');
} while (playAgain !='y' && playAgain !='n');
}
cout<<"Thank you for using a program made by Anmol Sethi!"<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cout<<""<<endl;
cin.get();
return 0;
}
我很困惑为什么会这样。我试过cout&lt;&lt;固定&lt;&lt; showpoint;但是当num无缘无故地输出15个零时,它看起来很不愉快。任何人都可以告诉我为什么会发生这种情况以及如何解决它?在一本书中,我读到了使用for循环算法的效率更高,所以我想使用for而不是while循环。我还有第二个问题。为什么我不能超过15位小数?如果我将精度设置为30/40,它对我的输出无效,即使在我的计算机计算器中也达到30/40。是否应该使用另一种整数类型而不是double?
很抱歉,如果它看起来像xD的大墙。
我急于在上午12:05为我输入这个原因。
提前致谢:D
p.s我现在要在这个论坛上发帖了。我曾经在大约一年前使用它,但我退出c ++编程一段时间,因为当我更小的xD时我无法理解它。所以我现在正在为它阅读一本书,我希望能够通过控制台应用程序和Windows一直使用。如果重要的话,我正在使用2012年的visual studio ultimate。的xD