8.01.2017

0801 C++공부

#include <iostream>

using namespace std;

void Func(int num) {
        if (num > 2) return;
        Func(num + 1);
        cout << num << endl;

}

/*void Function(int num) {
        if (num > 10) return;
        Function(num + 1);
        if (num <= 1) {
                cout << num;
        }
        else {
                cout << num << ", " ;
        }
}*/

void Function2(int a) {
        int sum = 0;
        sum += a;
        if (a <= 1) {
                cout << sum;
        }
        else cout << sum << " + ";

        Function2(a - 1);

}

int Function3(int i, int end, int total){
        if (i >= end) {
                cout << i << " + ";
                total += i;

                return total;
        }
        else {
                total += i;

                total = Function3(i + 1, end, total);
                if (i == 1) cout << i;
                else cout << i << " + ";
        }
return total;
}

void main() {
/*
재귀함수 : 자기자신을 다시 호출
: 또 다른 반복문
: 코드를 반복문보다 간결화한다

        void func(){
        if문 return; //함수를 종료시킬 수 있게 함
        func();
        )
        */
        int num = 1, integer, total = 0;
        cin >> integer;
        //Function(num);
        cout << " = " << Function2(integer);
        cout << " = " << Function3(num, integer, total);
}

============================================================================
#include <stdio.h>

typedef struct Test{
        int num;
        char ch;
        double db;
}test;


typedef int a; //int형을 a라는 이름으로 재정의
                  //a == int

        //함수 사용 이유
        //: 공통적 재사용성

void main() {

        /*
        구조체 : 하나 이상의 자료형을 모아놓은 사용자 지정 자료형
C언어 : 변수만 생성가능
변수에는 포기값 대입(x)
C++ : 함수, 변수 둘 다 사용 가능
변수에 초기값 대입(o)
        구조체 생성
        struct 구조체네임{
                (정수형, 문자, 문자열, 포인터, 배열)변수1;
                변수2;
                변수3;
        };

        //--C언어--
        //구조체 변수
        //struct 구조체네임 변수네임;
        */

        /*
        struct Test t1; // t1이라는 struct Test 자료형
                 //struct Test == test
        test t2; // struct Test t2;
        test t3 = { 10, 'A', 10.1234 };

        //구조체 변수 초기화
        //구조체 변수네임 = {};
        //- 구조체 변수를 만들었을 때 바로 초기화
        //- 순차적으로 변수에 값이 들어가게 됨


        // . : 연결자(도트 연산자)
        t1.num = 10; // t1변수 안에 있는 num 변수 구조체 변수 생성
        t1.ch = 'A';
        t1.db = 10.1234;

        t2.num = 20;
        t2.ch = 'B';
        t2.db = 20.1234;


        printf("%d %c %lf \n", t1.num, t1.ch, t1.db);
        printf("%d %c %lf \n", t2.num, t2.ch, t2.db);

        test t;
        test* p;
        p = &t;

        //포인터 변수 p를 이용하여 t구조체 변수의 num에 10 대입
        (*p).num = 10; //p->num = 10;

        printf("%d\n", t.num);
        //도트연산자가 접근연사자보다 우선순위가 높아서 괄호로 묶음
        //포인터 : int -> int*
        */

}

===============================================================================

#include <stdio.h>

typedef struct Student {
        char name[12];
        int kor, math, eng, total;
        double average;
}student;

void input(student* s) {

        //접근 : student* s = &s1;
        //값 변경 시 포인터 이용 : student* s = &s2;
        printf("학생의 이름을 입력하시오.\n");
        scanf("%s", s->name);
        printf("학생의 국어, 수학, 영어 점수를 입력하시오.\n");
        scanf("%d", &s->kor); //&s->kor == &(*s).kor
        scanf("%d", &s->math);
        scanf("%d", &s->eng);
        s->total = s->kor + s->math + s->eng;
        s->average = (double)s->total / 3;

}
void disp(student s) {
        //복사(대입) student s = s1;
        printf("학생들의 정보");
        printf("학생 이름 : &s \n", s.name);
        printf("국어점수 : &d \n", s.kor);
        printf("수학점수 : &d \n", s.math);
        printf("영어점수 : &d \n", s.eng);
        printf("총합점수 : &d \n", s.total);
        printf("평균점수 : %.2lf \n", s.average);

}
void main() {
        /* Student 구조체 생성
        구조체 내의 변수(멤버변수) :
        이름, 국어, 수학, 영어, 총점, 평균

        s1,s2,s3 구조체 변수 생성 후

        input 함수 : 모든 학생의 이름, 국어, 수학, 영어 입력

        disp함수 : 모든 학생의 정보 출력
        */
        student s1;
        student s2;
        student s3;

        input(&s1);
        input(&s2);
        input(&s3);
        disp(s1);
        disp(s2);
        disp(s3);

        /*
        c언어 구조체 : 구조체 멤버변수에 초기화 불가능
         무조건 변수만 들어가야함
                                접근 지정자 typedef을 이용하여 이름의 재정의를 해줌
                ex) typedef struct Test{}test; == test

        공통점 :
        구조체 생성 시 struct 예약어를 사용한다.
        구조체 변수 구조체명 구조체 변수;

        포인터(*) : 주소를 저장할 변수를 만들 때 사용
                      주소를 저장하여 접근하기 위해 사용
        */

        /*char name[12];
        scanf("%s", name);
        printf("%c%c", name[0], name[1]);
        printf("%c%c", name[2], name[3]);
        */
}

댓글 없음:

댓글 쓰기