구조체 할당
BMI
학생 정보
/* 1��
#include <stdio.h>
struct Student
{
char mName[20];
char mPhoneNumber[20];
int mStudentNumber;
void ShowInfo(void)
{
printf("name: %s\n", mName);
printf("phone number: %s\n", mPhoneNumber);
printf("student number: %d\n", mStudentNumber);
}
};
typedef struct Student Student_t;
int main(void)
{
Student_t kimlan = {};
Student_t* pKimlan = &kimlan;
printf("��� �л� �̸�: ");
scanf("%s", (*pKimlan).mName);
printf("��� �л� ���ѹ�: ");
scanf("%s", pKimlan->mPhoneNumber);
printf("��� �й�: ");
scanf("%d", &(pKimlan->mStudentNumber) );
pKimlan->ShowInfo();
Student_t park = {};
Student_t* pPark = &park;
printf("�ڰ�ȣ �л� �̸�: ");
scanf("%s\n", (*pPark).mName);
printf("�ڰ�ȣ �л� ���ѹ�: ");
scanf("%s\n", pPark->mPhoneNumber);
printf("�ڰ�ȣ �й�: ");
scanf("%d\n", &(pPark->mStudentNumber) );
pPark->ShowInfo();
return 0;
}
*/
/* ���� 2 ���� / student t ??????
#include <stdio.h>
struct student
{
char mName[20];
int mKor;
int mMath;
int mSci;
void ShowInfo(void)
{
printf("name: %s\n ", mName );
printf("korean: %d\n ", mKor );
printf("math: %d\n ", mMath );
printf("science: %d\n ", mSci );
}
int sum(void)
{
return mKor + mMath + mSci;
}
float avg(void)
{
return (float)sum() / 3;
}
};
typedef struct Student Student_t;
int main(void)
{
Student_t kimlan = {};
Student_t* pKimlan = &kimlan;
printf("��� �л� �̸�: ");
scanf("%s", pKimlan->mName);
printf("��� ���� ����: ");
scanf("%d", &(pKimlan->mKor) );
printf("��� ���� ����: ");
scanf("%d", &(pKimlan->mMath));
printf("��� ���� ����: ");
scanf("%d", &(pKimlan->mSci));
pKimlan->ShowInfo();
printf("sum: %d\n", pKimlan->sum() );
printf("avg: %.2f\n\n\n", pKimlan->avg() );
Student_t park = {};
Student_t* pPark = &park;
printf("�ڰ�ȣ �л� �̸�: ");
scanf("%s", pPark->mName);
printf("�ڰ�ȣ ���� ����: ");
scanf("%d", &(pPark->mKor) );
printf("�ڰ�ȣ ���� ����: ");
scanf("%d", &(pPark->mMath));
printf("�ڰ�ȣ ���� ����: ");
scanf("%d", &(pPark->mSci));
pPark->ShowInfo();
printf("sum: %d\n", pPark->sum() );
printf("avg: %.2f\n\n\n",pPark->avg() );
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int* pInteger = NULL;
size_t = uSize;
scanf("%zu", &uSize);
//���� �� �Ҵ� �Լ� �ñ״���
//void* malloc(size_t size);
//���� �� ���� �Լ� �ñ״�ó
//void free(void* pMellodyBlock);
//1. �� �Ҵ� �Լ� mlloc()�� ���� ���ϴ� �� ũ�⸦ ���ڰ����� ����
// 1-1. ��ȯ�� void*�� ���� ���ϴ� ������ �ڷ������� type casting.
// 1-2. �˸��� ������ �ڷ����� ������ ����.
//2. ������ �Լ� free()�� �����Ҵ�� ���� �����ּҸ� ���ڰ����� ����
// 2-1. �̿��� ����� �����ϱ� ���� �� ������ ���� �ּҿ� NULL�� ����
pInteger = (int*)malloc( sizeof(int*) * uSize );
if( pInteger != NULL )
{
// "���� �ʿ���" �� ũ�⸸ŭ �����Ҵ�� ���� ���� �ּ� pInteger�� Ȱ��
}
free(pInteger);
pInteger = NULL;
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pInteger = NULL;
size_t uSize;
printf("How many memories do you want?");
scanf("%zu", &uSize);
pInteger = (int*)malloc( sizeof(int*)* uSize ) ;
if( pInteger != NULL)
{
printf("success to memory allocation. please input data\n");
for( size_t i = 0; i < uSize; ++i)
{
//scanf("%d", *(pInteger + i) );
//pinteger + i ��� ���� ������ �� ������ ��� �迭�� ������ ������ ������,
//�ٸ�, �迭�� ������ �� ����. ��, ���α��� ������� �츮�ų� ���� �� ����.
//���� �� ������ �����ּ��� pinteger�� ���α��� ������� �츮�ų�malloc(), Ȥ�� ���� �� ���� (free())
}
printf("print data\n");
for( size_t i = 0; i < uSize; ++i )
{
printf("%d", *(pInteger + i) );
}
}
free(pInteger);
pInteger = NULL;
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
struct Human
{
char mName[20];
int mAge;
double mHeight;
double mWeight;
void ShowInfo(void)
{
printf("name: %s\n", mName );
printf("age: %d\n", mAge );
printf("height: %.2lfcm", mHeight );
printf("weight: %.2lfkg", mWeight );
}
double GetBMI(void)
{
double heightInMeter = mHeight / 100;
double bmi = mWeight / ( heightInMeter * heightInMeter );
return bmi;
}
}; typedef struct Human Human_t;
int main()
{
Human_t* pHumans = NULL; // == Human_t pHumans[uSize];
size_t uSize;
printf("How many people do you want? ");
scanf("%zu", &uSize );
pHumans = (Human_t*)malloc( sizeof(Human_t*) * uSize);
if( NULL != pHumans )
{
printf("Success to memory allocation. Please input data\n");
for(size_t i = 0;i < uSize; ++i )
{
scanf("%s", (pHumans + i)->mName );
scanf("%d", &(pHumans + i)->mAge );
scanf("%lf", &(pHumans + i)->mHeight );
scanf("%lf", &(pHumans + i)->mWeight );
}
printf("\n\nPrint data.\n");
for( size_t i = 0; i < uSize; ++i)
{
(pHumans + i)->ShowInfo();
printf("\tbmi: %.2lf\n" , (pHumans + i)->GetBMI());
}
}
free(pHumans);
pHumans = NULL;
return 0;
}
*/
/*
��ü���� ���α��� (object oriented programming)
����ü (structure ) -> Ŭ���� (class)
Def) ��ü�� û����, ����, DNA ���� ����
Note) ����ü�� ���� ���� -> Ŭ������ ���(Member)
��ü�� �Ӽ�/����
����ü�� �Լ� -> Ŭ������ ���� (Method or ����Լ� )
��ü�� ����/�ൿ
����ü�� ���� �Ҵ�(malloc()) -> Ŭ������ ������(Constructor))
����ü�� �� ����(free()) - > Ŭ������ �Ҹ���(Destructor)
*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Human
{
public: //���� ������
char mName[20];
int mAge;
double mHeight;
double mWeight;
// ������ �� �� �����Ҵ��� ���ش�,
Human( char* name, int age, double height, double weight )
{
strcpy( mName , name );
mAge = age;
mHeight = height;
nWeight = weight;
}
// �Ҹ��� �� ���Ҵ��� �������ش�
~Human(void)
{
strcpy( mName, "\0\0" );
mAge = 0;
mHeight = 0;
mWeight = 0;
}
void ShowInfo()
{
printf("name: %s\n", mName );
printf("age: %d\n", mAge );
printf("height: %.2lf\n", mHeight );
printf("weight: %.2lf\n", mWeight );
}
double GetBMI(void)
{
double heightInMeter = mHeight / 100;
double bmi = mWeight / ( heightInMeter * heightInMeter );
return bmi;
}
};
int main()
{
Human kimlan = ("Kimlan\0" , 28, 150.0, 80.0 ); // ~= kimlan = malloc();
// kimlan�� humanŬ������ ��ü
kimlan.ShowInfo(); // Human Ŭ������ ��ü kimlan �� ���� ShowInfo() ȣ��
printf("bmi: %.2lf", kimlan.GetBMI()); // Human Ŭ������ ��ü kimlan�� ���� GetBMI() ȣ��.
return 0; // main () �� ����
// - > main()�� �������� �Ҹ�
// - > �������� kimlan �� �Ҹ�
// -> "�ڵ�����" �Ҹ��ڰ� ȣ���. ~= free(kimlan)
}
*/
/*2�� ���� 1
// ============================================================
#include <stdio.h>
struct Student
{
char mName[20];
char mPhoneNumber[20];
int mStudentNumber;
void ShowInfo()
{
printf("name: %s\n", mName );
printf("phonenumber: %s\n", mPhoneNumber );
printf("studentnumber: %d\n", mStudentNumber );
}
};
typedef struct Student Student_t;
int main()
{
Student_t kimlan = {};
Student_t* pKimlan = &kimlan;
printf("��� �л��̸�: ");
scanf("%s", (*pKimlan).mName );
printf("��� �л� ����ȣ: ");
scanf("%s" , pKimlan->mPhoneNumber) ;
printf("��� �й�: ");
scanf("%d", &(pKimlan->mStudentNumber));
// print �� ���� �Ἥ �л��� ������ �ҷΤÿ�
// ==> �ʹ� ������
//�Լ��� ���뼺 + ����ü ��� �Լ� method ����
pKimlan->ShowInfo();
Student_t park = {};
Student_t* pPark = &park;
printf("�ڰ�ȣ �л��̸�: ");
scanf("%s", (*pPark).mName );
printf("�ڰ�ȣ �л� ����ȣ: ");
scanf("%s" , pPark->mPhoneNumber) ;
printf("�ڰ�ȣ �й�: ");
scanf("%d", &(pPark->mStudentNumber) );
// print �� ���� �Ἥ �л��� ������ �ҷΤÿ�
// ==> �ʹ� ������
//�Լ��� ���뼺 + ����ü ��� �Լ� method ����
pPark->ShowInfo();
return 0;
}
*/
/* ����2�� ����
#include <stdio.h>
struct Student
{
// ��� or �������
char mName[20];
int mKor;
int mMath;
int mSci;
void ShowInfo()
{
printf("name: %s\n", mName);
printf("korean: %d\n", mKor);
printf("math: %d\n", mMath);
printf("science: %d\n", mSci);
}
// ��ȯ�ڷ��� �Լ��� (�Ű��ڷ��� �Ű�������)
int sum(void)
{
return mKor + mMath + mSci;
}
float avg(void)
{
return (float)sum() / 3;
}
}; typedef struct Student Student_t;
int main()
{
Student_t kimlan = {};
Student_t* pKimlan = &kimlan;
printf("��� �л��̸�: ");
scanf("%s", pKimlan->mName );
printf("��� �����: ");
scanf("%d", &(pKimlan->mKor) );
printf("��� �����: ");
scanf("%d", &(pKimlan->mMath) );
printf("��� �����: ");
scanf("%d", &(pKimlan->mSci) );
// print�� ���� �ᷯ �л��� ������ �ҷչ� ������
//�Լ��� ���뼺 + ����ü ��� �Լ�(method) ����
pKimlan->ShowInfo();
printf("sum: %d\n", pKimlan->sum());
printf("avg: %.2lf\n", pKimlan->avg());
Student_t park = {};
Student_t* pPark = &park;
printf("�ڰ�ȣ �л��̸�: ");
scanf("%s", pPark->mName );
printf("�ڰ�ȣ �����: ");
scanf("%d", &(pPark->mKor) );
printf("�ڰ�ȣ ���м���: ");
scanf("%d", &(pPark->mMath) );
printf("�ڰ�ȣ ���м���: ");
scanf("%d", &(pPark->mSci) );
// print�� ���� �ᷯ �л��� ������ �ҷչ� ������
//�Լ��� ���뼺 + ����ü ��� �Լ�(method) ����
pPark->ShowInfo();
printf("sum: %d", pPark->sum());
printf("avg: %.2lf", pPark->avg());
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pInteger = NULL;
size_t uSize;
scanf("%zu", &uSize );
//���� �� �Ҵ� �Լ� �ñ״�ó
//void* malloc(size_t size);
//���� �� ���� �Լ� �ñ״�ó
//void free(void* pMemoryBlock);
//1. �� �Ҵ� �Լ� malloc()�� ���� ���ϴ� �� ũ�⸦ ���ڰ����� ����.
// 1-1. ��ȯ�� void*�� ���� ���ϴ� ������ �ڷ������� type casting.
// 1-2. �˸��� ������ �ڷ����� ������ ����.
// 2. �θ� ���� �Լ� free()�� �����Ҵ�� ���� �����ּҸ� ���ڰ����� ����.
// 2-1. �̿��� ����� �����ϱ� ���ؼ� �� ������ �����ּҿ� NULL�� ����
pInteger = (int*)malloc(sizeof(int*) * uSize);
if( pInteger != NULL )
{
//���� �ʿ��� �� ũ�⸸ŭ ���� �Ҵ�� ���� ���� �ּ� pInteger�� Ȱ��
}
free(pInteger);
pInteger = NULL;
return 0;
}
*/
/* ���� �������� �� �ڴ�
#include <stdio.h>
#include <stdlib.h>
int main()
{
int*pInteger = NULL;
size_t uSize;
printf("How many memories do you want?");
scanf("%zu", &uSize );
pInteger = (int*)malloc(sizeof(int*) * uSize ) ;
if( NULL != pInteger )
{
printf("Success to memory allocation. Please input data\n ");
for( size_t i = 0;i < uSize; ++i)
{
scanf("%d", (pInteger + i));
// PInteger + i ��� ������ ������ �� ������ ��� �迭�� ������ ������ ������
// �ٸ�, �迭�� ������ �� ����, �� ���α��� ������� �츮�ų� ���� �� �ִ�.
// ������ ������ ���� �ּ��� Pinteger�� ���α��� ������� �츮�ų� (malloc) Ȥ�� ���� �� ����(free())
}
printf("Print data. \n");
for( size_t i = 0;i < uSize; ++i)
{
printf("%d", *(pInteger + i));
}
}
free(pInteger);
pInteger = NULL;
return 0;
}
*/
/* SUCCESS ���� ����
#include <stdio.h>
#include <stdlib.h>
struct Human
{
char mName[20];
int mAge;
double mHeight;
double mWeight;
void ShowInfo()
{
printf("name: %s\n", mName);
printf("age: %d\n", mAge);
printf("height: %.2lfcm\n", mHeight);
printf("weight: %.2lfkg\n", mWeight);
}
double GetBMI()
{
double heightInMeter = mHeight / 100;
double bmi = mWeight / ( heightInMeter * heightInMeter );
return bmi;
}
};typedef struct Human Human_t;
int main()
{
Human_t* pHumans = NULL;
size_t uSize;
printf("%zu", &uSize );
pHumans = (Human_t*)malloc(sizeof(Human_t*) * uSize);
if( NULL != pHumans)
{
printf("Success to memory allocation. Please input data\n");
for( size_t i = 0; i < uSize; ++i )
{
scanf("%s", (pHumans + i)->mName );
scanf("%d", &((pHumans + i)->mAge) );
scanf("%lf", &((pHumans + i)->mHeight) );
scanf("%lf", &((pHumans + i)->mWeight) );
}
printf("\n\nPrint data.\n");
for(size_t i= 0 ; i < uSize; ++i )
{
(pHumans + i)->ShowInfo();
printf("\tbmi: %.2lf\n", (pHumans + i)->GetBMI() );
}
}
free(pHumans);
pHumans = NULL;
return 0;
}
*/
/*
��ü ���� ���α���(Object Oriented Programming)
����ü(Structure)->Ŭ����(Class)
Def) ��ü�� û����, ����, DNA���� ����
Note) ����ü�� ���� ���� -> Ŭ������ ���(Member)
��ü�� �Ӽ�/����
����ü�� �Լ� -> Ŭ������ ����(Method or ����Լ� )
��ü�� ����/�ൿ
����ü�� ���� �Ҵ�(malloc()) - > Ŭ������ ������(Constructor)
����ü�� �� ����(free()) - >Ŭ������ �Ҹ��� (Destructor)
*/
/* 2ȸ�� ��
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
class Human
{
public: // ���� ������
char mName[];
int mAge;
double mHeight;
double mWeight;
// ������, �� �� �����Ҵ��� ����
Human(char* name, int age, double height, double weight)
{
strcpy(mName,name);
mAge = age;
mHeight = height;
mWeight = weight;
}
// �Ҹ��� �� �� �Ҵ��� ����
~Human(void)
{
strcpy("mName" , "\0\0");
mAge = 0;
mHeight = 0.;
mWeight = 0.;
}
void ShowInfo(void)
{
printf("name: %s\n", mName );
printf("age: %d\n", mAge );
printf("height: %.2lfcm\n", mHeight );
printf("weight: %.2lfkg\n", mWeight );
}
double GetBMI(void)
{
double heightInMeter = mHeight / 100;
double bmi = mWeight / ( heightInMeter * heightInMeter );
return bmi;
}
};
int main()
{
Human kimlan = Human("KimLan\0" , 28, 150.0, 80.0); // ~= kimlan = malloc();
//kimlan�� Human Ŭ������ ��ü
kimlan.ShowInfo();// Human Ŭ������ ��ü kimlan�� ���� SHOWINFO() ȣ��
printf("bmi: %.2lf" , kimlan.GetBMI()); // Human Ŭ������ ��ü kimlan�� ���� GETbmi() ȣ��
return 0; // main�� ����
// -> main()�� �������� �Ҹ�
// -> "�ڵ�����" �Ҹ��ڰ� ȣ���. ~= free(kimlan);
}
*/
/* 3ȸ�� ����
#include <stdio.h>
struct Student
{
char mName[20];
char mPhoneNumber[20];
int mStudentNumber;
void ShowInfo(void)
{
printf("name: %s\n", mName );
printf("phone number: %s\n", mPhoneNumber );
printf("student number: %d\n", mStudentNumber );
}
};
typedef struct Student Student_t;
int main()
{
Student_t kimlan = {};
Student_t* pKimlan = &kimlan;
printf("��� �л��̸�: ");
scanf("%s", (*pKimlan).mName );
printf("��� �л� ����ȣ: ");
scanf("%s", pKimlan->mPhoneNumber );
printf("��� �й�: ");
scanf("%d", &(pKimlan->mStudentNumber) );
// printf()�� ���� �Ἥ �л��� ������ �ҷ��� ==>�ʹ� ������
// �Լ��� ���뼺 + ����ü ��� �Լ� ����
pKimlan->ShowInfo();
Student_t park = {};
Student_t* pPark = &park;
printf("�ڰ�ȣ �л��̸�: ");
scanf("%s", (*pPark).mName );
printf("�ڰ�ȣ �л� ����ȣ: ");
scanf("%s", pPark->mPhoneNumber );
printf("�ڰ�ȣ �й�: ");
scanf("%d", &(pPark->mStudentNumber) );
// printf()�� ���� �Ἥ �л��� ������ �ҷ��� ==>�ʹ� ������
pPark->ShowInfo();
return 0;
}
*/
/*
#include <stdio.h>
struct Student
{
// ��� or ��� ����
char mName[20];
int mKor;
int mMath;
int mSci;
//���� or ��� �Լ�
void ShowInfo()
{
printf("name: %s\n", mName);
printf("korean: %d\n", mKor);
printf("math: %d\n", mMath);
printf("science: %d\n", mSci);
}
//��ȯ �ڷ��� �Լ��� (�Ű� �ڷ��� �Ű� ������ )
int sum()
{
return mKor + mMath + mSci;
}
float avg()
{
return (float)sum() / 3;
}
};typedef struct Student Student_t;
int main()
{
Student_t kimlan = {};
Student_t* pKimlan = &kimlan;
printf("��� �л� �̸�: ");
scanf("%s" , pKimlan->mName);
printf("��� ���� ����: ");
scanf("%d" , &pKimlan->mKor);
printf("��� ���� ����: ");
scanf("%d" , &pKimlan->mMath);
printf("��� ���� ����: ");
scanf("%d" , &pKimlan->mSci );
// print()�� ���� ���Ἥ ������ ���뼺+ ����ü ��� �Լ� ����;
pKimlan->ShowInfo();
printf("sum: %d\n", pKimlan->sum() );
printf("avg: %.2lf\n\n\n", pKimlan->avg() );
Student_t park = {};
Student_t* pPark = &park;
printf("�ڰ�ȣ �л� �̸�: ");
scanf("%s" , pPark->mName);
printf("�ڰ�ȣ ���� ����: ");
scanf("%d" , &pPark->mKor);
printf("�ڰ�ȣ ���� ����: ");
scanf("%d" , &pPark->mMath);
printf("�ڰ�ȣ ���� ����: ");
scanf("%d" , &pPark->mSci );
// print()�� ���� ���Ἥ ������ ���뼺+ ����ü ��� �Լ� ����;
pPark->ShowInfo();
printf("sum: %d\n", pPark->sum() );
printf("avg: %.2lf\n\n\n", pPark->avg() );
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pInteger = NULL;
size_t uSize;
scanf("%zu", &uSize );
// ���� �� �Ҵ� �Լ� �ñ״�ó
// void* malloc(size_t size);
// ���� �� ���� �Լ� �ñ״�ó
// void free(void* pMemoryBlock);
// 1. �� �Ҵ� �Լ� malloc()�� ���� ���ϴ� �� ũ�⸦ ���ڰ����� ����
// 1-1. ��ȯ�� void*�� ���� ���ϴ� ������ �ڷ������� type casting.
// 1-2. �˸��� ������ �ڷ����� ������ ����.
// 2. �θ� ���� �Լ� free()�� �����Ҵ�� ���� �����ּҸ� ���ڰ����� ���� .
// 2-1. �̿��� ����� �����ϱ� ���ؼ� �� ������ ���� �ּҸ� null�� ����.
pInteger = (int*)malloc(sizeof(int*) * uSize );
if( NULL != pInteger )
{
// "���� �ʿ��� " �� ũ�� ��ŭ �Ҵ�� ���� ���� �ּ� pInteger�� Ȱ��
}
free(pInteger);
pInteger = NULL;
return 0;
}
*/
/*
#include <stdio.h>
#include <stdlib.h>
int main()
{
int* pInteger = NULL;
size_t uSize;
printf("How many memories do you want?");
scanf("%zu", &uSize );
pInteger = (int*)malloc(sizeof(int*) * uSize );
if( NULL != pInteger )
{
printf("Success to memory allocation. Please input data\n");
for( size_t i = 0; i < uSize; ++i)
{
scanf("%d" , (pInteger + i));
// "pInteger + i ��� ���� ������ �� ������ ��� �迭�� ������ ������ ������ "
//�ٸ� �迭�� ������ �� ���� �� ���α��� ������� �츮�ų� ���� �� ����. �� �Լ��� �����ų� ���α��� ������ ��
// ������ �� ������ �����ּ��� pInteger�� ���α��� ������� �츮�ų�(malloc()) Ȥ�� ����(free()) ����
}
printf("Print data.\n");
for(size_t i = 0; i < uSize; ++i )
{
printf("%d", *(pInteger + i));
}
}
free(pInteger);
pInteger = NULL;
return 0;
}
*/
/* bmi ���� !
#include <stdio.h>
#include <stdlib.h>
struct Human
{
char mName[20];
int mAge;
double mHeight;
double mWeight;
void ShowInfo()
{
printf("name: %s\n", mName );
printf("age: %d\n", mAge );
printf("height: %.2lfcm\n", mHeight );
printf("weight: %.2lfkg\n", mWeight );
}
double GetBMI()
{
double heightInMeter = mHeight / 100;
double bmi = mWeight / ( heightInMeter * heightInMeter );
return bmi;
}
};
typedef struct Human Human_t;
int main()
{
Human_t* pHumans = NULL; // ~= Human_t pHuman[uSize];
size_t uSize;
printf("How many people do you want?");
scanf("%zu", &uSize );
pHumans = (Human_t*)malloc(sizeof(Human_t*) * uSize);
if( NULL != pHumans)
{
printf("Success to memory allocation. Please input data\n");
for( size_t i = 0; i < uSize; ++i)
{
scanf("%s", (pHumans + i)->mName );
scanf("%d", &((pHumans + i)->mAge) );
scanf("lf", &((pHumans + i)->mHeight) );
scanf("lf", &((pHumans + i)->mWeight) );
}
printf("\n\nPrint data.\n");
for(size_t i = 0; i < uSize; ++i)
{
(pHumans + i)->ShowInfo();
printf("\tbmi: %.2lf\n" , (pHumans + i)->GetBMI());
}
}
free(pHumans);
pHumans = NULL;
return 0;
}
*/
/*
��ü���� ���α���
����ü (Structure) -> Ŭ���� (class)
Def) ��ü�� û����, ����, DNA ���� ����
Note) �������� �������� -> Ŭ������ ���(member)
��ü�� �Ӽ� /����
����ü�� �Լ� -> Ŭ������ ����
��ü�� ���� /�ൿ
����ü�� �����Ҵ�(malloc()) -> Ŭ������ ������(constructor)
����ü�� ������(free()) -> Ŭ������ �Ҹ���(destructor)
*/
/*
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
class Human
{
public: // ���� ������
char mName[20];
int mAge;
double mHeight;
double mWeight;
// ������ �� �� �����Ҵ��� ����
Human(char* name, int age, double height, double weight)
{
strcpy(mName , name);
mAge = age;
mHeight = height;
mWeight = weight;
}
// �Ҹ��� �� ���Ҵ��� ���� ����
~Human(void)
{
strcpy( mName, "\0\0");
mAge = 0;
mHeight = 0;
mWeight = 0;
}
void ShowInfo()
{
printf("name: %s\n",mName );
printf("age: %d\n",mAge );
printf("height: %.2lfcm\n",mHeight );
printf("weight: %.2lfkg\n",mWeight );
}
double GetBMI()
{
double heightInMeter = mHeight / 100;
double bmi = mWeight / ( heightInMeter * heightInMeter );
return bmi;
}
};
int main()
{
Human kimlan = Human("KimLan\0" , 28, 150.0, 80.0); // ~= kimlan = malloc();
// kimlan�� HumanŬ������ ��ü
kimlan.ShowInfo(); // human Ŭ������ ��ü kimlan�� ���� showinfo()ȣ�� ��
printf("bmi: %.2lf", kimlan.GetBMI() ); // Human Ŭ������ ��ü kimlan�� ���� getbmi() ȣ�߸�
return 0;
//main() ����
//main�� �������� �Ҹ�
//�������� kimlan�� �Ҹ�
//�ڵ����� �Ҹ��ڰ� ȣ��� ~= free(kimlan)
}
*/
'컴퓨터 과학 > C Language' 카테고리의 다른 글
Chapter 01. 함수(Function) (0) | 2022.01.30 |
---|---|
2022-01-27 재귀함수 (0) | 2022.01.28 |
2022-01-24 포인터 연습 (0) | 2022.01.25 |
2022-01-23 포인터 변수 (0) | 2022.01.24 |
2022-01-21 문자열 연습 (0) | 2022.01.22 |