文章中的Integer.h
在这篇文章中C语言写整数类(Integer)
。
简介:在模板与泛型还没有诞生的时候,怎么用C语言实现相似的功能了。
#ifndef _Element_h_
#define _Element_h_
#include "Integer.h"
typedef int ELEMENT;// 通过typedef来指定ELEMENT为需要的数据类型
/*
ElementInput 函数从键盘输入元素保存到内存地址 x 处。
ElementOutput 函数将内存地址 x 处的元素输出到屏幕上。
注:输入和输出均采用采用十进制的形式。
ElementGt 函数判断指针 x 所指元素大于指针 y 所指元素。
ElementGe 函数判断指针 x 所指元素大于等于指针 y 所指元素。
ElementLt 函数判断指针 x 所指元素小于指针 y 所指元素。
ElementLe 函数判断指针 x 所指元素小于等于 指针 y 所指元素。
ElementEq 函数判断指针 x 所指元素等于指针 y 所指元素。
ElementNe 函数判断指针 x 所指元素不等于指针 y 所指元素。
注:以上判断函数,若条件成立,则函数值为 1(真),否则为 0(假)。
*/
void ElementInput(ELEMENT *x);
void ElementOutput(const ELEMENT *x);
int ElementGt(const ELEMENT *x, const ELEMENT *y);
int ElementGe(const ELEMENT *x, const ELEMENT *y);
int ElementLt(const ELEMENT *x, const ELEMENT *y);
int ElementLe(const ELEMENT *x, const ELEMENT *y);
int ElementEq(const ELEMENT *x, const ELEMENT *y);
int ElementNe(const ELEMENT *x, const ELEMENT *y);
#endif
void ElementInput(ELEMENT *x)
{
scanf (" %d", x);
}
void ElementOutput(const ELEMENT *x)
{
printf("%d", *x);
}
int ElementGt(const ELEMENT *x, const ELEMENT *y)
{
return *x - *y > 0;
}
int ElementGe(const ELEMENT *x, const ELEMENT *y)
{
return (*x - *y) >= 0;
}
int ElementLt(const ELEMENT *x, const ELEMENT *y)
{
return *x - *y < 0;
}
int ElementLe(const ELEMENT *x, const ELEMENT *y)
{
return *x - *y <= 0;
}
int ElementEq(const ELEMENT *x, const ELEMENT *y)
{
return *x == *y;
}
int ElementNe(const ELEMENT *x, const ELEMENT *y)
{
return *x != *y;
}
void ElementSwap(ELEMENT *x, ELEMENT *y)
{
ELEMENT temp;
temp = *x;
*x = *y;
*y = temp;
}