C++Primer Plus 9章ソースコード
7890 ワード
C++Primer Plus第九章書上源コード
C++Primer Plus(第6版)第9章ソースコードの後ろにいくつかのプログラムが名前空間を話しているのを見て、私はただ本のコードを勉強していないことを知っただけです.
9.1
9.2
9.3
9.4
9.5
9.6
9.7
9.8
9.9
9.10
C++Primer Plus(第6版)第9章ソースコードの後ろにいくつかのプログラムが名前空間を話しているのを見て、私はただ本のコードを勉強していないことを知っただけです.
9.1
#ifndef COORDIN_H_
#define 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
9.2
#includes
#include"coordin.h"
#include"9.3.cpp"
using namespace std;
int main()
{
rect rplace;
polar pplace;
cout <>rplace.x>>rplace.y)
{
pplace = rect_to_polar(rplace);
show_polar(pplace);
cout << "Next two numbers (q to quit): ";
}
cout << "Bye!
";
return 0;
}
9.3
#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);
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
";
}
9.4
#include
void oil(int x);
int main()
{
using namespace std;
int texas = 31;
int year = 2013;
cout << " In main (),texas = " << texas << ",&texas = ";
cout << &texas << endl;
cout << " In main(),year = " << year << ", &year = ";
cout << &year << endl;
oil(texas);
cout << "In main(),texas = " << texas << ", & texas = ";
cout << &texas << endl;
cout << " In main(),year = " << year << ", &year = ";
cout << &year << endl;
return 0;
}
void oil(int x)
{
using namespace std;
int texas = 5;
cout << "In oil(),texas = " << texas << ",&texas = ";
cout << &texas << endl;
cout << "In oil(),x = " << x << ", &x = ";
cout << &x << endl;
{
int texas = 133;
cout << "In block, texas = " << texas;
cout << ",&texas = " << &texas << endl;
cout << "In block,x = " << x << ", &x = ";
cout << &x << endl;
}
cout << "Post-block texas = " << texas;
cout << ",&texas = " << &texas << endl;
}
9.5
#include
#include"9.6.cpp"
using namespace std;
double warming = 0.3;
void update(double dt);
void local();
int main()
{
cout << "Global warming is " << warming << " degrees.
";
update(0.1);
cout << "Global warming is " << warming << " degrees.
";
local();
cout << "Global warming is " << warming << " degrees.
";
return 0;
}
9.6
#include
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;
cout << " degrees.
";
}
void local()
{
double warming = 0.8;
cout << "Local warming = " << warming << " degrees.
";
cout << "But global warming = " << ::warming;
cout << " degrees.
";
}
9.7
#include
int tom = 3;
int dick = 30;
static int harry = 300;
void remote_access();
int main()
{
using namespace std;
cout << "main() reports the following addresses:
";
cout << &tom << " = &tom," << &dick << " = &dick, ";
cout << &harry << " = &harry
";
remote_access();
return 0;
}
9.8
#include
extern int tom;
static int dick = 10;
int harry = 200;
void remote_access()
{
using namespace std;
cout << "remoto_access() reports the following addresses:
";
cout << &tom << " = &tom, " << &dick << " = &dick, ";
cout << &harry << " = &harry
";
}
9.9
#include
const int ArSize = 10;
void strcount(const char *str);
int main()
{
using namespace std;
char input[ArSize];
char next;
cout << "Enter a line:
";
cin.get(input, ArSize);
while(cin)
{
cin.get(next);//
while(next!='
')
cin.get(next);
strcount(input);
cout << "Enter next line (empty line to quit):
";
cin.get(input, ArSize);
}
cout << "Bye
";
return 0;
}
void strcount(const char *str)
{
using namespace std;
static int total = 0;
int count = 0;
cout << "\"" << str << "\"contains ";
while (*str++)
count++;
total += count;
cout << count << " characters
";
cout << total << " characters total
";
}
9.10
#include
#include
const int BUF = 512;
const int N = 5;
char buffer[BUF];
int main()
{
using namespace std;
double *pd1, *pd2;
int i;
cout << "Calling new and placement new :
";
pd1 = new double[N];
pd2 = new (buffer) double[N];
for (i = 0; i < N;i++)
pd2[i] = pd1[i] = 100 + 20.0 * i;
cout << "Memory addresse:
"
<< " heap: " << pd1
<< " static: " << (void *)buffer << endl;
cout << "Memory contents:
";
for (i = 0; i < N;i++)
{
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd2[i] << endl;
}
cout << "
Calling new and placement new a second time:
";
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 << "Meory contents:
";
for (i = 0; i < N;i++)
{
cout << pd3[i] << " at " << &pd3[i] << "; ";
cout << pd4[i] << " at " << &pd4[i] << endl;
}
cout << "
Calling new and placement new a third time:
";
delete[] pd1;
pd1 = new double[N];
pd2 = new (buffer + N * sizeof(double)) double[N];
for (i = 0; i < N;i++)
pd2[i] = pd1[i] = 1000 + 60.0 * i;
cout << "Memory contents:
";
for (i = 0; i < N;i++)
{
cout << pd1[i] << " at " << &pd1[i] << "; ";
cout << pd2[i] << " at " << &pd2[i] << endl;
}
delete[] pd1;
delete[] pd3;
return 0;
}