C++自制.h文件

· · 个人记录

这是我与qingchenMC共同自制的.h文件(1.2版本)。

更新日志:

  1. 1.0版本(2023/12/16),配备了输出换行、空格,加减乘除法,阶乘,无限循环。
  2. 1.2版本(2023/12/17),新添了关机,取消关机,时间显示,自制stack,自制queue,自制分数类,快读快写。
  3. 1.21版本(2024/6/23),新添了矩阵以及矩阵运算(未经正经测试,请勿在重要的场合下使用)。

如何加入到你的C++中?

  1. 复制我的代码。
  2. 新建一个文件名为Betterc.h,并把它放在C++文件的include文件中。
  3. 粘贴我的代码在Betterc.h里面。
  4. 保存并运行。
  5. 你会发现它会提示“源文件未编译”,不用管它,这样就完成了。

如果上述方法不会的话请参考如下方法:

  1. 新建文件夹,名字随便(最好是include)。
  2. 在这个文件夹里面新建一个文件名为Betterc.h,并把我的代码复制到里面。
  3. 保存并运行。
  4. 打开Dev-C++。
  5. 点击上方工具键。
  6. 点击编译选项。
  7. 点击目录。
  8. 点击C++包含文件。
  9. 点击删除无效的右边的文件样子的按钮。
  10. 选择你刚刚的文件。
  11. 点击添加。
  12. 点击确定。

如何食用?引用头文件#include<Betterc.h>就可以了。

代码:

#ifndef Betterc_h
#define Betterc_h
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
const int lijunzhuo_qingchenMC_N_lijunzhuo_qingchenMC_=1e5+20;
/*
This is a C++file jointly created by Lijunzhuo and qingchenMC.
If you want to modify or repost it,please indicate the source.
*/
#define LF putchar('\n'); //换行
#define SP putchar(' '); //空格
int Lijunzhuo_ans_Lijunzhuo_int;
long long Lijunzhuo_ans_Lijunzhuo_longlong;
void Li_inf(){while(1);}             //无限循环
auto add(auto a,auto b){return a+b;} //加法
auto sub(auto a,auto b){return a-b;} //减法
auto mul(auto a,auto b){return a*b;} //乘法
auto div(auto a,auto b){return a/b;} //除法
void close_computer(){system("shutdown -s -t 60");}//关机 
void cz_close_computer(){system("shutdown -a");}   //取消关机
void f_cmd(){system("title 命令提示符");system("cmd");} //仿cmd
template<typename T>void kread(T&x){x=0;char ch=getchar();long long f=1;while(!isdigit(ch)){if(ch=='-')f*=-1;ch=getchar();}while(isdigit(ch)){x=x*2+ch-48;ch=getchar();}x*=f;}template<typename T,typename...Args>void kread(T&first,Args&...args){kread(first);read(args...);}template<typename T>void kwrite(T arg){T x=arg;if(x<0){putchar('-');x=-x;}if(x>9)kwrite(x/10);putchar(x%10+'0');}template<typename T,typename...Ts>void kwrite(T arg,Ts...args){kwrite(arg);if(sizeof...(args)!=0){SP;kwrite(args...);}} //快读快写。 
int fact(int a)                      //int阶乘 
{
    Lijunzhuo_ans_Lijunzhuo_int=1;
    for(int i=1;i<=a;i++)
        Lijunzhuo_ans_Lijunzhuo_int*=i;
    return Lijunzhuo_ans_Lijunzhuo_int;
}
long long fact(long long a)          //longlong阶乘 
{
    Lijunzhuo_ans_Lijunzhuo_longlong=1;
    for(int i=1;i<=a;i++)
        Lijunzhuo_ans_Lijunzhuo_longlong*=i;
    return Lijunzhuo_ans_Lijunzhuo_longlong;
}
void gotoxy(int x=0,int y=0)  //display_time()附件,同system("cls")。 
{
    COORD c;
    c.X=x;
    c.Y=y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),c);
}
void display_time()           //显示时间 
{
    int a,b,c,d,e,f;
    while(1)
    {
        time_t now = time(0);
        tm *tm_ptr = localtime(&now);
        a=1900+tm_ptr->tm_year,b=tm_ptr->tm_mon+1,c=tm_ptr->tm_mday;
        d=(tm_ptr->tm_hour ) % 24,e=tm_ptr->tm_min,f=tm_ptr->tm_sec;
        printf("现在时间:\n%04d年%02d月%02d日\n",a,b,c);
        if(d>12) printf("下午 %02d时:%02d分:%02d秒\n",d-12,e,f);
        else  printf("上午 %02d时:%02d分:%02d秒\n",d,e,f);
        Sleep(500);
        gotoxy(0,0);
    }
}
template<typename T>
class mystack
{
    private:
        T A[lijunzhuo_qingchenMC_N_lijunzhuo_qingchenMC_];
        int tot=0;
    public:
        bool push(T i)
        {
            if(tot+1>lijunzhuo_qingchenMC_N_lijunzhuo_qingchenMC_) return false;
            A[++tot]=i;
            return true;
        }
        bool pop()
        {
            if(tot-1) return false;
            tot--;
            return true;
        }
        int size(){return tot;}
        bool empty(){return !tot;}
        T top(){return A[tot];}
};
template<typename T>
class myqueue
{
    private:
        struct Node
        {
            T data;
            Node*next=nullptr;
            Node(const T&value):data(value),next(nullptr){}
        };
        Node*head=nullptr;
        Node*tail=nullptr;
        size_t count=0;
    public:
        void push(const T&element)
        {
           Node*new_node=new Node(element);
           if (tail!=nullptr)
               tail->next=new_node;
            tail=new_node;
           if (head==nullptr)
               head=tail;
           count++;
        }
        void pop()
        {
            if (head!=nullptr)
            {
                Node*temp=head;
                head=head->next;
                delete temp;
                count--;
                if (head==nullptr)
                   tail=nullptr;
           }
        }
        T front() const{return head->data;}
        bool empty() const{return count==0;}
        size_t size() const{return count;}
        ~myqueue()
        {
            while (head!=nullptr)
            {
                Node*temp=head;
                head=head->next;
                delete temp;
            }
        }
};
long long minimumcommonmultipleoffractions;
long long maximumcommonfactoroffractions;
class fraction
{
    private:
        long long a=1,b=1,ansa,ansb;
        long long gcd(long long i,long long j){return j==0?i:gcd(j,i%j);}
        long long lcm(long long i,long long j){return i/gcd(i,j)*j;}
        fraction add(fraction firstfraction,fraction secondfraction)
        {
            ansa=firstfraction.a*secondfraction.b+firstfraction.b*secondfraction.a;
            ansb=firstfraction.b*secondfraction.b;
            reductionofafraction(ansa,ansb);
            return fraction(ansa,ansb);
        }
        fraction sub(fraction firstfraction,fraction secondfraction)
        {
            ansa=firstfraction.a*secondfraction.b-firstfraction.b*secondfraction.a;
            ansb=firstfraction.b*secondfraction.b;
            reductionofafraction(ansa,ansb);
            return fraction(ansa,ansb);
        }
        fraction mul(fraction firstfraction,fraction secondfraction)
        {
            ansa=firstfraction.a*secondfraction.a;
            ansb=firstfraction.b*secondfraction.b;
            reductionofafraction(ansa,ansb);
            return fraction(ansa,ansb);
        }
        fraction div(fraction firstfraction,fraction secondfraction)
        {
            ansa=firstfraction.a*secondfraction.b;
            ansb=firstfraction.b*secondfraction.a;
            reductionofafraction(ansa,ansb);
            return fraction(ansa,ansb);
        }
    public:
        fraction():a(0),b(1){}
        fraction(long long aa,long long bb):a(aa),b(bb){}
        void readnumerator(){scanf("%lld",&a);}
        void readdenominator(){scanf("%lld",&b);}
        void readfraction(){readnumerator();readdenominator();}
        long double thevalueofthescore(){return a/b;}
        void reductionofafraction(long long&i,long long&j)
        {
            minimumcommonmultipleoffractions=gcd(i,j);
            i/=minimumcommonmultipleoffractions;
            j/=minimumcommonmultipleoffractions;
        }
        friend istream&operator>>(istream&is,fraction&f)
        {
            scanf("%lld/%lld",&f.a,&f.b);
            return is;
        }
        friend ostream&operator<<(ostream&os, const fraction&f)
        {
            os<<f.a<<"/"<<f.b;
            return os;
        }
        fraction operator+(const fraction fa){return add(*this,fa);}
        fraction operator-(const fraction fa){return sub(*this,fa);}
        fraction operator*(const fraction fa){return mul(*this,fa);}
        fraction operator/(const fraction fa){return div(*this,fa);}
        fraction operator+=(const fraction fa){return *this=add(*this,fa);}
        fraction operator-=(const fraction fa){return *this=sub(*this,fa);}
        fraction operator*=(const fraction fa){return *this=mul(*this,fa);}
        fraction operator/=(const fraction fa){return *this=div(*this,fa);}
};
const int NNNNNNNNNNNNNNNNNNNNNNNNNNNNN=105;
class Matrix
{
    private:
        long long M[NNNNNNNNNNNNNNNNNNNNNNNNNNNNN][NNNNNNNNNNNNNNNNNNNNNNNNNNNNN];
        int n,m;
    public:
        Matrix():n(0),m(0){}
        Matrix(int a,int b):n(a),m(b){}
        long long at(int a,int b)
        {
            return M[a][b];
        }
        void scan()
        {
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    scanf("%lld",&M[i][j]);
        }
        void print()
        {
            for(int i=0;i<n;i++)
            {
                for(int j=0;j<m;j++)
                    printf("%lld ",M[i][j]);
                printf("\n");
            }
        }
        Matrix operator +(const Matrix B)
        {
            Matrix C;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    C.M[i][j]=M[i][j]+B.M[i][j];
            return C;
        }
        Matrix operator -(const Matrix B)
        {
            Matrix C;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    C.M[i][j]=M[i][j]-B.M[i][j];
            return C;
        }
        Matrix operator *(const Matrix B)
        {
            Matrix C;
            for(int i=0;i<n;i++)
                for(int j=0;j<B.m;j++)
                    C.M[i][j]=0;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    for(int z=0;z<B.m;z++)
                        C.M[i][z]=(C.M[i][z]+M[i][j]*B.M[j][z]);
            return C;
        }
        Matrix operator %(const long long p)
        {
            Matrix C;
            for(int i=0;i<n;i++)
                for(int j=0;j<m;j++)
                    C.M[i][j]=M[i][j]%p;
            return C;
        }
};
#endif

我是有底线的。