C++,解一元二次方程

来源:互联网 发布:玉帝如来佛知乎 编辑:程序博客网 时间:2024/04/28 06:04

 

 

#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();}


原创粉丝点击