📄 function.c
字号:
/***************************************
Filename : function.c
Version : Formula Language V1.1
Author : Shen Tu Hongnan
Modify :
Date : 2001/08/26
Remark :
***************************************/
#define UNIX
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <time.h>
#ifdef UNIX
#include <sys/types.h>
#include <sys/times.h>
#endif
#include "function.h"
//#include "d:\mds\function.h"
/*************************************************************************/
#if defined(__alpha) && defined(__osf__)
typedef unsigned int UINT4;
#else
typedef unsigned long UINT4;
#endif
/* 是否闰年 */
#define IsLeapYear(x) (((x)%4 == 0 && (x)%100 != 0) || ((x)%400 == 0))
/* 公用函数 */
char *IntToStr(int n, int len, char *str);
int StrToInt(char **str);
struct tm *TimeToTm(time_t t, struct tm *ptm);
time_t TmToTime(struct tm *ptm);
/* 可调用的函数数组下标值,不得改变此值 */
int MaxFun = Total_of_Fun - 1;
/*************************************************************************
Function: IntToStr()
Purpose : 将正整数 n 转换成 len 位字符串,不足前面加 0
Input : 正整数 n, 转换后长度 len, 存放串的字符数组 str
Return : str
Modify : str 数组的内容
Remark : str size >= len,返回的字符串没有用空字符结尾!
*************************************************************************/
char *IntToStr(int n, int len, char *str)
{
char *cp;
cp = str + len;
while (cp != str)
{
*(--cp) = n%10 + 48;
n = n/10;
}
return (str);
}
/*************************************************************************
Function: StrToInt()
Purpose : 将字符串转换成正整数;
同时修改字符串指针的位置到下个数字串的开始位置
Input : 数字字符串 str 的指针
Return : 正整数
Modify : 无
Remark : 字符串不必用空字符结束
*************************************************************************/
int StrToInt(char **str)
{
int n;
char *cp;
cp = *str;
if (isdigit(*cp))
{
n = *cp++ - 48;
while (isdigit(*cp))
n = 10*n + *cp++ - 48;
*str = ++cp;
return (n);
}
else
return (0);
}
/*************************************************************************
Function: TimeToTm()
Purpose : 将 time_t 格式的 t 转换成 tm 结构
Input : time_t 格式的 t, tm 结构指针 ptm
Return : tm 结构指针 ptm
Modify : 结构指针 tm 的内容
Remark : tm 结构中年份已为四位数,不用再加 1900
*************************************************************************/
struct tm *TimeToTm(time_t t, struct tm *ptm)
{
long day;
int mdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
/* 时区调整 */
t += TIME_AREA;
if (t < 0) t = 0;
/* 计算日数 */
day = floor(t/86400);
t -= 86400*day;
/* 时 */
ptm->tm_hour = floor(t/3600);
t -= 3600*ptm->tm_hour;
/* 分及秒 */
ptm->tm_min = floor(t/60);
ptm->tm_sec = t - 60*ptm->tm_min;
/* 初始年月 */
ptm->tm_year = 1970;
ptm->tm_mon = 0;
/* 计算年 */
while (1)
if ( IsLeapYear(ptm->tm_year) )
{
if (day >= 366)
{
ptm->tm_year++;
day -= 366;
}
else
{
mdays[1] = 29;
break;
}
}
else
{
if (day >= 365)
{
ptm->tm_year++;
day -= 365;
}
else
{
mdays[1] = 28;
break;
}
}
/* 月及日 */
while (1)
if (day >= mdays[ptm->tm_mon])
day -= mdays[ptm->tm_mon++];
else
{
ptm->tm_mday = day + 1;
break;
}
return (ptm);
}
/*************************************************************************
Function: TmToTime()
Purpose : 将 tm 结构转换成 time_t
Input : tm 结构指针 ptm
Return : time_t
Modify : 无
Remark : tm 结构中年份应为四位数,即已加 1900
*************************************************************************/
time_t TmToTime(struct tm *ptm)
{
int i;
long day;
int mdays[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
/* 计算日期的年日 */
if ( IsLeapYear(ptm->tm_year) )
mdays[1] = 29;
else
mdays[1] = 28;
ptm->tm_yday = ptm->tm_mday;
for (i = 0; i < ptm->tm_mon; i++)
ptm->tm_yday += mdays[i];
/* 计算日数 */
day = ptm->tm_yday - 1;
for (i = 1970; i < ptm->tm_year; i++)
if ( IsLeapYear(i) )
day += 366;
else
day += 365;
/* 计算秒数 */
return (60*(60*(24*day + ptm->tm_hour) + ptm->tm_min) + ptm->tm_sec - TIME_AREA);
}
/*************************************************************************
Function: Not(x)
Purpose : 返回逻辑非 !(x)
Modify : 堆栈
Remark :
*************************************************************************/
int Not(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = !(mysp->num);
return (0);
}
/*************************************************************************
Function: Abs(x)
Purpose : 取绝对值
Modify : 堆栈
Remark :
*************************************************************************/
int Abs(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = fabs(mysp->num);
return (0);
}
/*************************************************************************
Function: Ceil(x)
Purpose : 向上取整
Modify : 堆栈
Remark :
*************************************************************************/
int Ceil(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = ceil(mysp->num);
return (0);
}
/*************************************************************************
Function: Dec(x)
Purpose : 取小数部分
Modify : 堆栈
Remark :
*************************************************************************/
int Dec(PACK *pp)
{
double integer;
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = modf(mysp->num, &integer);
return (0);
}
/*************************************************************************
Function: Floor(x)
Purpose : 向下取整
Modify : 堆栈
Remark :
*************************************************************************/
int Floor(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = floor(mysp->num);
return (0);
}
/*************************************************************************
Function: Gradin(x, a, b)
Purpose : x 位于区间(a,b)中的值,b = 0 表示区间右端点为无穷大
Modify : 堆栈
Remark :
*************************************************************************/
int Gradin(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
pp->sp -= 2;
mysp = (pp->sp) - 1;
/* x 在 a 的左侧结果为 0 */
if (mysp->num <= (mysp+1)->num)
{
mysp->num = 0;
return (0);
}
/* b 为无穷大结果为 x-a */
if ((mysp+2)->num == 0)
{
mysp->num = mysp->num - (mysp+1)->num;
return (0);
}
/* 结果为 x>b ? b-a : x-a */
mysp->num = (mysp->num >= (mysp+2)->num) ? (mysp+2)->num - (mysp+1)->num
: mysp->num - (mysp+1)->num;
return (0);
}
/*************************************************************************
Function: Max(x, y)
Purpose : 取较大值
Modify : 堆栈
Remark :
*************************************************************************/
int Max(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
(pp->sp)--;
mysp = (pp->sp) - 1;
if (mysp->num < (mysp+1)->num)
mysp->num = (mysp+1)->num;
return (0);
}
/*************************************************************************
Function: Min(x, y)
Purpose : 取较小值
Modify : 堆栈
Remark :
*************************************************************************/
int Min(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
(pp->sp)--;
mysp = (pp->sp) - 1;
if (mysp->num > (mysp+1)->num)
mysp->num = (mysp+1)->num;
return (0);
}
/*************************************************************************
Function: Mod(x, y)
Purpose : 取整除 x/y 的余数
Modify : 堆栈
Remark :
*************************************************************************/
int Mod(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
(pp->sp)--;
mysp = (pp->sp) - 1;
mysp->num = fmod(mysp->num, (mysp+1)->num);
return (0);
}
/*************************************************************************
Function: Pow(x, y)
Purpose : 计算 x 的 y 次方
Modify : 堆栈
Remark :
*************************************************************************/
int Pow(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
(pp->sp)--;
mysp = (pp->sp) - 1;
mysp->num = pow(mysp->num, (mysp+1)->num);
return (0);
}
/*************************************************************************
Function: Round(x, n)
Purpose : 按指定位数四舍五入
Modify : 堆栈
Remark :
*************************************************************************/
int Round(PACK *pp)
{
int k;
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
(pp->sp)--;
mysp = (pp->sp) - 1;
k = pow(10, (int)((mysp+1)->num));
mysp->num = (mysp->num >= 0) ? floor(k * mysp->num + 0.5) / k
: -floor(-k * mysp->num + 0.5) / k;
return (0);
}
/*************************************************************************
Function: Asc(S)
Purpose : 求字符串首字符的 ASCII 码
Modify : 堆栈
Remark :
*************************************************************************/
int Asc(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = (int) *(mysp->str);
mysp->type = NUMBER;
return (0);
}
/*************************************************************************
Function: Atol(S)
Purpose : 将字符串转换成长整数
Modify : 堆栈
Remark :
*************************************************************************/
int Atol(PACK *pp)
{
FIELD *mysp;
/* 调整返回堆栈指针并取得参数位置 */
mysp = (pp->sp) - 1;
mysp->num = atol(mysp->str);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -