[ create a new paste ] login | about

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

C, pasted on Nov 24:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
int STRLEN(char s[])
{
	int dem = 0;
	while (s[dem] != '\0')
	{
		dem++;
	}
	return dem;
}

//Chuyển chuỗi SỐ s sang giá trị int.
/*
"1234"
Lần lượt lấy từng ký tự ra rồi đổi sang số
tương ứng

ta thấy kết quã mong muốn là 1234
1234 = 1 * 10^3 + 2 * 10^2 + 3 * 10^1 + 4 * 10^0;
*/
int ATOI(char s[])
{
	int tong = 0;
	int length = STRLEN(s);
	int temp = length;
	// Xác định temp để biết phạm vi mình sẽ duyệt
	for(int i = 0; i < length; i++)
	{
		if(s[i] < '0' || s[i] > '9') // Không phãi là số
		{
			temp = i;
			break;
		}
	}
	// Tính ra kết quã
	int tam = temp;
	for(int i = 0; i < temp; i++)
	{
		tong +=(s[i] - 48) * pow(10.0, --tam); // đổi ký tự sang số rồi nhân cho 10 mũ ...
	}
	return tong;
}

int main()
{
	char str1[100];

    printf("Nhap chuoi: ");
	//scanf("%s",str2);
    gets(str1);  // phải dùng gets
	int x = ATOI(str1);
	printf("\nChuyen ky tu SO %s sang so: %d", str1, x);


	getch();
	return 0;
}


Output:
1
2
3
4
5
6
Line 17: error: conio.h: No such file or directory
In function 'ATOI':
Line 30: error: 'for' loop initial declaration used outside C99 mode
Line 40: error: redefinition of 'i'
Line 30: error: previous definition of 'i' was here
Line 40: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: