[ create a new paste ] login | about

Link: http://codepad.org/1VM1MjQi    [ raw code | output | fork ]

C, pasted on Apr 21:
/*
Tính S(x, n) = x^n
Ta cần biết
+ Với n > 0 (ví dụ là 3) thì x^3 là x * x * x
+ Với n < 0 (ví dụ là -3) thì x^-3 là: 1/(x^3) = 1/x * 1/x * 1/x

*/

#include<stdio.h>
#include<conio.h>


float deQuy(float x, int n) // đệ quy
{
	if(n == 0)
		return 1;
	if(n == -1)
		return 1.0/x;
	if(n < 0)
		return deQuy(x, n + 1) * 1.0 / x;
	else if(n == 0)
		return 1;
	return deQuy(x, n - 1) * x;
}

float deQuyDuoi(float x, int n, float y = 1)  // đệ quy đuôi
{
	if(n == 0)
		return y;
	if(n < 0)
		return deQuyDuoi(x, n + 1, y * 1.0 / x);
	else if(n == 0)
		return 1;
	return deQuyDuoi(x, n - 1, y * x);
}

float khuDeQuy(float x, int n) // khử đệ quy
{
	bool Check = true; 
	float tich = 1;

	if(n == 0)
		return tich;
	else if(n < 0)
	{
		Check = false;
		n *= -1; // biến thành dương lại
	}

	for(int i = 1; i <= n; i++)
	{
		tich *= x; 
	}
	if(Check == true)
		return tich;
	return 1.0/tich;
}
int main()
{
	float x = 2;
	int n;
	printf("\nNhap n: "); scanf("%d", &n);

	printf("\nKhu de quy(%f, %d) = %f", x, n, khuDeQuy(x, n));
	printf("\nDe quy(%f, %d) = %f", x, n, deQuy(x, n));
	printf("\nDe quy duoi(%f, %d) = %f", x, n, deQuyDuoi(x, n));

	
	 
	getch();
	return 0;
}


Output:
1
2
3
4
5
6
7
8
9
10
11
Line 17: error: conio.h: No such file or directory
Line 26: error: expected ';', ',' or ')' before '=' token
In function 'khuDeQuy':
Line 39: error: 'bool' undeclared (first use in this function)
Line 39: error: (Each undeclared identifier is reported only once
Line 39: error: for each function it appears in.)
Line 39: error: expected ';' before 'Check'
Line 46: error: 'Check' undeclared (first use in this function)
Line 46: error: 'false' undeclared (first use in this function)
Line 50: error: 'for' loop initial declaration used outside C99 mode
Line 54: error: 'true' undeclared (first use in this function)


Create a new paste based on this one


Comments: