728x90
300x250

[프로그래밍 퀴즈(Quiz)] C언어의 구조체 문제(Structure Problems in C Language)


구조체는 서로 관계가 있는 변수들을 한 데 모아 하나의 묶음으로 표현한 자료 형식을 말한다.

이러한 구조체는 여러 가지 상황에서 유용하게 쓰인다.

첫째로 함수는 하나의 값만을 리턴할 수 있지만, 구조체 변수를 리턴하게 되면 여러 변수의 값을 되돌리는 것도 가능하게 된다.

둘째로 다양한 자료구조의 표현이 가능하게 된다.


"스택(Stack), 큐(Queue), 트리(Tree)" 등을 만드는 데 있어서 구조체는 필수적이다.


구조체는 다음처럼 선언한다.


(A structure is a data format that represents a set of related variables.
This structure is useful in many situations.
First, a function can return only one value, but returning a structure variable also makes it possible to return the values of several variables.
Second, various data structures can be represented.

Structures are essential in creating "stacks, queues, and trees."

The structure is declared as follows:)


typedef struct{

      char name[30];

      int age;

} STUDENT;


typedef는 사용자가 임의의 문자열을 원하는 문자열로 치환해 주는 명령어이다.
(typedef is a command that replaces an arbitrary string with a string you want.)


typedef unsigned int UINT;

      UINT a;


struct는 이 변수들의 모음이 구조체임을 선언하는 것이다.

마지막의 STUDENT는 구조체 변수의 이름이다. 따라서 실제도 선언된 구조체를 사용할 때는 다음처럼 사용하면 된다.
(struct declares that this collection of variables is a structure.
The last STUDENT is the name of the structure variable. Therefore, when using a structure that is actually declared, you can use:)


STUDENT lee;

     lee.name = '이철수';

     lee.age = 22;


구조체 변수 안에 반드시 일반 자료형만을 넣을 수 있는 것은 아니다. 배열, 포인터, 구조체도 구조체 변수의 멤버로 선언할 수 있다.
(You can't just put generic data types in structure variables.
Arrays, pointers, and structures can also be declared as members of structure variables.)


* 구조체 연산(Structure operations)

구조체 변수를 사용할 때는 다음처럼 사용하면 된다. (When using a structure variable, use.)


STUDENT one;

     one.math = 100;

     printf("Math Score = %d", one.math);


(참고로 포인터는 C언어 카테고리에서 게시물을 확인할 수 있음. 찾아보기 바람.)

(Note that the pointer can be found in the C language category. Please browse.)


위와 같이 구조체 변수, 멤버 변수의 형태로 사용할 수 있다. 하지만 구조체 변수가 포인터 변수로 선언되었다면 다르게 사용해야 한다.


STUDENT *two;

     two->math = 100;
     printf("Math Score = %d", two->math);


typedef struct{
     char name[30];
     int kor, eng, math, total;
     float ave;
} STUDENT;

void main(void){
        STUDENT y, *py;

       
        py = &y;

        strcpy(y.name, "lee");

        y.kor = 100;
        y.eng = 90;
        y.math = 80;

        y.total = y.kor + y.eng + y.math;
        y.ave = (float)y.total / 3;

       printf("Name: %s\n", py->name);
       printf("Korean = %d\n", py->kor);
       printf("English = %d\n", py->eng);
       printf("Mathematics = %d\n", py->math);

       printf("Total = %d\n", py->total);
       printf("Average = %f\n", py->ave);
}


구조체 포인터를 매개 변수로 쓰는 방법(How to write a structure pointer as a parameter)

void func(STUDENT *one);


포인터 변수 선언하듯이 변수 앞에 *만 붙여주면 된다.(Just like * declares a pointer variable, just put * before the variable.)

반응형
728x90
300x250

[프로그래밍 퀴즈(Quiz)] 범용적인 문제 - 재귀 호출(General Problem-Recursive Calls)


1부터 n까지의 합을 구하는 프로그램을 작성한다.

과 같이 표현되는 것은 순차적인 방법으로 표현되어서 for, while과 같은 반복문을 사용해서 구현할 수 있을 것이다.

두 번째, sum(n)을 다른 방법으로 즉, 재귀적인 방법으로 표현하면 다음과 같다.


            

            


컴퓨터 프로그래밍 코드를 통해서 알아보도록 하자.


#include <iostream>

using namespace std;

int sum(int n);
int main(){

    cout << sum(10) << endl;
}

int sum(int n){

    if ( n == 1 ){
        return 1;
    }
    else{
        return sum(n-1) + n;
    }
}



반응형
728x90
300x250
[프로그래밍 퀴즈(Quiz)] 프로그래밍 기초 퀴즈(C++)


초급적인 문제입니다.



(입력1)


입력

출력

12345

5,4,3,2,1



(소스코드)


#include <iostream>
#include <string>
#include <vector>
#include <math.h>

using namespace std;

vector<int> solution(long long n) {
    vector<int> answer;
    string strNumber;

    long long conversionNumber;
    int div = 10;
    int size = 0;
    int tmp = n;

    while (tmp > 0) {

        tmp = tmp / 10;
        size++;
    }

    strNumber = to_string(n);

    while (size > 0) {

        conversionNumber = std::stoll(strNumber.substr((size - 1), 1));
        //cout << strNumber.substr( (size - 1), 1) << endl;
        answer.push_back(conversionNumber);
        size--;
    }

    return answer;
}

int main() {

    solution(12345);

}


반응형

+ Recent posts