[ create a new paste ] login | about

Link: http://codepad.org/18ocSsBW    [ raw code | output | fork | 3 comments ]

C, pasted on Aug 23:
#include<iostream>
#include<cmath>
using namespace std;

const int NoSolution = 0, Undetermined = -1;
int EqualDeg1(double a, double b, double &x)                            // Phương trình bậc 1
{
	int nSolution1;                                                     // Số nghiệm
	if(a != 0) 
	{
		x = -b / a;
		nSolution1 = 1;
	}
	else                                                               // a = 0
	{
		x = 0;
		if(b == 0)
			nSolution1 = Undetermined;                                  // Phương trình vô định ( có vô số nghiệm)
		else
			nSolution1 = NoSolution;                                    // Phương trình vô nghiệm
	}
	return nSolution1;
}
int EqualDeg2(double a, double b, double c, double &x1, double &x2)    // Phương trình bậc 2
{
	int nSolution2; x1 = x1 = 0;                                      // Mặc định
	if(a == 0)                                                           
		nSolution2 = EqualDeg1(b,c, x1);
	else
	{
		double Delta = b * b - 4 * a * c; double two_a = 2 * a;
		if(Delta < 0)
			nSolution2 = NoSolution;                                  // Phương trình vô nghiệm
		else if(Delta == 0)
		{
			x1 = x2 = -b / two_a;
			nSolution2 = 1;                                           // Phương trình có 1 nghiệm kép
		}
		else
		{
			double sqrtDelta = sqrt(Delta);
			x1 = (-b - sqrtDelta) / two_a;
			x2 = (-b + sqrtDelta) / two_a;
			nSolution2 = 2;                                           // Phương trình có 2 nghiệm phân biệt
		}
	}
	return nSolution2;
}
int EqualQuartic(double a, double b, double c, 
				 double &x1, double &x2, double &x3, double &x4)       // Phương trình trùng phương
{
	int nSolution, nSolution1, nSolution2;
	double y1, y2;
	x1 = x2 = 0 ; x3 = x4 = 0;                                            //  Mặc định
	nSolution1 = EqualDeg2(a, b, c, y1, y2);
	switch(nSolution1)
	{
	case NoSolution: case Undetermined:
		nSolution = nSolution1; break;
	case 1:
		nSolution = EqualDeg2(1, 0, -y1, x1, x2); break;
	case 2:
		nSolution2 = EqualDeg2(1, 0, -y1, x1, x2);
		switch(nSolution2)
		{
		case NoSolution:
			nSolution = EqualDeg2(1, 0, -y2, x1, x2); break;
		case 1:
			nSolution = 1 + EqualDeg2(1, 0, -y2, x2, x3); break;
		case 2:
			nSolution = 2 + EqualDeg2(1, 0, -y2, x3, x4); break;
		}
	}
	return nSolution;
}
int main()
{
	/*double a, b, x;
	cout << "Input a = " ;
	cin >> a;
	cout << "Input b = " ;
	cin >> b;
	
	int nSolution = EqualDeg1(a, b, x);
	cout << "So nghiem cua phuong trinh la: " << nSolution << endl;
	if(a != 0)
	cout << "Nghiem cua phuong trinh " << a << "x + " << b << " = 0 "    "la: " << x << endl;
	else
		if(b == 0)
		cout << "Nghiem cua phuong trinh " << a << "x + " << b << " = 0 "    "la: vo so nghiem "  << endl;
		else
		cout << "Nghiem cua phuong trinh " << a << "x + " << b << " = 0 "    "la: vo nghiem "  << endl;*/

	double a, b, c, x1, x2;
	
	cout << "Input a = " ;
	cin >> a;
	cout << "Input b = " ;
	cin >> b;
	cout << "Input c = " ;
	cin >> c;
	double Delta = b * b - 4 * a * c; double two_a = 2 * a;
	int numSolution = EqualDeg2(a, b, c, x1, x2);
	cout << "So nghiem cua phuong trinh la: " << numSolution << endl;
	if(a == 0)
	{
		if(b != 0)
		cout << "Nghiem cua phuong trinh " << b << "x + " << c << " = 0 "   "la: " << x1 << endl;
	    else
		   if(c == 0)
		   cout << "Nghiem cua phuong trinh " << b << "x + " << c << " = 0 "    "la: vo so nghiem "  << endl;
		   else
		   cout << "Nghiem cua phuong trinh " << b << "x + " << c << " = 0 "    "la: vo nghiem "  << endl;
	}
	else
	{
		if(Delta < 0)
			cout << "Phuong trinh " << a << "x^2 + " << b << "x + " << c << " = 0 "    "la: vo nghiem " << endl;
		else if(Delta == 0)
			cout << "Phuong trinh " << a << "x^2 + " << b << "x + " << c << " = 0 "    "co 1 nghiem kep la: x1 = x2 = " << x1 << endl;
		else
			cout << "Phuong trinh " << a << "x^2 + " << b << "x + " << c << " = 0 "    "co 2 nghiem phan biet: " << x1 << " va " << x2 << endl;
	}
	system("pause");
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
12
Line 18: error: iostream: No such file or directory
Line 15: error: cmath: No such file or directory
Line 3: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'namespace'
Line 6: error: expected ';', ',' or ')' before '&' token
Line 24: error: expected ';', ',' or ')' before '&' token
Line 50: error: expected ';', ',' or ')' before '&' token
In function 'main':
Line 96: error: 'cout' undeclared (first use in this function)
Line 96: error: (Each undeclared identifier is reported only once
Line 96: error: for each function it appears in.)
Line 97: error: 'cin' undeclared (first use in this function)
Line 104: error: 'endl' undeclared (first use in this function)


Create a new paste based on this one


Comments:
posted by manhha on Feb 17
đang học c lại nhảy sang c++
reply
posted by yonnon on Jul 12
#include<bits/stdc++.h>

using namespace std;
int main(){
int a;
int b;
int c;
int delta=0;
float x1;
float x2;
do{
cout <<"Enter a: ";
cin >> a;
cout <<"Enter b: ";
cin >> b;
cout <<"Enter c: ";
cin >> c;
if(a==0 && b==0){
cout<<"Many solutions";
return 0;
}
if (a==0 ){
cout << "No solution ";
return 0;
}

}while(a==0 || (a==0 && b==0));
delta=pow(b,2)-(4*a*c);
if(delta<0){
cout << "No solution";
return 0;
}
else if(delta == 0){
cout<< "Phuong trinh co nghiem kep x1 = x2 = "<<-b/(2*a);
return 0;
}
else if(delta>0){
x1=(-b-sqrt(delta))/(2*a);
x2=(-b+sqrt(delta))/(2*a);
cout<<"Phuong trinh co 2 nghiem phan biet"<<
endl;
cout<<"x1 = "<<x1<<
endl;
cout<<"x2 = "<<x2<<
endl;
return 0;

}
cin.ignore();
return 0;

}
reply
posted by phamquanghoaz on Sep 1
trùng phương thì e không nói làm gì, tưởng giải thuần ax^4 + b x^3 + cx^2 +d =0, làm search youtube giải tay thấy khó quá trời :v
reply