C++1차원 포인터
1차원 포인터를 활용한 성적관리 프로그램
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 |
#include <iostream>
using namespace std;
typedef struct _student {
char name[10];
int kor, eng, math;
int total;
float avg;
} Student;
void MemAlloc(Student **p, int cnt)
{
*p = new Student[cnt];
cout << "메모리할당완료" << endl;
}
void Input(Student *stu, int cnt)
{
for (int i = 0; i < cnt; i++) {
cout << "Input name : ";
cin >> (stu + i)->name;
cout << "Input Kor : ";
cin >> (stu + i)->kor;
cout << "Input Eng : ";
cin >> (stu + i)->eng;
cout << "Input Math : ";
cin >> (stu + i)->math;
}
cout << "입력완료" << endl;
}
void GradeProSys(Student *stu, int cnt)
{
for (int i = 0; i < cnt; i++) {
(stu + i)->total = (stu + i)->kor + (stu + i)->eng + (stu + i)->math;
(stu + i)->avg = (stu + i)->total / 3.f;
}
cout << "연산 완료 " << endl;
}
void Output(Student *stu, int cnt)
{
cout << "NAME / KOR / ENG / MATH / TOTAL / AVG" << endl;
for (int i = 0; i < cnt; i++) {
cout << (stu + i)->name << "\t" << (stu + i)->kor <<
"\t" << (stu + i)->eng << "\t" << (stu + i)->math <<
"\t" << (stu + i)->total << "\t" << (stu + i)->avg << endl;
}
}
void DelMem(Student *stu, int cnt)
{
delete[]stu;
cout << "해제 완료" << endl;
}
void main()
{
int cnt;
Student *stu = NULL;
cout << "몇사람 입력? : ";
cin >> cnt;
// 메모리 할당
MemAlloc(&stu, cnt);
// 입력
Input(stu, cnt);
// 연산
GradeProSys(stu, cnt);
// 출력
Output(stu, cnt);
// 해제
DelMem(stu, cnt);
}
|
cs |
다차원 포인터를 이용한 성적관리프로그램(구조체 사용안함)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 |
#include <iostream>
#include <windows.h>
using namespace std;
// function prototype declaration (함수 프로토타입 선언)
void Input(char(*name)[10], int(*score)[4], int cnt);
void Operation(int(*score)[4], float *avg, int cnt);
void Output(char(*name)[10], int(*score)[4], float *avg, int cnt);
void main()
{
char (*name)[10]; // 다음 행이 10칸 점프한다는 걸 인식하겠다는 뜻
int (*score)[4];
float *avg;
int cnt;
// people counting
cout << "몇 사람? : ";
cin >> cnt;
name = new char[cnt][10];
score = new int[cnt][4];
avg = new float[cnt];
Input(name, score, cnt);
Operation(score, avg, cnt);
Output(name, score, avg, cnt);
delete[]name;
delete[]score;
delete[]avg;
}
void Input(char (*name)[10], int (*score)[4], int cnt)
{
int i, j;
for (i = 0; i < cnt; i++) {
cout << "Input name : ";
cin >> name[i]; // == *(name + i)
for (j = 0; j < 3; j++) {
if (j == 0) cout << "Input Korean : ";
else if (j == 1) cout << "Input English : ";
else cout << "Input Mathematics : ";
cin >> score[i][j];
// == *(score[i] + j), *(*(score + i) + j);
}
}
}
void Operation(int (*score)[4], float *avg, int cnt)
{
int i, j;
for (i = 0; i < cnt; i++) {
*(score[i] + 3) = 0;
for (j = 0; j < 3; j++) {
*(score[i] + 3) += *(score[i] + j);
}
*avg = *(score[i] + 3) / 3.f;
}
}
void Output(char (*name)[10], int (*score)[4], float *avg, int cnt)
{
int i, j;
system("cls");
cout << "NAME / KOR / ENG / MATH / TOTAL / AVG" << endl;
for (i = 0; i < cnt; i++) {
cout << *(name + i) << "\t";
for (j = 0; j < 3; j++) {
cout << *(score[i] + j) << "\t";
}
cout << *avg << endl;
}
}
|
cs |
'C++' 카테고리의 다른 글
[C++][string만들기] (0) | 2018.04.16 |
---|---|
[JAVA][instanceof 보자] (1) | 2018.03.22 |
C++setw()=함수 (0) | 2018.03.21 |
[C++]깊은 대입연산자함수 (0) | 2018.03.21 |
[C++] operator 함수 (0) | 2018.03.21 |