CPP_Basic_Code_P9.1-PP9.6.4

CPP_Basic_Code_P9.1-PP9.6.4

//  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

//P9.1-P9.3
cmake_minimum_required(VERSION 3.6)
project(CLion_Version)

set(CMAKE_CXX_STANDARD 11)

set(SOURCE_FILES coordin.h file1.cpp file2.cpp)//资源文件包含说明
add_executable(CLion_Version ${SOURCE_FILES})//可使用指定源文件引入可执行文件

coordin.h
#ifndef CLION_VERSION_COORDIN_H
#define CLION_VERSION_COORDIN_H

struct polar//极坐标结构
{
    double distance;
    double angle;
};
struct rect//直角坐标结构
{
    double x;
    double y;
};

polar rect_to_polar(rect xypos);//直角转为极坐标
void show_polar(polar dapos);//显示极坐标

#endif //CLION_VERSION_COORDIN_H

file1.cpp
#include 
#include "coordin.h"

int main()
{
    using namespace std;
    rect rplace;//声明两个结构以存储输入的数据
    polar pplace;

    cout<<"Enter the x and y value: ";
    while (cin>>rplace.x>>rplace.y)//连续使用cin可行,且忽略空格等
    {
        pplace=rect_to_polar(rplace);//结果赋值给第二个结构
        show_polar(pplace);//显示
        cout<<"Next two numbers (q to quiit): ";
    }
    cout<<"Done.\n";
    return 0;
}

file2.cpp
#include 
#include 
#include "coordin.h"


polar rect_to_polar(rect xypos)
{
    using namespace std;
    polar answer;
    answer.distance=sqrt(xypos.x*xypos.x+xypos.y*xypos.y);
    answer.angle=atan2(xypos.y,xypos.x);//y/x计算arctan
    return answer;//返回结构
}

void show_polar(polar dapos)
{
    using namespace std;
    const double Rad_to_deg=57.29577951;//弧度转换为度数的常数因子
    cout<<"Distance = "<
void oil(int x);

int main()
{
    using namespace std;

    int texas=31;
    int year=2011;
    cout<<"In main(),texas= "<

extern double warming;//引用外部变量声明
void update(double dt);
void local();

using std::cout;

void update(double dt)
{
    extern double warming;//引用外部变量变量
    warming+=dt;//修改外部变量
    cout<<"Updating global warming to "<
void remote_access();

int tom=3;//外部声明
int dick=30;//外部声明
static int harry=300;//内部声明

int main()
{
    using namespace std;
    cout<<"main() reports the following addresses:\n";
    cout<<&tom<<" = &tom, "<<&dick<<" = &dick, "<<&harry<<" = &harry\n";
    remote_access();
    return 0;
}

SubFunctions.cpp
#include 

extern int tom;//外部引用
static int dick=10;//内部声明
int harry=200;//外部声明

void remote_access()
{
    using namespace std;
    cout<<"remote_access() reports the following addresses:\n";
    cout<<&tom<<" = &tom, "<<&dick<<" = &dick, "<<&harry<<" = &harry\n";
}

//P9.9
#include 
const int ArSize=15;
void strcount(const char* str);

int main()
{
    using namespace std;
    char input[ArSize];
    char next;

    cout<<"Enter a line:\n";
    cin.get(input,ArSize);
    while (cin)
    {
        cin.get(next);//读取回车
        while (next!='\n')//检查是否读取了回车确定是否有字符未被读取
            cin.get(next);//丢弃过多的字符
        strcount(input);//计算字符数的函数
        cout<<"Enter next line(empty line to quit):\n";
        cin.get(input,ArSize);
    }
    cout<<"Bye.\n";
    return 0;
}

void strcount(const char* str)
{
    using namespace std;
    static int total=0;//每次调用仅第一次才初始化
    int count=0;//参照对象

    cout<<"\""<
//#include 
const int BUF=512;
const int N=5;
char buffer[BUF];

int main()
{
    using namespace std;
    double *pd1,*pd2;//十分小心!此处易写成double* pd1,pd2;
    int i;
    cout<<"Calling new and placement new:\n";
    pd1=new double[N];
    pd2=new (buffer) double[N];
    for (i=0;i

namespace pers
{
    struct Person
    {
        std::string fname;
        std::string lname;
    };
    void getPerson(Person&);
    void showPerson(const Person&);
}

namespace debts
{
    using namespace pers;
    struct Debt
    {
        Person name;
        double amount;
    };
    void getDebt(Debt&);
    void showDebt(const Debt&);
    double sumDebts(const Debt ar[],int n);
}
#endif

Main.cpp
#include 
#include "Z_Head.h"
void other(void);
void another(void);

int main()
{
    using debts::Debt;
    using debts::showDebt;

    Debt golf {{"Benny","Goatsniff"},120.0};
    showDebt(golf);
    other();
    another();
    return 0;
}

void other(void)
{
    using std::cout;
    using std::endl;
    using namespace debts;
    Person dg {"Doodles","Glister"};
    showPerson(dg);//倒着先last后first输出名字
    cout<
#include "Z_Head.h"
namespace pers
{
    using std::cout;
    using std::cin;
    void getPerson(Person& rp)
    {
        cout<<"Enter first name: ";
        cin>>rp.fname;
        cout<<"Enter last name: ";
        cin>>rp.lname;
    }
    void showPerson(const Person& rp)
    {
        std::cout<>rd.amount;
    }
    void showDebt(const Debt& rd)
    {
        showPerson(rd.name);//注意此函数位于pers空间且参数使用子成员.name
        std::cout<<": $"<
#include "Z_Head.h"

int main()
{
    using namespace std;
    golf ZhangHu[Num];
    int i;
    for (i=0;i
#include "Z_Head.h"

using namespace std;
void setgolf(golf& g,const char* name,int hc)
{
    strcpy(g.fullname,name);
    g.hangdicap=hc;
}

int setgolf(golf& g)
{
    cout<<"Please enter a name: ";
    cin.get(g.fullname,Len);
    if (g.fullname[0]=='\0')
        return 0;
    cout<<"Please enter a rank: ";
    while (!(cin>>g.hangdicap))//注意输入被置于条件内!
    {
        cin.clear();
        while (cin.get()!='\n')
            continue;
        cout<<"Try again!"<
void strcount(const std::string str);

int main()
{
    using namespace std;
    string input;
    char next;

    cout<<"Enter a line:\n";
    getline(cin,input);
    while (input!="")
    {
        strcount(input);//计算字符数的函数
        cout<<"Enter next line(empty line to quit):\n";
        getline(cin,input);
    }
    cout<<"Bye.\n";
    return 0;
}

void strcount(const std::string str)
{
    using namespace std;
    static int total=0;//每次调用仅第一次才初始化
    int count=0;//参照对象

    cout<<"\""<
const int BUF=512;
char buffer[BUF];
struct chaff
{
    char dross[20];
    int slag;
};
int main()
{
    using namespace std;
    chaff *ps1=new chaff[2];
    strcpy(ps1->dross,"EnochHugh");
    ps1->slag=5;
    strcpy((ps1+1)->dross,"YangHsuChou");
    (ps1+1)->slag=9;
    for (int i=0;i<2;i++,ps1++)
    {
        cout<<"ps1: Droos: "<dross<slag<dross,"EnochHugh");
    ps2->slag=5;
    strcpy((ps2+1)->dross,"YangHsuChou");
    (ps2+1)->slag=9;
    const chaff *end=ps2+2;//强行使用指针循环
    for (;ps2dross<slag<
#include "Z_Head.h"

int main()
{
    const double price[SALES::QUARTERS] {12.34,34.54,66.99,99.123};
    int ZHS=10;
    SALES::Sales* xv=new SALES::Sales[2];
    SALES::setSales(*xv,price,ZHS);
    SALES::showSales(*xv);
    SALES::setSales(*(xv+1));
    SALES::showSales(*(xv+1));
    delete xv;
    return 0;
}

SubFunctions.cpp
#include 
#include "Z_Head.h"

namespace SALES
{
    void setSales(Sales& s,const double ar[],int n)
    {
        int realv=(QUARTERS>n?n:QUARTERS);
        double total=0;
        for (int i=0;i>s.sales[i];
            if (!cin)
            {
                cin.clear();
                while (cin.get()!='\n')
                    continue;
            }
            total+=s.sales[i];
            s.average=total/QUARTERS;
            s.max=findMax(s.sales,QUARTERS);
            s.min=findMin(s.sales,QUARTERS);
        }
    }

    void showSales(const Sales& s)
    {
        using std::cout;
        using std::endl;
        cout<<"*xv sales:";
        for (int i=0;imax)
                max=ar[i];
        }
        return max;
    }

    double findMin(double ar[],int ARSZ)
    {
        double min=ar[0];
        for (int i=1;i

你可能感兴趣的:(CPP_Basic_Code_P9.1-PP9.6.4)