[[연습문제 1]]
#include <iostream>
#include <stdlib.h>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
using std::setw;
struct Student
{
char name[20];
int kor, eng, mat, sum;
};
void input(Student *p, int in);
void outpt(Student *p, int out);
int main()
{
int in;
cout << "학생수 입력 : ";
cin >> in;
Student *pts;
pts = new Student[in];
cout << "실행중에 메모리 할당(동적 할당) -> new 연산자 이용\n";
input(pts, in);
outpt(pts, in);
delete [] pts;
return 0;
}
void input(Student *p, int in)
{
for(int i=0; i<in; i++)
{
system("cls");
cout << "\n[" << i+1 << "번째 입력]\n";
cout << "\n 이 름 : ";
cin >> p[i].name;
cout << "\n 국 어 : ";
cin >> p[i].kor;
cout << "\n 영 어 : ";
cin >> p[i].eng;
cout << "\n 수 학 : ";
cin >> p[i].mat;
p[i].sum = p[i].kor + p[i].mat + p[i].eng;
}
}
void outpt(Student *p, int out)
{
system("cls");
cout << "\n\n\t\t***** 점 수 출 력 *****\n\n";
cout << setw(6) << "번호" << setw(15) << "이름" << setw(7) << "국어"
<< setw(7) << "영어" << setw(7) << "수학" << setw(7) << "총점" << endl;
for(int i=0; i<out; i++)
{
cout << setw(6) << i+1 << setw(15) << (p+i)->name << setw(7) << (p+i)->kor
<< setw(7) << (p+i)->eng << setw(7) << (p+i)->mat << setw(7) << (p+i)->sum << endl;
}
}
[[연습문제 2]]
#include <iostream>
using std::cin; using std::cout; using std::endl;
#define MEMO_CNT 5
#define MEMO_LENGTH 1024
int main()
{
char *p[MEMO_CNT];
char buf[MEMO_LENGTH];
for(int i=0; i<MEMO_CNT; i++)
{
cout << "Input MEMO : ";
fflush(stdin);
cin.getline(buf , MEMO_LENGTH);
p[i] = new char[strlen(buf)+1];
strcpy(p[i], buf);
}
cout << "\n\n\t\t***** Output MEMO *****\n";
for(i=0; i<MEMO_CNT; i++)
{
cout << p[i] << endl;
}
for(i=0; i<MEMO_CNT; i++)
{
delete [] p[i];
cout << "동적 메모리 해제\n";
}
return 0;
}