You are on page 1of 18

1

Mine:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

int main()
{FILE *fptr;
int n,i,t;int a[n];
fptr = fopen("MANG.IN","r");
if (fptr==NULL)
{
printf("\n khong mo duoc tep.");getch();
return 0;
}

fscanf(fptr,"%d",&n);
for (i=0;i<n;i++)
{
fscanf(fptr,"%d",&a[i]);
}

printf ("%d\n",n);
for (i=0;i<n;i++)
printf ("%d ",a[i]);
printf ("\n");

printf(" file da sap xep:\n");
printf("%d\n", n);
for(int i=0;i<n;i++)
for(int j=i+1;j<n+1;j++)
{
if(a[i]>a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
};
};
for(int i=0;i<n;i++)
printf("%d ",a[i]);
fclose(fptr);
//return 0;
//getche();
2

fptr = fopen("MANG.OUT","w");
fprintf (fptr, "%d\n",n);
for(int i=0;i<n;i++)
fprintf(fptr,"%d, ", a[i]);
getche();
}
Mang con tro:
Cho file text DATA.IN cha 2 dy s nguyn, mi dy nm trn 1 dng, s
u tin ca dy cho bit
s lng phn t ca dy, v d:
. Vit chng trnh:
1. c file DATA.IN ly 2 dy s trong file vo 2 mng 1 chiu (mng A
v mng B).
2. To mt mng C gm cc phn t c tnh theo cng thc
C[i]=A[i]+B[i]. Theo file
DATA.IN v d trn, mng C={7, 11, -4, 12, 3,-1, 10}
3. Sp xp mng C theo th t gim dn
4. Copy mng C sp xp sang mt danh sch lin kt.
5. Ghi tt c cc phn t ca danh sch lin kt vo file DATA.OUT theo
cu trc sau:
u ghi s lng phn t ca mng C
2 ghi ln lt cc gi tr ca mng C, cch nhau bi du phy
Nh trong v d y, file DATA.OUT nh th ny:
Ch :
- Yu cu s dng con tr khai bo v thao tc mng (cp pht b nh
ng cho mng)
#include <stdio.h>
#include <stdlib.h>
#include<conio.h>
int main ()
{
int i,j, tam,n1,n2;
FILE *fptr, *fptr2;
int *a,*b, *c;

fptr = fopen("DATA.IN","r");

//kiem tra mo file
if (fptr == NULL)
{
printf("Loi mo file");
exit(1);
3
}
else
{
fscanf(fptr,"%d",&n1); printf("mang a chua dong 1 cua file ");
printf("n1 = %d ",n1);
a = (int *)malloc(n1*sizeof(int));
for(i=0;i<n1;i++)
{
fscanf(fptr,"%d",(a+i));
printf("%d ",*(a+i));
}
printf("\n");
fscanf(fptr,"%d",&n2); printf("mang b chua dong 2 cua file ");
printf("n2 = %d ",n2);
b = (int *)malloc(n2*sizeof(int));
for(i=0;i<n2;i++)
{
fscanf(fptr,"%d",(b+i));
printf("%d ",*(b+i));

}
}
// Tao mang C
printf("\nMang C: ");
c = (int *)malloc(n2*sizeof(int));

for(i=0;i<n2;i++)
{
*(c+i)= *(a+i) + *(b+i);
if(i>=n1)
{
*(c+i) = *(b+i);
}
printf("%d ",*(c+i));
}

//sap xep c giam dan

for(i=0;i<n2;i++)
{
for(j=0;j<n2;j++)
{
if(*(c+i)>*(c+j))
4
{
tam = *(c+i);
*(c+i) = *(c+j);
*(c+j) = tam;
}
}
}
printf("\nMang C da sap xep: ");
for(i=0;i<n2;i++)
{
printf("%d ",*(c+i));
}
fclose(fptr);


//Ghi vao FILE DATA>OUT

fptr2 = fopen("DATA.OUT","w");
fprintf (fptr2, "%d\n",n2);
for(int i=0;i<n2;i++)
fprintf(fptr,"%d, ", c[i]);

fclose(fptr);
getch();
}
Vi Du:
#include <stdio.h>
#include <stdlib.h>

int main()
{
int **a; //Khai bao con tro dung de quan ly mang cac con tro kieu int
(mang 2 chieu)
a=(int**) malloc(5*sizeof(int*)); //Xin cap phat bo nho cho mang cac con
tro kieu int
for (int i=0; i<5; i++)
*(a+i) = (int*) malloc(10*sizeof(int)); //Xin cap phat bo nho cho 5
mang, moi mang chua cac so nguyen
for (int i=0; i<5; i++)
for (int j=0; j<10; j++)
*(*(a+i)+j)=i+j; //Gan gia tri
for (int i=0; i<5; i++)
{
5
for (int j=0; j<10; j++)
printf("%3d",*(*(a+i)+j)); //In ra man hinh, su dung ky hieu
con tro
printf("\n");
}
printf("\n");
for (int i=0; i<5; i++)
{
for (int j=0; j<10; j++)
printf("%3d",a[i][j]); //In ra man hinh, su dung ky hieu chi
so mang
printf("\n");
}

for (int i=0; i<5; i++)
free(a+i); //Giai phong bo nho cho 5 mang 1 chieu
free(a); //giai phong bo nho cho mang 2 chieu
}
1. Vit hm quy tnh x
n
vi x l s thc v n s nguyn.
float Muduong(float x, int n)
{
if (n==0) return (1);
return x * Muduong(x,n-1);
}
float Mu(float x, int n)
{
if (n>=0) return Muduong(x,n);
return (1/Muduong(x,-n));
}
2. Hm quy tr v USCLN ca 2 s nguyn

int USCLN(int a, int b) {
if (a == 0) return(b);
return (USCLN(b % a, a));
}
1.Hm quy nhn vo mt s nguyn dng n v thc hin tnh
S = 1! + 2! + + n!
#include <stdio.h>
#include <stdlib.h>
unsigned long gt(int n)
{
if (n==1) return (1);
return gt(n-1)*n;
6
}
unsigned long S(int n)
{
if (n==1) return gt(n);
return S(n-1)+gt(n);
}
int main()
{ long a=S(19);
printf("%d", a );
}

1. Vit hm tnhtng S sau vi chnh xc epsilon (epsilon l s thc
lm tham s cho hm, <0epsilon <<1)
1+ 1/2
2
+ 1/3
2
+....+

float S(float epsilon)
{
float tmp=0;
int i=1;
float sh=(float) 1/(i*i);
while (sh>=epsilon)
{
tmp = tmp + sh;
i++;
sh=(float) 1/(i*i);
}
return (tmp);

2. Vit hm gii phng trnh bc hai vi tham s l 03 h s a, b, c
ca phng trnh.

#include <stdio.h>
#include <math.h>

int PTB1(float b, float c, float &x)
{
if (b==0)
if (c==0)
return -1; //VSN
else
return 0; //VN
else
{
7
x=-c/b;
return 1; //1 nghiem
}
}
int PTB2(float a, float b, float c, float &x1, float &x2)
{
printf("%.2f*x^2 + %.2f*x + %.2f = 0\n", a, b, c);
if (a==0)
return (PTB1(b,c,x1)); //goi ham giai ptb1
else
{
float delta = b*b - 4*c*c;
if (delta<0)
return 0; //VN
else if (delta==0)
{
x1=x2=-b/(2*a);
return 3; //nghiem kep
}
else
{
x1=(sqrt(delta)-b)/(2*a);
x2=(sqrt(delta)+b)/(2*a);
return 2;//2 nghiem phan biet
}
}
}

int main(){
float x1,x2;
int kq= PTB2(2,-4,1,x1,x2);
switch (kq)
{
case -1:
printf("PTVN"); break;
case 0:
printf("PTVN"); break;
case 1:
printf("PT co 1 nghiem duy nhat x= %.3f", x1); break;
case 2:
printf("PT co 2 nghiem phan biet: x1= %.3f, x2=%.3f", x1, x2);
break;
case 3:
8
printf("PT co nghiem kep x1=x2= %.3f", x1); break;
}
}

3.4. Cho 1 dy s nguyn c n s ( 100000 n 1000000), vit chng trnh
con tm nhanh 1 s nguyn x c trong dy s hay khng. Nu c x trong
dy, chng trnh con s tr v v tr ca x trong dy, ngc li chng
trnh con s tr v -1.
typedef struct thongso{
int giatri;
int vitri;
};
typedef struct node{
thongso ts;
struct node *next;
};
typedef struct node *PTR;
PTR First;
PTR mang[10];
void khoitao(){
for(int i=0;i<=9;i++){
mang[i]=NULL;
}

}
void Insert(PTR &First,thongso ts1){
PTR p=new node;
p->ts=ts1;p->next=NULL;
if(First==NULL){
First =p;
}
else{
PTR Last;
for(Last=First;Last->next!=NULL;Last=Last->next);
Last->next=p;
}
}
//////////////////
int ngaunhien(int a, int b){
int r;
r=rand()%(b-a+1)+a ;
return r;
}
9
void taoday(long n){
for(long i=0;i<n;i++){
int j=ngaunhien(1,10);
int tam=j%10;
thongso temp;
temp.giatri=j;
temp.vitri=i;
Insert(mang[tam],temp);
printf("%d--%d---%d\n",j,i,tam);
}
}
int tim(PTR First,int x){
PTR Last;
for(Last=First;Last!=NULL&&Last->ts.giatri!=x;Last=Last-
>next);
if(Last==NULL)
return -1;
else
return Last->ts.vitri;
}
int main(){
khoitao();
long n;int x;
srand(time(NULL));
printf("NHAP N: ");
scanf("%d",&n);
taoday(n);
printf("\nNHAP X:");
scanf("%d",&x);
int t=tim(mang[x%10],x);
printf("\nVi tri:%d", t);
return 1;
}

Hm malloc (trong
<alloc.h>)
Vi du: struct hocvien {char hoten[30]; int tuoi;} hv;
FILE *fptr;
char tenfile[67], tuoi[3];
strcpy(tenfile,"F1.BIN");
if ((fptr=fopen(tenfile,"rb")) == NULL)
{ printf ("Loi mo file\n"); getch(); exit(0); }
printf(" Ho va ten Tuoi");
10
while (fread(&hv,sizeof(hv),1,fptr) ==1)
{
printf("\n%-20s",hv.hoten);
printf("%3d",hv.tuoi);
}
fclose (fptr);
Vi du : struct hocvien {char hoten[30]; int tuoi;} hv;
FILE *fptr;
char tenfile[67], tuoi[3];
strcpy(tenfile,"F1.BIN");
if ((fptr=fopen(tenfile,"wb")) == NULL)
{ printf ("Loi tao file\n"); getch(); exit(0); }
do
{ printf("Nhap ho ten hoc vien :"); gets(hv.hoten);
if (strlen(hv.hoten) !=0)
{ printf("Nhap tuoi :");
gets(tuoi);
hv.tuoi = atoi(tuoi);
fwrite(&hv, sizeof(hv), 1, fptr); }}
while (strlen(hv.hoten)!=0); fclose (fptr);

V d: Nhp xut mng A dng con tr
int A[10], *p, i;
p = A;
for (i = 0; i< 9; i++)
scanf (%d, p+i );
for (i = 0; i< 9;i++)
printf (%d, *(p+i));
2.15. Vit cc CTC sau:
Tm s nguyn t nh nht ln hn mi gi tr trong mng nguyn
Hy tm c chung ln nht ca tt c pt trong mng nguyn
Hy tm bi s chung nh nht trong mng nguyn
int ktngto(int n){
if(n<=1) return 0;
for(int i=2; i<=sqrt(n);i++)
if(n%i==0) return 0;
return 1; }
int timso_max(int a[],int n){
int max;
max=a[0];
for(int i=1;i<n;i++){
if(a[i]>max)
max=a[i];
11
}
return max;
}
void timngto_max(int a[],int n){
int max=timso_max(a,n);
for(int i=max+1;1; i++){
if(ktngto(i)==1)
// printf("so ngto max: %5d",i);
break;
}
printf("so ngto max:%5d",i);
printf("\n");
}
int UCLN(int a,int b){
if(a==0||b==0)
return a+b;
while(a!=b){
if(a>b)
a=a-b;
else b=b-a;
}
return a;
}
void UCLN_mang(int a[],int n){
int uc=a[0];
for(int i=1;i<n;i++)
uc=UCLN(uc,a[i]);
printf("UCLN cua mang la: %d",uc);
printf("\n");
}
void BCNN_mang(int a[],int n){
int bc=a[0];
for(int i=1;i<n;i++)
bc=(bc*a[i]/UCLN(bc,a[i]));
printf("BCNN cua mang la:%d",bc);
}
void main(int a[]){
clrscr();
int n;
printf("nhap vao so phan tu n=:");
scanf("%d",&n);
for(int i=0; i<n;i++)
scanf("%d",&a[i]);
12
// printf("%d",
timngto_max(a,n);
UCLN_mang(a,n);
BCNN_mang(a,n);
getch();
}
2.5. Cho 1 file text cha cc k t, trong c cc ch ci A .. Z. Vit
chng trnh con lit k tn sut xut hin ca cc k t ch ci trong file
text
int dem[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int i;
int main(){
FILE *f,*fp;
f=fopen("C:/2_5.txt","r");
if(f==NULL){
printf("\n file bi loi");
exit(0);
}
char ch;
while((ch=getc(f))!=EOF){
ch=toupper(ch);
printf("%c",ch);
if(ch>='A' && ch<='Z')
ch=ch-'A';
dem[ch]++;
}
fclose(f);
printf("\n");
fp=fopen("C:/2_5_out.txt","w");
int n=65;
int j=0;
while(n<=90&& j<26){
printf("\n %c xh :%d",n,dem[j]);
fprintf(fp,"\n %c xh :%d",n,dem[j]);
n++;
j++;
}
fclose(fp);
return 0;
}
1.15. Digital root
13
"Digital root" ca mt s nguyn dng c tnh ton bng cch cng tt
c cc ch s ca s . Nu kt qu l mt ch s th l "Digital root".
Nu khng, ton b qu trnh c lp li cho n khi kt qu l mt ch
s.
V d:
S 24: 2+4 = 6, 6 l "Digital root" ca 24
S 39: 3+9 = 12, 1+2 = 3, 3 l "Digital root" ca 39
Vit chng trnh con tr v s Digital root ca 1 s nguyn dng c
di khng qu 100 ch s.
int n; char s[100];
int Chuyenso(int n){
int s=0;
while(n!=0){
s=s+(n%10);
n=n/10;
}
return s;
}
void Root(char *s){
int kq=0,cap=0;
for(int i=0;i<strlen(s);i++){
if(s[i]>='0'&&s[i] <='9'){
kq=kq+(s[i]-48);
}
}
while(kq>9){
kq=Chuyenso(kq);
cap++;
}
printf("\nDigital_Root cua %s la : %d ",s,kq);
printf("\nCap cua %s la : %d ",s,cap);
}
1.14. Cho 1 s nguyn dng n. Vit cc chng trnh con :
a. Tr v tng cc ch s ca s nguyn n. V d: n=32045 th tng s l 14
b. Tr v s m vi cc k s l cc k s o ngc ca s n. V d:
n=32045 th m s l 54023
int TinhTong(){
int s=0;
while(n>0){
s+=n%10;
printf("%d",n%10);
14
n=n/10;
}
return s; }
int main(){
printf("Nhap n: ");
scanf("%d",&n);
printf("\n%d",TinhTong());
}


1.9. Vit ch-ng trnh con cho php ta kim tra mt
password l ng hay sai (nhp ti a password 3
ln).
void main(){
int count;
char n[30];
count=0;
t:printf("Hay nhap vao Password:");
scanf("%s",n);
count=count+1;
if(strcmp(n,"123456")!=0&& count<3){
printf("Sai pass !!! Hay nhap lai ( ban con %d lan nhap
tiep)\n",3-count);
goto t;
}
if((count==3)&&strcmp(n,"123456")!=0){
printf("Qua 3 lan nhap sai.Exit\n");
//exit(0);
}
else printf("Dung pass\n");
getch();
}
1.7. Nhap ho ten, tach ho ten ra lam 2 phan ho va ten rieng
void TachHoTen(char *st){
int i,j;
xau ho,ten;
for (i=0; i<strlen(st); i++)
if (st[i]!=' ')
ho[i]=st[i];
ho[i]='\0';
j=0;
for (i=strlen(st)-1; i>=0; i--)
15
if (st[i]!=' ') {
ten[j]=st[i]; j++;
}
ten[j]='\0'; strrev(ten);
j=0;
printf("Ho: ");puts(ho);
printf("Ten: ");puts(ten);
}
int main(){
char * st;
printf("\nNhap ten: ");
fflush(stdin);
gets(st);
TachHoTen(st);
puts(st);
}
1.5. a/ Vit hm qui, khng qui tnh hm m
x
n
, vi n nguyn.
b/ Vit hm qui, khng qui tnh phn t th n
ca hm Fibonacci
float x,s;
int n;
void Nhap(){
printf("Nhap x,n: ");
scanf("%f%d",&x,&n); }
float Mu(){
s=pow(x,n);
printf("\n%0.2f",s);
}
float MuDQ(float x, int n){
if (n==1)
return x;
return (x*MuDQ(x,n-1));
}
int Fibonaci(int n){
if(n==0)return 0;
if(n==1)return 1;
return Fibonaci(n-1)+Fibonaci(n-2);
}
void Fibonacci2(int n){
16
int i = 0, f1 = 0, f2 = 1, f3;
if(n < 1)
return;
printf("Fibonacci: 0 1 ");
for(int i=1;i<n-1;i++) {
f3 = f1 + f2;
printf("%d ", f3);
f1 = f2;
f2 = f3;
}
printf("\n");
}
int main(){
printf("Nhap n: ");
scanf("%d",&n);
Fibonacci2(n);}
1.2. Chng ta u bit nh l Pitago ni ting v tam gic vung. Bi ton
t ra l cho trc di 3 cnh . Hy xc nh xem c phi tam gic
vung hay khng.
I nput
- Gm nhiu b test. Mi b test vit trn mt dng 3 s nguyn dng
khng qu 30000, ln lt l di ba cnh.
- Input kt thc vi 3 s 0
Output
- Vi mi b test, in ra mn hnh, trn mt dng, ch right nu l mt
tam gic vung, wrong nu ngc li.

V d
INPUT OUTPUT
6 8 10
25 52 60
5 12 13
0 0 0
right
wrong
right
int check_Pytago(int a, int b, int c) {
if(a*a == b*b + c*c || b*b == a*a + c*c || c*c == b*b + a*a)
return 1;
return 0;
}

main() {
FILE *input = NULL;
17
FILE *output = NULL;
int a, b, c; a = 0; b = 0; c = 0;

input = fopen("input.txt", "r");
output = fopen("output.txt", "w");

if(input == NULL)
printf("Error opening input file. \n");
if(output == NULL)
printf("Error opening output file. \n");
if(input != NULL && output != NULL) {
while(fscanf(input, "%d %d %d", &a, &b, &c) == 3) {
if(a == 0 && b == 0 && c == 0)
break;
else {
if(check_Pytago(a, b, c))
fprintf(output, "Right. \n");
else
fprintf(output, "Wrong. \n");
}
}
}

fclose(input);
fclose(output);
getch();
}


1.1. Vit cc chng trnh con:
a. Xt 1 s c phi l s nguyn t hay khng ?
b. In trn mn hnh n s nguyn t u tin, k t s
int main(){
int n;
// int tong=0,n,i=0;
printf("nhap n:");
scanf("%d",&n);
printf("%d so nguyen to dau tien la:\n",n);
int dem = 0;
for(int i=2;i<1000;i++){
if(nto(i)){
18
printf("%4d",i);
dem++;
}
if(dem==n)
break;
}
}

int nto(int n){
if(n==0||n==1) return 0;
for(int i=2;i<=(int)sqrt(n);i++)
if(n%i==0)return 0;
return 1;
}

-------------------------------------------------------
m s k t
while (!feof(fptr))
{ ch=fgetc(fptr);
dem++;
}
-------------------------------------------------------
m s t
while ((ch=getc(fptr)) !=EOF)
{ if ((ch>='a' && ch <='z') || (ch>='A' &&
ch<='Z'))
tu=1;
if ((ch==' ' || ch=='\n' || ch=='\t') &&
tu)
{ dem++;
tu=0;
}
}
-------------------------------------------------------
Xa file
if (remove(filename) == 0)
printf("Removed %s.\n",filename);
else
perror("remove");
------------------------------

You might also like