Tải bản đầy đủ (.doc) (28 trang)

Các bài tập c căn bản

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (93.52 KB, 28 trang )

CÁc bài tập C căn bản P4
---------------------------------
/* Bai tap 1_93 - In tat ca cac uoc so cua so n */
#include <stdio.h>
#include <conio.h>
void main()
{
int n, i;
printf("Cho gia tri N = ");
scanf("%d", &n);
printf("Cac uoc so cua %d la :\n", n);
for (i=1; i<n; i++)
if ((n % i) == 0)
printf("%5d", i);
getch();
}
----------------------------
#include <stdio.h>
unsigned USCLN (unsigned n, unsigned m)
{
while (n != 0 && m != 0)
if (n>m)
n -= m;
else
m -= n;
if (n == 0)
return m;
else
return n;
}
unsigned BSCNN (unsigned n, unsigned m)


{
return n * m / USCLN(n, m);
}
void main()
{
unsigned n, m;
printf("\nNhap hai vao so nguyen duong : ");
scanf("%u%u", &n, &m);
printf("\nUSCLN cua %u va %u = %u", n, m, USCLN(n,m));
printf("\nBSCNN cua %u va %u = %u", n, m, BSCNN(n,m));
getch();
}
----------------------------
/* Xem tap tin cho phep cuon */
#include <stdio.h>
#include <conio.h>
void main()
{
FILE *fp;
char filename[50], s[255], *hang[1000], c;
int nline = 0, line = 0, i;
printf("\nNhap ten tap tin muon xem : ");
gets(filename);
if ((fp = fopen(filename,"r")) == NULL)
printf("\nKhong the mo tap tin %s", filename);
else
{
while (!feof(fp))
{
fgets(s, 255, fp);

hang[nline] = (char *)malloc(strlen(s));
strcpy(hang[nline], s+1);
nline++;
}
do {
clrscr();
i = line;
while (i<line+26 && i<nline)
printf("%s", hang[i++]);
c = getch();
if (c == 0)
{
c = getch();
switch(c)
{
case 72 : if (line > 0)
line--;
break;
case 80 : if (line+25 < nline)
line++;
break;
}
}
} while (c != 27);
fclose(fp);
}
}
---------------------------
/* Bai tap 9_5 - Tim so tu trong chuoi */
#include <stdio.h>

#include <string.h>
#include <conio.h>
#pragma warn -pia
char *trim(char *chuoi)
{
char *p;
while (p = strstr(chuoi, " "))
memmove(p, p+1, strlen(chuoi) - (p - chuoi));
if (chuoi[0] == ' ')
memmove(chuoi, chuoi+1, strlen(chuoi) - 1);
if (chuoi[strlen(chuoi)-1] == ' ')
chuoi[strlen(chuoi)-1] = 0;
return chuoi;
}
void main()
{
char chuoi[125];
int i = 0, count = 0;
printf("\nNhap chuoi mau : ");
textattr(0x1e);
gets(chuoi);
trim(chuoi);
if (strlen(chuoi))
{
while(chuoi[i] != 0)
if (chuoi[i++] == ' ')
count++;
count++;
}
printf("\nSo tu co trong chuoi : %d", count);

getch();
}
--------------------------------------
--------------------------------
#include <stdio.h>
void main()
{
char s[100];
int i;
printf("\nNhap vao mot chuoi : ");
gets(s);
i = 0;
while (s[i] != 0)
{
if (isalpha(s[i]))
if (s[i] < 97)
s[i] += 32;
else
s[i] -= 32;
i++;
}
printf("\nChuoi bien thanh : %s", s);
getch();
}
---------------------------------------
/* Bai tap 5_3 - Tim phuong an doi tien */
#include <stdio.h>
#define TONGSOTIEN 300000
void main()
{

long i, j, k, l, m, count=0, soluong = 0;
for (i=0; i<=TONGSOTIEN/1000; i++)
for (j=0; j<=TONGSOTIEN/2000; j++)
for (k=0; k<=TONGSOTIEN/5000; k++)
for (l=0; l<=TONGSOTIEN/10000; l++)
for (m=0; m<=TONGSOTIEN/20000; m++)
{
if ((i*1000 + j*2000 + k*5000 + l*10000 + m*20000) == TONGSOTIEN)
printf("\n%5ld - %5ld%5ld%5ld%5ld%5ld", ++count, i, j, k, l, m);
soluong++;
}
printf("so luong = %ld", soluong);
getch();
}
-----------------------------------------
/* Bai tap 9_4 - Tim chuoi doi xung */
void main()
{
char chuoi[125];
int i = 0, j;
printf("\nNhap vao chuoi kiem tra : ");
gets(chuoi);
j = strlen(chuoi) - 1;
while(chuoi[i++] == chuoi[j--])
;
if (--i>=++j)
printf("Chuoi doi xung");
else
printf("Chuoi khong doi xung");
getch();

}
------------------------------------------
/* Bai tap 1_87 - Demo ve hop dung conio */
#include <conio.h>
#include <stdlib.h>
#include <dos.h>
char doublebox[] ="ÉÍ»ºÈ¼";
char singlebox[] ="ÚÄ¿³ÀÙ";
void swap(int *x, int *y)
{
int temp;
temp = *y;
*y = *x;
*x = temp;
}
void drawbox(int x1, int y1, int x2, int y2, int type)
{
char *boxtype;
int i;
if (type == 2)
boxtype = doublebox;
else
boxtype = singlebox;
gotoxy(x1,y1);
cprintf("%c", boxtype[0]);
for (i=x1 + 1; i < x2; i++)
cprintf("%c", boxtype[1]);
cprintf("%c", boxtype[2]);
for (i=y1+1; i<y2; i++)
{

gotoxy(x1, i);
cprintf("%c", boxtype[3]);
gotoxy(x2, i);
cprintf("%c", boxtype[3]);
}
gotoxy(x1,y2);
cprintf("%c", boxtype[4]);
for (i=x1 + 1; i < x2; i++)
cprintf("%c", boxtype[1]);
cprintf("%c", boxtype[5]);
}
void drawbox_color(int x1, int y1, int x2, int y2, int type, int color)
{
if (x1>x2) swap(&x1, &x2);
if (y1>y2) swap(&y1, &y2);
textattr(color);
window(x1, y1, x2, y2);
clrscr();
window(1,1,80,25);
drawbox(x1, y1, x2, y2, type);
}
void main()
{
int x1, y1, x2, y2, type, color;
cprintf("\n\rDRAWBOX DEMO.\n\rPress any key to begin...");
getch();
randomize();
while (!kbhit())
{
x1 = random(80);

do {
x2 = random(80);
} while (x1 == x2);
y1 = random(25);
do {
y2 = random(25);
} while (y1 == y2);
type = random(2) + 1;
color = random(256);
drawbox_color(x1,y1,x2,y2,type,color);
delay(100);
}
}
-----------------------------------------
#include <stdio.h>
void main()
{
double fahrenheit, celsius;
printf("\nNhap gia tri do Fahrenheit : ");
scanf("%lf", &fahrenheit);
if (fahrenheit != 32.0)
{
celsius = 5.0 * (fahrenheit-32.0) / 9.0 ;
printf("Do Celsius tuong ung = %5.2lf", celsius);
}
getch();
}
----------------------------------------
/* Bai tap 1_21 - In day so Fibonanci */
#include <stdio.h>

void main()
{
int n, i, fib1 = 1, fib2 = 1, fib = 2;
printf("\nNhap gia tri N : ");
scanf("%d", &n);
printf("%d %d ", fib1, fib2);
while (fib1+fib2 < n)
{
fib = fib1 + fib2;
printf("%d ", fib);
fib2 = fib1;
fib1 = fib;
}
getch();
}
------------------------------------
/* Bai tap 1_68 - Tap hop thong tin */
#include <stdio.h>
void main()
{
char s[100], s1[100], s2[100], filename1[50], filename2[50];
FILE *fp1, *fp2;
printf("\nCho viet ten tap tin thu nhat : ");
gets(filename1);
if ((fp1 = fopen(filename1, "w+")) == NULL)
printf("\nKhong the tao tap tin %s", filename1);
else
{
printf("\nCho viet ten tap tin thu nhat : ");
gets(filename2);

if ((fp2 = fopen(filename2, "w+")) == NULL)
printf("\nKhong the tao tap tin %s", filename2);
else
{
do {
printf("\nHo ten (ENTER de ket thuc) : ");
gets(s);
if (strlen(s) > 0)
{
strcat(s, "\n");
fputs(s, fp1);
printf("\nTuoi : ");
gets(s);
strcat(s, "\n");
fputs(s, fp1);
printf("\nDia chi : ");
gets(s);
strcat(s, "\n");
fputs(s, fp1);
}
} while (strlen(s) > 0);
fclose(fp1);
fp1 = fopen(filename1, "r");
while (fgets(s, 100, fp1) != NULL)
{
fgets(s1, 100, fp1);
fgets(s2, 100, fp1);
s[strlen(s)-1] = 0;
s1[strlen(s1)-1] = 0;
fprintf(fp2, "%s %s %s", s, s1, s2);

}
fclose(fp2);
}
}
}
------------------------------------------
/* Bai tap 1_86 - Doc va ghi len file mot mang 2 chieu 10x10 */
#include <stdio.h>
#define MAX 10
char filename[] = "MANG1010.DAT";
void main()
{
int mang[MAX][MAX], i, j;
FILE *fp;
if ((fp = fopen(filename, "w+")) == NULL)
printf("\nKhong the tao tap tin %s", filename);
else
{
printf("\nNhap cac phan tu cua mang : ");
for (i=0; i<MAX; i++)
for (j=0; j<MAX; j++)
{
printf("\nMang[%d][%d] = ", i, j);
scanf("%d", &mang[i][j]);
}
fwrite(&mang, sizeof(int), MAX * MAX, fp);
fclose(fp);
printf("\nViec ghi hoan tat.");
}
getch();

}
---------------------------------------
/* Bai tap 3_1 - Ham tinh N! */
#include <stdio.h>
unsigned long giaithua(int n)
{
unsigned long ketqua = 1;
int i;
for (i=2; i<=n; i++)
ketqua *= i;
return ketqua;
}
void main()
{
int n;
printf("\nNhap vao gia tri N : ");
scanf("%d", &n);
printf("%d! = %lu", n, giaithua(n));
getch();
}
----------------------------------
#include <stdio.h>
void main()
{
int a[10], i;
printf("\nNhap vao 10 phan tu nguyen cua day : ");
for (i=0; i<10; i++)
scanf("%d", &a[i]);
i = 1;
while (a[i] <= a[i-1] && i<10)

i++;
if (i==10)
printf("Day da cho la day giam");
else
printf("Day da cho khong la day giam");
getch();
}
------------------------------------------------
/* Bai tap 2_6 - Cho biet goc thuoc goc vuong nao */
#include <stdio.h>
void main()
{
int goc;
printf("\nNhap vao gia tri goc : ");
scanf("%d", &goc);
printf("Goc do thuoc goc vuong thu %d", (goc/90)%4 + 1);
getch();
}
----------------------------------
/* Bai tap - Bai toan Gonbach */
#include <stdio.h>
#define MAX 100
void main()
{
unsigned long nguyento[MAX], i, j, l, k=0, n;
for (i=1; i<MAX; i++)
if (i%2 == 1)
{
for (j=2; j<i; j++)
if (i%j == 0)

break;
if (j >= i)
nguyento[k++] = i;
}
for (n=0; n<k; n++)
for (i=0; i<k; i++)
for (j=0; j<k; j++)
for (l=0; l<k; l++)
if (nguyento[i] + nguyento[j] + nguyento[l] == nguyento[n])
printf("\n%ld = %ld + %ld + %ld", nguyento[n], nguyento[i],
nguyento[j], nguyento[l]);
getch();
}
-----------------------------------------
/* Bai tap 1_20 - Tinh n!! */
#include <stdio.h>
void main()
{
int n, start, i;
unsigned long gthua = 1;
printf("\nNhap gia tri N : ");
scanf("%d", &n);
if (n%2 == 0)
start = 2;
else
start = 1;
for (i=start; i<=n; i = i+2)
gthua *= i;
printf("\n%d!! = %ld", n, gthua);
getch();

}
-------------------------------------------

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×