안녕하세요. 2학년 1학기(2018학년도)동안 정보과학 교과 수업을 수강하며 실습했던 프로그램들 목록을 정리해서 올려봅니다.
목록
1. 1부터 n까지 홀수의 평균, 짝수의 합 구하기(04/04)
2. 자연수를 입력받다가 100 이상의 값이 입력되면 평균과 합 출력하기(04/05)
3. 음수 짝수 판별하기(04/05)
4. 구구단 a단부터 b단까지 출력하기(04/06)
5. 크기가 100인 배열을 차례로 입력받다가 0이 입력되면 반대 순서로 배열 출력하기(04/06)
6. 반지름 길이가 r인 원넓이 출력하기(04/10)
7. 50 이하의 자연수를 입력받고, 내림차순으로 출력하기(04/13)
8. 100 이하의 자연수를 입력받고, 1부터 그 자연수까지의 합을 출력하기(04/16)
9. 자연수 N을 입력받고 짝수이면 2, 홀수이면 3으로 나누는 작업을 반복, 1이 되면 나눈 횟수 출력(04/16)
10. 배열을 입력받고 배열 원소 중 짝수, 홀수의 개수 출력하기(04/23)
11. 배열을 입력받고 배열 원소 중 최댓값, 최솟값 출력하기(04/24)
12. 이름, 전화번호, 주소를 문자열로 입력받아 각각의 배열에 저장하고 출력하기(05/15)
13. 두 사각형의 꼭짓점 좌푯값을 각각 입력받고, 두 사각형이 겹치는 범위와 두 사각형을 포함하는 최소의 사각형의 좌푯값을 출력(05/17)
14. 노드 데이터베이스(첫 번째로 노드 삽입, 가운데 노드 삽입, 마지막으로 노드 삽입, 출력, 삭제)(06/12)
15. 원형 노드리스트(07/03)
16. 다항식 계산기(단항 구조체)
17. 이중연결리스트
다음은 예제와 수업시간 제가 작성한 코드입니다.
1. 1부터 n까지 홀수의 평균, 짝수의 합 구하기
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n; int i = 0; int sum = 0;
printf("100 이하의 정수를 입력하세요 : ");
scanf("%d", &n);
for(i=0;i<=n;i++){
if((i%2)==0){
sum = sum + i;
}
}
printf("\n\n짝수의 합은 %d", sum);
sum = 0;
for(i=1;i<=n;i++){
if((i%2)!=0){
sum+=i;
sum/=(n/2);
}
}
printf("\n\n홀수의 평균은 %d\n\n", sum);
return 0;
}
2. 자연수를 입력받다가 100 이상의 값이 입력되면 평균과 합 출력하기
#include <stdio.h>
#include <stdlib.h>
//계속 n값을 입력받다가 100이상의 수를 입력받으면 합과 평균을 출력한다.
int main()
{
int n; int sum = 0; int time = 1;
do{
printf("\n%d : ", time);
scanf("%d", &n);
sum += n;
time++;
printf("\n%d times you entered, now %d, sum is %d\n", time-1, n, sum);
}while(n<100);
printf("\nsum is %d\n", sum);
printf("everage is %.1f\n", (float)sum/(time-1));
return 0;
}
3. 입력받은 정수가 음수인지 양수인지 판별하기
#include <stdio.h>
#include <stdlib.h>
int main()
{
int n = 0;
printf("정수 n을 입력하세요. 종료하려면 0을 입력하세요.");
do{
printf("\n\nn : ");
scanf("%d", &n);
if(n>0)
printf("%d은(는) 양수입니다.\n", (int)n);
else if(n<0)
printf("%d은(는) 음수입니다.\n", (int)n);
}while(n!=0);
printf("프로그램을 종료합니다.");
return 0;
}
4. 구구단 a단부터 b단까지 출력하기
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i; int j; int a; int b;
printf("구구단! a단부터 b단까지!\n\n");
printf(" a단 : ");
scanf("%d", &a);
printf(" b단 : ");
scanf("%d", &b);
if(a>=b){
printf("b는 a보다는 커야 합니다!");
}
else{
for(i=a;i<=b;i++){
printf("\n%d단 : \n", i);
for(j=1;j<=9;j++){
printf("%d*%d=%d\n", i, j, i*j);
}
printf("\n\n");
}
}
return 0;
}
5. 크기가 100인 배열을 차례로 입력받다가 0이 입력되면 반대 순서로 배열 출력하기
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 0;
int arr[100] = {};
for(i=0;i<100;i++){
printf("int arr[%d] : ", i);
scanf("%d", &arr[i]);
if(arr[i]==0){
break;
}
}
for(i+=1;i>=0;i--){
printf("arr[%d] = %d", i, arr[i]);
}
return 0;
}
6. 반지름 길이가 r인 원넓이 출력하기
#include <stdio.h>
#include <stdlib.h>
int r = 0; double s = 0;
int Cir(int a)
{
s = a*a*3.1415926535897932;//384626433832795;
return s;
}
int main(void)
{
printf("r을 입력하세요 : ");
scanf("%d", &r);
Cir(r);
printf("%lf", s);
return 0;
}
7. 50 이하의 자연수를 입력받고, 내림차순으로 출력하기
#include <stdio.h>
#include <stdlib.h>
void f(int b)
{
if(b>=1){
printf("%d\n", b);
f(b-1);
}
}
int scanprint(void)
{
int a;
printf("number : ");
scanf("%d", &a);
return a;
}
int main(void)
{
int n;
n = scanprint();
if(n<=50&&n>=1){
f(n);
}
else{
printf("\nenter the natural number smaller than 50\n");
}
return 0;
}
8. 100 이하의 자연수를 입력받고, 1부터 그 자연수까지의 합을 출력하기
#include <stdio.h>
#include <stdlib.h>
//# 4월 16일 100이하의 자연수를 입력받아 1부터의 합을 출력하는 함수
int scan(void) //정수를 입력받는 함수
{
int enter;
printf("number : ");
scanf("%d", &enter);
return enter;
}
void intprint(int print) //정수를 간단히 출력하는 함수
{
printf("\n%d\n", print);
}
int numsize(int min, int max, int num) //수의 범위를 측정하는 함수
{
if(min<=num&&num<=max){
return 1;
}
else{
return 2;
}
}
int f(int n)
{
int slc;
slc = numsize(1, 100, n);
if(slc == 1){
n+=f(n-1);
//printf("\n%d\n", n);
return n;
}
else return 0;
}
int main()
{
int n;
n = scan();
intprint(f(n));
return 0;
}
9. 자연수 N을 입력받고 짝수이면 2, 홀수이면 3으로 나누는 작업을 반복, 1이 되면 나눈 횟수 출력
#include <stdio.h>
#include <stdlib.h>
//# 4월 16일 100만 이하의 자연수 N을 입력받아 짝수이면 2로 홀수이면 3으로 나누는 작업을 반복하다가 그 값이 1이 되면 그때까지 나누었던 작업의 횟수를 출력하는 프로그램을 구성하시오.
int scan(void) //정수를 입력받는 함수
{
int enter;
printf("number : ");
scanf("%d", &enter);
return enter;
}
void intprint(int print) //정수를 간단히 출력하는 함수
{
printf("\nresult : %d\n", print);
return 0;
}
int numsize(int min, int max, int num) //수의 범위를 측정하는 함수
{
if(min<=num&&num<=max){
return 1;
}
else{
return 2;
}
}
int f(int n)
{
int slc = numsize(2,1000000,n); //수의 범위를 지정
if(slc==1){
/*if(n%2==0){ //짝수이면
return 1 + f(n/2);
}
else{ //홀수이면
return 1 + f(n/3);
}*/
return 1 + f(n / ((n%2==0)?2:3)); //삼항연산자 사용 -> (조건)?(참일때):(거짓일때)
}
else return 0;
}
int main()
{
int n;
n = scan();
intprint(f(n));
return 0;
}
10. 배열을 입력받고 배열 원소 중 짝수, 홀수의 개수 출력하기
#include <stdio.h>
#include <stdlib.h>
int scanint(void)
{
int size;
printf("size of arr : ");
scanf("%d", &size);
return size;
}
void scanarr(int* arr, int size)
{
int i;
for(i=0;i<size;i++){
printf("arr[%d] : ", i);
scanf("%d", arr+i);
}
}
void f(int* arr, int* even, int size)
{
int i;
for(i=0;i<size;i++){
if(!(*(arr+i)%2))
(*even)++;
}
}
int main(void)
{
int even = 0; int size = scanint();
int arr[size];
scanarr(arr, size);
f(arr, &even, size);
printf("\neven : %d\nodd : %d", even, size-even);
return 0;
}
11. 배열을 입력받고 배열 원소 중 최댓값, 최솟값 출력하기
#include <stdio.h>
#include <stdlib.h>
int intscan(void)
{
int n;
printf("size of arr : ");
scanf("%d", &n);
return n;
}
int maxf(int* temp, int size)
{
int max = *temp; int i;
for(i=0;i<size;i++){
if( *(temp+i) > max )
max = *(temp+i);
}
return max;
}
int minf(int* temp, int size)
{
int min = *temp; int i;
for(i=0;i<size;i++){
if( *(temp+i) < min )
min = *(temp+i);
}
return min;
}
int main(void)
{
int n = intscan(); int i; int* p = (int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++){
printf("arr[%d] : ", i);
scanf("%d", p+i);
}
printf("max(arr) : %d, min(arr) : %d", maxf(p, n), minf(p, n));
return 0;
}
12. 이름, 전화번호, 주소를 문자열로 입력받아 각각의 배열에 저장하고 출력하기
#include <stdio.h>
#include <stdlib.h>
struct info
{
char name[100];
char number[100];
char city[100];
};
int main(void)
{
struct info a;
printf("이름, 전화번호, 주소를 입력하세요 : ");
scanf("%s %s %s", a.name, a.number, a.city);
printf("이름 : %s\n", a.name);
printf("전화번호 : %s\n", a.number);
printf("주소 : %s\n", a.city);
return 0;
}
13. 두 사각형의 꼭짓점 좌푯값을 각각 입력받고, 두 사각형이 겹치는 범위와 두 사각형을 포함하는 최소의 사각형의 좌푯값을 출력
/** 정보과학 문제 풀이 답안 설명용 / 171037 2PAC 이호은 **/
#include <stdio.h>
#include <stdlib.h>
#include <windows.h> //색깔 출력하는 함수
#include <conio.h> //색깔 출력하는 함수
//변수명을 sqi로 하고 x, y를 정수값으로 갖는 구조체 선언
typedef struct{
int x;
int y;
}sqi;
/** 메인 함수 **/
int main(int argc, char *argv[]){
//구조체 변수 선언(사각형 A, B 각각 두 개씩(직사각형을 구성하기 위한 최소 좌표))
sqi a_one, a_two, b_one, b_two;
//x, y 좌표 정렬을 위한 x, y 4칸짜리 배열 선언
int arr_x[4]; int arr_y[4];
//x, y 입력받음
printf("enter x, y\nenter x (space) y ex) 3 4\n");
printf("first point of squar one : ");
scanf("%d %d", &a_one.x, &a_one.y);
printf("second point of squar one : ");
scanf("%d %d", &a_two.x, &a_two.y);
printf("first point of squar two : ");
scanf("%d %d", &b_one.x, &b_one.y);
printf("second point of squar two : ");
scanf("%d %d", &b_two.x, &b_two.y);
//입력 받은 x, y의 좌표들을 차례대로 정렬 없이 배열에 대입
arr_x[0] = a_one.x; arr_x[1] = a_two.x; arr_x[2] = b_one.x; arr_x[3] = b_two.x;
arr_y[0] = a_one.y; arr_y[1] = a_two.y; arr_y[2] = b_one.y; arr_y[3] = b_two.y;
//정렬을 위해 temp 변수(임시 변수)에 크기, 순서 상관 없이 x, y의 첫 번째 좌표 값을 넣어줌
int temp_x = arr_x[0]; int temp_y = arr_y[0];
//반복문 변수 i, j 선언 그리고 x, y에 최소, 최대를 저장할 변수 선언
int i, j; int minx, miny, maxx, maxy, temp;
//x정렬(temp로 하나씩 비교해가며 비교한 수 작고 큰 수를 대입)
for(i=0;i<4;i++)
{
for(j=i;j<4;j++)
{
if(arr_x[i]>arr_x[j])
{
temp=arr_x[i];
arr_x[i]=arr_x[j];
arr_x[j]=temp;
}
}
}
//y정렬(temp로 하나씩 비교해가며 비교한 수 작고 큰 수를 대입)
for(i=0;i<4;i++)
{
for(j=i;j<4;j++)
{
if(arr_y[i]>arr_y[j])
{
temp=arr_y[i];
arr_y[i]=arr_y[j];
arr_y[j]=temp;
}
}
}
//크기순으로 정렬된 배열에서 최대, 최소값을 변수에 저장
minx = arr_x[0];
maxx = arr_x[3];
miny = arr_y[0];
maxy = arr_y[3];
/** 겹치는 부분 출력 **/
//j는 y좌표 반복하여 설정(y의 최댓값+2부터 0까지)
for(j=maxy+2;j>=0;j--){
//i는 x좌표 반복하여 설정(0부터 x의 최댓값+3까지)
for(i=0;i<maxx+3;i++){
//만약 현재 가르키고 있는 좌표(j, i)가 두 사각형 범위 안에 있다면 겹치는 것으로 처리
if((i>=a_one.x&&i<=a_two.x&&j>=a_one.y&&j<=a_two.y)&&(i>=b_one.x&&i<=b_two.x&&j>=b_one.y&&j<=b_two.y)){
//빨간색으로 글자 출력
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_INTENSITY);
printf("■");
}
//만약 현재 가르키고 있는 좌표(j, i)가 한 사각형 범위 안에만 있다면 한 사각형 범위로 처리
else if(i>=a_one.x&&i<=a_two.x&&j>=a_one.y&&j<=a_two.y){
//노란색으로 글자 출력
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("■");
}
else if(i>=b_one.x&&i<=b_two.x&&j>=b_one.y&&j<=b_two.y){
//초록색으로 글자 출력
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN | FOREGROUND_INTENSITY);
printf("■");
}
//만약 현재 가르키고 있는 좌표(j, i)가 어느 사각형 범위에도 포함되어 있지 않다면
else{
//흰색으로 글자 출력
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("■");
}
}
//해당 y좌표 라인에서 x좌표의 반복이 끝나면 엔터를 쳐서 다음 y좌표(y-1)로 넘어감
printf("\n");
}
/** 포함하는 최소 크기의 사각형 출력 **/
//x, y 각각의 최소값, 최대값 좌표를 출력
printf("points of the squar is (%d, %d), (%d, %d)", minx, miny, maxx, maxy); printf("\n");
//j는 y좌표 반복하여 설정(y의 최댓값+2부터 0까지)
for(j=maxy+2;j>=0;j--){
//i는 x좌표 반복하여 설정(0부터 x의 최댓값+3까지)
for(i=0;i<maxx+3;i++){
//만약 현재 가르키고 있는 좌표(j, i)가 최소, 최대 범위에 들어있다면
if(i>=minx&&i<=maxx&&j>=miny&&j<=maxy){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("■");
}else{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY);
printf("■");
}
}
printf("\n");
}
//메인 함수 종료
return 0;
}
14. 노드 데이터베이스(첫 번째로 노드 삽입, 가운데 노드 삽입, 마지막으로 노드 삽입, 출력, 삭제)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node* link;
}node;
void makeFirstNode(node_h* P)
{
node* newNode;
int x;
newNode = (node*)malloc(sizeof(node));
printf("입력할 데이터 : ");
scanf("%d", &x);
printf("a");
newNode->data = x;
printf("a");
newNode->link = P->head;
printf("a");
P->head = newNode;
printf("a");
printf("%p", P->head);
printf("a");
}
void makeMiddleNode(node_h* P)
{
node* newNode; int x;
newNode = (node*)malloc(sizeof(node));
printf("입력할 데이터 : ");
scanf("%d", &x);
if(P->head == NULL){
newNode->data = x;
P->head = newNode;
newNode->link = NULL;
}
else{
int preD; //선행 노드 데이터
node* temp;
node* pre;
printf("선행 노드의 데이터를 입력하세요 : ");
scanf("%d", &preD);
temp = P->head;
while(temp!=NULL){
if(temp->data==preD){
pre=temp;
break;
}
temp=temp->link;
}
newNode->link = pre->link;
pre->link = newNode;
}
}
void makeLastNode(node_h* P)
{
node* newNode; int x;
newNode = (node*)malloc(sizeof(node*));
printf("입력할 데이터 : ");
scanf("%d", &x);
newNode->data = x;
newNode->link = NULL;
if(P == NULL){
P = newNode;
return 0;
}
node* temp;
temp = P;
while(temp!=NULL){
temp = temp->link;
}
temp->link = newNode;
}
void printNode(node_h* P)
{
node* temp;
temp = P;
while(temp!=NULL){
printf("%d", temp->data);
temp=temp->link;
if(temp!=NULL)
printf(", ");
}
}
void deleteNode(node_h* P)
{
//if()
return 0;
}
int main(void)
{
node_h* P;
int slc; int x;
while(1){
printf("1. 첫 번째로 노드 삽입 \n2. 가운데 노드 삽입 \n3. 마지막으로 노드 삽입 \n4. 노드 출력 \n5. 노드 삭제 \n6. 종료 \n\n입력 : ");
scanf("%d", &slc);
printf("P 주소값 : %p", P);
if(slc==1){
makeFirstNode(P);
printf("complete!\n");
}
else if(slc==2){
makeMiddleNode(P);
printf("complete!\n");
}
else if(slc==3){
makeLastNode(P);
printf("complete!\n");
}
else if(slc==4){
printNode(P);
}
else if(slc==5){
deleteNode(P);
}
else if(slc==6){
break;
}
}
return 0;
}
15. 원형 노드리스트
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node* link;
}node;
void insertFirstNode(node** CL, int x)
{
node* newNode;
newNode = (node*)malloc(sizeof(node));
newNode->data = x;
if(*CL==NULL){
*CL = newNode;
newNode->link=newNode;
} else {
node* temp;
temp = *CL;
while((temp->link)!=(*CL)){
temp = temp->link;
}
newNode->link = temp->link;
temp->link = newNode;
*CL = newNode;
}
}
void insertMiddleNode(node** CL, node* pre, int x)
{
node* newNode;
newNode = (node*)malloc(sizeof(node));
newNode->data = x;
if(*CL==NULL){
*CL = newNode;
newNode->link = newNode;
} else {
newNode->link = pre->link;
pre->link = newNode;
}
}
node* searchNode(node** CL, int preD)
{
node* temp;
temp = *CL;
while((temp->link)!=(*CL)){
if(temp->data==preD)
return temp;
temp=temp->link;
}
return NULL;
}
void deleteNode(node** CL, node* pre)
{
if(*CL==NULL){
printf("there is no any Node!\n");
return;
} else {
if(*CL==NULL){
free(*CL);
return;
} else {
node* old;
old = pre->link;
pre->link = old->link;
if(old==(*CL)){
*CL = old->link;
}
free(old);
}
}
}
void printNode(node** CL)
{
node* temp;
temp = *CL;
int i = 1;
printf("\n");
while((temp->link)!=(*CL)){
printf("[%d](%p/%d)", i, temp, temp->data);
i++;
temp=temp->link;
if((temp->link)!=(*CL))
printf(" -> ");
}
printf(" -> [%d](%p/%d)", i, temp, temp->data);
printf(" -> ∞ ");
printf("\n\n-----print done-----\n\n");
}
int main(void)
{
printf("Circular Node Program made by Hoeun with C\n\n");
node* CL;
CL = NULL;
int slc; int x;
while(1){
slc = 0;
printf("[1] insult [2] delete [3] print [4] exit \nenter : ");
scanf("%d", &slc);
if(slc==1){
printf("[1] first [2] middle \nenter : ");
scanf("%d", &slc);
printf("data : ");
scanf("%d", &x);
printf("\n");
if(slc==1){
insertFirstNode(&CL, x);
}
else if(slc==2){
int preD;
printf("data of the pre-node : ");
scanf("%d", &preD);
insertMiddleNode(&CL, searchNode(&CL, preD), x);
}
}
else if(slc==2){
int delD;
printf("data to delete : ");
scanf("%d", &delD);
deleteNode(&CL, searchNode(&CL, delD));
}
else if(slc==3){
printNode(&CL);
}
else if(slc==4){
break;
}
}
return 0;
}
16. 다항식 계산기(단항 구조체)
/**
*
* [2018 정보과학 다항식 계산 프로젝트]
*
* [171037 이호은]
*
**/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//계수, 지수를 멤버변수로 갖는 단항 구조체 선언
typedef struct term
{
int coef; //계수
int expo; //지수
struct term* link; //다음 노드로의 연결 링크
} term;
//다항식을 만드는 함수
void insertPoly(term** L)
{
int i = 0; int co;
term* temp = NULL;
printf("degree : ");
scanf("%d", &i);
for(;i>=0;i--){
printf("enter coef of %dth term : ", i);
scanf("%d", &co);
if(co!=0){
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->coef = co;
newNode->expo = i;
if((*L)==NULL){
*L = newNode;
newNode->link = NULL;
} else {
temp = *L;
while(temp->link!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode->link = NULL;
}
}
}
return;
}
//다항식 F의 F(x) 값을 구하는 함수
float insertX(term** L, int x)
{
term* temp;
temp = *L;
float sum = 0;
float tempN = 1;
int i = 1;
while(temp!=NULL){
for(;i<=(temp->expo);i++){
tempN = tempN*x;
}
sum += (tempN)*(temp->coef);
temp=temp->link;
}
return sum;
}
//다항식 출력 함수
void printPoly(term** L)
{
printf("\n");
term* temp;
temp = *L;
while((temp->link)!=NULL){
printf("%dX^%d", temp->coef, temp->expo);
printf(" + ");
temp = temp->link;
}
printf("%d", temp->coef);
return;
}
//다항식 덧셈 함수
void addPoly(term** A, term** B, term** C)
{
term* tempA = *A;
term* tempB = *B;
term* temp = NULL;
while((tempA!=NULL)&&(tempB!=NULL)){
if((tempA->expo)>(tempB->expo)){
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->expo = tempA->expo;
newNode->coef = tempA->coef;
if(*C == NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode = NULL;
}
temp = NULL;
tempA = tempA->link;
}
else if((tempB->expo)>(tempA->expo)){
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->expo = tempB->expo;
newNode->coef = tempB->coef;
if(*C == NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode = NULL;
}
temp = NULL;
tempB = tempB->link;
}
else if((tempA->expo)==(tempB->expo)){
if((tempA->coef)+(tempB->coef)==0){
tempA = tempA->link;
tempB = tempB->link;
} else {
term* newNode;
newNode = (term*)malloc(sizeof(term));
(newNode->expo) = (tempA->expo);
(newNode->coef) = ((tempA->coef)+(tempB->coef));
if(*C==NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode->link = NULL;
}
temp = NULL;
tempA = tempA->link;
tempB = tempB->link;
}
}
}
return;
}
//다항식 뺄셈 함수
void subPoly(term** A, term** B, term** C)
{
//뺄셈 방식 선택
printf("[1] A-B [2] B-A");
int slcF;
scanf("%d", &slcF);
term* tempA; term* tempB;
//B-A일 경우
if(slcF==2){
tempA = *B;
tempB = *A;
} else {
if(slcF!=1)
printf("\nwrong number! Choosed 1.\n");
tempA = *A;
tempB = *B;
}
term* temp = NULL;
while((tempA!=NULL)&&(tempB!=NULL)){
if((tempA->expo)>(tempB->expo)){
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->expo = tempA->expo;
newNode->coef = tempA->coef;
if(*C == NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode = NULL;
}
temp = NULL;
tempA = tempA->link;
}
else if((tempB->expo)>(tempA->expo)){
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->expo = tempB->expo;
newNode->coef = tempB->coef;
if(*C == NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode = NULL;
}
temp = NULL;
tempB = tempB->link;
}
else if((tempA->expo)==(tempB->expo)){
if((tempA->coef)-(tempB->coef)==0){
tempA = tempA->link;
tempB = tempB->link;
} else {
term* newNode;
newNode = (term*)malloc(sizeof(term));
(newNode->expo) = (tempA->expo);
(newNode->coef) = ((tempA->coef)-(tempB->coef));
if(*C==NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode->link = NULL;
}
temp = NULL;
tempA = tempA->link;
tempB = tempB->link;
}
}
}
return;
}
//다항식 곱셈 함수
void mulPoly(term** A, term** B, term** C)
{
term* tempA;
term* tempB;
term* temp;
while((tempA->link)!=NULL){
while((tempB->link)!=NULL){
tempB = tempB->link;
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->coef = (tempA->coef)*(tempB->coef);
newNode->expo = (tempA->expo)*(tempB->expo);
if(*C==NULL){
*C = newNode;
newNode->link = NULL;
} else {
temp = *C;
while((temp->link)!=NULL){
temp = temp->link;
}
temp->link = newNode;
newNode->link = NULL;
}
}
tempA = tempA->link;
}
printPoly(C);
return;
}
//다항식 A(x)의 도함수 A'(x)를 구하는 함수
void difPoly(term** A, term** C)
{
term* temp;
temp = *A;
term* tempC = NULL;
while((temp/*->link*/)!=NULL){
term* newNode;
newNode = (term*)malloc(sizeof(term));
newNode->coef = (temp->coef)*(temp->expo);
newNode->expo = ((temp->expo)-1);
printf("%dX^%d", newNode->coef, newNode->expo);
term* tempC;
tempC = *C;
if(*C==NULL){
*C = newNode;
newNode = NULL;
printf("null");
} else {
printf("notnull");
while((tempC->link)!=NULL){
tempC = tempC->link;
}
newNode->link = NULL;
tempC->link = newNode;
}
printf("before %p\n", temp);
temp = temp->link;
printf("after %p\n", temp);
}
printf("c");
printPoly(C);
return;
}
//다항식을 구간 a에서 b까지 적분한 값을 구하는 함수
float intgPoly(term** A)
{
int left = 0; int right= 0;
printf("enter range to integral\nleft limit : ");
scanf("%d", &left);
printf("\nright limit : ");
scanf("%d", &right);
term* temp;
temp = *A;
while(temp!=NULL){
temp->coef = (temp->coef)/((temp->expo)+1);
(temp->expo)++;
temp = temp->link;
}
float intg;
printf("%f - %f", insertX(A, left), insertX(A, right));
intg = insertX(A, left) - insertX(A, right);
return intg;
}
int main(int argc, char *argv[])
{
printf("Calculation of Polynomials");
while(1){
term* A = NULL;
term* B = NULL;
term* C = NULL;
int slc = 0;
printf("\n\n[1] addition [2] subtraction [3] multiplication [4] differential [5] integral \nenter : ");
scanf("%d", &slc);
if(slc==1){
printf("\ninsert Polynomial A\n");
insertPoly(&A);
printf("\ninsert Polynomial B\n");
insertPoly(&B);
addPoly(&A, &B, &C);
printPoly(&C);
}
else if(slc==2){
printf("\ninsert Polynomial A\n");
insertPoly(&A);
printf("\ninsert Polynomial B\n");
insertPoly(&B);
subPoly(&A, &B, &C);
printPoly(&C);
}
else if(slc==3){
printf("insert Polynomial A\n");
insertPoly(&A);
printf("insert Polynomial B\n");
insertPoly(&B);
mulPoly(&A, &B, &C);
}
else if(slc==4){
printf("insert Polynomial \n");
insertPoly(&A);
difPoly(&A, &C);
}
else if(slc==5){
printf("insert Polynimial");
insertPoly(&A);
printf("%f", intgPoly(&A));
}
else{
printf("[!] error\n");
}
free(A);
free(B);
free(C);
}
return 0;
}
17. 이중연결리스트
#include <stdio.h>
#include <stdlib.h>
//구조체 선언
typedef struct node
{
struct node* Rlink;
int data;
struct node* Llink;
} node;
//이중연결노드 삽입 함수
void insertNode(node** L, int intdata)
{
node* newNode;
newNode = (node*)malloc(sizeof(node));
newNode->data = intdata;
if(*L==NULL){
*L = newNode;
newNode->Llink=NULL;
newNode->Rlink=NULL;
return;
} else {
newNode->Rlink = *L;
*L = newNode;
newNode->Llink = NULL;
newNode->Rlink->Llink = newNode;
return;
}
}
//정방향 출력 함수
void printForwardNode(node** L)
{
node* temp = *L;
int i = 1;
while((temp->Rlink)!=NULL){
printf("[%d](%p/%d)", i, temp, temp->data);
i++;
temp = temp->Rlink;
printf(" <-> ");
}
printf("[%d](%p/%d)", i, temp, temp->data);
return;
}
//역방향 출력 함수
void printBackwardNode(node** L)
{
node* temp = *L;
int i = 1;
while((temp->Rlink)!=NULL){
temp = temp->Rlink;
i++;
}
while((temp->Llink)!=NULL){
printf("[%d](%p/%d)", i, temp, temp->data);
i--;
temp = temp->Llink;
printf(" <-> ");
}
printf("[%d](%p/%d)", i, temp, temp->data);
return;
}
int main(int argc, char *argv[])
{
printf("이중연결리스트 예제 실습\n\n");
node* L = NULL;
while(1){
printf("\n");
printf("[1] insert [2] print Forward [3] print Backward [4] shutdown\nenter : ");
int slc;
scanf("%d", &slc);
if(slc==1){
int intdata;
int predata;
printf("\ndata : ");
scanf("%d", &intdata);
insertNode(&L, intdata);
}
else if(slc==2){
printf("\n");
printForwardNode(&L);
printf("\n");
}
else if(slc==3){
printf("\n");
printBackwardNode(&L);
printf("\n");
}
else if(slc==4){
return 0;
}
else {
printf("error!");
}
}
}
한 학기동안 이를 제외하고도 많은 예제를 직접 부딪혀가며 해결해가면서 프로그래밍 소양도 많이 길렀고, 제 실력을 크게 성장하게 해주었던 것 같습니다.