C언어 비트단기 5일차(함수)

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

함수 실습

//Program.c

#include <stdio.h>

#include <assert.h> //~이어야만 한다. 입력 인자로 하나의 값을 입력 인자로 받고 입력 인자의 값이 참이면 아무것도 동작 안하고 0이면 오류message창 띄어준다.

#include "fun.h"

void TestAdd();

void TestSub();

void TestSum();

void TestIsPrime();

void TestHowManyPrime();

void TestNSum();

void TestSwap();

void TestGetMaxNumberPtr();

void TestSelectionSort();



int main(void)

{

TestAdd();

printf("\n");

TestSub();

printf("\n");

    TestSum();

printf("\n");

TestIsPrime();

printf("\n");

TestHowManyPrime();

printf("\n");

TestNSum();

printf("\n");

TestSwap();

printf("\n");

TestGetMaxNumberPtr();

printf("\n");

TestSelectionSort();

printf("\n");

return 0;

}


void TestSub()

{

printf("Test Sub Function\n");

assert(Sub(2,3) == -1);

assert(Sub(6,3) == 3);

assert(Sub(1,9) == -8);

printf("Success Test Sub Function.\n");

}


void TestAdd()

{

printf("Test Add Function\n");

assert(Add(2,3) == 5);

assert(Add(6,3) == 9);

assert(Add(1,9) == 10);

printf("Success Test Add Function.\n");

}


void TestSum()

{

printf("Test Sum Function\n");

assert(Sum(1,10) == 55);

assert(Sum(5,10) == 45);

printf("Success Test Sum Function.\n");

}


void TestIsPrime()

{

printf("Test IsPrime Function\n");

assert(IsPrime(2) == 1);

assert(IsPrime(3) == 1);

assert(IsPrime(4) == 0);

assert(IsPrime(5) == 1);

assert(IsPrime(6) == 0);

assert(IsPrime(11) == 1);

printf("Success Test IsPrime Function.\n");

}


void TestHowManyPrime()

{

printf("Test HowManyPrime Function\n");

assert(HowManyPrime(2,10) == 4);

assert(HowManyPrime(10,20) == 4);

assert(HowManyPrime(20,30) == 2);

printf("Success Test HowManyPrime Function.\n");

}


void TestNSum()

{

int arr[10] = {1,3,6,7,10,9,34,22,6,9};

printf("Test NSum Function\n");

assert(NSum(arr,3) == 10);

assert(NSum(arr+1,3) == 16);

assert(NSum(arr,5) == 27);

printf("Success Test NSum Function.\n");

}


void TestSwap()

{

int a = 2;

int b = 3;

int c = 7;

int d = 8;

printf("Test Swap Function\n");

Swap(&a,&b);

assert((a==3) && (b==2));

Swap(&c,&d);

assert((c==8) && (d==7));

printf("Success Test Swap Function.\n");

}


void TestGetMaxNumberPtr()

{

int arr[10] = {1,3,6,7,10,9,34,22,6,9};

printf("Test GetMaxNumberPtr Function\n");

assert(GetMaxNumberPtr(arr,10) == arr+6);

assert(GetMaxNumberPtr(arr,5) == arr+4);

printf("Success Test GetMaxNumberPtr Function.\n");

}


void TestSelectionSort()

{

int arr[10] = {1,3,6,7,10,9,34,22,6,9};

int i = 0;

printf("Test SelectionSort Function\n");

SelectionSort(arr,10);

for(i =1; i < 10; i++)

{

assert(arr[i-1] >= arr[i]); //내림차순

//assert(arr[i-1] <= arr[i]); 오름차순

}

printf("Success Test SelectionSort Function.\n");

}





//fun.c

#include "fun.h"

#include <assert.h>

#include <stdio.h>


int Add(int a, int b)

{

return a+b;

}


int Sub(int a, int b)

{

return a-b;

}


//a. 범위 내의 정수 합계를 구하는 함수 - Sum

int Sum(int a, int b)

{

int i = 0;

int sum = 0;


for(i = a; i <= b; i++)

{

sum += i;

}

return sum;

}

//b. 특정 수가 소수(Prime Number)인지 판단하는 함수 - IsPrime

int IsPrime(int num)

{

int count = 0;

int i = 0;

int j = 0;


if(num <= 1)

{

                     return 0;

}


for(i = 2; i <= num; i++)

{

count = 0;

for(j = 1; j <= num; j++)

{

if(i % j == 0)

{

count++;

}

}

}

if(count == 2)

{

return 1;

}

else

{

return 0;

}

}

//b. 특정 수가 소수(Prime Number)인지 판단하는 함수 - IsPrime

int HowManyPrime(int num1, int num2)

{

int i = 0;

int j = 0;

int count = 0;


for(i = num1; i <num2; i++)

{

if(IsPrime(i))

{

count++;

}

}

return count;

}



//c. 범위 내의 정수중에 소수(Prime Number)의 개수를 구하는 함수 - HowManyPrime

int HowManyPrime(int num1, int num2)

{

int count1 = 0;

int i = 0;

int j = 0;

int count2 = 0;


for(i = num1; i <num2; i++)

{

count1 = 0;

for(j = 1; j <= i; j++)

{

if(i % j == 0)

{

count1++;

}

}

if(count1 == 2)

{

count2++;

}

}

return count2;

}

//d. n 개의 정수의 합계를 구하는 함수 - NSum

int NSum(int *base, int n)

{

int *arr = 0;

int i = 0;

int sum = 0;


arr = base;


for(i = 0; i < n; i++)

{

sum += *(arr+i);

}


return sum;

}

//e. 두 수를 바꾸는 함수 - Swap

int Swap(int *p1, int *p2)

{

int temp = 0;


temp = *p1;

*p1 = *p2;

*p2 = temp;


return temp;

}

//f. n 개의 정수에서 제일 큰 정수가 있는 메모리 주소를 구하는 함수 - GetMaxNumberPtr

int *GetMaxNumberPtr(int *base,int n)

{

int *max = 0;

int i = 0;


max = base;

for(i = 0; i < n; i++)

{

if(*max < *(base+i) ) // *(base+i) == base[i]랑 같은 표현이다.

{

max = (base+i);

}

}

return max;

}

//g. n 개의 정수를 크기 순으로 정렬하는 함수(내림차순, 선택 정렬 알고리즘으로 정렬) -SelectionSort

void SelectionSort(int *base, int n)

{

int *arr = 0;

int i = 0;

int j = 0;

int temp = 0;


arr = base;


for(i = 0; i <= n-1; i++)

{

for( j = i+1; j <= n; j++)

{

if(*(arr+i) < *(arr+j))

{

temp = *(arr+i);

*(arr+i) = *(arr+j);

*(arr+j) = temp;

}

}

}

}



//fun.h

#pragma once

//두 수를 더하는 함수 - Add

int Add(int a, int b);

//두 수를 빼는 함수 - Sub

int Sub(int a, int b);

//a. 범위 내의 정수 합계를 구하는 함수 - Sum

int Sum(int a, int b);

//b. 특정 수가 소수(Prime Number)인지 판단하는 함수 - IsPrime

int IsPrime(int num);

//c. 범위 내의 정수중에 소수(Prime Number)의 개수를 구하는 함수 - HowManyPrime

int HowManyPrime(int num, int count);

//d. n 개의 정수의 합계를 구하는 함수 - NSum

int NSum(int *base, int n);

//e. 두 수를 바꾸는 함수 - Swap

int Swap(int *p1, int *p2);

//f. n 개의 정수에서 제일 큰 정수가 있는 메모리 주소를 구하는 함수 - GetMaxNumberPtr

int *GetMaxNumberPtr(int *base,int n);

//g. n 개의 정수를 크기 순으로 정렬하는 함수(내림차순, 선택 정렬 알고리즘으로 정렬) -SelectionSort

void SelectionSort(int *base, int n);


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

피보나치 수열의 n 항을 구하는 함수를 구현하시오.


//fun.c

#include "fun.h"

#include <assert.h>


int FiBoNaChi(int n)

{

if(n <= 1)

{

return n;

}

else

{

return FiBoNaChi(n-1) + FiBoNaChi(n-2);

}

}



//Program.c

#include "fun.h"

#include <stdio.h>

#include <assert.h>


void TestFiBoNaChi();


int main( void)

{

int num = 0;

int i = 0;


printf("피보나치 수열 몇번째 항의 합이 궁금하신가요?\n");

scanf_s("%d",&num);


printf("피보나치 수열 출력\n");

for(i = 1; i <= num; i++)

{

printf("%5d",FiBoNaChi(i));

}

printf("\n");

printf("피보나치 수열의  n항은 %d입니다.\n",FiBoNaChi(num));

printf("\n");

TestFiBoNaChi();

printf("\n");

}


void TestFiBoNaChi()

{

printf("Test FiBoNaChi Function \n");

assert(FiBoNaChi(1) == 1);

assert(FiBoNaChi(2) == 1);

assert(FiBoNaChi(3) == 2);

assert(FiBoNaChi(4) == 3);

assert(FiBoNaChi(5) == 5);

printf("Success FiBoNaChi Function \n");

}



//fun.h

#pragma once


//피보나치 수열의 n 항을 구하는 함수를 구현하시오 - FiBoNaChi

int FiBoNaChi(int n);



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

특정 구간 내에 n의 배수의 개수를 구하는 함수를 구현하시오.



//Program.c

#include "fun.h"

#include <stdio.h>

#include <assert.h>


void TestNMultipleCount();


int main(void)

{

int num = 0;

int i = 0;

int a = 0;

int b = 0;


printf("a를 입력하세요 :\n");

scanf_s("%d",&a);

printf("b를 입력하세요 :\n");

scanf_s("%d",&b);


printf("정수 하나를 입력해 주세요");

scanf_s("%d",&num);


printf("a부터 b까지의 범위에서 num의 배수의 개수 =  %d개 입니다.\n",NMultipleCount(a,b,num));


TestNMultipleCount();

}


void TestNMultipleCount()

{

printf("Test NMultipleCount Function \n");

assert(NMultipleCount(1, 10, 2) == 5);

printf("Success NMultipleCount Function \n");

}




//fun.c

#include "fun.h"

#include <assert.h>



int NMultipleCount(int a, int b, int n)

{

int i = 0;

int count = 0;


if(a < b)

{

for(i = a; i <= b; i++)

{

if( i % n  == 0)

{

count++;

}

}

}

else if(a > b)

{

for(i = b; i <= a; i++)

{

if( i % n  == 0)

{

count++;

}

}

}

return count;

}





//fun.h

#pragma once


//특정 구간 내에 n의 배수의 개수를 구하는 함수를 구현하시오.N


int NMultipleCount(int a, int b, int n);




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

에서 n 사이의 수 중에서 랜덤한 수를 m개 발급하는 함수를 구현하시오.


//fun.c

#include "fun.h";

#include <assert.h>

#include <stdlib.h>

#include <time.h>

#include <stdio.h>


void RandomIssue(int n, int a)

{

int i = 0;

int j = 0;

int re = 0;

for(i = 0; i < a; i++)

{

re = rand() % (n+1);

printf("%2d 번째 : %2d\n",i+1,re);

}

}




//Program.c

#include <stdio.h>

#include "fun.h"

#include <assert.h>


void TestRandomIssue();


int main(void)

{

int num = 0;

int count = 0;


printf("몇개의 랜덤한 수를 원하시나요 ?\n");

scanf_s("%d",&count);

printf("랜덤한 수의 범위를 입력하세요 : \n");

scanf_s("%d",&num);

RandomIssue(num,count);

}


void TestRandomIssue()

{

printf("Test TestRandomIssue Function \n");

printf("Success TestRandomIssue Function \n");

}






//fun.h

#pragma once


//1에서 n 사이의 수 중에서 랜덤한 수를 m개 발급하는 함수를 구현하시오. - RandomIssue


void RandomIssue(int n, int a);



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

두 수의 최대 공약수를 구하는 함수를 구현하시오.


//fun.c

#include "fun.h"

#include <assert.h>


int GreatestCommonMeasure(int a, int b)

{

int i = 0;

int max = 0;


if(a > b)

{

for(i = 1; i <= a; i++)

{

if((a % i) == 0 &&( b % i) == 0)

{

max = i;

}

}

}

else if( b > a)

{

for(i = 1 ; i <= b; i++)

{

if(a % i == 0 && b % i == 0)

{

max = i;

}

}

}

else

{

max = a;

}


return max;

}





//Program.c

#include <stdio.h>

#include <assert.h>

#include "fun.h"


void TestGreatestCommonMeasure();


int main(void)

{

int num1 = 0;

int num2 = 0;


printf("num1을 입력하세요 :\n");

scanf_s("%d",&num1);

printf("num2을 입력하세요 :\n");

scanf_s("%d",&num2);


printf("최대 공약수 = %d입니다.\n",GreatestCommonMeasure(num1,num2));


TestGreatestCommonMeasure();

}


void TestGreatestCommonMeasure()

{

printf("Test GreatestCommonMeasure  Function\n");

assert(GreatestCommonMeasure(6,10) == 2);

assert(GreatestCommonMeasure(6,12) == 6);

assert(GreatestCommonMeasure(5,20) == 5);

printf("Success GreatestCommonMeasure Function\n");

}




#pragma once


//두 수의 최대 공약수를 구하는 함수를 구현하시오



int GreatestCommonMeasure(int a, int b);



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

두 수의 최소 공배수를 구하는 함수를 구현하시오


//fun.c

#include "fun.h"

#include <assert.h>

#include <stdio.h>


int LeastCommonMultiple(int a, int b)

{

int i = 0;

int j = 1;

int max = 0;

int count = 0;


for(i = j; ;i++)

{

if(i % a == 0 && i % b == 0)

{

count++;

}

if(count == 1)

{

max = i;

break;

}

}

}





//Program.c

#include <stdio.h>

#include "fun.h"

#include <assert.h>


void TestLeastCommonMultiple();


int main(void)

{

int num1 = 0;

int num2 = 0;


printf("num1을 입력하세요 :\n");

scanf_s("%d",&num1);

printf("num2을 입력하세요 :\n");

scanf_s("%d",&num2);


printf("최소 공배수 = %d입니다.\n",LeastCommonMultiple(num1,num2));


TestLeastCommonMultiple();

}


void TestLeastCommonMultiple()

{

printf("Test void TestLeastCommonMultiple Function \n");

assert(LeastCommonMultiple(12,16)== 48);

assert(LeastCommonMultiple(29,11)== 319);

printf("Success void TestLeastCommonMultiple \n");

}







//fun.h

#pragma once


//두 수의 최소 공배수를 구하는 함수를 구현하시오.- least common multiple


int LeastCommonMultiple(int a, int b);




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

두 수의 공약수의 개수를 구하는 함수를 구현하시오.



//fun.c

#include <stdio.h>

#include "fun.h"

#include <assert.h>


int CommonDivisorCount(int a, int b)

{

int i = 0;

int count = 0;

if(a > b)

{

for(i = 1; i <=a; i++)

{

if((a%i) == 0 && (b%i) == 0)

{

count++;

}

}

}

else if(b > a)

{

for(i = 1; i <= b; i++)

{

if((a%i) == 0 && (b % i) == 0)

{

count++;

}

}

}

else

{

count = 1;

}


return count;

}





//Program.c

#include <stdio.h>

#include "fun.h"

#include <assert.h>


void  TestCommonDivisorCount();


int main(void)

{

int num1 = 0;

int num2 = 0;


printf("num1 을 입력하세요 :\n");

scanf_s("%d",&num1);

printf("num2 을 입력하세요 :\n");

scanf_s("%d",&num2);


printf("두수의 공약수의 개수 = %d개 입니다. \n",CommonDivisorCount(num1,num2));

TestCommonDivisorCount();

}


void  TestCommonDivisorCount()

{

printf("Test CommonDivisorCount Function\n");

assert(CommonDivisorCount(2,6) == 2);

assert(CommonDivisorCount(3,6) == 2);

printf("Success CommonDivisorCount Function\n");

}





//fun.h


#pragma once


//두 수의 공약수의 개수를 구하는 함수를 구현하시오.-CommonDivisorCount


int CommonDivisorCount(int a, int b);



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

1부터 n 사이에 두 수의 공배수의 개수를 구하는 함수를 구현하시오.


//fun.c

#include "fun.h"

#include <assert.h>


int CommonMultipleCount(int a, int b, int n)

{

int i = 0;

int j = 1;

int count = 0;


for( i = 1; i <= n; i++)

{

if(i % a == 0 && i % b == 0)

{

count++;

}

}


return count;

}




//fun.c

#include "fun.h"

#include <assert.h>


int CommonMultipleCount(int a, int b, int n)

{

int i = 0;

int j = 1;

int count = 0;


for( i = 1; i <= n; i++)

{

if(i % a == 0 && i % b == 0)

{

count++;

}

}


return count;

}




#pragma once


//1부터 n 사이에 두 수의 공배수의 개수를 구하는 함수를 구현하시오.

//CommonMultipleCount


int CommonMultipleCount(int a, int b,int n);



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

 버블 정렬 알고리즘을 학습하여 n 개의 정수를 정렬하는 함수를 구현하시오.


//fun.c

#include "fun.h"

#include <stdio.h>

#include <assert.h>


void BubbleSort(int *a, int n)

{

int *arr = 0;

int i = 0;

int j = 0;

int temp = 0;


for(i = n; i >= 0; i--)

{

for(j = 1; j <= i; j++)

{

if( arr[j-1]  > arr[j] )

{

temp = arr[j-1];

arr[j-1] = arr[j];

arr[j] = temp;

}

}

}

}






//Program.c

#include <stdio.h>

#include "fun.h"

#include <assert.h>


void TestBubbleSort();


int main(void)

{

TestBubbleSort();

}


void TestBubbleSort()

{

int arr[10] = {3,5,2,11,43,65,4,21,7,100};

printf("Test BubbleSort Function \n");

assert(BubbleSort(arr,5));

printf("Success BubbleSort Function \n");

}




#pragma once


//버블 정렬 알고리즘을 학습하여 n 개의 정수를 정렬하는 함수를 구현하시오. - BubbleSort


void BubbleSort(int *a, int n);









728x90

이 글을 공유하기

댓글

Designed by JB FACTORY