Get me outta here!

September 24, 2015

Tổng hợp các bài tập lập trình C (Phần 4)

1. Bài tập về tạo file trong C
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
#define max 100
typedef struct hocsinh{
char hoten[35];
char lop [7];
float diemtoan, diemtin ;
}HOCSINH;
HOCSINH danhsach[max];
int numrecords = 0 ;
char filename[]= "DANHDACH.dat";
//================================================== ======
int menu(){
char c;
system("cls");
printf("--------Menu------------");
printf("\n ---------------------");
printf("\n +1 Nhap ");
printf("\n +2 Liet ke ");
printf("\n +3 Ghi vao dia " );
printf("\n +4 Lay tu dia ");
printf("\n +5 Thoat\n");
do{
printf(" Chon: ");
c = getchar();
getchar();
}while(c < '1' || c > '5');
return (c - '0');
}
//================================================== ======
void nhapmoi ()
{
int done = 0 ;
char hoten[35];
float diem;
do {
printf("\n Ho ten (bo trong de thoat ) : ");
gets(hoten);
if(strlen(hoten)==0)
done = 1;
else
{
strcpy(danhsach[numrecords].hoten, hoten );
printf("\n Diem toan : ");
scanf("%f", &diem); getchar();
danhsach[numrecords].diemtoan = diem;
printf("\n Diem Tin  : ");
scanf("%f", &diem); getchar();
danhsach[numrecords].diemtin = diem ;
numrecords++;
}
} while (!done);
}
//================================================== =====
void lietke(){
int i;
if (numrecords != 0){
printf("\n "); for(i = 1; i <= 59; i++) printf("-");
printf("\n | %3s | %-35s | %4s | %4s |", "STT", "HO VA TEN", "Toan", "Tin");
printf("\n "); for(i = 1; i <= 59; i++) printf("-");
for (i = 0 ; i < numrecords; i++)
{
printf ("\n | %3d |", i+1);
printf (" %-35s | %4.1f | %4.1f | ", danhsach[i].hoten, danhsach[i].diemtoan , danhsach[i].diemtin );
}
printf("\n "); for( i = 1; i <= 59; i++) printf("-");
}
getch();
}
//========================================================
void getdata(){
char c;
int start , nrec;
FILE *fp;
long curpos, length ;
start = 0 ;
if (numrecords != 0){
printf("\n Dang co du lieu trong bo nho .Co muon noi them vao ko ? (C/K)");
c = toupper(getchar()); getchar();
if (c == 'C')
start = numrecords;
else if(c == 'K'){
numrecords = 0;
start = 0 ;
}else return;
}
if ((fp = fopen (filename , "r")) == NULL )
printf(" Khong the mo tap tin %s " , filename);
else{
curpos = ftell(fp);
fseek(fp, 0L, SEEK_END);
length = ftell(fp);
rewind(fp);
nrec = length / sizeof(HOCSINH);
fread(&danhsach[start], sizeof(HOCSINH), nrec, fp);
fclose(fp);
printf("\n %s %d records tu file. ", (numrecords == 0)? "Mo": "Them vao", nrec);
numrecords = numrecords + nrec;
getch();
}
}
//========================================================
void putdata (){
FILE *fp ;
if ((fp = fopen(filename , "w+")) == NULL )
printf("\n Khong thay ghi tap tin %s ", filename );
else{
fwrite(&danhsach[0], sizeof(HOCSINH), numrecords, fp);
fclose(fp);
printf("\n Ghi %d records vao file." , numrecords);
getch();
}
}
//================================================== ======
int main(){
int chon , done = 0 ;
do {
chon = menu ();
switch(chon)
{
case 1 : nhapmoi();
break;
case 2 : lietke();
break ;
case 3 : putdata ();
break ;
case 4 : getdata ();
break ;
//default : done = 1;
}
} while (!done );
}
2. Có 3 chữ số mà tổng bằng tích
#include<stdio.h>
#include<conio.h>
main()
{
    int n,tong=0,tich=1;
    printf("cac so co 3 chu so ma tong bang tich la: ");
    for(n=100;n<=999;n++)
    {    int i;
         while(i!=0)
      {  tong = tong+i;
         tich = tich*i;
         i=n%10;
          n=n/10; }
        if(tong == tich)
        printf("\n%d",&tong);
        }
   getch();
   }      
3. Dãy fibonacci
#include<stdio.h>
#include<conio.h>
int fibonacci(int n)
{
    if(n==0 || n==1) return 1;
    else
    return (fibonacci(n-2)+fibonacci(n-1));
    }
main()
{
      int n,i;
      printf("nhap vao gioi han day so muon tim: ");
      scanf("%d",&n);
      for(i=0;i<n;i++){
      printf("%2d",fibonacci(n));}
      getch();
      }    
4. Đổi giờ phút giây
#include<stdio.h>
#include<conio.h>
main()
{
      int a,gio,phut,giay;
      printf("nhap vao so giay : ");
      scanf("%d",&a);
      gio = (a/3600);
      phut = (a%3600)/60;
      giay = (a%3600)%60;
      printf("%d giay = %d gio %d phut %d giay",a,gio,phut,giay);
      getch();
   }

0 comments:

Post a Comment