4.20.2012

Gợi ý BTVN 20/04

LASTEST VERSION !!!
Dưới đây là toàn bộ code Homework. Mong các bạn đọc và hiểu nó thay vì copy và paste (đây cũng là lý do mình không muốn post full vì sợ các bạn sẽ không chịu suy nghĩ về bài tập dẫn đến hậu quả xấu trong kỳ thi thì mình chẳng muốn chút nào), mọi thắc mắc xin để lại comment tại blog hoặc trao đổi qua yahoo. Thân !
[Click vào dấu ngắt nhảy để xem, nếu có]




/* Thong Tin Nhan Vien by ... "Yourself" */

#include "stdio.h"
#include "string.h"
#include "conio.h"
#include "iostream.h"
#include "stdlib.h"
#include "windows.h"

struct nhanvien
{
 char hoten[50];
 char tuoi[2];
};
void write(FILE *f,nhanvien nv[],int k)
{

 f=fopen("E:\\test.dat","wb");
 if(f==NULL) 
 {
  cout<<"Cannot create file";
  exit(1);
 }
 fflush(stdin);
 for(int i=0;i<k;i++)
 {
  fwrite(&nv[i].hoten,sizeof(nhanvien),1,f);
  fwrite(&nv[i].tuoi,sizeof(nhanvien),1,f);
 }
 fclose(f);
}
void read(FILE *f,int k)
{
 fflush(stdout);
 f=fopen("E:\\test.dat","rb");
 if(f==NULL) 
 {
  cout<<"Cannot open file";
  exit(1);
 }
 void *s;
 s=new nhanvien[k];
 cout<<"Thong tin nhan vien ban da nhap la\n";
 cout<<"\tHo Ten\t\tTuoi\n";
 for(int i=0;i<k;i++)
 {
  fread(s,sizeof(nhanvien),1,f);
  printf("%d.\t %s\t\t  ",i+1,s);
  fread(s,sizeof(nhanvien),1,f);
  printf("%s  \n",s);
 }
 fclose(f);
}
int main()
{
        FILE *f;
 unsigned int k; 
 struct nhanvien *nv;
 cout<<"Nhap so luong nhan vien can nhap thong tin: ";cin>>k;
 cout<<"\n"; 
 nv= new nhanvien[k];
 for(int i=0;i<k;i++)
 {
  fflush(stdin);
  cout<<"Nhap ten nguoi thu "<<i+1<<" : ";  
  gets(nv[i].hoten);
  cout<<"Nhap tuoi: ";
  gets(nv[i].tuoi);
 }
 write(f,nv,k);
 read(f,k);
// delete("E:\\test.dat");
 getch();
}



0 Nhận xét:

Đăng nhận xét

Đăng ký Đăng Nhận xét [Atom]

<< Trang chủ