C++ Primer Plus 9章源码

C++ Primer Plus 第九章书上源码

记录我看C++ Primer Plus(第六版)第九章源代码
后面有几个程序是讲命名空间的,我只是了解了一下并没有学习书上代码

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

#include<iostream>s
#include"coordin.h"
#include"9.3.cpp"
using namespace std;
int main()
{
    rect rplace;
    polar pplace;
    cout <<"Enter the x and y values: ";
    while(cin>>rplace.x>>rplace.y)
    {
        pplace = rect_to_polar(rplace);
        show_polar(pplace);
        cout << "Next two numbers (q to quit): ";
    }
    cout << "Bye!\n";
    return 0;
}

9.3

#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);
    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";
}

9.4

#include<iostream>
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<iostream>
#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.\n";
    update(0.1);
    cout << "Global warming is " << warming << " degrees.\n";
    local();
    cout << "Global warming is " << warming << " degrees.\n";
    return 0;
}

9.6

#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;
    cout << " degrees.\n";
}
void local()
{
    double warming = 0.8;
    cout << "Local warming = " << warming << " degrees.\n";
    cout << "But global warming = " << ::warming;
    cout << " degrees.\n";
}

9.7

#include<iostream>
int tom = 3;
int dick = 30;
static int harry = 300;
void remote_access();
int main()
{
    using namespace std;
    cout << "main() reports the following addresses:\n";
    cout << &tom << " = &tom," << &dick << " = &dick, ";
    cout << &harry << " = &harry\n";
    remote_access();
    return 0;
}

9.8

#include<iostream>
extern int tom;
static int dick = 10;
int harry = 200;
void remote_access()
{
    using namespace std;
    cout << "remoto_access() reports the following addresses:\n";
    cout << &tom << " = &tom, " << &dick << " = &dick, ";
    cout << &harry << " = &harry\n";
}

9.9

#include<iostream>
const int ArSize = 10;
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++)
        count++;
    total += count;
    cout << count << " characters\n";
    cout << total << " characters total\n";
}

9.10

#include<iostream>
#include<new>
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 :\n";
    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:\n"
         << " heap: " << pd1
         << " static: " << (void *)buffer << endl;
    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 placement 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 << "Meory 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 placement new a third time:\n";
    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:\n";
    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;
}

猜你喜欢

转载自blog.csdn.net/damowangsx/article/details/107710932