CPP_Basic_Code_P9.1-PP9.6.4
阿新 • • 發佈:2018-11-02
##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 <iostream>
#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 <iostream>
#include <cmath>
#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 = "<<dapos.distance;
cout<<",angle = "<<dapos.angle*Rad_to_deg;
cout<<" degrees\n";
}
//P9.4
#include <iostream>
void oil(int x);
int main()
{
using namespace std;
int texas=31;
int year=2011;
cout<<"In main(),texas= "<<texas<<",&texas= "<<&texas<<endl;
cout<<"In main(),year= "<<year<<",&tear= "<<&year<<endl;
oil(texas);
cout<<"In main(),texas= "<<texas<<",&texas= "<<&texas<<endl;
cout<<"In main(),year= "<<year<<",&tear= "<<&year<<endl;
return 0;
}
void oil(int x)
{
using namespace std;
int texas=5;
cout<<"In oil(),texas= "<<texas<<",&texas= "<<&texas<<endl;
cout<<"In oil(),x= "<<x<<",&x= "<<&x<<endl;
{
int texas=113;
cout<<"In block,texas= "<<texas<<",&texas= "<<&texas<<endl;
cout<<"In block,x= "<<x<<",&x= "<<&x<<endl;
}//程式碼塊內新定義暫時隱藏以前的定義
cout<<"Post-block texas="<<texas<<",&texas= "<<&texas<<endl;
}
//P9.5-P9.6
Main.cpp
#include <iostream>
double warming=0.3;//外部變數定義和初始化
void update(double dt);
void local();
using namespace std;
int main()
{
cout<<"Global warming is "<<warming<<" degrees.\n";
update(0.1);
cout<<"Global warming is "<<warming<<" degrees.\n";
local();
cout<<"Global warming is "<<warming<<" degrees.\n";
return 0;
}
SubFunctions.cpp
#include <iostream>
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 "<<warming<<" degrees.\n";
}
void local()
{
double warming=0.8;
cout<<"Local warming = "<<warming<<" degrees.\n";
cout<<"But global warming = "<<::warming<<" degrees.\n";//作用域解析::訪問全域性變數
}
//P9.7-P9.8
Main.cpp
#include <iostream>
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 <iostream>
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 <iostream>
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<<"\""<<str<<"\" contains ";
while (*str++)//*str將獲取陣列第一個元素也就是第一個字母
count++;//計算字串裡有多少字元
total+=count;
cout<<count<<" characters\n";
cout<<total<<" characters total.\n";
}
//P9.10
#include <iostream>
//#include <new>
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<N;i++)
pd2[i]=pd1[i]=1000+20.0*i;
cout<<"Memory addresses:\n"<<" heap: "<<pd1<<" static: "<<(void*)buffer<<endl;
//此處(void*)強制型別轉換成空型別是為了輸出地址,因為p1是double指標,buffer是char指標
cout<<"Memory contents:\n";
for (i=0;i<N;i++)
{
cout<<pd1[i]<<" at "<<&pd1[i]<<"; ";
cout<<pd2[i]<<" at "<<&pd2[i]<<endl;
}
cout<<"\nCalling new and palcement new a second time:\n";
double* pd3,* pd4;
pd3=new double[N];
pd4=new (buffer) double[N];//將依然使用之前的地址
for (i=0;i<N;i++)
pd4[i]=pd3[i]=1000+40.0*i;
cout<<"Memory contents:\n";
for (i=0;i<N;i++)
{
cout<<pd3[i]<<" at "<<&pd3[i]<<"; ";
cout<<pd4[i]<<" at "<<&pd4[i]<<endl;
}
cout<<"\nCalling new and palcement new a third time:\n";
delete pd1;
pd1=new double[N];
pd2=new (buffer+N* sizeof(double)) double[N];//提供了5*8bytes的起始偏移量,地址改變
for (i=0;i<N;i++)
pd2[i]=pd1[i]=1000+60.0*i;
cout<<"Memory contents:\n";
for (i=0;i<N;i++)
{
cout<<pd1[i]<<" at "<<&pd1[i]<<"; ";
cout<<pd2[i]<<" at "<<&pd2[i]<<endl;
}
delete [] pd1;
delete [] pd3;
//不釋放pd2和pd4是因為它們並非指向堆的new出的記憶體,而是定位new
return 0;
}
//P9.11-P9.13
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H
#include <string>
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 <iostream>
#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<<endl;
Debt zippy[3];//結構陣列
int i;
for (i=0;i<3;i++)
getDebt(zippy[i]);//姓名和錢款同時獲取
for (i=0;i<3;i++)
showDebt(zippy[i]);//將輸入全部輸出顯示
cout<<"Total debt: $"<<sumDebts(zippy,3)<<endl;
return;//無返回值
}
void another(void)
{
using pers::Person;
Person collector {"Milo","Rightshift"};
pers::showPerson(collector);
std::cout<<std::endl;
}
SubFunctions.cpp
#include <iostream>
#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<<rp.lname<<", "<<rp.fname;
}
}
namespace debts
{
void getDebt(Debt& rd)
{
getPerson(rd.name);
std::cout<<"Enter debt: ";
std::cin>>rd.amount;
}
void showDebt(const Debt& rd)
{
showPerson(rd.name);//注意此函式位於pers空間且引數使用子成員.name
std::cout<<": $"<<rd.amount<<std::endl;
}
double sumDebts(const Debt ar[],int n)
{
double total=0;
for (int i=0;i<n;i++)
total+=ar[i].amount;
return total;
}
}
//PP9.6.1
Z_Head.h
#ifndef CLION_VERSION_Z_HEAD_H
#define CLION_VERSION_Z_HEAD_H
const int Len=40;
const int Num=3;
struct golf
{
char fullname[Len];
int hangdicap;
};
void setgolf(golf& g,const char* name,int hc);
int setgolf(golf& g);
void handicap(golf& g,int hc);
void showgolf(const golf& g);
#endif
Main.cpp
#include <iostream>
#include "Z_Head.h"
int main()
{
using namespace std;
golf ZhangHu[Num];
int i;
for (i=0;i<Num;i++)
{
int NMX=setgolf(ZhangHu[i]);
if (NMX==0)
break