SLIDE1

Wednesday, January 21, 2015

phân tích 1 số nguyên ra thừa số nguyên tố

//lập trình c phân tích một số nguyên ra thừa số nguyên tố
#include<stdio.h>
long snt(long n)
{
if(n<2)return 0;
if(n==2) return 1;
for(long i=2;i<n;i++)
if(n%i==0)return 0;
return 1;
}
void main()
{
long i,n,x,d=1;
printf("nhap N= ");scanf("%d",&n);
printf("%d = ",n);
while(n>1)
{
x=1;
while(1) if(snt(++x)==1 && n%x==0) break;
if(d) {printf("%d",x);d=0;}
else printf(" * %d",x);
n/=x;
}
printf("\n");
}

Monday, January 19, 2015

nhập vào biểu thức cộng trừ tùy ý, in kết quả ra màn hình


//nhập vào 1 biểu thức cộng trừ tùy ý và in ra màn hình kết quả của biểu thức đó
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void main()
{
char s[1000];
loop:;
do
{
printf("nhap bieu thuc: ");fflush(stdin);gets(s);
if(strlen(s)!=0)
{
int dem=0;
for(int b=0;b<strlen(s);b++)
if(s[b]==' ')dem++;
if(dem==0) break;
else printf("khong go <space>\n");
}
}while(1);
int i=strlen(s)-1,d=0,a[1000],k,j,z;
char x[1000],q,dau[1000];
while(i>=0)
{
k=0;
while(s[i]>='0' && s[i]<='9') x[k++]=s[i--];
i--;
x[k]='\0';
for(j=0;j<strlen(x)/2;j++){q=x[j];x[j]=x[strlen(x)-1-j];x[strlen(x)-1-j]=q;}
a[d++]=atoi(x);
}
for(i=0;i<d/2;i++){z=a[i];a[i]=a[d-1-i];a[d-1-i]=z;}
k=0;
for(i=0;i<strlen(s);i++)
if(s[i]=='+' || s[i]=='-') dau[k++]=s[i];
int t=a[0];
for(i=1;i<d;i++)
if(dau[i-1]=='+') t+=a[i];
else t-=a[i];
printf("\t\t= %d \n\n",t);
printf("----------------------------\n| tiep tuc: phim <space>   |\n| ket thuc: phim bat ky    |\n----------------------------\n\n");
char ket=getch();
if(ket==32) goto loop;
}

Saturday, January 17, 2015

sắp xếp ma trận A mxn giảm dần từ trái qua phải đồng thời giảm dần từ trên xuống dưới


//sắp xếp ma trận A mxn giảm dần từ trái qua phải đồng thời giảm dần từ trên xuống dưới.
//ma trận được cấp phát bộ nhớ động và tạo ngẫu nhiên bằng hàm rand();
#include<stdio.h>
#include<stdlib.h>
void xuat(int **a,int h,int c)
{
printf("\n\n");
for(int i=0;i<h;i++)
{
printf("\n\n");
for(int j=0;j<c;j++) printf("%-4d",a[i][j]);
}
printf("\n\n");
}
void sapxep(int *a,int h,int c)
{
for(int i=0;i<h*c-1;i++)
for(int j=i+1;j<h*c;j++)
if(a[i]<a[j]){int t=a[i];a[i]=a[j];a[j]=t;}
}
void matran(int **a,int h,int c)
{
int i,j,d=0,*b=(int*)malloc(h*c*sizeof(int));
for(i=0;i<h;i++)
for(j=0;j<c;j++) b[d++]=a[i][j];
sapxep(b,h,c);
d=0;
for(i=0;i<h;i++)
for(j=0;j<c;j++) a[i][j]=b[d++];
}
void main()
{
int h,c,i,j;
printf("nhap so hang, so cot:\n");
scanf("%d%d",&h,&c);
int **a=(int**)malloc(h*sizeof(int*));
for(i=0;i<h;i++) a[i]=(int*)malloc(c*sizeof(int));
for(i=0;i<h;i++)
for(j=0;j<c;j++) a[i][j]=rand()%100;
printf("ma tran da tao:\n");
xuat(a,h,c);
matran(a,h,c);
printf("ma tran sau khi sap xep:\n");
xuat(a,h,c);
}

sắp xếp ma trận A mxn tăng từ trái qua phải, đồng thời từ trên xuống dưới


//sắp xếp ma trận A mxn tăng dần từ trái qua phải đồng thời tăng dần từ trên xuống dưới.
//ma trận được cấp phát bộ nhớ động và tạo ngẫu nhiên bằng hàm rand();
#include<stdio.h>
#include<stdlib.h>
void doi(int &a,int &b)
{
int t=a;a=b;b=t;
}
void sapxep(int *a,int h,int c)
{
for(int i=0;i<h*c-1;i++)
for(int j=i+1;j<h*c;j++)
if(a[i]>a[j]) doi(a[i],a[j]);
}
void xuat(int **a,int h,int c)
{
for(int i=0;i<h;i++)
{
printf("\n\n");
for(int j=0;j<c;j++) printf("%-4d",a[i][j]);
}
printf("\n\n");
}
void tao(int **a,int h,int c)
{
for(int i=0;i<h;i++)
for(int j=0;j<c;j++)
a[i][j]=rand()%100;
}
void sapxepmatran(int **a,int h,int c)
{
int i,j,d=0;
int *b=(int*)malloc(h*c*sizeof(int));
for(i=0;i<h;i++)
for(j=0;j<c;j++) b[d++]=a[i][j];
sapxep(b,h,c);
a[0][0]=b[0];d=1;
if(h==c)
{
for(i=1;i<h;i++)
{
for(j=0;j<i;j++)
{
a[j][i]=b[d++];a[i][j]=b[d++];
}
a[i][i]=b[d++];
}
}
else if(h<c)
{
for(i=1;i<h;i++)
{
for(j=0;j<i;j++)
{
a[j][i]=b[d++];a[i][j]=b[d++];
}
a[i][i]=b[d++];
}
for(i=h;i<c;i++)
for(j=0;j<h;j++) a[j][i]=b[d++];
}
else
{
for(i=1;i<c;i++)
{
for(j=0;j<i;j++)
{
a[j][i]=b[d++];a[i][j]=b[d++];
}
a[i][i]=b[d++];
}
for(i=c;i<h;i++)
for(j=0;j<c;j++) a[i][j]=b[d++];
}
}
void main()
{
int h,c,i;
printf("nhap so hang , so cot: \n");
scanf("%d%d",&h,&c);
int **a=(int**)malloc(h*sizeof(int));
for(i=0;i<h;i++) a[i]=(int*)malloc(c*sizeof(int));
tao(a,h,c);
printf("ma tran da tao:\n");
xuat(a,h,c);
sapxepmatran(a,h,c);
printf("ma tran sau khi sap xep: \n");
xuat(a,h,c);
}

Friday, January 16, 2015

đếm chữ số xuất hiện ít nhất trong ma trận A mxn

đếm chữ số xuất hiện ít nhất trong ma trận A mxn, chữ số chứ không phải là phần tử xuất hiện ít nhất( đọc kỹ đề). và đấy là bài giải: ma trận ở đây tôi cấp phát ngẫu nhiên bằng hàm rand() cho nhanh:

#include<stdio.h>
#include<stdlib.h>
void xuat(int **a,int h,int c)
{
for(int i=0;i<h;i++)
{
printf("\n\n");
for(int j=0;j<c;j++) printf("%-4d",a[i][j]);
}
printf("\n");
}
int min(int **a,int h,int c)
{
int i,j,b[10]={0,0,0,0,0,0,0,0,0,0};
for(i=0;i<h;i++)
for(j=0;j<c;j++)
do{
b[a[i][j]%10]++;
a[i][j]/=10;
}while(a[i][j]!=0);
int min,vt;
for(i=0;i<10;i++)if(b[i]!=0){min=b[i];vt=i;break;}
for(i=0;i<10;i++) if(min>b[i] && b[i]!=0){min=b[i];vt=i;}
return vt;
}
void main()
{
printf("nhap so hang, so cot:\n");
int h,c;scanf("%d%d",&h,&c);
int **a=(int**)malloc(h*sizeof(int*));
for(int i=0;i<h;i++) a[i]=(int*)malloc(c*sizeof(int));
for(int i=0;i<h;i++)
for(int j=0;j<c;j++)
a[i][j]=rand()%51;
xuat(a,h,c);
printf("\n\nchu so xuat hien it nhat: %d\n",min(a,h,c));
}

Sunday, January 11, 2015

ghi nội dung mảng lên file nhị phân


//ghi nội dung mảng lên file nhị phân
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void nhapmang(int a[],int n)
{
for(int i=0;i<n;i++)
{
printf("A[%d]=",i+1);scanf("%d",&a[i]);
}
}
void main()
{
FILE *t=fopen("E:\\vd1.bin","w+b");
if(t==NULL)
{
printf("error");exit(0);
}
int n;
printf("so phan tu cua mang: ");
scanf("%d",&n);
int *a=(int*)malloc(n*sizeof(int));
printf("nhap mang: \n");
nhapmang(a,n);
fwrite(&a,n*sizeof(int),1,t);//dung lượng cho mỗi phần tử là int => của cả mảng là n*sizeof(int)
fseek(t,0,2);
int m=ftell(t)/sizeof(int);//so luong bang ghi=dung lượng mảng chia dung lượng của 1 phần tử
int *b=(int*)malloc(m*sizeof(int));
rewind(t);
fread(&b,sizeof(int),1,t);
printf("mang da ghi len file: \n");
for(int i=0;i<m;i++) printf("%d ",b[i]);
fclose(t);
}

Saturday, January 10, 2015

Viết chương trình in nội dung file văn bản, chỉ in các ký tự chữ cái và chữ số.


//Bài 7. Viết chương trình in nội dung file văn bản, chỉ in các ký tự chữ cái và chữ số.
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
FILE *t=fopen("E:\\bai7.txt","r");
if(t==0)
{
printf("error\n");exit(0);
}
char c;
while((c=fgetc(t))!=EOF)
if((c>='a' && c<='z')||(c>='A' && c<='Z')||(c>='0' && c<='9')) printf("%c ",c);
getch();
}

Bài 6. Viết chương trình đão ngược nội dung của một file văn bản.


//Bài 6. Viết chương trình đão ngược nội dung của một file văn bản. 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void dao(char *s)
{
char c;
for(int i=0;i<strlen(s)/2;i++)
{
c=s[i];s[i]=s[strlen(s)-1-i];s[strlen(s)-1-i]=c;
}
}
void dois(char *a,char *b)
{
int i;char c[1000];
for(i=0;i<strlen(a);i++)c[i]=a[i];c[strlen(a)]='\0';
for(i=0;i<strlen(b);i++)a[i]=b[i];a[strlen(b)]='\0';
for(i=0;i<strlen(c);i++)b[i]=c[i];b[strlen(c)]='\0';
}
void nhap(FILE *t)
{
printf("nhap noi dung:\n\n");
char s[1000];
int dem=0;
do
{
gets(s);
if(strlen(s)>0 && dem==0)fputs(s,t);
if(strlen(s)>0 && dem>0)
{
fputs("\n",t);fputs(s,t);
}
if(strlen(s)>0)dem++;
}while(strlen(s)>0);
}
void xuat(FILE *t)
{
char s[1000];
while(fgets(s,1000,t)!=0)printf("%s",s);
}
void main()
{
FILE *t=fopen("E:\\bai6.txt","w+");
if(t==0)
{
printf("error\n");exit(0);
}
nhap(t);
rewind(t);
char s[1000],a[1000][1000];int n=0;
while(fgets(s,1000,t)!=0)
{
strcpy(a[n],s);n++;
}
for(int i=0;i<n/2;i++)dois(a[i],a[n-i-1]);
for(int i=0;i<n;i++)dao(a[i]);
rewind(t);
for(int i=0;i<n;i++)fputs(a[i],t);
rewind(t);
printf("noi dung dao nguoc:\n\n");
xuat(t);
getch();
}

Friday, January 9, 2015

Viết chương trình đão ngược nội dung của một file văn bản.


//Bài 6. Viết chương trình đão ngược nội dung của một file văn bản. 
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
void dao(char *a)
{
for(int i=0;i<strlen(a)/2;i++)
{
char c=a[i];
a[i]=a[strlen(a)-1-i];
a[strlen(a)-1-i]=c;
}
}
void main()
{
FILE *t;
t=fopen("E:\\bai6.txt","w+");
if(t==0)
{
printf("error\n");exit(0);
}
char s[1000];
char cd;
printf("nhap: ");fflush(stdin);gets(s);
fprintf(t,"%s\n",s);
printf("--noi dung dao nguoc\n");
rewind(t);
fgets(s,1000,t);
dao(s);
fprintf(t,"%s\n",s);
fgets(s,1000,t);
printf("%s\n",s);
fclose(t);
}

thao tác trên tập tin: tu sửa và thêm mẫu tin


//Bài 3.thao tác trên tập tin: tu sửa và  thêm mẫu tin 
///* Chuong trinh file truy nhap tuan tu -  : tao,xem,sua,them vao cuoi file,cho phep chon ten file  */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<conio.h>
struct danhsach
{
char ten[100];
int tuoi;
};
void nhap(FILE *t)
{
danhsach ds;
char s[3];
do
{
printf("ten: ");gets(ds.ten);
if(strlen(ds.ten)!=0)
{
printf("tuoi: ");gets(s);ds.tuoi=atoi(s);
fwrite(&ds,sizeof(ds),1,t);
}
}while(strlen(ds.ten)!=0);
}
void sua(FILE *t)
{
danhsach ds;
char s[3];
fseek(t,0,2);
int n=ftell(t)/sizeof(ds);
rewind(t);
for(int i=0;i<n;i++)
{
fseek(t,i*sizeof(ds),0);
fread(&ds,sizeof(ds),1,t);
printf("\nhien tai:\nten:%s\ntuoi:%d\n--sua=<enter>--ok=<0>\n",ds.ten,ds.tuoi);
char c=getch();
if(c!='0')
{
printf("ten:");gets(ds.ten);
printf("tuoi: ");gets(s);ds.tuoi=atoi(s);
fseek(t,i*sizeof(ds),0);
fwrite(&ds,sizeof(ds),1,t);
}
}
}
void xuat(FILE *t)
{
danhsach ds;
while(fread(&ds,sizeof(ds),1,t)!=0)
{
printf("\nten:%s\ntuoi:%d\n",ds.ten,ds.tuoi);
}
}
void main()
{
FILE *t;
t=fopen("E:\\bai3.txt","w+b");
nhap(t);
rewind(t);
sua(t);
rewind(t);
xuat(t);
fclose(t);
}

nhập xuất các mẫu tin có cấu trúc vào file nhị phân


//Bài 2. 
// Viết chương trình thực hiện các yêu cầu:
//• Mở tập tin mới và nhập vào một số mẫu tin. 
// Mỗi mẫu tin bao gồm các trường: họ tên, tuổi
// Quá trình nhập dữ liệu kết thúc khi họ tên nhập vào là rỗng. 
// • Thêm dữ liệu vào tập tin. 
// • Mở tập tin để đọc và hiển thị ra màn hình nội dung tập tin. 
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct danhsach
{
char ten[100];
int tuoi;
};
void nhap(FILE *t)
{
danhsach ds;
char s[3];
do
{
printf("ten: ");
gets(ds.ten);
if(strlen(ds.ten)!=0)
{
printf("tuoi: ");
gets(s);
ds.tuoi=atoi(s);
fwrite(&ds,sizeof(ds),1,t);
}
}while(strlen(ds.ten)!=0);
}
void xuat(FILE *t)
{
danhsach ds;
while(fread(&ds,sizeof(ds),1,t)!=0)
{
printf("\nten: %s\ntuoi: %d\n",ds.ten,ds.tuoi);
}
}
void main()
{
FILE *t;
t=fopen("E:\\bai2.txt","w+b");
nhap(t);
rewind(t);
xuat(t);
nhap(t);
rewind(t);
xuat(t);
fclose(t);
}

nhập 10 số thực vào một file văn bản có tên là input.


/*bài 1.*/ 
// viết chương trình thực hiện các yêu cầu: 
//• nhập 10 số thực vào một file văn bản có tên là input. 
//• đọc nội dung file input.  
//• tính tổng bình phương các số có trong file input. 
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void nhap(FILE *t)
{
float a;
for(int i=1;i<=10;i++)
{
printf("so %d: ",i);
scanf("%f",&a);
fprintf(t,"%f ",a);
}
}
void doc(FILE *t)
{
float a;
while(fscanf(t,"%f",&a)!=EOF)
{
printf("%.2f ",a);
}
}
float tongbinhphuong(FILE *t)
{
float a,tong=0;
while(fscanf(t,"%f",&a)!=EOF) tong+=a*a;
return tong;
}
void main()
{
FILE *t;
t=fopen("E:\\input.txt","w+");
if(t==NULL)
{
printf("error\n");
exit(0);
}
nhap(t);
rewind(t);
doc(t);
rewind(t);
printf("\ntong binh phuong = %.2f\n",tongbinhphuong(t));
fclose(t);
}

[một số chú ý] kiểu FILE trong ngôn ngữ c

kiểu file trong lập trình c


  • fread(biến địa chỉ, bộ nhớ, số cấu trúc cần đọc, file); //trả về số cấu trúc  đọc được, nếu hết file có nghĩa là trả về 0.
  • fwrite(biến đại chỉ, bộ nhớ, số cấu trúc cần ghi, file);//trả về số cấu trúc ghi được, trả về 0 nếu ghi không được hay lên file thất bại.
    /* các cấu trúc có thể mảng, 1 biến bình thường hay biến dữ liệu kiểu cấu trúc, thay thế được các vòng for trong việc đọ hay ghi thông thường */
  • fscanf(file, kiểu, biến địa chỉ); //trả về giá trị EOF nếu quét đến cờ hiệu kết thúc file EOF (-1).
  • feof(file); //trả về giá trị là vị trí của con trỏ nếu con trỏ file đang đứng ở vị trí cờ hiệu kết thúc file. chưa kết thúc file thì trả về 0.
  • getc(file);//nếu quét đến cờ hiệu kết thúc file thì trả về giá trị char tương ứng là EOF hay (-1).
  • fopen("tên file/ có thể là đường dẫn file","kiểu mở"); //trả về địa chỉ FILE trên bộ nhớ nếu thành công, và trả về giá trị NULL nếu thất bại.

Thursday, January 8, 2015

tạo 1 file trực tiếp từ bàn phím


//tạo 1 file trực tiếp từ bàn phím, quá trình dừng lại khi ấn phím enter
#include<stdio.h>
void main()
{
printf("ten file kem duong dan:");
char s[67];
char c;
gets(s);
FILE *t;
t=fopen(s,"w");
if(t==NULL) printf("error");
else
{
printf("nhap noi dung: ");
while((c=getchar())!=10) putc(c,t);
}
fclose(t);
}

in nội dung tạp tin ra màn hình


//in nội dung tạp tin ra màn hình
#include<stdio.h>
void main()
{
FILE *t;
char s[100],c;
printf("nhap file:");
gets(s);
t=fopen(s,"r");
if(t==NULL) printf("error");
else
{
while((c=getc(t))!=EOF)
{
printf("%c",c);
}
}
fclose(t);
}