Notice
Recent Posts
Recent Comments
Link
«   2026/08   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
Archives
Today
Total
관리 메뉴

강동영의 일상

[쉽게 풀어쓴 C언어 Express] 9일 차 본문

coding

[쉽게 풀어쓴 C언어 Express] 9일 차

rokaf6444 2023. 1. 3. 02:30

01. 문자열을 받아서 문자열에 포함된 문자를 대문자로 변환하는 함수 str_upper(char *s)를 작성하고 테스트하라.

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

void str_upper(char *s) {
	for (int i = 0; s[i] != '\0'; i++) { // 문자열이 끝나면 중단
		if (s[i] >= 'a' && s[i] <= 'z') {
			s[i] -= 32; // 대문자와 소문자의 아스키 코드값 차이 : 32
		}
	}
}
int main() {
	char myString[1000];

	printf("문자열을 입력하시오: ");
	scanf("%s", myString); // myString 배열에 입력
	str_upper(myString);
	printf("변환된 문자열: %s\n", myString);

	return 0;
}

 

문자열을 입력하시오: abcdef
변환된 문자열: ABCDEF

 

 

 

02. 구조체를 이용하여 복소수를 다음과 같이 정의하고 복소수의 덧셈을 수행하는 함수를 작성하고 테스트하라.

 

struct complex {

double real;

double imag;

};

struct complex complex_add(struct complex c1, struct complex c2){ }

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct complex {
	double real; // 실수부
	double imag; // 허수부
};
struct complex complex_add(struct complex c1, struct complex c2) {
	struct complex result;

	result.real = c1.real + c2.real; // 실수부의 합
	result.imag = c1.imag + c2.imag; // 허수부의 합

	return result;
}
int main() {
	struct complex c1;
	struct complex c2;
	struct complex c3;

	printf("1번 복소수의 실수부와 허수부를 입력하세요(실수부,허수부) : ");
	scanf("%lf,%lf", &c1.real, &c1.imag);
	printf("2번 복소수의 실수부와 허수부를 입력하세요(실수부,허수부) : ");
	scanf("%lf,%lf", &c2.real, &c2.imag);

	c3 = complex_add(c1, c2);
	printf("%lf+%lfi\n", c3.real, c3.imag);

	return 0;
}

 

1번 복소수의 실수부와 허수부를 입력하세요(실수부,허수부) : 1,2
2번 복소수의 실수부와 허수부를 입력하세요(실수부,허수부) : 2,3
3.000000+5.000000i

 

 

 

03. equal() 함수를 다음과 같이 구조체의 포인터를 받도록 변경하여서 작성하고 테스트하라.

 

int equal(struct point *p1, struct point *p2)

 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>

struct point { // 점의 좌표 표현하는 구조체
	int x, y;
};
int equal(struct point* p1, struct point* p2) { // 두 점이 같은 좌표를 가지는지 판단하는 함수
	if (p1->x == p2->x && p1->y == p2->y) { // 구조체 포인터를 통하여 접근할 때는 p->x1과 같이 접근
		return 1;
	}
	return 0;
}
int main() {
	point p1, p2;

	printf("1번 점의 좌표 입력(x1,y1) : ");
	scanf("%d,%d", &p1.x, &p1.y);
	printf("2번 점의 좌표 입력(x2,y2) : ");
	scanf("%d,%d", &p2.x, &p2.y);

	if (equal(&p1, &p2) == 1)
		printf("(%d, %d) == (%d, %d)\n", p1.x, p1.y, p2.x, p2.y);
	else
		printf("(%d, %d) != (%d, %d)\n", p1.x, p1.y, p2.x, p2.y);

	return 0;
}

 

1번 점의 좌표 입력(x1,y1) : 1,2
2번 점의 좌표 입력(x2,y2) : 3,5
(1, 2) != (3, 5)