一个小C库

#include <stdio.h>
#include <stdlib.h>

#define true 1
#define false 0
#define bool _Bool
#define _Bool bool
#define and    &&
#define and_eq &=
#define bitand &
#define bitor  |
#define compl  ~
#define not    !
#define not_eq !=
#define or     ||
#define or_eq  |=
#define xor    ^
#define xor_eq ^=
#define INT_MAX 2147483647
#define INT_MIN -2147483648
#define LONG_INT_MAX 9223372036854775807
#define LONG_INT_MIN -9223372036854775807
#define PI_2 3.14
#define PI_4 3.1415
#define PI_7 3.1415926
#define PI_13 3.1415926535897
#define PI_20 3.14159265358979655845
typedef const char* Errtype;
const Errtype InputError = "InputError";
const Errtype OutputError = "OutputError";
const Errtype IndexError = "IndexError";
const Errtype NameError = "NameError";
const Errtype TypeError = "TypeError";
const Errtype SyntaxError = "SyntaxError";
const Errtype IncludeError = "IncludeError";
const Errtype SystemError = "SystemError";
const Errtype UnicodeError = "UnicodeError";
const Errtype ValueError = "ValueError";
const Errtype Exit = "Exit";
struct Fraction{
    int Denominator;
    int Molecule;
};
struct ListNode{
    int Value;
    struct ListNode *Prev;
    struct ListNode *Next;
};

double FractionToDouble(struct Fraction Fract) {
    return (double)Fract.Denominator / (double)Fract.Molecule;
}

FILE* WriteTo(char *Filename){
    return freopen(Filename, "w", stdout);
}

FILE* ReadFrom(char *Filename){
    return freopen(Filename, "r", stdin);
}

void CloseFile(FILE *File){
    fclose(File);
}

void Int_Input(int* Val){
    int s = scanf("%d", Val);
    if(!s){
        printf("Scanned {errtype InputError}, trying to fixed……");
        Int_Input(Val);
    }
}

void String_Input(char* Val){
    int s = scanf("%s", Val);
    if(!s){
        printf("Scanned {errtype InputError}, trying to fixed……");
        String_Input(Val);
    }
}

void Int_Output(int Val){
    int s = printf("%d\n", Val);
    if(!s){
        printf("Scanned {errtype OutputError}, trying to fixed……");
        Int_Output(Val);
    }
}

void String_Output(char *Val){
    int s = printf("%s\n", Val);
    if(!s){
        printf("Scanned {errtype OutputError}, trying to fixed……");
        String_Output(Val);
    }
}

const void* Max(const void* Val1, const void* Val2){
    return Val1 > Val2 ? Val1 : Val2;
}

const void* Min(const void* Val1, const void* Val2){
    return Val1 < Val2 ? Val1 : Val2;
}

int Compare(const void* Val1, const void* Val2){
    if(Val1 == Val2){
        return 0;
    }
    return Val1 > Val2 ? 1 : -1;
}

void Reverse(int *List, int From, int To){
    for(int i=From;i<=To;i++){
        int tmp = List[i];
        List[i] = List[To-i];
        List[To-i] = tmp;
    }
}

long long QuickPower(long long Base, long long Index){
    long long result = 1;
    while(Index > 0){
        if(Index & 1) result = result * Base % LONG_INT_MAX;
        Index >>= 1;
        Base = Base * Base % LONG_INT_MAX;
    }
    return result;
}

void QuickSort(int *Number, int First, int Last) {
    int i, j, pivot;
    int temp;
    if (First<Last) {
        pivot = First;
        i = First;
        j = Last;
        while (i<j) {
            while (Number[i] <= Number[pivot] && i<Last)
                i++;
            while (Number[j]>Number[pivot])
                j--;
            if (i<j) {
                temp = Number[i];
                Number[i] = Number[j];
                Number[j] = temp;
            }
        }
        temp = Number[pivot];
        Number[pivot] = Number[j];
        Number[j] = temp;
        QuickSort(Number, First, j - 1);
        QuickSort(Number, j + 1, Last);
    }
}

void StringCopy(char *Str1, const char *Str2) {
    while ((*Str1++ = *Str2++)) ;
}

int StringLength(const char* Str){
    int i=0;
    while (*Str++ != 0){i++;}
    return i;
}

int StringCompare(const char *Str1, const char *Str2){
    int c1=0,c2=0,l1=StringLength(Str1),l2=StringLength(Str2);
    if(l1 == l2) {
        for(int i=0;i<l1;i++){
            if(Str1[i] > Str2[i])c1++;
            if(Str1[i] < Str2[i])c2++;
        }
        if(c1 > c2)return 1;
        if(c1 < c2)return -1;
        return 0;
    } else {
        if(l1>l2){
            return 1;
        }
        if(l1<l2){
            return -1;
        }
    }
    return INT_MAX;
}

int* Range(int Start, int Stop, int Step) {
    static int List[20000] = {0};
    int Tail = 0;
    for(int This=Start;This<Stop;This+=Step){
        List[Tail++] = This;
    }
    return List;
}

int In(int Value, int *List, int Size){
    for(int i=0;i<Size;i++){
        if(List[i] == Value){
            return true;
        }
    }
    return false;
}

struct ListNode* Range_ListNode(int Start, int Stop, int Step){
    struct ListNode *head = NULL;
    struct ListNode *prev = NULL;
    for(int i=Start; i<Stop; i+=Step){
        struct ListNode *This = (struct ListNode*)malloc(sizeof(struct ListNode));
        if(head == NULL){
            head = This;
        }
        if(prev != NULL){
            prev->Next = This;
        }
        This->Value = i;
        This->Prev = prev;
        This->Next = NULL;
        prev = This;
    }
    return head;
}

struct ListNode* Push_ListNode(struct ListNode *Head, int Value){
    struct ListNode *This = Head;
    while (This->Next != NULL) {
        This = This->Next;
    }
    struct ListNode *Next = (struct ListNode*)malloc(sizeof(struct ListNode));
    Next->Value = Value;
    Next->Next = NULL;
    Next->Prev = This;
    This->Next = Next;
    return Head;
}

struct ListNode* Pop_ListNode(struct ListNode *Head, int Index){
    struct ListNode *This;
    int idx=0;
    while (This->Next != NULL && idx < Index) {
        This = This->Next;
    }
    This->Prev->Next = This->Next;
    This->Next->Prev = This->Prev;
    free(This);
    return Head;
}

int Length_ListNode(struct ListNode *Head){
    struct ListNode *This = Head;
    int count=0;
    while (This->Next != NULL) {
        count++;
        This = This->Next;
    }
    return ++count;
}

int Find(int *List, int Value, int Size){
    int Flag = 0;
    while(List[Flag++]!=Value&&Flag<Size);
    return --Flag;
}

int Count(int *List, int Value, int Size){
    int count=0;
    for(int i=0;i<Size;i++){
        if(List[i] == Value){
            count++;
        }
    }
    return count;
}

int Count_ListNode(struct ListNode *Head, int Value){
    int count=0;
    struct ListNode *This = Head;
    while (This != NULL) {
        if(This->Value == Value){
            count++;
        }
        This = This->Next;
    }
    return count;
}

void List_Output(int *List, int From, int To){
    for(int i=From;i<To;i++){
        printf("%d ", List[i]);
    }
    printf("\n");
}

void Throw(Errtype Error){
    if(StringCompare(Error, IndexError)==0){
        String_Output("Error: Your Index Is Invaild.");
    } else if(StringCompare(Error, NameError)==0){
        String_Output("Error: Your Varible Name Is Invaild.");
    } else if(StringCompare(Error, InputError)==0){
        String_Output("Error: Your Stdin Is Invaild.");
    } else if(StringCompare(Error, OutputError)==0){
        String_Output("Error: Your Stdout Is Invaild.");
    } else if(StringCompare(Error, TypeError)==0){
        String_Output("Error: Your Varible Type Is Invaild.");
    } else if(StringCompare(Error, SyntaxError)==0){
        String_Output("Error: Syntax Invaild.");
    } else if(StringCompare(Error, SystemError)==0){
        String_Output("Error: System Error.");
    } else if(StringCompare(Error, UnicodeError)==0){
        String_Output("Error: Your  Execution Unicode Is Invaild.");
    } else if(StringCompare(Error, ValueError)==0){
        String_Output("Error: Your Fuction Value Is Invaild.");
    } else if(StringCompare(Error, IncludeError)==0){
        String_Output("Error: Your Includes Is Invaild.");
    } else if(StringCompare(Error, Exit)==0){
        String_Output("You Exit The Execution.");
    } else{
        String_Output("Error: Unknown Error");
    }
    exit(0);
}

char* StringUnconst(const char *Str){
    char AStr[5000] = {0};
    char *P = AStr;
    StringCopy(P, Str);
    return P;
}

const char* StringConst(char *Str){
    return Str;
}

void ListCopy(int *List1, int *List2, int From, int To){
    for(int i=From;i<To;i++,*List1++=*List2++);
}

int Swap_Int(int *Val1, int *Val2){
    int Tmp = *Val1;
    *Val1 = *Val2;
    *Val2 = Tmp;
    return 1;
}

int Swap_Char(char *Val1, char *Val2){
    char Tmp = *Val1;
    *Val1 = *Val2;
    *Val2 = Tmp;
    return 1;
}

void StringReverse(char *Str){
    char *Str_End = Str + StringLength(Str) - 1, *Str_Mid = Str + (StringLength(Str)/2);
    while(Swap_Char(Str++, Str_End--) && Str != Str_Mid);
}

unsigned long long int StringToInteger(const char *StrNum){
    unsigned long long Num=0,This=1;
    char *Str = StringUnconst(StrNum);
    StringReverse(Str);
    for(int i=0;i<StringLength(Str);i++){
        int this = Str[i] - '0';
        if(this < 0 || this > 9){
            continue;
        }
        Num += this*This;
        This *= 10;
    }
    return Num;
}

int Find_AscList(int *List, int Size, int Target){
    int left = 0, right = Size - 1, mid = (right + left) / 2;
    while (left < right && List[left] != List[right]) {
        if (List[mid] >= Target) {
            if (right == mid) right--;
            else right = mid;
        } else {
            if (left == mid) left++;
            else left = mid;
        }
        mid = (right + left) / 2;
    }
    if (List[mid] != Target) left = -2;
    return left+1;
}

int Find_DescList(int *List, int Size, int Target){
    Reverse(List, 0, Size+1);
    int answer = Find_AscList(List, Size, Target);
    Reverse(List, 0, Size+1);
    return answer;
}

char* IntegerToString(int Number){
    static char StrNum[1024] = {0};
    int Tail = 0;
    while(Number)StrNum[Tail++] = Number % 10 + '0',Number = Number / 10;
    return StrNum;
}

#define Exit() Throw(Exit)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值