C语言写整数类
简介:本文讲解,如何用C语言来写整数类。
Integer.h
#ifndef _Integer_h_ #define _Integer_h_ int IntMax(int x, int y); // IntMax 函数求两个整数的最大值。 int IntMin(int x, int y); // IntMin 函数求两个整数的最小值。 int Gcd(int x, int y); // Gcd 函数求两个整数的最大公约数。 int Lcm(int x, int y); // Lcm 函数求两个整数的最小公倍数。 void IntSwap(int *x, int *y); // IntSwap 函数交换两个整型变量的值。 #endif
Integer.c
int IntMax(int x, int y) { return x > y ? x : y; } int IntMin(int x, int y) { return x > y ? y : x; } int Gcd(int x, int y) { x = abs (x); y = abs (y); if (x == 0 && y == 0) { return 1; } else { return y == 0 ? x : Gcd (y, x % y); } } int Lcm(int x, int y) { if (x == 0 || y == 0) { return 0; } else { return abs ((x / Gcd (x, y) * y)); } } void IntSwap(int *x, int *y) { int t = *x; *x = *y; *y = t; }