Last active
December 17, 2015 08:28
-
-
Save dyama/28cdf8fb41014050ca25 to your computer and use it in GitHub Desktop.
Ratinal Test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <math.h> | |
/* 有理数型 */ | |
struct Rational | |
{ | |
int numer; /* 分子 */ | |
int denom; /* 分母 */ | |
}; | |
typedef struct Rational Rational; | |
/* スワップ */ | |
void swap(int* a, int* b) | |
{ | |
int x = *a; | |
*a = *b; | |
*b = x; | |
return; | |
} | |
/* 最大公約数を求める */ | |
/* (ユークリッドの互除法) */ | |
int greatest_common_divisor(int n, int m) | |
{ | |
int a; | |
if (n > m) { | |
swap(&n, &m); | |
} | |
for (;m % n != 0;) { | |
if (n > m) { | |
swap(&n, &m); | |
} | |
a = m; | |
m = n; | |
n = a % n; | |
} | |
return abs(n); | |
} | |
/* 約分 */ | |
void normal(Rational* val) | |
{ | |
int f = greatest_common_divisor(val->numer, val->denom); | |
if (f > 0) { | |
val->numer /= f; | |
val->denom /= f; | |
} | |
return; | |
} | |
/* 自然数を有理数型にして返す */ | |
Rational to_r(int n) | |
{ | |
Rational res = {n, 1}; | |
return res; | |
} | |
/* 有理数型を浮動小数点数にして返す */ | |
double to_f(Rational n) | |
{ | |
return n.numer / n.denom; | |
} | |
/* 有理数同士の加算 */ | |
Rational add(Rational a, Rational b) | |
{ | |
// 通分 | |
Rational res = a; | |
a.numer *= b.denom; | |
a.denom *= b.denom; | |
b.numer *= res.denom; | |
b.denom *= res.denom; | |
// 計算 | |
res.denom = a.denom; | |
res.numer = a.numer + b.numer; | |
// 約分 | |
normal(&res); | |
return res; | |
} | |
/* 有理数同士の減算 */ | |
Rational sub(Rational a, Rational b) | |
{ | |
// 通分 | |
Rational res = a; | |
a.numer *= b.denom; | |
a.denom *= b.denom; | |
b.numer *= res.denom; | |
b.denom *= res.denom; | |
// 計算 | |
res.denom = a.denom; | |
res.numer = a.numer - b.numer; | |
// 約分 | |
normal(&res); | |
return res; | |
} | |
/* 有理数同士の乗算 */ | |
Rational mult(Rational a, Rational b) | |
{ | |
Rational res; | |
// 計算 | |
res.denom = a.denom * b.denom; | |
res.numer = a.numer * b.numer; | |
// 約分 | |
normal(&res); | |
return res; | |
} | |
/* 有理数同士の除算 */ | |
Rational div(Rational a, Rational b) | |
{ | |
Rational res = a; | |
// 計算 | |
res.denom *= b.numer; | |
res.numer *= b.denom; | |
// 約分 | |
normal(&res); | |
return res; | |
} | |
int main(int argc, char const* argv[]) | |
{ | |
// 約分テスト | |
Rational value = {1029, 1071}; | |
printf("%d/%d => ", value.numer, value.denom); | |
normal(&value); | |
printf("%d/%d\n", value.numer, value.denom); | |
// 計算テスト | |
Rational val1 = { 1, 2 }; // 1/2 | |
Rational val2 = { 1, 3 }; // 1/3 | |
Rational ans = add(val1, val2); | |
printf("ans = %d/%d\n", ans.numer, ans.denom); | |
ans = sub(val1, val2); | |
printf("ans = %d/%d\n", ans.numer, ans.denom); | |
ans = mult(val1, val2); | |
printf("ans = %d/%d\n", ans.numer, ans.denom); | |
ans = div(val1, val2); | |
printf("ans = %d/%d\n", ans.numer, ans.denom); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment