๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โœ’๏ธ C Programming/๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ์ฆ๊ธฐ๋Š” C ์–ธ์–ด ์ฝ˜์„œํŠธ_๊ฐœ์ • 3ํŒ

[๋ˆ„๊ตฌ๋‚˜ ์‰ฝ๊ฒŒ ์ฆ๊ธฐ๋Š” C ์–ธ์–ด ์ฝ˜์„œํŠธ - ๊ฐœ์ • 3ํŒ] 8์žฅ Programming ํ’€์ด (2)

by A Lim Han 2023. 1. 20.

# 8์žฅ Programming ํ’€์ด (2)

 

 

337p_8๋ฒˆ)

#include<stdio.h>
#include<math.h>

void cal(double a, double b, double c) 
{
    if (b * b - 4 * a * c > 0) 
    {
        double one = (-b + sqrt(b * b - 4 * a * c)) / (2 * a);
        double two = (-b - sqrt(b * b - 4 * a * c)) / (2 * a);

        printf("์ฒซ ๋ฒˆ์งธ ๊ทผ = %.4lf\n", one);
        printf("๋‘ ๋ฒˆ์งธ ๊ทผ = %.4lf\n", two);
    }
    else
    {
        printf("๊ทผ์ด ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.");
    }
}

int main() 
{
    double x, y, z;

    printf("2์ฐจ ๋ฐฉ์ •์‹์˜ ๊ณ„์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค:\n");
    printf("a: ");
    scanf_s("%lf", &x);
    printf("b: ");
    scanf_s("%lf", &y);
    printf("c: ");
    scanf_s("%lf", &z);

    cal(x, y, z);

    return 0;
}

 

 

 

 

338p_9๋ฒˆ)

#include <stdio.h>
#include <stdlib.h>

int monte()
{
	srand(time(NULL));
	int num = rand() % 2;

	return num;
}

int main()
{
	while (1)
	{
		int user = 0;

		printf("์•ž(0) ๋˜๋Š” ๋’ค(1)๋ฅผ ์„ ํƒํ•˜์‹œ์˜ค(์ข…๋ฃŒ๋Š” -1): ");
		scanf_s("%d", &user);

		if (user == -1)
		{
			printf("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n");
			break;
		}

		int a = monte();

		if (a == user)
		{
			printf("์ปดํ“จํ„ฐ ์Šน๋ฆฌ\n");
		}
		else
		{
			printf("์‚ฌ์šฉ์ž ์Šน๋ฆฌ\n");
		}
	}

	return 0;
}

 

 

 

 

338p_10๋ฒˆ)

#include <stdio.h>
#include <stdlib.h>

int dice()
{
	int num = rand() % 6;
	return num;
}

int main()
{
	srand(time(NULL));

	int a = dice();
	int b = dice();
	int c = dice();
	int d = dice();
	int e = dice();
	int f = dice();
	int user_sum = a + b + c;
	int com_sum = d + c + f;

	printf("์‚ฌ์šฉ์ž ์ฃผ์‚ฌ์œ„ = (%d, %d, %d) = %d\n", a, b, c, user_sum);
	printf("์ปดํ“จํ„ฐ ์ฃผ์‚ฌ์œ„ = (%d, %d, %d) = %d\n", d, e, f, com_sum);

	if (user_sum > com_sum)
	{
		printf("์‚ฌ์šฉ์ž ์Šน๋ฆฌ\n");
	}
	else if (user_sum == com_sum)
	{
		printf("๋น„๊ฒผ์Šต๋‹ˆ๋‹ค.\n");
	}
	else
	{
		printf("์ปดํ“จํ„ฐ ์Šน๋ฆฌ\n");
	}

	return 0;
}

 

 

 

 

338p_11๋ฒˆ)

#include <stdio.h>

static int add = 1;
static int sub = 1;
static int mul = 1;
static int div = 1;

void calculator(int a, char k, int b)
{
	if (k == '+')
	{
		printf("๋ง์…ˆ์€ ์ด %d๋ฒˆ ํ˜ธ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", add++);
		printf("์—ฐ์‚ฐ ๊ฒฐ๊ณผ: %d\n", a + b);
		
	}
	else if (k == '-')
	{
		printf("๋บ„์…ˆ์€ ์ด %d๋ฒˆ ํ˜ธ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", sub++);
		printf("์—ฐ์‚ฐ ๊ฒฐ๊ณผ: %d\n", a - b);
	}
	else if (k == '/')
	{
		printf("๋‚˜๋ˆ—์…ˆ์€ ์ด %d๋ฒˆ ํ˜ธ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", div++);
		printf("์—ฐ์‚ฐ ๊ฒฐ๊ณผ: %d\n", a / b);
	}
	else
	{
		printf("๊ณฑ์…ˆ์€ ์ด %d๋ฒˆ ํ˜ธ์ถœ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.\n", mul++);
		printf("์—ฐ์‚ฐ ๊ฒฐ๊ณผ: %d\n", a * b);
	}
}

int main()
{
	while (1)
	{
		int x = 0;
		int y = 0;
		char cal;

		printf("\n์—ฐ์‚ฐ์„ ์ž…๋ ฅํ•˜์‹œ์˜ค.(์ข…๋ฃŒ๋Š” Q): ");
		scanf_s("%d", &x);
		scanf_s("%c", &cal);
		scanf_s("%d", &y);

		if (cal == 'Q')
		{
			printf("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n");
			break;
		}

		calculator(x, cal, y);
	}

	return 0;
}

 

 

 

 

339p_12๋ฒˆ)

#include <stdio.h>

static int sum = 0;

void save(int amount)
{
	sum += amount;

	printf("์ง€๊ธˆ๊นŒ์ง€์˜ ์ €์ถ•์•ก์€ %d์ž…๋‹ˆ๋‹ค.\n", sum);
}

void draw(int amount)
{
	sum -= amount;

	printf("์ง€๊ธˆ๊นŒ์ง€์˜ ์ €์ถ•์•ก์€ %d์ž…๋‹ˆ๋‹ค.\n", sum);
}

int main()
{
	int num = 0;

	while (1)
	{
		int choose = 0;

		printf("\n์ €์ถ•(1)๊ณผ ์ธ์ถœ(0) ์ค‘ ๋ฌด์—‡์„ ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?(์ข…๋ฃŒ๋Š” -1): ");
		scanf_s("%d", &choose);

		if (choose == -1)
		{
			printf("ํ”„๋กœ๊ทธ๋žจ์„ ์ข…๋ฃŒํ•ฉ๋‹ˆ๋‹ค.\n");
			break;
		}
		else if (choose == 0)
		{
			printf("์–ผ๋งˆ๋ฅผ ์ธ์ถœํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?(์ข…๋ฃŒ๋Š” -1): ");
			scanf_s("%d", &num);

			draw(num);
		}
		else
		{
			printf("์–ผ๋งˆ๋ฅผ ์ €์ถ•ํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?(์ข…๋ฃŒ๋Š” -1): ");
			scanf_s("%d", &num);

			save(num);
		}
	}

	return 0;
}

 

 

 

 

339p_13๋ฒˆ)

#include <stdio.h>

int show_digit(int x) 
{
    int num = x % 10;

    if (num == 0 && x == 0)	// x๊ฐ€ 10์˜ ๋ฐฐ์ˆ˜์ผ ๋•Œ๋„ ๋‚˜๋จธ์ง€๋Š” 0์ด๋‹ˆ ์ด์™€ ๊ฐ™์€ ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•จ
    {
        return;
    }
    else
    {
        show_digit((x - num) / 10);
    }

    printf("%d ", num);
}

int main() 
{
    int n = 0;

    printf("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค: ");
    scanf_s("%d", &n);

    show_digit(n);

    return 0;
}

 

 

 

 

340p_14๋ฒˆ)

#include <stdio.h>

int sum = 0;

int cal(int m)
{
	if (m >= 1)
	{
		sum += m * m * m;
		cal(m - 1);
	}
	else
	{
		return sum;
	}
}

int main()
{
	int num = 0;

	printf("์ •์ˆ˜๋ฅผ ์ž…๋ ฅํ•˜์‹œ์˜ค: ");
	scanf_s("%d", &num);

	printf("%d\n", cal(num));

	return 0;
}