
#include <iostream>
#include <fstream>
#include <string>
#include <math.h>
using namespace std;
void main()
{
const char* filename = "c:/解方程程序结果.txt";
ofstream ofile(filename);
if(!ofile.is_open())
{
std::cerr << "Error opening file";
exit(EXIT_FAILURE);
}
int y;
cout<<"输入Y:";
cin>>y;
ofile<<"输入y: "<<y<<endl;
int y3 = (int)pow(y,1.0/3.0) +1;
int y2 = (int)pow(y,1.0/2.0) +1;
int a=0,b=0,c=0;
int x,sum;
cout<<"方程 a*x^2 + b*x + c = y 的解是: "<<endl;
ofile<<"方程 a*x^2 + b*x + c = y 的解是: "<<endl;
for(a=1;a<y2;a++){
for(b=0;b<y2;b++){
for(c=0;c<y2;c++){
for(x=y3;x<y2;x++){
sum = a*x*x +b*x +c;
if(sum==y&&a<=x&&b<=x&&c<=x){
cout<<"a = "<<a<<" b = "<<b<<" c = "<<c<<" x ="<<x<<endl;
ofile<<"a = "<<a<<" b = "<<b<<" c = "<<c<<" x = "<<x<<endl;
}
}
}
}
}
ofile.close();
}


本文详细介绍了如何使用C++编程解决一元二次方程的求解问题,包括公式推导、代码实现及误差处理,适合初学者和进阶者学习。
2265

被折叠的 条评论
为什么被折叠?



