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
#include <stdio.h> #include "bunsuu.h" _bunsuu::_bunsuu(int bunshi, int bunbo) { if (bunbo == 0) throw "bunbo can't set 0"; if (bunshi == 0) bunbo = 1; if (bunbo < 0) { bunbo = -bunbo; bunshi = -bunshi; } int kouyaku = saidai_kouyaku_suu(bunshi, bunbo); this->bunbo = bunbo / kouyaku; this->bunshi = bunshi / kouyaku; } int _bunsuu::saidai_kouyaku_suu(int big, int small) { while (small != 0) { int temp = small; small = big % small; big = temp; } return big; } void _bunsuu::show() { if (bunbo == 1) printf("%d", bunshi); else printf("%d/%d", bunshi, bunbo); }