코락 CoRock
코딩하는 락스타
코락 CoRock
  • 분류 전체보기 (393)
    • frameworks (19)
      • spring (19)
      • spring-boot (0)
      • testing (0)
    • languages (94)
      • java (39)
      • kotlin (0)
      • python (42)
      • r (13)
    • libraries (0)
    • programming (239)
      • android (13)
      • c (17)
      • cpp (22)
      • database (18)
      • design-pattern (4)
      • data-structures (11)
      • git (8)
      • hadoop (6)
      • html-css (7)
      • issue (4)
      • javascript (26)
      • jsp (34)
      • os (29)
      • php (6)
      • preferences (19)
      • etc (15)
    • discography (37)
      • k-pop (18)
      • pop (19)
    • blog (3)

블로그 메뉴

  • Programming
  • Java
  • JavaScript
  • Discography
  • K-Pop Songs
  • Pop Songs
  • Blog
  • Guestbook

공지사항

인기 글

태그

  • python
  • jsp
  • linux
  • Android
  • 파이썬
  • javascript
  • Java
  • oracle
  • r
  • 자바스크립트
  • Spring
  • CentOS

최근 댓글

최근 글

티스토리

반응형
hELLO · Designed By 정상우.
코락 CoRock

코딩하는 락스타

programming/cpp

성적처리프로그램

2018. 3. 6. 11:09
반응형

case 1 : structure를 이용

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
#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);
}
Colored by Color Scripter
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
#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;
    }
}
Colored by Color Scripter
cs


반응형
저작자표시 비영리 변경금지 (새창열림)
    'programming/cpp' 카테고리의 다른 글
    • [C++] Class : 절차지향을 넘어 객체지향으로!
    • [C++] Overloading
    • [C++] setw()
    • [C++] Pointer
    코락 CoRock
    코락 CoRock
    A COder dreaming of being a ROCKstar

    티스토리툴바