CPP_Basic_Code_P8.1-PP8.8.7

CPP_Basic_Code_P8.1-PP8.8.7

//  The Notes Created by Z-Tech on 2017/2/17.
//  All Codes Boot on 《C++ Primer Plus》V6.0
//  OS:MacOS 10.12.4
//  Translater:clang/llvm8.0.0 &g++4.2.1
//  Editer:iTerm 2&Sublime text 3
//  IDE: Xcode8.2.1&Clion2017.1

//P8.1
#include 
inline double square(double x) {return x*x;}//注意内联函数结尾没有;
int main()
{
    using namespace std;
    double a,b;
    double c=13.0;

    a=square(5.0);
    b=square(4.5+7.5);
    cout<<"a= "<
int main()
{
    using namespace std;
    int rats=101;
    int& rodents=rats;//引用rodents
    cout<<"rats= "<
double cube(double a);
double refcube(double &ra);//引用式参数
int main()
{
    using namespace std;
    double x=3.0;

    cout<
#include 
struct free_throws
{
    std::string name;
    int made;
    int attempts;
    float percent;
};
void display(const free_throws& ft);
void set_pc(free_throws& ft);
free_throws& accumulate(free_throws& target,const free_throws& source);
int main()
{
    free_throws one {"Ifelsa Branch",13,14};
    free_throws two {"Andor Knots",10,16};
    free_throws three {"Minnie Max",7,9};
    free_throws four {"Whily Looper",5,9};
    free_throws five {"Long Long",6,14};
    free_throws team {"Throwgoods",0,0};

    free_throws dup;

    set_pc(one);
    display(one);
    accumulate(team,one);
    display(team);

    display(accumulate(team,two));
    accumulate(accumulate(team,three),four);
    display(team);

    dup=accumulate(team,five);
    std::cout<<"Displaying team:\n";
    display(team);
    std::cout<<"Displaying dup after assignment:\n";
    display(dup);
    set_pc(four);

    accumulate(dup,five)=four;//此类语句应该避免,最后dup仍为four
    std::cout<<"Displaying dup after ill-advised assignment:\n";
    display(dup);
    return 0;
}

void display(const free_throws& ft)
{
    using namespace std;
    cout<<"Name: "<
#include //非必需
using namespace std;
string version1(const string& s1,const string& s2);
const string& version2(string& s1,const string& s2);//单边效应
const string& version3(string& s1,const string& s2);//糟糕的设计,虽clion中未崩溃

int main()
{
    string input;
    string copy;
    string result;

    cout<<"Enter a string: ";
    getline(cin,input);
    copy=input;
    cout<<"Your string as entered: "<
#include 
//#include //非必需
using namespace std;
void file_it(ostream& os,double fo,const double fe[],int n);
const int LIMIT=5;

int main()
{
    ofstream fout;
    const char* fn="ep-data.txt";//字面字符串赋值必需使用const
    fout.open(fn);
    if (!fout.is_open())//检测打开是否正常
    {
        cout<<"Can't open"<>objective;
    double eps[LIMIT];
    cout<<"Enter the focal length,in mm,of "<>eps[i];
    }
    file_it(fout,objective,eps,LIMIT);
    file_it(cout,objective,eps,LIMIT);
    cout<<"Done.\n";
    return 0;
}

void file_it(ostream& os,double fo,const double fe[],int n)
{
    ios_base::fmtflags initial;//声明格式化存储信息initial,存储格式化设置
    initial=os.setf(ios_base::fixed);//使用定点表示法模式
    os.precision(0);//定点模式下有效,指定小数位数设置
    os<<"Focal length of objective: "<
const int ArSize=80;
char* left(const char*str,int n=1);
int main()
{
    using  namespace std;
    char sample[ArSize];
    cout<<"Enter a string:\n";
    cin.get(sample,ArSize);
    char* ps=left(sample,4);
    cout<
unsigned long left(unsigned long num, unsigned ct);
char* left(const char* str,int n=1);

int main()
{
    using namespace std;
    const char* trip="Hawaii!!";
    unsigned long n=12345678;
    int i;
    char* temp;

    for (i=1;i<10;i++)
    {
        cout<ct)
    {
        ct=digits-ct;
        while (ct--)
            num/=10;//删除ct位后就是所要的num
        return num;
    }
    else
        return num;
}

char* left(const char* str,int n)
{
    if (n<0)
        n=0;
    char*p=new char[n+1];
    int i;
    for (i=0;i
template  //or use 
void Swap(T& a,T& b);//引用式模板交换函数

int main()
{
    using namespace std;
    int i=10;
    int j=20;
    cout<<"i,j= "<//定义前面也必须带这个模板声明
void Swap(T& a,T& b)
{
    T temp;
    temp=a;
    a=b;
    b=temp;
}

//P8.12
#include 
template 
void Swap(T& a,T& b);//指向引用

template 
void Swap(T* a,T* b,int n);//指向指针
void show(int a[]);
const int Lim=8;
int main()
{
    using namespace std;
    int i=10,j=20;
    cout<<"i,j= "<
void Swap(T& a,T& b)
{
    T temp;
    temp=a;
    a=b;
    b=temp;
}

template 
void Swap(T* a,T* b,int n)
{
    T temp;
    for (int i=0;i
template 
void Swap(T& a,T& b);

struct job
{
    char name[40];
    double salary;
    int floor;
};

template <> void Swap(job& j1,job& j2);//显式具体化
void show(job& j);

int main()
{
    using namespace std;
    cout.precision(2);
    cout.setf(ios::fixed,ios::floatfield);
    int i=10,j=20;
    cout<<"i,j= "<
void Swap(T& a,T& b)
{
    T temp;
    temp=a;
    a=b;
    b=temp;
}

template <> void Swap(job& j1,job& j2)
{
    double t1;
    int t2;
    t1=j1.salary;
    j1.salary=j2.salary;
    j2.salary=t1;
    t2=j1.floor;
    j1.floor=j2.floor;
    j2.floor=t2;
}

void show(job& j)
{
    using namespace std;
    cout<

template 
void ShowArray(T arr[],int n);//接收指针

template 
void ShowArray(T* arr[],int n);//接收指向指针的指针

struct debts
{
    char name[50];
    double amount;
};

int main()
{
    using namespace std;
    int thing[6] {13,31,103,301,310,130};
    debts mr_E[3]
    {
        {"Ima Wolfe",2400.0},
        {"Ura Foxe",1300.0},
        {"Iby Stout",1800.0}
    };
    double* pd[3];//指针数组

    for (int i=0;i<3;i++)
        pd[i]=&mr_E[i].amount;//全部指向结构体中的double

    cout<<"Listing Mr.E's counts of thing:\n";
    ShowArray(thing,6);//thing是数组名,是一个指向第一个元素的指针
    cout<<"Listing Mr.E's debts:\n";
    ShowArray(pd,3);//pd是指针数组的数组名,是指向第一个指针的指针

    return 0;
}

template 
void ShowArray(T arr[],int n)
{
    using namespace std;
    cout<<"template A\n";
    for (int i=0;i
void ShowArray(T* arr[],int n)
{
    using namespace std;
    cout<<"template B\n";
    for (int i=0;i

template 
T lesser(T a,T b)//返回值较小的那个
{
    return a(m,n)<提示选择模板函数
    cout<(x,y)<
void show(const char* str,int n=0);
int main()
{
    using namespace std;
    const char* strr="Hello,word.\n";
    show(strr);
    int number;
    cout<<"Enter a number: ";
    cin>>number;
    show(strr,number);
    cout<<"Done.\n";

    return 0;
}

void show(const char* str,int n)
{
    using namespace std;
    if (n<=0)
        n=1;
    for (int i=0;i
struct CandyBar
{
    char Brandname[30];
    double Weight;
    int Calaries;
};
void fill_struct(CandyBar& strc,const char* nm="Millennium Munch",double wt=2.85,int clr=350);
void show_struct(CandyBar& strc);
int main()
{
    using namespace std;
    CandyBar* xv=new CandyBar;
    cout<<"Please enter Brandname: ";
    cin>>xv->Brandname;
    cout<<"\nEnter Weight of product: ";
    cin>>xv->Weight;
    cout<<"\nEnter Calaries of product: ";
    cin>>xv->Calaries;
    show_struct(*xv);

    fill_struct(*xv);
    show_struct(*xv);

    fill_struct(*xv,"快说你是不是傻",9.99,9999);
    show_struct(*xv);

    delete xv;
    return 0;
}

void fill_struct(CandyBar& strc,const char* nm,double wt,int clr)
{
    strcpy(strc.Brandname,nm);
    strc.Weight=wt;
    strc.Calaries=clr;

}

void show_struct(CandyBar& strc)
{
    using namespace std;
    cout<<"BrandName: "<
//#include 
//#include 
void fuct_up(std::string& str);

int main()
{
    using namespace std;
    string something;
    cout<<"Enter a string (q to quit): ";
    while (getline(cin,something)&&something!="q"&&something!="Q")
    {
        fuct_up(something);
        cout<
#include 
using namespace std;
struct stringy
{
    char* str;
    int ct;
};

void set(stringy& strct,char* str);
void show(const char* str,int num=1);
void show(const stringy& strct,int num=1);

int main()
{
    stringy beany;
    char testing[]="Reality isn't what it used to be.";

    set(beany,testing);

    show(beany);
    show(beany,2);

    testing[0]='D';//直接数组式操作调整字符串内容
    testing[1]='u';

    show(testing);
    show(testing,3);
    show("Done!");

    delete beany.str;//释放内存
    return 0;
}

void set(stringy& strct,char* str)
{
    char* xvr=new char[strlen(str)+1];
    strct.str=xvr;//使结构成员指向new出的指针
    strcpy(strct.str,str);//复制字符串内容
    strct.ct=strlen(str);
}

void show(const char* str,int num)
{
    using namespace std;
    for (int i=0;i
//#include 
template 
T maxn(T arry[],int n);

template <> const char* maxn(const char* arry[],int n);//字面值的字符串务必使用const

int main()
{
    using namespace std;
    int arr_int[6]{1,3,23,4,11,66};
    double  arr_db[4]{22.4,23.6,74.8,9.9};
    cout<<"Now using maxn():\n";
    cout<<"Array_int_Max: "<
T maxn(T arry[],int n)
{
    T Max=arry[0];
    for (int i=0;i const char* maxn(const char* arry[],int n)//具体化
{
    const char* xv=arry[0];
    for (int i=1;istrlen(xv))
            xv=arry[i];
    }
    return xv;//返回指针
}


//PP8.8.7
#include 

template 
void SumArray(T arr[],int n);//接收指针

template 
void SumArray(T* arr[],int n);//接收指向指针的指针

struct debts
{
    char name[50];
    double amount;
};

int main()
{
    using namespace std;
    int thing[6] {13,31,103,301,310,130};
    debts mr_E[3]
            {
                    {"Ima Wolfe",2400.0},
                    {"Ura Foxe",1300.0},
                    {"Iby Stout",1800.0}
            };
    double* pd[3];//指针数组

    for (int i=0;i<3;i++)
        pd[i]=&mr_E[i].amount;//全部指向结构体中的double

    cout<<"Listing Mr.E's counts of thing:\n";
    SumArray(thing,6);//thing是数组名,是一个指向第一个元素的指针
    cout<<"Listing Mr.E's debts:\n";
    SumArray(pd,3);//pd是指针数组的数组名,是指向第一个指针的指针

    return 0;
}

template 
void SumArray(T arr[],int n)
{
    using namespace std;
    cout<<"Result_Sum: \n";
    int count=0;
    for (int i=0;i
void SumArray(T* arr[],int n)
{
    using namespace std;
    cout<<"Result_Sum: \n";
    int count=0;
    for (int i=0;i

你可能感兴趣的:(CPP_Basic_Code_P8.1-PP8.8.7)