Professional Documents
Culture Documents
#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#include "ctype.h"
// String
// Struct
struct Student
{
int id;
char name[40];
float pc,cf,hdj;
};
typedef struct Student STUDENT;
typedef struct Student* pStudent;
void ExampleUseString();
void ExampleUseStruct();
void StringFine(char*,char*);
void PrintEachWord(char*);
void PrintReverseString(char*);
void InputStudent();
void SearchStudent();
void DisplayAllStudent();
int CountWord(char*);
bool CheckPalyndrome(char*);
char* StrCat(char*,char*);
void OutputToBinaryFile(char*,pStudent,int);
void ReadInformationFromFile(char*,pStudent,int&);
Student stu[20];
int nStudent;
void ExampleUseStruct()
{
int choice;
char *reset = "C:\\Documents and Settings\\admin.PC\\Desktop\\";
char *fileName = "C:\\Documents and Settings\\admin.PC\\Desktop\\";
while (choice != 9)
{
printf("1. Input the student \n");
printf("2. Search student by id \n");
printf("3. Display all student\n");
printf("4. Output to binary file \n");
printf("5. Read information from file \n");
printf("9. Quit \n");
printf("\nPlease choose one number in the menu : ");
scanf_s("%d",&choice);
switch (choice)
{
case 1:
InputStudent();
break;
case 2:
SearchStudent();
break;
case 3:
DisplayAllStudent();
break;
case 4:
//strcpy(fileName,reset);
//StrCat(fileName,"output.dtc")
OutputToBinaryFile("C:\\Documents and
Settings\\admin.PC\\Desktop\\output.dtc",stu,nStudent);
break;
case 5:
//strcpy(fileName,reset);
//StrCat(fileName,"output.dtc")
ReadInformationFromFile("C:\\Documents and
Settings\\admin.PC\\Desktop\\output.dtc",stu,nStudent);
break;
case 9:
exit(0);
}
printf("\n\n\n");
}
}
void InputStudent()
{
char cont = 'Y';
int i = 0;
float temp;
while (toupper(cont) == 'Y')
{
printf("\nEnter the id of the student %d : ",i+1);
scanf_s("%d",&stu[i].id);
printf("\nEnter the student name : ");
fflush(stdin);
gets(stu[i].name);
printf("\nEnter the pc mark : ");
scanf_s("%f",&temp);
stu[i].pc = temp;
printf("\nEnter the cf mark : ");
scanf_s("%f",&temp);
stu[i].cf = temp;
printf("\nEnter the hdj mark : ");
scanf_s("%f",&temp);
stu[i].hdj = temp;
i++;
printf("\n\nContinue(Y/N)? : ");
fflush(stdin);
scanf_s("%c",&cont);
}
nStudent = i;
}
void SearchStudent()
{
int id;
bool flag = false;
printf("\nEnter the id of the student for searching : ");
scanf_s("%d",&id);
for (int i=0; i < nStudent; i++)
{
if (stu[i].id == id)
{
printf("\nStudent's id : %d",stu[i].id);
printf("\nStudent's name : %s",stu[i].name);
printf("\nPC mark : %.2f",stu[i].pc);
printf("\nCF mark : %.2f",stu[i].cf);
printf("\nHDJ mark : %.2f",stu[i].hdj);
printf("\nAverage mark : %.2f",(stu[i].pc + stu[i].cf +
stu[i].hdj)/3);
flag = true;
break;
}
}
if (flag == false)
{
printf("\nThe student's id %d is not exist.",id);
}
}
void DisplayAllStudent()
{
printf("\nNo\t Id\t Name\t PC\t CF\t HDJ\t Average");
for (int i=0; i < nStudent; i++)
{
printf("\n%d\t ",i+1);
printf("%d\t ",stu[i].id);
printf("%s\t ",stu[i].name);
printf("%.2f\t ",stu[i].pc);
printf("%.2f\t ",stu[i].cf);
printf("%.2f\t ",stu[i].hdj);
printf("%.2f\t ",(stu[i].pc + stu[i].cf + stu[i].hdj)/3);
}
}
void ExampleUseString()
{
char s1[40],s2[40];
printf("Nhap chuoi s1: ");
gets(s1);
StringFine(s1,s2);
printf("\nChuoi s2: ");
puts(s2);
PrintEachWord(s2);
PrintReverseString(s2);
int count = CountWord(s2);
printf("\nThe number of words in the string s2 is %d",count);
}