You are on page 1of 6

// BTAPC_TH1.cpp : main project file.

#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "conio.h"
#include "string.h"
#include "ctype.h"

using namespace System;

// 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;

int main(array<System::String ^> ^args)


{
ExampleUseStruct();
printf("\n\n\n\n");
return 0;
}

void ReadInformationFromFile(char* filename,pStudent pStd,int &iCount)


{
STUDENT temp;
FILE *fp = fopen(filename,"r+b");
if (fp == NULL)
{
printf("\nCan not open file !");
}
iCount = 0;
while (!feof(fp))
{
fread(&temp,sizeof(STUDENT),1,fp);
memcpy(&pStd[iCount],&temp,sizeof(STUDENT));
iCount++;
}
// nhap phan tu cuoi den 2 lan
iCount--;
fclose(fp);
}

void OutputToBinaryFile(char *filename,pStudent pStd,int nStd)


{
FILE *fp = fopen(filename,"w+b");
if (fp == NULL)
{
printf("\nCan not open file !");
}
for (int i=0; i < nStd; i++)
{
fwrite(&pStd[i],sizeof(Student),1,fp);
}
fclose(fp);
}

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 StringFine(char *s1,char *s2)


{
int len,j;
len = strlen(s1);
j = 0;
for(int i=0; i < len; i++)
{
if (s1[i] != ' ')
{
while(s1[i] != ' ')
{
s2[j] = s1[i];
j++;
i++;
}
s2[j] = s1[i];
j++;
}
}
if (s2[j-1] == ' ')
s2[j-1] = '\0';
else
s2[j] = '\0';
}

int CountWord(char *s1)


{
int len,count;
len = strlen(s1);
count = 0;
for(int i=0; i < len; i++)
{
if (s1[i] == ' ')
count++;
}
return count+1;
}

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);
}

void PrintEachWord(char *s)


{
int len,start,end;
len = strlen(s);
start = end = 0;
printf("\nPrint one word on each line : \n");
for(int i=0; i < len; i++)
{
end = i;
while(s[i] != ' ' && i < len)
{
end++;
i++;
}
for(; start < end; start++)
{
printf("%c",s[start]);
}
printf("\n");
start = end+1;
}
}

void PrintReverseString(char *s)


{
printf("\nThe reverse string s is : ");
for(int i=strlen(s)-1; i >= 0; i--)
{
printf("%c",s[i]);
}
printf("\n");
}

bool CheckPalyndrome(char *s)


{
bool flag = true;
int len = strlen(s);
for(int i=0; i < len/2; i++)
{
if(s[i] != s[len-1-i])
{
flag = false;
break;
}
}
return flag;
}

char* StrCat(char *s1,char *s2)


{
int len1,len2,len;
len1 = strlen(s1);
len2 = strlen(s2);
len = len1 + len2;
char *temp = new char[len1];
strcpy(temp,s1);
s1 = new char[len];
int i = 0;
for (int j=0; j < len1; j++)
{
s1[i] = temp[j];
i++;
}
for (int j=0; j < len2; j++)
{
s1[i] = s2[j];
i++;
}
s1[i] = '\0';
return s1;
}

You might also like