这个自定义库包含两部分,tools.h和tools.c,前者声明自定义函数以及预定义,后者包含前者,并且实现函数。
tools.h
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#include<float.h>
#include<math.h>
#include<time.h>
#include<ctype.h>
#include<string.h>
#include<stdarg.h>
typedef enum {P_IN,P_SIDE,P_CORNER,P_OUT} in_type;
typedef enum {NO_ROOT,ROOT_OK} er_type;
typedef enum {DATA_OK,TOO_SMALL,TOO_BIG,NO_INPUT=-1} error_t;
typedef enum {false,true} loob;
typedef char* string;
typedef FILE* stream;
//typedef char* va_list;
typedef char* va_start;
#define NAME "kingwords"
/*#define UNIX*/
#define CLASS "Program starting....."
#define ENATURAL 2.71828182845990451
#define GRAVITY 9.80665
#define PI 3.1415927
#define strequal(a,b) !strcmp(a,b)
#define strnequal(a,b) !strncmp(a,b,n)
/*#define PRN */
void fbanner(stream sout);
void banner(void) { fbanner(stdout); };
void bye(void);
void clearscreen(void) {fprintf(stderr,"\f"); };
void clean_and_log(stream sin,stream aout);
void cleanline(stream sin);
void delay(int seconds);
void swap(double *fp1,double *fp2);
double average(double x[],int n);
int error(void);
loob y_n(void);
int evenly(int range);
void when(char date[],char hour[]);
void fatal(string format, ...);
void hold(void);
char menu_c(string title,int n,const string menu[]);
int munu_i(string title,int n,const string menu[]);
string oclock(char hour[]);
double randy(void);
/*int round(double d);*/
void say(string format, ...);
//int strequal(string s1,string s2);
//int strnequal(string s1,string s2,int n);
string today(char date[]);
tools.c
#include"tools.h"
void fbanner(stream sout)
{
char date[16],time[9];
when(date,time);
fprintf(sout,"\n-----------------------------------------------------");
fprintf(sout,"\n\t %s \n\t %s \n\t %s %s\n",NAME,CLASS,date,time);
fprintf(sout,"-----------------------------------------------------\n");
}
void bye(void) //10
{
fputs("\n\nIt is normal over!\n",stderr);
}
void clean_and_log(stream sin,stream sout)
{
char ch;
do{
ch=fgetc(sin);
fputc(ch,sout);
}while(!feof(sin) && ch != '\n');
}
void cleanline(stream sin)
{
char ch;
do{}while(!feof(sin) && ch!='\n');
}
void delay(int seconds)
{
time_t goal=time(NULL)+seconds; /*add seconds to current time*/
do{ /*nothing */
}while(time(NULL)<goal);
}
void swap(double *fp1,double *fp2)
{
double swapper=*fp1;
*fp1=*fp2;
*fp2=swapper;
}
int error(void)
{
printf("\nError!\n\a\a\n");
}
double average(double x[],int n)
{
double sum;
int k;
for(sum=k=0;k<n;++k){
printf("\nx[%i]=%.2f",k,x[k]);
sum+=x[k];
}
return sum/n;
}
loob y_n(void)
{
char more;
puts("\nDo you want to continue (Y/N)?");
scanf(" %c",&more);
more=toupper(more);
if(more=='Y') return true;
}
int evenly(int range)
{
int num;
int limit;
limit=RAND_MAX-RAND_MAX%range;
do{
num=rand();
}while(num>=limit);
return num%range;
}
void when(char date[],char hour[])
{
time_t now;
string nowstring;
now=time(NULL);
nowstring=ctime(&now);
strncpy(date,nowstring,10);
strncpy(&date[10],&nowstring[19],5);
date[15]='\0';
strncpy(hour,&nowstring[11],8);
hour[8]='\0';
}
void fatal(string format, ...)
{
va_list vargs;
va_start(vargs,format);
vfprintf(stderr,format,vargs);
fprintf(stderr,"\a\a\n" );
hold();
exit(1);
}
void hold(void)
{
fputs("\n\n\nPress . and \"Enter\" to continue",stderr);
while(getchar()!='.');
(void)getchar();
}
char menu_c(string title,int n,const string menu[])
{
int k;
char ch;
printf("\n%s\n\n",title);
for(k=0;k<n;++k) printf("\t %s \n",menu[k]);
printf("\nEnter letter of desired item:");
scanf(" %c",&ch);
return ch;
}
int munu_i(string title,int n,const string menu[])
{
int ok,k,choice;
printf("\n%s\n\n",title);
for(;;){
for(k=0;k<n;++k) printf("\t %i. %s\n",k,menu[k]);
printf("\n Enter number of desired item:");
ok=scanf("%i",&choice);
if(ok==1 && 0<=choice && choice<n) break;
cleanline(stdin);
puts("\nIllegal choice or input error;try again.\n");
}
return choice;
}
string oclock(char hour[])
{
time_t now;
string nowstring;
now=time(NULL);
nowstring=ctime(&now);
strncpy(hour,&nowstring[11],8);
hour[8]='\0';
return hour;
}
double randy()
{
return rand()/(double)RAND_MAX;
}
/*
int round(double d)
{
if(d>=0.0) return (int)(d+0.5);
else return (int)floor(d+0.5);
}
*/
void say(string format,...)
{
va_list vargs;
va_start(vargs,format);
vfprintf(stderr,format,vargs);
fprintf(stderr,"\a\a\n");
}
string today(char date[])
{
time_t now;
string nowstring;
now=time(NULL);
nowstring=ctime(&now);
strncpy(date,nowstring,10);
strncpy(&date[10],&nowstring[19],5);
date[15]='\0';
return date;
}