c언어 + gcd lcm + 최대공약수 최소공배수
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define __x__ *
typedef struct gcd_and_lcm {
int value_1;
int value_2;
int gcd; // greatest common divisor
int lcm; // least common divisor
}gcd_lcm;
// function prototype
void __error__(char* errorMessage);
void __init__( gcd_lcm ** stu );
void __input__( gcd_lcm ** stu );
void __gcd__( gcd_lcm ** stu );
void __lcm__( gcd_lcm ** stu );
int main(void) {
gcd_lcm* stu = NULL;
__init__(&stu);
__input__(&stu);
__gcd__(&stu);
__lcm__(&stu);
printf("gcd(%d, %d) = %d \n", stu->value_1, stu->value_2, stu->gcd );
printf("lcm(%d, %d) = %d \n", stu->value_1, stu->value_2, stu->lcm );
return 0;
}
void __error__(char* errorMessage) {
fprintf(stderr, "%s", errorMessage);
exit(0); // END
} // end of __error__ function
void __init__( gcd_lcm** stu ) {
(*stu) = (gcd_lcm*)malloc(sizeof(gcd_lcm));
if ( (*stu) == NULL ) {
__error__("malloc error!!");
} else { // (*stu) != NULL
memset( (*stu), 0, sizeof(stu) );
}
} // end of __init__ function
void __input__( gcd_lcm** stu ) {
int temp;
printf("value_1 input : ");
scanf( "%d", &((*stu)->value_1) );
printf("value_2 input : ");
scanf( "%d", &((*stu)->value_2) );
// value2 를 기준으로 하겠다.
if ( (*stu)->value_2 < (*stu)->value_1 ) {
temp = (*stu)->value_2;
(*stu)->value_2 = (*stu)->value_1;
(*stu)->value_1 = temp;
}
} // end of __input__ function
void __gcd__( gcd_lcm** stu ) {
if ( (*stu)->value_2 % (*stu)->value_1 == 0 ) {
(*stu)->gcd = (*stu)->value_1;
} else { // (*stu)->value2 % (*stu)->value1 != 0
(*stu)->gcd = 1;
}
} // end of __gcd__ function
void __lcm__( gcd_lcm** stu ) {
(*stu)->lcm = (*stu)->gcd __x__ ( (*stu)->value_1 / (*stu)->gcd ) __x__ ( (*stu)->value_2 / (*stu)->gcd );
} // end of __lcm__ function
'언어 > c언어' 카테고리의 다른 글
별찍기 관련 c언어 (0) | 2016.06.06 |
---|---|
test (0) | 2016.06.02 |
중복없이 6개 출력 (0) | 2016.05.22 |
구간 소수판별 (0) | 2016.05.22 |
네이버 지식인 c언어 구구단 답변 준것 (0) | 2016.05.17 |