#include <stdio.h>
void main() {
/*
정렬 : 선택정렬, 버블정렬, 퀵정렬
선택정렬 : 최고 낮은 값을 찾아서 앞에서부터 차례대로 쌓아주는 기법
*/
int arr[] = { 5,72,3,1,92,7,25,19,13 };
int min; //낮은 값을 갖는 변수
int min_index; //낮은 값의 위치(인덱스)
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
printf("%d ", arr[i]);
}printf("\n");
for (int i = 0; i < sizeof(arr) / sizeof(int) - 1; i++) {
//기준값을 잡을 반복문
min = arr[i];
min_index = i;
for (int k = i + 1; k < sizeof(arr) / sizeof(int); k++) {
//가장 낮은 값을 찾기 위한 반복문
if (arr[k] < min) {
min = arr[k];
min_index = k;
}
}//end for(k)
arr[min_index] = arr[i];
arr[i] = min;
}
for (int i = 0; i < sizeof(arr) / sizeof(int); i++) {
printf("%d ", arr[i]);
}printf("\n");
}
8.26.2017
8.19.2017
LinkedList 자료구조
#include <stdio.h>
#include <stdlib.h>
typedef struct Test {
int data;
struct Test* p; //주소를 담을 변수 생성
}test;
void main(){
test* head = (test*)malloc(sizeof(test)); //시작위치
test t1;
test t2;
test t3;
printf("%d\n", &t1);
printf("%d\n", &t2);
printf("%d\n\n", &t3);
t1.data = 10;
t2.data = 20; //(*t1.p).data = 20;
t3.data = 30;
t1.p = &t2;
t2.p = &t3;
t3.p = NULL;
printf("%d\n", t1.p);
printf("%d\n", t2.p);
printf("%d\n", t3.p);
}
============================================================================
#include <stdio.h>
#include <stdlib.h>
typedef struct TEST {
int data;
struct TEST* next;
}test;
void main() {
//연결리스트 : 주소를 연결해 놓은 집합체
// 마지막 노드의 next는 NULL
// head는 시작위치를 알려줄 수 있게끔
// 데이터 X
test* head = (test*)malloc(sizeof(test));
head->next = NULL;
test* new1 = (test*)malloc(sizeof(test));
(*head).next = new1;
new1->next = NULL;
test* new2 = (test*)malloc(sizeof(test));
new1->next = new2;
new2->next = NULL;
test* new3 = (test*)malloc(sizeof(test));
new2->next = new3;
new3->next = NULL;
new1->data = 10;
new2->data = 20;
new3->data = 30;
test* move = head->next;
while (move != NULL) {
printf("%d", move->data);
move = move->next;
}printf("\n"); // 출력
=============================================================================
#include <stdio.h>
#include <stdlib.h>
typedef struct Test {
int data;
struct Test* next;
}test;
test* head;
void input(int data) {
test* newnode = (test*)malloc(sizeof(test));
newnode->data = data;
newnode->next = head->next;
head->next = newnode;
}
void del_node1() {
test* move = head->next;
printf("삭제할 데이터 입력 : "); int data;
scanf("%d", &data);
while (move != NULL) {
if (move->next->data == data) {
test* temp = move->next;
move->next = move->next->next;
free(temp);
break;
}
move = move->next;
}
}
void del_node2() {
test* move = head->next; //삭제할 노드를 찾을 변수
test* move2 = head; //삭제할 노드의 이전 노드
printf("삭제할 데이터 입력 : "); int data;
scanf("%d", &data);
while (move != NULL) {
if (move->data == data) {
move2->next = move->next;
free(move);
break;
}
move = move->next;
move2 = move2->next;
}
}
void main() {
head = (test*)malloc(sizeof(test));
head->next = NULL;
input(10);
input(20);
input(30);
input(40);
input(50);
del_node2();
test* move = head->next;
while (move != NULL) {
printf("%d", move->data);
move = move->next;
}printf("\n"); // 출력
}
#include <stdlib.h>
typedef struct Test {
int data;
struct Test* p; //주소를 담을 변수 생성
}test;
void main(){
test* head = (test*)malloc(sizeof(test)); //시작위치
test t1;
test t2;
test t3;
printf("%d\n", &t1);
printf("%d\n", &t2);
printf("%d\n\n", &t3);
t1.data = 10;
t2.data = 20; //(*t1.p).data = 20;
t3.data = 30;
t1.p = &t2;
t2.p = &t3;
t3.p = NULL;
printf("%d\n", t1.p);
printf("%d\n", t2.p);
printf("%d\n", t3.p);
}
============================================================================
#include <stdio.h>
#include <stdlib.h>
typedef struct TEST {
int data;
struct TEST* next;
}test;
void main() {
//연결리스트 : 주소를 연결해 놓은 집합체
// 마지막 노드의 next는 NULL
// head는 시작위치를 알려줄 수 있게끔
// 데이터 X
test* head = (test*)malloc(sizeof(test));
head->next = NULL;
test* new1 = (test*)malloc(sizeof(test));
(*head).next = new1;
new1->next = NULL;
test* new2 = (test*)malloc(sizeof(test));
new1->next = new2;
new2->next = NULL;
test* new3 = (test*)malloc(sizeof(test));
new2->next = new3;
new3->next = NULL;
new1->data = 10;
new2->data = 20;
new3->data = 30;
test* move = head->next;
while (move != NULL) {
printf("%d", move->data);
move = move->next;
}printf("\n"); // 출력
}
=============================================================================
#include <stdio.h>
#include <stdlib.h>
typedef struct Test {
int data;
struct Test* next;
}test;
test* head;
void input(int data) {
test* newnode = (test*)malloc(sizeof(test));
newnode->data = data;
newnode->next = head->next;
head->next = newnode;
}
void del_node1() {
test* move = head->next;
printf("삭제할 데이터 입력 : "); int data;
scanf("%d", &data);
while (move != NULL) {
if (move->next->data == data) {
test* temp = move->next;
move->next = move->next->next;
free(temp);
break;
}
move = move->next;
}
}
void del_node2() {
test* move = head->next; //삭제할 노드를 찾을 변수
test* move2 = head; //삭제할 노드의 이전 노드
printf("삭제할 데이터 입력 : "); int data;
scanf("%d", &data);
while (move != NULL) {
if (move->data == data) {
move2->next = move->next;
free(move);
break;
}
move = move->next;
move2 = move2->next;
}
}
void main() {
head = (test*)malloc(sizeof(test));
head->next = NULL;
input(10);
input(20);
input(30);
input(40);
input(50);
del_node2();
test* move = head->next;
while (move != NULL) {
printf("%d", move->data);
move = move->next;
}printf("\n"); // 출력
}
8.05.2017
08.05 creating character
#include <stdio.h>
#include <stdlib.h>
typedef struct Character {
char nickname[12];
int strength, level, exp; //체력, 레벨, 경험치
}character;
character* c;
void create(int index) {
c = (character*)realloc(c, sizeof(character) * (index+ 1));
printf("Enter character's nickname.\n");
scanf("%s", c[index].nickname);
printf("Enter character's strength.\n");
scanf("%d", &c[index].strength);
printf("Enter character's level.\n");
scanf("%d", &c[index].level);
printf("Enter character's experience point.\n");
scanf("%d", &c[index].exp);
}
void show(int index) {
for (int i = 0; i < index; i++) {
printf("About your character...\n");
printf("nickname : %s \n", c[i].nickname);
printf("strength : %d \n", c[i].strength);
printf("level : %d \n", c[i].level);
printf("experience point : %d \n", c[i].exp);
}
}
void deleteCharacter(int index) {
char name[20];
printf("Which nickname do you want to erase?");
scanf("%s", name);
for (int i = 0; i < index; i++) {
if (!strcmp(c[i].nickname, name));
//strcmp (compare values) : if same -> 0
// if different -> 1, -1
for (int k = i; k < index- 1; k++) {
//앞으로 당겨주는 작업을 할 반복문
c[i] = c[i + 1];
c = (character*)realloc(c, sizeof(character)*(index- 1));
}//end for(k)
break; //더 이상 입력할 닉네임을 찾을 필요가 없기 때문
}//end if
}
void main() {
int input = 0, index = 0;
c = (character*)malloc(sizeof(character));
while (input != 4) {
printf("[menu]\n1.Create your own Character 2.Show your character 3.Delete your character 4.End this program\n");
scanf("%d", &input);
if (input == 1) {
create(index);
index++;
}
else if (input == 2) show(index);
else if (input == 3) {
deleteCharacter(index);
index--;
}
}
}
#include <stdlib.h>
typedef struct Character {
char nickname[12];
int strength, level, exp; //체력, 레벨, 경험치
}character;
character* c;
void create(int index) {
c = (character*)realloc(c, sizeof(character) * (index+ 1));
printf("Enter character's nickname.\n");
scanf("%s", c[index].nickname);
printf("Enter character's strength.\n");
scanf("%d", &c[index].strength);
printf("Enter character's level.\n");
scanf("%d", &c[index].level);
printf("Enter character's experience point.\n");
scanf("%d", &c[index].exp);
}
void show(int index) {
for (int i = 0; i < index; i++) {
printf("About your character...\n");
printf("nickname : %s \n", c[i].nickname);
printf("strength : %d \n", c[i].strength);
printf("level : %d \n", c[i].level);
printf("experience point : %d \n", c[i].exp);
}
}
void deleteCharacter(int index) {
char name[20];
printf("Which nickname do you want to erase?");
scanf("%s", name);
for (int i = 0; i < index; i++) {
if (!strcmp(c[i].nickname, name));
//strcmp (compare values) : if same -> 0
// if different -> 1, -1
for (int k = i; k < index- 1; k++) {
//앞으로 당겨주는 작업을 할 반복문
c[i] = c[i + 1];
c = (character*)realloc(c, sizeof(character)*(index- 1));
}//end for(k)
break; //더 이상 입력할 닉네임을 찾을 필요가 없기 때문
}//end if
}
void main() {
int input = 0, index = 0;
c = (character*)malloc(sizeof(character));
while (input != 4) {
printf("[menu]\n1.Create your own Character 2.Show your character 3.Delete your character 4.End this program\n");
scanf("%d", &input);
if (input == 1) {
create(index);
index++;
}
else if (input == 2) show(index);
else if (input == 3) {
deleteCharacter(index);
index--;
}
}
}
0805 자료구조 복습
#include <stdio.h>
#include <stdlib.h>
void main() {
/*
정적할당 : 고정되어있는 값만 사용
ex) int num, int arr[5];
동적할당 : 계속해서 변하는 값
컴파일을 실행할 때 저장할 변수를 늘리거나 줄일 때 사용
#include <stdlib.h>
할당 : malloc(), calloc() //재할당 : realloc()
*/
//(void*)malloc(바이트크기)
int* p = (int*)malloc(sizeof(int) * 4);
// malloc를 이용하여 4개짜리 배열
// 생성 후, 포인터 변수 p 에 배열의 첫번쨰 인덱스의 주소(배열의 시작주소)를 대입해줬다.
/*
0번째 인덱스 = 10, 1 = 20, 2 = 30
*/
/*int arr[5];
int* p;
*/
*p = 10;
p[1] = 20;
p[2] = 30;
printf("%d %d %d \n", *(p + 0), p[1], p[2]);
//calloc(개수, 사이즈);
// c : clean
// 값이 없을 경우엔 0으로 초기화
int* c = (int*)calloc(4, sizeof(int));
*&c[0] = 50;
c[1] = 60;
c[2] = 70;
printf("%d %d %d \n", c[0], c[1], c[2]);
printf("c : %d, m : %d \n", c[3], p[3]);
//_msize() : 동적할당된 배열의 사이즈
// sizeof : 정적할당된 변수
printf("&dbyte \n", _msize(p)); //16byte
printf("&dbyte \n", _msize(c)); //16byte
//realloc(누구, 사이즈?);
p = (int*)realloc(p, sizeof(int)* 5);
//4개짜리 공간 -> 5개짜리 공간
printf("%dbyte \n", _msize(p)); //20byte
printf("%d %d %d \n", p[0], p[1], p[2]); //20byte
/*
*& : 서로 상쇄
&* 상쇄(X)
arr[0] == *p == *(p+0) == *&p[0] == p[0]
arr[1] == *(p+1) == *&p[1] == p[1]
*/
/*int num;
int *p2 = #
*p2 = 20;
*/
// *p2 == num
// p2가 가지고 있는 주소로 접근(num으로 이동)하여 20을 대입하겠다.
}
#include <stdlib.h>
void main() {
/*
정적할당 : 고정되어있는 값만 사용
ex) int num, int arr[5];
동적할당 : 계속해서 변하는 값
컴파일을 실행할 때 저장할 변수를 늘리거나 줄일 때 사용
#include <stdlib.h>
할당 : malloc(), calloc() //재할당 : realloc()
*/
//(void*)malloc(바이트크기)
int* p = (int*)malloc(sizeof(int) * 4);
// malloc를 이용하여 4개짜리 배열
// 생성 후, 포인터 변수 p 에 배열의 첫번쨰 인덱스의 주소(배열의 시작주소)를 대입해줬다.
/*
0번째 인덱스 = 10, 1 = 20, 2 = 30
*/
/*int arr[5];
int* p;
*/
*p = 10;
p[1] = 20;
p[2] = 30;
printf("%d %d %d \n", *(p + 0), p[1], p[2]);
//calloc(개수, 사이즈);
// c : clean
// 값이 없을 경우엔 0으로 초기화
int* c = (int*)calloc(4, sizeof(int));
*&c[0] = 50;
c[1] = 60;
c[2] = 70;
printf("%d %d %d \n", c[0], c[1], c[2]);
printf("c : %d, m : %d \n", c[3], p[3]);
//_msize() : 동적할당된 배열의 사이즈
// sizeof : 정적할당된 변수
printf("&dbyte \n", _msize(p)); //16byte
printf("&dbyte \n", _msize(c)); //16byte
//realloc(누구, 사이즈?);
p = (int*)realloc(p, sizeof(int)* 5);
//4개짜리 공간 -> 5개짜리 공간
printf("%dbyte \n", _msize(p)); //20byte
printf("%d %d %d \n", p[0], p[1], p[2]); //20byte
/*
*& : 서로 상쇄
&* 상쇄(X)
arr[0] == *p == *(p+0) == *&p[0] == p[0]
arr[1] == *(p+1) == *&p[1] == p[1]
*/
/*int num;
int *p2 = #
*p2 = 20;
*/
// *p2 == num
// p2가 가지고 있는 주소로 접근(num으로 이동)하여 20을 대입하겠다.
}
8.03.2017
0803 공부
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
typedef struct Account {
char name[12], id[20], password[20];
int accountNumber, surplus;
}account;
void Create(account* a) {
printf("이름을 입력하시오.\n");
scanf("%s", (*a).name);
printf("아이디를 입력하시오.\n");
scanf("%s", a->id);
printf("비밀번호를 입력하시오.\n");
scanf("%s", a->password);
printf("계좌번호를 입력하시오.\n");
scanf("%d", &a->accountNumber);
printf("잔금을 입력하시오.\n");
scanf("%d", &a->surplus);
}
void main() {
int b = 0;
//배열 아니면 scanf에서 주소연산자가 필요
// ' ' : 문자
account a;
while(b!=3){
printf("메뉴 // 1. 회원가입 2.로그인 3.종료");
scanf("%d", &b);
if (b == 1)
Create(&a);
else if (b == 2) {
char id2[20], password2[20];
printf("아이디 입력");
scanf("%s", id2);
printf("비밀번호 입력");
scanf("%s", password2);
// -> : * .
if (!strcmp(id2, a.id) && !strcmp(password2, a.password)) { //(id2 == a->id) && (password2 == a->password)
printf("이름은 %s", a.name);
printf("계좌번호는 %d", a.accountNumber);
printf("잔금은 %d", a.surplus);
}
}
else if (b == 3) {
printf("프로그램 종료");
}
}
/*
함수의 차이점
C언어 : 인자값이 2개면, 매개변수는 2개여야만 한다.
함수의 매개변수에 초기값 대입은 불가능하다.
C++ : 인자값이 2개일 때, 매개변수가 여러 개 여도 상관없다.
단, 매개 변수에 초기값이 되어야만 한다.
인자값 보다는 매개변수가 많아야 한다.
만약, 보내주는 값이 있을 경우 그 값으로 대체한다.
*/
}
#include <stdio.h>
#include <string.h>
typedef struct Account {
char name[12], id[20], password[20];
int accountNumber, surplus;
}account;
void Create(account* a) {
printf("이름을 입력하시오.\n");
scanf("%s", (*a).name);
printf("아이디를 입력하시오.\n");
scanf("%s", a->id);
printf("비밀번호를 입력하시오.\n");
scanf("%s", a->password);
printf("계좌번호를 입력하시오.\n");
scanf("%d", &a->accountNumber);
printf("잔금을 입력하시오.\n");
scanf("%d", &a->surplus);
}
void main() {
int b = 0;
//배열 아니면 scanf에서 주소연산자가 필요
// ' ' : 문자
account a;
while(b!=3){
printf("메뉴 // 1. 회원가입 2.로그인 3.종료");
scanf("%d", &b);
if (b == 1)
Create(&a);
else if (b == 2) {
char id2[20], password2[20];
printf("아이디 입력");
scanf("%s", id2);
printf("비밀번호 입력");
scanf("%s", password2);
// -> : * .
if (!strcmp(id2, a.id) && !strcmp(password2, a.password)) { //(id2 == a->id) && (password2 == a->password)
printf("이름은 %s", a.name);
printf("계좌번호는 %d", a.accountNumber);
printf("잔금은 %d", a.surplus);
}
}
else if (b == 3) {
printf("프로그램 종료");
}
}
/*
함수의 차이점
C언어 : 인자값이 2개면, 매개변수는 2개여야만 한다.
함수의 매개변수에 초기값 대입은 불가능하다.
C++ : 인자값이 2개일 때, 매개변수가 여러 개 여도 상관없다.
단, 매개 변수에 초기값이 되어야만 한다.
인자값 보다는 매개변수가 많아야 한다.
만약, 보내주는 값이 있을 경우 그 값으로 대체한다.
*/
}
7.18.2017
Bubble Sort
#include <stdio.h>
void BubbleSort(int DataSet[], int Length){
for (int i = 0; i < Length - 1; i++) {
for (int j = 0; j < Length - (i+1); j++) {
if (DataSet[j] > DataSet[j + 1]) {
int temp = DataSet[j];
DataSet[j] = DataSet[j + 1];
DataSet[j + 1] = temp;
}
}
}
}
void main() {
int DataSet[] = { 6,4,2,3,1,5 };
int Length = sizeof(DataSet) / sizeof(int);
BubbleSort(DataSet, Length);
for (int i = 0; i < Length; i++)
printf("%d", DataSet[i]);
printf("\n");
}
void BubbleSort(int DataSet[], int Length){
for (int i = 0; i < Length - 1; i++) {
for (int j = 0; j < Length - (i+1); j++) {
if (DataSet[j] > DataSet[j + 1]) {
int temp = DataSet[j];
DataSet[j] = DataSet[j + 1];
DataSet[j + 1] = temp;
}
}
}
}
void main() {
int DataSet[] = { 6,4,2,3,1,5 };
int Length = sizeof(DataSet) / sizeof(int);
BubbleSort(DataSet, Length);
for (int i = 0; i < Length; i++)
printf("%d", DataSet[i]);
printf("\n");
}
피보나치 수열 ( Fibonacci )
#include <stdio.h>
int fibonacci(int n) {
if (n < 1) return 0;
if (n < 3) return 1;
return fibonacci(n-2) + fibonacci(n - 1);
}
void main() {
for (int i = 0; i < 50; i++)
printf("%2d : %3d\n", i, fibonacci(i));
}
int fibonacci(int n) {
if (n < 1) return 0;
if (n < 3) return 1;
return fibonacci(n-2) + fibonacci(n - 1);
}
void main() {
for (int i = 0; i < 50; i++)
printf("%2d : %3d\n", i, fibonacci(i));
}
하노이 타워 ( Hanoi Tower )
#include <stdio.h>
#include <stdlib.h>
void HanoiTower(int n, char x, char y, char z) {
if(n==1)
printf("%c -> %c\n", x, y);
else {
HanoiTower(n - 1, x, z, y);
printf("%c -> %c\n", x, y);
HanoiTower(n - 1, z, y, x);
}
}
void main() {
HanoiTower(3, 'A', 'B', 'C');
}
==============================================================================
//출력
#include <stdlib.h>
void HanoiTower(int n, char x, char y, char z) {
if(n==1)
printf("%c -> %c\n", x, y);
else {
HanoiTower(n - 1, x, z, y);
printf("%c -> %c\n", x, y);
HanoiTower(n - 1, z, y, x);
}
}
void main() {
HanoiTower(3, 'A', 'B', 'C');
}
==============================================================================
//출력
4.14.2017
피드 구독하기:
글 (Atom)
