[ create a new paste ] login | about

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

C, pasted on Jun 8:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 100


void NhapMang(int a[][MAX], int dong, int cot)
{
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			printf("\nNhap vao a[%d][%d] = ",i, j);
			scanf("%d",&a[i][j]);
		}
	}
}
void XuatMang(int a[][MAX], int dong, int cot)
{
	for(int i = 0; i < dong; i++)
	{
		for(int j = 0; j < cot; j++)
		{
			printf("%4d", a[i][j]);
		}
		printf("\n\n");
	}
}
// Nếu hợp lệ => trả về 1
// Không hợp lê => trả về 0
int KiemTraLaSoNguyen(char *s)
{
	int length = strlen(s);
	for(int i = 0; i < length; i++)
	{
		if((s[i] < '0' || s[i] > '9') && (s[i] != '-'))
		{
          return 0;  // Sai
		}
		if(s[i] != '-' && s[i + 1] == '-')
		{
			return 0; // Sai
		}
	}
	return 1;
}
// Nếu hợp lệ => trả về 1
// Không hợp lê => trả về 0

/*
- Ngoài trừ số ra thì nếu có thì chỉ có thêm tối đa 1 dấu '.'
- Dấu '.' không đặt ở đầu, không đặt cuối mà phải đặt ở đoạn giữa giữa.
*/
int KiemTraLaSoThuc(char *s)
{
	int dem = 0; // Biến đếm xem có bao nhiêu dấu chấm
	int vitri = -1; // Biến lưu vị trí của dấu chấm

	int length = strlen(s);
	// "-5", "--5", "-5-"
	for(int i = 0 ; i < length; i++)
	{
		if(s[i] == '.')
		{
			vitri = i;
			dem++;
		}
		if((s[i] < '0' || s[i] > '9') && (s[i] != '.'))
		{
			return 0; // Sai
		}
		if(s[i] != '-' && s[i + 1] == '-')
		{
			return 0; // Sai
		}
	}
	if(dem > 1 || vitri == 0 || vitri == length - 1)
	{
		return 0;
	}
	return 1;
}
int main()
{
	 
	int a[MAX] [MAX], dong, cot;

	//// Khai báo trực tiếp mảng 1 chiều
	//int a[] = {1,2,3,4,5};
	//
	//// khai báo trực tiếp mảng 2 chiều
	//int a[][3] = {{1, 2, 3}, {4, 5, 6}};

	//printf("%d", a[1][1]);

	//Nhập số dòng
	//do
	//{
	//	printf("\nNhap vao so dong: ");
	//	// Cách tà đạo: scanf("dong =%d",&dong);  // Lúc nhập phải viết thêm  chữ ( dong =  ) ở khung console
	//	scanf("%d",&dong);

	//	if(dong < 1 || dong > MAX)
	//	{
	//		printf("\nSo dong khong hop le. Xin kiem tra lai!");

	//	}

	//}while(dong < 1 || dong > MAX);

	//Nhập số cột
	/*do
	{
		printf("\nNhap vao so cot: ");
		scanf("%d",&cot);

		if(cot < 1 || cot > MAX)
		{
			printf("\nSo cot khong hop le. Xin kiem tra lai!");

		}

	}while(cot < 1 || cot > MAX);*/

	 //Nhập số dòng - kiểm tra lỗi kiểu dữ liệu
	int CheckDong;
	do{
		char s[20];

		fflush(stdin); // Xóa bộ nhớ đệm
		printf("\nNhap vao so dong: ");
		gets(s);

		CheckDong = KiemTraLaSoNguyen(s);

		if(CheckDong == 0)
		{
			printf("\nKieu du lieu khong hop le. Xin kiem tra lai !");
		}
		else
		{
			dong = atoi(s); // Chuyển sang số.

			if(dong < 1 || dong > MAX)
			{
				CheckDong = 0;
				printf("\nSo dong khong hop le. Xin kiem tra lai !");
			}
		}
	}while(CheckDong == 0);

	 //Nhập số cột - kiểm tra lỗi kiểu dữ liệu
	int CheckCot;
	do{
		char s[20];

		fflush(stdin); // Xóa bộ nhớ đệm
		printf("\nNhap vao so cot: ");
		gets(s);

		CheckCot = KiemTraLaSoNguyen(s);

		if(CheckCot == 0)
		{
			printf("\nKieu du lieu khong hop le. Xin kiem tra lai !");
		}
		else
		{
			cot = atoi(s); // Chuyển sang số.

			if(cot < 1 || cot > MAX)
			{
				CheckCot = 0;
				printf("\nSo dong khong hop le. Xin kiem tra lai !");
			}
		}
	}while(CheckCot == 0);
	

	

	NhapMang(a,dong,cot);
	XuatMang(a,dong,cot);

	// Nhập số thực
	//int Check;
	//do{
	//	char s[20];

	//	fflush(stdin); // Xóa bộ nhớ đệm
	//	printf("\nNhap vao so thuc: ");
	//	gets(s);

	//	Check = KiemTraLaSoThuc(s);

	//	if(Check == 0)
	//	{
	//		printf("\nKieu du lieu khong hop le. Xin kiem tra lai !");
	//	}
	//}while(Check == 0);
	

	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
In function 'NhapMang':
Line 10: error: 'for' loop initial declaration used outside C99 mode
Line 12: error: 'for' loop initial declaration used outside C99 mode
In function 'XuatMang':
Line 21: error: 'for' loop initial declaration used outside C99 mode
Line 23: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraLaSoNguyen':
Line 35: error: 'for' loop initial declaration used outside C99 mode
In function 'KiemTraLaSoThuc':
Line 62: error: 'for' loop initial declaration used outside C99 mode


Create a new paste based on this one


Comments: