C언어

C언어 파일나누기,명함만들기,구구단,계산기

blockjane 2023. 9. 26. 15:54
728x90
반응형
SMALL

배웠던거 실습하기

플로우차트 작성 후 코드짜기

#include <stdio.h>

typedef enum __syllable
{
	Do , Re=100 ,  Mi , Fa , So , La , Ti 
}syllable;

void Sound(syllable sy);

int main(void)
{
	syllable tone;
	printf("%d\n", Fa);
	for (tone = Do;tone <= Ti;tone+=1 )
	{
		Sound(tone);
	}
	return 0;
}

void Sound(syllable sy)
{
	switch (sy)
	{
	case Do:
		puts("도는 하얀 도라지"); return;
	case Re:
		puts("레는 둥근 레코드"); return;
	case Mi:
		puts("미는 파란 미나리"); return;
	case Fa:
		puts("파는 예쁜 파랑새"); return;
	case So:
		puts("솔은 작은 솔방울"); return;
	
	}

	puts("다 함께 부르세 ~ 도레미파 솔라시도 솔 도 ~ 짠~");
}

//////////////////////////////////////////


#include <stdio.h>

#define NAME         "박진영"
#define AGE           35
#define PRINT_ADDR    puts("주소:전라남도 광양시\n");

#define PI 3.14
#define PROUDCT(x,y)  ((x)*(y))
#define CIRCLE_AREA(R) (PROUDCT((R),(R))*PI)

int main()
{
	printf("이름:%s \n", NAME);
	printf("나이:%d \n", AGE);
	PRINT_ADDR;

	double rad = 2.1;
	printf("반지름%g인 원의 넓이:%g\n", rad, CIRCLE_AREA(rad));

	return 0;
}

////////////////////////////

//명함만들기

#include <stdio.h>
#include <string.h>
#pragma warning(disable:4996)
#define SHORT_STR             40
#define LONG_STR              80
#define MAX_CNT               100

enum { INSERT = 1, SEARCH = 2, EXIT = 3 };

typedef struct __namecard
{
	char name[SHORT_STR];
	char company[LONG_STR];
	char position[SHORT_STR];
	char phone[SHORT_STR];
}namecard;

void ShowMenu();

int ReadChoice();
void InsertData(namecard arr[], int* cntPtr);
void SearchData(namecard arr[], int cnt);
void ExitProg();

int main()
{
	int dataCnt = 0;
	namecard ncList[MAX_CNT];

	while (1)
	{
		int choice;
		ShowMenu();
		choice = ReadChoice();
		getchar();


		switch (choice)
		{
		case INSERT:
			InsertData(ncList, &dataCnt);
			break;
		case SEARCH:
			SearchData(ncList, dataCnt);
			break;
		case EXIT:
			ExitProg();
			return 0;

		}
	}

	return 0;
}

void ShowMenu()
{
	puts("선택하세요");
	puts("1. 정보입력");
	puts("2. 정보 검색");
	puts("3. 종료");
	printf(">> ");
}

int ReadChoice()
{
	int choice;
	scanf("%d", &choice);
	fflush(stdin);
	return choice;
}

void InsertData(namecard arr[], int* cntPtr)
{
	int i;
	int cnt = *cntPtr;
	namecard read;

	printf("이름: "); gets(read.name);
	printf("회사: "); gets(read.company);
	printf("직급: "); gets(read.position);
	printf("전화번호: "); gets(read.phone);

	for (int i = 0; i < cnt; i++)
	{
		if (!strcmp(read.name, arr[i].name)
			&& !strcmp(read.phone, arr[i].phone))
		{
			puts("동일인의 정보가 입력되었습니다.\n");
			return;
		}
	}

	arr[cnt] = read;
	(*cntPtr)++;
	printf("\n");
}

void SearchData(namecard arr[], int cnt)
{
	int i;
	char name[SHORT_STR];
	printf("찾고자 하는 이름 입력: "); gets(name);

	for (int i = 0; i < cnt; i++)
	{
		if (!strcmp(name, arr[i].name))
		{
			printf("이름: "); puts(arr[i].name);
			printf("회사: "); puts(arr[i].company);
			printf("직급: "); puts(arr[i].position);
			printf("전화번호: "); puts(arr[i].phone);
		}
	}
	printf("\n");
}
void ExitProg()
{
	puts("이용해 주셔서 감사합니다.");
}
////////////////////////////
구구단

#include <stdio.h>

int main()
{
	int val1 = 0;
	int val2 = 0;

	for (int val2 = 1; val2 < 10; val2++)
	{
		for (int val1 = 1; val1 < 10; val1++)
		{
			printf("%d x %d = %d\n", val2, val1, val1 * val2);
		}
		
	}
	
	return 0;
}

/////////////////////////////
계산기

#include <stdio.h>
#pragma warning(disable:4996)

int main()
{
	int num, num1;
	char sum;
	
	printf("숫자 두개를 수식과 함께 입력하세요");
	scanf("%d %c %d", &num,&sum,&num1);
	
	switch (sum)
	{
	case '+':
		
		printf("두 수의 합은: %d", num + num1);
		break;

	case '-':
		
		printf("두 수의 뺀 값은: %d", num - num1);
		break;
	case 'x':
		
		printf("두 수의 곱한 값은: %d", num * num1);
		break;
	case '/':
		
		printf("두 수를 나눈값 은: %f", num / num1);
		

		 break;
	}
	
	return 0;
}

////////////////////////////////////////////////////////////
728x90
반응형
LIST