Bai tap lab 10
3 posters
Diễn Đàn Thảo Luận Máy Tính :: Thảo luận chung về chương trình học tại FPT - APTECH :: Programming in C
Trang 1 trong tổng số 1 trang
dathl_c00493- Mod
- Tổng số bài gửi : 28
Ngày tham gia : 10/12/2010
Tuổi : 33
Đến từ : THAI BINH
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Người chơi
bai 1
- Code:
/*
tao file myfile.txt,
nhap noi dung tu ban phim, ky tu, chuoi roi luu vao file
mo file doc cac ndung da co
*/
#include<stdio.h>
int writeFile(char *fileName, char *str);
//doc het cac ndung trong file (count)
int readFile(char *fileName, char *str);
int main(){
char str[81];
readFile("myfile.txt", str);
printf("\nnhap vao chuoi: ");
fflush(stdin);
gets(str);
strcat(str, " ");
writeFile("myfile.txt", str);
fflush(stdin);
getchar();
}
int writeFile(char *fileName, char *str){
FILE *f;
f = fopen(fileName,"a");
if(f == NULL)
return 0;
fputs(str, f);
fclose(f);
}
int readFile(char *fileName, char *str){
FILE *f;
//char *str;
f = fopen(fileName, "r");
if(f == NULL)
return 0;
while(!feof(f)){
fgets(str, 81, f);
printf("%s", str);
}
fclose(f);
}
dathl_c00493- Mod
- Tổng số bài gửi : 28
Ngày tham gia : 10/12/2010
Tuổi : 33
Đến từ : THAI BINH
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Người chơi
bai 2
- Code:
#include<stdio.h>
typedef struct{
char name[50];
short age;
char address[100];
}infoPerson;
infoPerson inputInfo();
int writeFile(char *fileName, infoPerson Person);
int readFile(char *fileName, infoPerson *Person);
void displayPerson(infoPerson Person);
int main(){
infoPerson Person;
char fileName[10] = "myfile.dat";
readFile(fileName, &Person);
displayPerson(Person);
Person = inputInfo();
writeFile(fileName, Person);
fflush(stdin);
getchar();
}
infoPerson inputInfo(){
infoPerson Person;
printf("Ten: ");
fflush(stdin);
gets(Person.name);
printf("Tuoi: ");
scanf("%d", &Person.age);
printf("Dia chi: ");
fflush(stdin);
gets(Person.address);
return Person;
}
int writeFile(char *fileName, infoPerson Person){
FILE *f;
f = fopen(fileName,"wb");
if(f == NULL)
return 0;
fwrite(&Person, sizeof(infoPerson), 1, f);
fclose;
}
int readFile(char *fileName, infoPerson *Person){
FILE *f;
f = fopen(fileName, "rb");
if(f == NULL)
return 0;
fread(Person, sizeof(infoPerson), 1, f);
fclose(f);
return 1;
}
void displayPerson(infoPerson Person){
printf("Ten: ");
printf("%s\n", Person.name);
printf("Tuoi: ");
printf("%d\n", Person.age);
printf("Dia chi: ");
printf("%s\n\n", Person.address);
}
dathl_c00493- Mod
- Tổng số bài gửi : 28
Ngày tham gia : 10/12/2010
Tuổi : 33
Đến từ : THAI BINH
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Người chơi
Re: Bai tap lab 10
- Code:
#include<stdio.h>
#include<string.h>
int menu();
struct dict{
char word[30];
char description[50];
char type;
};
struct dict inputVocab();
int writeFile(char *fileName, int count, struct dict *vocab);
int readFile(char *fileName, struct dict *vocab);
void showHeader();
void showLine();
void displayVocab(struct dict *vocab, int count);
int searchWord(struct dict *vocab, int count, char *word);
int main(){
int choice;
int count = 0;
char fileName[20] ="dict.dat";
struct dict vocab[50];
char word[30];
count = readFile(fileName, vocab);
do{
choice = menu();
switch(choice){
case 1:vocab[count++] = inputVocab();
break;
case 2:displayVocab(vocab, count);
break;
case 3:printf("nhap vao 1 tu: ");
fflush(stdin);
gets(word);
int vt;
vt = searchWord(vocab, count, word);
if(vt == -1)
printf("khong tim thay tu ban vua nhap!\n");
else
printf("tu ban vua nhap co trong file!\n");
break;
}
}while(choice != 4);
writeFile(fileName, count, vocab);
fflush(stdin);
getchar();
}
int menu(){
printf("\t\tDICTIONARY\n");
printf("==============================\n");
printf("1. Add new word\n");
printf("2. List all\n");
printf("3. Search\n");
printf("4. Exit\n");
printf("==============================\n");
printf("chon: ");
int choice;
while(1){
fflush(stdin);
scanf("%d", &choice);
if(choice < 1 || choice > 4){
printf("ban da nhap sai!!!");
}
else
break;
}
return choice;
}
struct dict inputVocab(){
struct dict vocab;
printf("Tu: ");
fflush(stdin);
gets(vocab.word);
printf("Nghia: ");
gets(vocab.description);
printf("danh tu or dong tu or tinh tu('n','v','a'): ");
scanf("%c", &vocab.type);
return vocab;
}
int writeFile(char *fileName, int count, struct dict *vocab){
FILE *f;
f = fopen(fileName, "wb");
if(f == NULL)
return 0;
fwrite(&count, sizeof(int), 1, f);
fwrite(vocab, sizeof(struct dict), count, f);
fclose(f);
}
int readFile(char *fileName, struct dict *vocab){
FILE *f;
f = fopen(fileName, "rb");
if(f == NULL)
return 0;
int m;
fread(&m, sizeof(int), 1, f);
fread(vocab, sizeof(struct dict), m, f);
fclose(f);
return m;
}
void showLine(){
char word[30] = "------------------------";
char description[50] = "------------------------------------";
printf("+-%-1s-+-%-24s-+-%-36s-+-%-6s-+","-", word, description, "------");
}
void showHeader(){
showLine();
printf("| %-1s | %-24s | %-36s | %-6s |","#","word","description"," type");
}
void displayVocab(struct dict *vocab, int count){
int i;
showHeader();
for(i = 0; i < count; i++){
showLine();
printf("| %-1d | %-24s | %-36s | %-6c |", i+1, vocab[i].word, vocab[i].description, vocab[i].type);
}
showLine();
}
int searchWord(struct dict *vocab, int count, char *word){
int i;
for(i = 0; i < count; i++)
if(strcmp(vocab[i].word, word) == 0){
return i;
break;
}
return -1;
}
dathl_c00493- Mod
- Tổng số bài gửi : 28
Ngày tham gia : 10/12/2010
Tuổi : 33
Đến từ : THAI BINH
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Người chơi
Re: Bai tap lab 10
Bài 10 giống đề 3 Assig nhỉ... sao ko thêm cái setting nữa bác
TidusAnhTu- Thành viên cấp 2
- Tổng số bài gửi : 144
Ngày tham gia : 05/12/2010
Tuổi : 34
Đến từ : Hà Nội Ciudad
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Trọng tài
Re: Bai tap lab 10
Bạn Tidus làm được bài nào rồi post cho anh em tham khảo với!!
AiNq_C00498- Mod
- Tổng số bài gửi : 69
Ngày tham gia : 11/12/2010
Tuổi : 40
Đến từ : XM
Re: Bai tap lab 10
Đề bài chỉ có vậy thôi! Làm thêm thầy trử điểm mất
dathl_c00493- Mod
- Tổng số bài gửi : 28
Ngày tham gia : 10/12/2010
Tuổi : 33
Đến từ : THAI BINH
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Người chơi
Re: Bai tap lab 10
AiNq_C00498 đã viết:Bạn Tidus làm được bài nào rồi post cho anh em tham khảo với!!
chưa làm bài nào cả
ak mà sao lên CMS lẫn mail dều ko thấy bài Lab10 nào nhỉ
PS: Mod nào thấy bài nào hay thi pick lên chú ý cho ae dễ tìm nhé
TidusAnhTu- Thành viên cấp 2
- Tổng số bài gửi : 144
Ngày tham gia : 05/12/2010
Tuổi : 34
Đến từ : Hà Nội Ciudad
Thành viên FAT3-T1010I
HP:
(50/50)
MP:
(50/50)
Quyền hạn: Trọng tài
Diễn Đàn Thảo Luận Máy Tính :: Thảo luận chung về chương trình học tại FPT - APTECH :: Programming in C
Trang 1 trong tổng số 1 trang
Permissions in this forum:
Bạn không có quyền trả lời bài viết
Sun Feb 16, 2014 3:23 pm by Admin
» Tải game APO Snow cho android
Tue Dec 17, 2013 10:17 am by ducls1102
» Bài hát cho những người lính
Tue Aug 13, 2013 11:20 am by Admin
» Nhạc hay của tui :(
Fri May 04, 2012 10:44 pm by Admin
» Tìm lại giây phút yên bình của ngày hôm qua
Mon Apr 09, 2012 1:09 am by Admin
» PHIÊN BẢN MỚI CHO THIÊN ĐƯỜNG CÁ Ô LA LA !!!
Wed Mar 07, 2012 3:40 pm by nikitait
» Đáp án Assignment từ đề 1 đến đề 3
Tue Sep 13, 2011 1:13 pm by viennv170993
» Aloooooooooooooooooooooooooooooooooooo
Sat Jun 04, 2011 2:21 pm by shanshiaro
» Tro Lai Cua 1 Mod
Sat Jun 04, 2011 2:21 pm by shanshiaro