www.pudn.com > 文件恢复及修补 C 语言源程序.zip > LEJLIB.C


/* The following are Microsoft c libraries to be included at compile time */ 
#include                               /* std I/O library on c disk */ 
 
/* The following are global definitions to be used throughout the program */ 
#define EOF -1                                  /* logical ^Z EOF ends copy */ 
#define YES 1 
#define NO 0 
#define NL '\n' 
#define EOS '\0' 
#define MAXLINE 1024 
#define SIZ 9 
#define FF '\014'                               /* Form feed */ 
#define BELL '\007' 
#define DIGMAX 10 
#define CLEAR 12                                /* ASCII form feed */ 
 
/* The following are external variables that can be used in any functions */ 
int inword; 
int nl; 
int nw; 
int nc; 
int ndigits[DIGMAX]; 
int c; 
 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
main()                                /*                                    */ 
{ 
 
        int len;                        /* current line length */ 
        int maxlen;                        /* max length so far */ 
        char line[MAXLINE]; 
        char save[MAXLINE]; 
        char lineupper[MAXLINE]; 
        char linelower[MAXLINE]; 
        char answer[MAXLINE]; 
 
        printf(FF); 
        printf("\n"); 
        printf("\n"); 
        printf("Jordan's function test ...\n"); 
        printf("\n"); 
 
 
 
        inchar("Input password .....\b\b\b\b\b",5,answer); 
        printf("\n\nThe password is %s\n", answer); 
 
} 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
redirect()                                     /* redirect keybd to CRT */ 
{ 
        int c; 
 
        printf("\n"); 
        printf("\n"); 
        printf("Jordan's redirect function...\n"); 
        printf("\n"); 
        printf("Input numbers, space characters, etc...\n"); 
        printf("?"); 
 
 
        while ((c = getchar()) != EOF)         /* get char until EOF found */ 
                putchar(c);                    /* send char to scrn: */ 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
 
wordcnt()                             /* count lines, words, chars in input */ 
{ 
 
        double c, nc, nl, nw;                         /* counters are float */ 
        int inword;                                   /* logical is interger */ 
 
        inword = NO; 
        nl = nw = nc = 0.0;                           /* initialize variables */ 
 
        printf("\n"); 
        printf("\n"); 
        printf("Jordan's word count function ...\n"); 
        printf("\n"); 
        printf("Input text for character, word and line count search.\n"); 
        printf("\n"); 
 
 
        while ((c = getchar()) != EOF) { 
                nc = nc + 1; 
 
                if (c == '\n')                         /* if c=LF then inc nl */ 
                     nl = nl + 1; 
 
                if (c == ' ' || c == '\n' || c == '\t')  /* if char is space, */ 
                     inword = NO;                       /* LF or TAB then it */ 
                                                        /* cannot be a word */ 
 
                else if (inword == NO) {                /* otherwise it is a */ 
                     inword = YES;                      /* word */ 
                     nw = nw + 1; 
                } 
        } 
        printf("\n"); 
        printf("\n"); 
        printf("Number of characters = "); 
        printf("%.0f\n",nc);                       /* prt float # with no dec */ 
        printf("Number of words = "); 
        printf("%.0f\n",nw); 
        printf("Number of lines = "); 
        printf("%.0f\n",nl); 
 
        printf("\n"); 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
maxline()                       /* gets maximum length line in file */ 
{ 
        int len;                        /* current line length */ 
        int maxlen;                        /* max length so far */ 
        char line[MAXLINE]; 
        char save[MAXLINE]; 
 
        maxlen = 0; 
 
        printf("\n"); 
        printf("\n"); 
        printf("Jordan's get maximum length line function ...\n"); 
        printf("\n"); 
        printf("Input text for maximum line length search.\n"); 
        printf("\n"); 
 
 
        while ((len = getline(line, MAXLINE)) > 0)   /* getline defined below */ 
                if (len > maxlen){ 
                        maxlen = len; 
                        copy(line, save);              /* function from below */ 
                } 
        if (maxlen > 0) 
            printf("%s", save); 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
getline(s, lim)                 /* getline into s, return length of s */ 
char s[]; 
int lim; 
{ 
        int c,i; 
 
        for (i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i) 
                s[i] = c; 
        if (c == '\n'){ 
                s[i] = c; 
                ++i; 
        } 
        s[i] = '\0'; 
        return(i); 
} 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
copy(s1, s2)                    /* copy s1 to s2; assume s2 big enough */ 
char s1[], s2[]; 
{ 
        int i; 
 
        i = 0; 
        while (s2[i] = s1[i] != EOS) 
                ++i; 
} 
 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
power(x, n)                         /* raise x to n-th power; n > 0 */ 
int x, n; 
{ 
        int i, p; 
 
        p = 1; 
        for (i=1; i<=n; ++i) 
            p=p*x; 
        return(p); 
} 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
digicnt()                                /* count digits, spaces, others */ 
{ 
        int c, i, nspaces, nother;       /* integers */ 
        int ndigits[DIGMAX];             /* integer array */ 
 
        nspaces = nother = 0; 
        for (i= 0; i < 10; ++i) 
                ndigits[i] = 0;          /* initialize arry to 0 */ 
 
 
        printf("\n"); 
        printf("\n"); 
        printf("Jordan's number counter...\n"); 
        printf("\n"); 
        printf("Input numbers, space characters, etc...\n"); 
        printf("?"); 
 
        while ((c = getchar()) != EOF) 
                if (c >= '0' && c <= '9') 
                      ++ndigits[c]; 
                else if (c == ' ' || c == '\n' || c == '\t') 
                        ++nspaces; 
                else 
                        ++nother; 
 
 
 
        printf("\n"); 
        printf("\n"); 
        printf("Jordan's number counter results...\n"); 
        printf("\n"); 
        printf("Number of digits = "); 
        for (i=0; i < 10; ++i) 
               printf(" %d", ndigits[i]); 
        printf("\nNumber of spaces = %d,  Other = %d\n", 
               nspaces, nother); 
        printf("\n"); 
} 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
strlen(s)                               /*  return length of string s */ 
char s[]; 
{ 
        int i; 
 
        i = 0; 
        while (s[i] != '\0') 
                ++i; 
        return(i); 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
atoi(s)                         /* convert numeric string into numeric equiv */ 
char s[]; 
{ 
 
        int i; 
        int n;                /* need double precision for no > 32K */ 
 
        n = 0; 
        for (i = 0; s[i] >= '0' && s[i] <= '9'; ++i) 
              n = 10 * n + s[i] - '0'; 
 
        return(n); 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
upper(s)                        /*  converts lower case to upper */ 
char s[]; 
{ 
        int i, j; 
 
        for (i = j = 0; s[i] != '\0'; i++,j++)          /* inc i after using it */ 
        if (s[i] >= 'a' && s[i] <= 'z')             /* if upeer case do next */ 
                s[j] = s[i] + 'A' - 'a'; 
        else 
                s[j] = s[i]; 
 
        s[++j] = '\0'; 
 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
lower(s)                        /*  converts upper case to lower */ 
char s[]; 
{ 
        int i, j; 
 
        for (i = j = 0; s[i] != '\0'; i++)        /* inc i after using it */ 
        if (s[i] >= 'A' && s[i] <= 'Z')           /* if upeer case do next */ 
                s[j++] = s[i] + 'a' - 'A';          /* convert ASCII value */ 
        else 
                s[j++] = s[i];                      /* only copy as is */ 
 
        s[j++] = '\0'; 
 
} 
 
 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
isdigit(c)                      /* test character to see if it is a digit */ 
int c; 
{ 
        int isdigit; 
 
        return(isdigit = c >= '0' && c<= '9');  /* isdigit true = 1 */ 
                                                /* isdigit false = 0 */ 
 
} 
 
 
/* ---------------------------------------------------------------------- */ 
 
 
stuf(s)                         /* stuffs string s with letters A-I */ 
char s[]; 
{ 
        int i,x; 
 
        for(x = 65, i = 0; i < SIZ; ++x, ++i) 
                s[i] = x; 
} 
 
 
/* ---------------------------------------------------------------------- */ 
                         /* prints mid string 'stradr' beggining at  */ 
                         /* 'start' and going for 'num' characters */ 
mid(stradr,start,num) 
int start, num; 
char stradr[]; 
{ 
        int i; 
 
        for(i = 0; i < num; ++i) 
                printf("%c", stradr[start + i]); 
} 
 
 
/* ---------------------------------------------------------------------- */ 
                                /*  prompts for answer of 'num' chars */ 
                                /*  then returns 'answer' to main */ 
 
inchar(prompt,num,answer) 
char *prompt, answer[]; 
int num; 
{ 
        int j; 
 
        printf("\n%s", prompt); 
 
        for(j = 0; j < num; ++j) 
                answer[j] = getchar(); 
        answer[num] = EOS; 
 
        return(answer); 
} 
 
 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */ 
/* ---------------------------------------------------------------------- */