0% found this document useful (0 votes)
8 views6 pages

ctdl

The document is a C program that manages a word list, allowing users to input, output, sort, search, update, and delete words along with their meanings. It includes functions for reading from and writing to a file, as well as handling user interactions through a menu-driven interface. The program utilizes a structure to store word data and employs various standard library functions for string manipulation and file operations.

Uploaded by

bientran2306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views6 pages

ctdl

The document is a C program that manages a word list, allowing users to input, output, sort, search, update, and delete words along with their meanings. It includes functions for reading from and writing to a file, as well as handling user interactions through a menu-driven interface. The program utilizes a structure to store word data and employs various standard library functions for string manipulation and file operations.

Uploaded by

bientran2306
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#include <stdio.

h>
#include <string.h>
#include <stdbool.h> // Thêm thu vi?n này d? s? d?ng ki?u d? li?u bool
#include <stdlib.h> // Thêm thu vi?n này d? s? d?ng hàm `remove`

struct WORD
{
char Name[256];
char Meaning[512];
};

void Input_Word(WORD &a)


{
printf("Nhap Name: ");
fflush(stdin);
fgets(a.Name, sizeof(a.Name), stdin);
a.Name[strlen(a.Name) - 1] = '\0'; // Lo?i b? ký t? '\n' th?a t? fgets

printf("Nhap Meaning: ");


fflush(stdin);
fgets(a.Meaning, sizeof(a.Meaning), stdin);
a.Meaning[strlen(a.Meaning) - 1] = '\0'; // Lo?i b? ký t? '\n' th?a t? fgets
}

void Input_WordList(WORD a[], int &n)


{
printf("Nhap so phan tu: ");
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
printf("Nhap WORD[%d]\n", i);
Input_Word(a[i]);
}
}

void Output_Word(WORD a)
{
printf("Name:%s\n", a.Name);
printf("Meaning:%s\n", a.Meaning);
}

void Output_WordList(WORD a[], int n)


{
for (int i = 0; i < n; i++)
{
printf("WORD[%d]\n", i);
Output_Word(a[i]);
}
}

void Swap(WORD &c, WORD &b)


{
WORD temp;
temp = b;
b = c;
c = temp;
}

void wordsortaz(WORD a[], int n)


{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (strcmp(a[i].Name, a[j].Name) > 0)
Swap(a[i], a[j]);
}
}
}

void Search_Word(WORD a[], int n, const char *searchName)


{
for (int i = 0; i < n; i++)
{
if (strcmp(a[i].Name, searchName) == 0)
{
printf("Tim thay tu:\n");
Output_Word(a[i]);
return; // K?t thúc sau khi tìm th?y
}
}
printf("Khong tim thay tu co Name: %s\n", searchName);
}

void Write_Word_To_File(FILE *file, const WORD &a)


{
fprintf(file, "%s|%s\n", a.Name, a.Meaning);
}

void Read_Word_From_File(FILE *file, WORD &a)


{
fscanf(file, "%255[^|]|%511[^\n]\n", a.Name, a.Meaning);
}

void Write_WordList_To_File(const WORD a[], int n, const char *filename)


{
FILE *file = fopen(filename, "w");
if (file == NULL)
{
printf("Khong the mo tep de ghi\n");
return;
}

for (int i = 0; i < n; i++)


{
Write_Word_To_File(file, a[i]);
}

fclose(file);
}

void Update_Meaning_In_File(FILE *file, const char *searchName, const char


*newMeaning, const char *filename)
{
FILE *tempFile = fopen("temp.txt", "w");
if (tempFile == NULL)
{
printf("Khong the mo tep tam de cap nhat\n");
return;
}

WORD word;
while (fscanf(file, "%255[^|]|%511[^\n]\n", word.Name, word.Meaning) == 2)
{
if (strcmp(word.Name, searchName) == 0)
{
strcpy(word.Meaning, newMeaning);
}
Write_Word_To_File(tempFile, word);
}

fclose(file);
fclose(tempFile);
remove(filename);
rename("temp.txt", filename);
}

void Delete_Word_From_File(FILE *file, const char *searchName, const char


*filename)
{
FILE *tempFile = fopen("temp.txt", "w");
if (tempFile == NULL)
{
printf("Khong the mo tep tam de xoa\n");
return;
}

WORD word;
while (fscanf(file, "%255[^|]|%511[^\n]\n", word.Name, word.Meaning) == 2)
{
if (strcmp(word.Name, searchName) != 0)
{
Write_Word_To_File(tempFile, word);
}
}

fclose(file);
fclose(tempFile);
remove(filename);
rename("temp.txt", filename);
}

int main(int argc, char *argv[])


{
WORD one, many[100];
int choice, n;
bool check = true;
const char *filename = "wordlist.txt"; // Tên t?p m?c d?nh

while (check)
{
printf("0.Thoat\n");
printf("1.Input word\n");
printf("2.Output word\n");
printf("3.Input wordlist\n");
printf("4.Output wordlist\n");
printf("5.Sort wordlist\n");
printf("6.Search word by Name\n");
printf("7.Write a word to file\n");
printf("8.Read a word from file\n");
printf("9.Update Meaning in file\n");
printf("10.Delete word from file\n");
printf("11.Write wordlist to file\n");

printf("Chon chuc nang can thuc hien: ");


scanf("%d", &choice);
switch (choice)
{
case 0:
check = false;
break;
case 1:
Input_Word(one);
break;
case 2:
Output_Word(one);
break;
case 3:
Input_WordList(many, n);
break;
case 4:
Output_WordList(many, n);
break;
case 5:
wordsortaz(many, n);
printf("Da sap xep theo Name tang dan\n");
break;
case 6:
{
char searchName[256];
printf("Nhap ten can tim: ");
fflush(stdin);
fgets(searchName, sizeof(searchName), stdin);
searchName[strlen(searchName) - 1] = '\0'; // Lo?i b? ký t? '\n' th?a
t? fgets
Search_Word(many, n, searchName);
break;
}
case 7:
{
FILE *file = fopen(filename, "a");
if (file == NULL)
{
printf("Khong the mo tep de ghi\n");
}
else
{
WORD word;
Input_Word(word);
Write_Word_To_File(file, word);
fclose(file);
}
break;
}
case 8:
{
FILE *file = fopen(filename, "r");
if (file != NULL)
{
n = 0; // Ð?t n v? 0 tru?c khi d?c t? t?p d? tránh vi?c ghi dè lên các t?
cu
while (fscanf(file, "%[^|]|%[^\n]\n", many[n].Name, many[n].Meaning) == 2)
{
n++;
}
fclose(file);
}
break;
}

case 9:
{
char updateName[256];
char newMeaning[512];
printf("Nhap ten can cap nhat: ");
fflush(stdin);
fgets(updateName, sizeof(updateName), stdin);
updateName[strlen(updateName) - 1] = '\0'; // Lo?i b? ký t? '\n' th?a
t? fgets
printf("Nhap nghia moi: ");
fflush(stdin);
fgets(newMeaning, sizeof(newMeaning), stdin);
newMeaning[strlen(newMeaning) - 1] = '\0'; // Lo?i b? ký t? '\n' th?a
t? fgets
FILE *file = fopen(filename, "r+");
if (file != NULL)
{
Update_Meaning_In_File(file, updateName, newMeaning, filename);
fclose(file);
}
break;
}
case 10:
{
char deleteName[256];
printf("Nhap ten can xoa: ");
fflush(stdin);
fgets(deleteName, sizeof(deleteName), stdin);
deleteName[strlen(deleteName) - 1] = '\0'; // Lo?i b? ký t? '\n' th?a
t? fgets
FILE *file = fopen(filename, "r+");
if (file != NULL)
{
Delete_Word_From_File(file, deleteName, filename);
fclose(file);
}
break;
}
case 11:
Write_WordList_To_File(many, n, filename);
break;
default:
break;
}
}
return 0;
}

You might also like