검색결과 리스트
진법변환에 해당되는 글 1건
- 2009.10.03 과제#4 배열과 문자열
글
과제#4 배열과 문자열
언어로그/C/C++
2009. 10. 3. 21:50
1. 윤년계산
- 해당년도의 1월 1일부터 몇 번째 날인지를 계산하고 그 결과를 출력하는 프로그램
- 입력은 년월일 형태로 형태로 처리 (예: 2009 10 15)
- 2월의 경우 윤달여부를 판별하여 일수를 계산한다.
- 윤년은 4로 나누어 떨어지는 해이며, 그중 100으로 나누어 떨어지는 해는 평년. 하지만 400으로 나누어 떨어지는 해는 다시 윤년임
입력
오늘 날짜는? (예: 2009.10.15 -> 2009 10 15) : 2008 3 1
출력
현재 61째 되는 날입니다.
문제해결
- 매년 매월의 날수는 동일하지만, 윤년의 2월은 1일이 더 많다는 것에 주의!
코드
/*
입력 : 년 월 일 (2009 10 15)
출력 : 해당년도 1월 1일 부터 몇번 째 날인지를 출력
조건 : 윤년 고려
*/
#include <stdio.h>
// 1~12월까지 날수를 담는 배열 , 인덱스 0 는 무시.
int month_days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int IsEmptyYear(int year);
int HowManyDays(int year, int month, int day);
int main (int argc, char const* argv[])
{
int year, month, day, days;
printf("오늘 날짜는 ? (예: 2009.10.15 -> 2009.10.15) : ");
scanf("%d %d %d", &year, &month, &day);
days = HowManyDays(year, month, day);
printf("\n 현재 %d째 되는 날입니다. \n", days);
return 0;
}
// 윤년인지 여부 반환
int IsEmptyYear(int year) {
if ( (year % 4 == 0) && ((year % 100) || (year % 400 == 0)) ) {
return 1;
} else {
return 0;
}
}
int HowManyDays(int year, int month, int day) {
int i, days;
days = 0;
for (i = 1; i <= month - 1; i++) {
days += month_days[i];
}
days += day;
// 윤년이면 1 을 더함
if (IsEmptyYear(year)) {
days += 1;
}
return days;
}
2. 진법 변환
- 임의의 정수와 변환될 진법을 입력 받아 출력하는 프로그램 작성.
입력
변환될 수(decimal)는 ? 12345
변환할 진법( base)은 ? 16
출력
변환된 수 = 1E240
문제해결
- 십진수를 진법수로 나눈 나머지들의 역순이 해당 진법으로 변경된 수이다. 이 때 9진법 이상에서는 10부터는 알파벳 문자 A로 표기된다
코드
/*
입력 : 십진수, 변환할 진법
출력 : 변환된 수
조건 : 8진수, 16진수 변환문자 사용금지, 배열 사용
*/
#include <stdio.h>
#include <string.h>
#define MAX_SIZE 100
void base_conversion(char *num, int decimal, int base);
void reverse_str(char *s);
int main (int argc, char const* argv[])
{
int decimal, base;
char num[MAX_SIZE];
printf("변환할 수(decimal) 는? ");
scanf("%d", &decimal);
printf("변환할 진법(base)은 ? ");
scanf("%d", &base);
base_conversion(num, decimal, base);
printf("변환된 수 = %s \n", num);
return 0;
}
// 십진수 decimal을 base진수로 변환하여 문자배열 num에 담는 함수
void base_conversion(char *num, int decimal, int base) {
int i, share;
char *p = num;
i = 0;
while (decimal > 0) {
share = decimal % base;
if (share > 9) {
p[i++] = 'A' + share - 10;
} else {
p[i++] = '0' + share;
}
decimal /= base;
}
p[i] = '\0';
reverse_str(num);
}
// 문자열을 거꾸로 뒤집는 함수
void reverse_str(char *s) {
char ch, *pre, *rear;
pre = s;
rear = s + strlen(s) - 1;
while (pre < rear) {
ch = *pre;
*pre++ = *rear;
*rear-- = ch;
}
}
3. 배열에서 최대값과 최소값 구하기
- 정수 값을 갖는 배열에서 최대값과 최소값을 찾는 프로그램을 작성
- 배열의 초기값: 45, -12, 67, 30, 81, -3, 13, 74, 3, 20
- 최소값과 최대값의 위치, 최소값과 최소값의 위치 구하기.
입력
array[] = { 45, -12, 67, -30, 81, -3, 13, 74, 3, 20}
최대값 : 81, 위치 : 5
최소값: -30, 위치 : 4
출력
변환된 수 = 1E240
문제해결
- 배열에서 최대값, 최소값이 있는 인덱스를 알면 최대값과 최소값을 알수 있다.
코드
/*
입력 : 배열 초기값
출력 : 최소값과 최대값의 위치, 최소값과 최대값의 위치
*/
#include <stdio.h>
#define BUF_SIZE 10
void array_printf(int *arr, int size);
void max_min_finder(int *arr, int size, int *idx_max, int *idx_min);
int main (int argc, char const* argv[])
{
int array[BUF_SIZE] = { 45, -12, 67, -30, 81, -3, 13, 74, 3, 20 };
int max_index, min_index;
array_printf(array, BUF_SIZE);
max_min_finder(array, BUF_SIZE, &max_index, &min_index);
printf(" 최대값 : %d , 위치 : %d \n", array[max_index], max_index+1);
printf(" 최소값 : %d , 위치 : %d \n", array[min_index], min_index+1);
return 0;
}
void array_printf(int *arr, int size) {
int i;
printf("array[] = { ");
for (i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("} \n\n");
}
void max_min_finder(int *arr, int size, int *idx_max, int *idx_min) {
int i, max, min;
min = max = arr[0];
for (i = 0; i < size; i++) {
if (arr[i] > max) {
max = arr[i];
*idx_max = i;
}
if (arr[i] < min) {
min = arr[i];
*idx_min = i;
}
}
}
4. 대소문자 변환
- 임의의 문자열을 입력받아, 소문자는 대문자로, 대문자는 소문자로 변환하는 함수 작성 (제어문, 포인터 사용)
void change(char *to_str, const char *from_str);
문제해결
- 대문자 -> 소문자 / 소문자 -> 대문자로 변경하고, 이외의 문자의 그대로 복사가 되어야 한다.
코드
/*
입력 : 임의의 문자열
출력 : 소문자는 대문자로, 대문자는 소문자로 변환된 문자열
조건 : 제어문과 포인터 사용
*/
#include <stdio.h>
#define MAX_SIZE 100
void change(char *to_str, const char *from_str);
int main (int argc, char const* argv[])
{
char to_str[MAX_SIZE], from_str[MAX_SIZE];
printf("문자열 입력 : ");
fgets(from_str, MAX_SIZE, stdin);
change(to_str, from_str);
printf("변환된 문자열 : %s", to_str);
return 0;
}
// 대소문자 상호 변환
void change(char *to_str, const char *from_str) {
while (*from_str != '\0') {
if (*from_str >= 'a' && *from_str <= 'z') {
*to_str++ = *from_str++ - ('a' - 'A');
} else if (*from_str >= 'A' && *from_str <= 'Z') {
*to_str++ = *from_str++ + ('a' - 'A');
} else {
*to_str++ = *from_str++;
}
}
}
'언어로그 > C/C++' 카테고리의 다른 글
| C 프로그램 실행과정 (0) | 2011.02.22 |
|---|---|
| C 언어란? (0) | 2011.02.22 |
| 비트필드 (0) | 2010.12.10 |
| 함수의 이해 / 데이터 전달 (0) | 2010.12.10 |
| 과제 #3 함수, 포인터의 사용 (0) | 2010.12.10 |
| 과제#4 배열과 문자열 (0) | 2009.10.03 |
3756101_1_how_many_day.c