You are on page 1of 5

#include <stdio.

h>
#include <string.h>

struct hs;
char* lines(char c, int len);
char* center(char s[],int len);
void printLine(char c);
void printTitle();
void showAll();
void saveToFile();
int main()
{
int n = 1;
struct hs {}list[100];
for(int i=0;i<n;i++){
printf("Ho va Ten: ");
gets(list[i].name);
printf("Nam sinh: ");
scanf("%d%*c",&list[i].yob);
printf("Gioi tinh: ");
scanf("%d%*c",&list[i].sex);
}
printTitle();
showAll(list,n);

return 0;
}
struct hs{
char name[100];
int yob;
int sex;
};

char* lines(char c, int len){


static char s[1000];
for(int i = 0; i < len; i++)
s[i] = c;
s[len] = '\0';
return s;
}

char* center(char s[],int len){


int llen = len - strlen(s);
llen /= 2;
static char s_new[1000] = "";
for(int i = 0; i < llen; i++)
s_new[i] = 32;
for(int i = 0; i < strlen(s); i++)
s_new[llen+i] = s[i];
for(int i = llen + strlen(s); i < len; i++)
s_new[i] = 32;
s_new[len] = '\0';
return s_new;
}
void printLine(char c){
printf("+%s", lines(c, 52));
printf("+%s+", lines(c, 10));
printf("%s+", lines(c, 12));
}
void printTitle(){
char colunms[][10] = {"Ho va Ten","Nam sinh","Gioi tinh"};

printLine('=');

printf("\n| %s ",center(colunms[0], 50));


printf("| %s |", center(colunms[1], 8));
printf(" %s |\n",center(colunms[2], 10));
printLine('=');
}

void showAll(struct hs list[], int size){


char sex[][5] = {"---", "Nu", "Nam"};
for(int i = 0; i < size; i++){
printf("\n| %s", list[i].name, 50);
printf("| %4d%5s", list[i].yob, " ");
if(list[i].sex >= 0 && list[i].sex <3){
printf("| %s |\n", center(sex[list[i].sex], 10));
}
printLine('-');
}
}
void saveToFile(struct hs list[], int size){
FILE *f;
f = fopen("C:\\Users\\MAY 1\\Documents\\DevC","w");
if(f == NULL){
printf("Khong tao duoc file");
return;
}
for(int i=0; i<size; i++){
fwrite(&list[i],sizeof(struct hs), 1, f);
if(fwrite == 0){
printf("Luu khong thanh cong");
return;
}
}
fclose(f);
}

000000000000000000
/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include <stdio.h>
#include <string.h>

struct hs{
char name[100];
int yob;
int sex;
};
char* lines(char c, int len){
static char s[1000];
for(int i = 0; i < len; i++)
s[i] = c;
s[len] = '\0';
return s;
}
char* center(char s[],int len){
int llen = len - strlen(s);
llen /= 2;
static char s_new[1000] = "";
for(int i = 0; i < llen; i++)
s_new[i] = 32;
for(int i = 0; i < strlen(s); i++)
s_new[llen+i] = s[i];
for(int i = llen + strlen(s); i < len; i++)
s_new[i] = 32;
s_new[len] = '\0';
return s_new;
}
void printLine(char c){
printf("+%s", lines(c, 52));
printf("+%s+", lines(c, 10));
printf("%s+", lines(c, 12));
}
void printTitle(){
char colunms[][10] = {"Ho va Ten","Nam sinh","Gioi tinh"};

printLine('=');

printf("\n| %s ",center(colunms[0], 50));


printf("| %s |", center(colunms[1], 8));
printf(" %s |\n",center(colunms[2], 10));
printLine('=');
}
void showAll(struct hs list[], int size){
char sex[][5] = {"---", "Nữ", "Nam"};
for(int i = 0; i < size; i++){
printf("\n| %s", list[i].name, 50);
printf("| %04d%05s", list[i].yob, " ");
if(list[i].sex >= 0 && list[i].sex <3){
printf("| %s |\n", center(sex[list[i].sex], 10));
}
printLine('-');
}
}
int main()
{
int n = 1;
struct hs list[100];
for(int i=0;i<n;i++){
printf("Ho va Ten: ");
gets(list[i].name);
printf("Nam sinh: ");
scanf("%d%*c",&list[i].yob);
printf("Gioi tinh: ");
scanf("%d%*c",&list[i].sex);
}
printTitle();
showAll(list,n);

return 0;
}

You might also like