实例9.1(头文件)
#pragma once
#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>
#include"9.1coordin.h"
using namespace std;
int main()
{
rect rplace;
polar pplace;
cout << "Enter the x and y value:";
while (cin >> rplace.x >> rplace.y)
{
pplace = rect_to_polar(rplace);
show_polar(pplace);
cout << "Next two numbers (q to quit):";
}
cout << "Bye!\n";
system("pause");
return 0;
}实例9.3
#include<iostream>
#include<cmath>
#include"9.1coordin.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 = 2011;
cout << "In main(),texas = " << texas << ",&texas = ";
cout << &texas << endl;
cout << "In main(),years = " << year << ",&year = ";
cout << &year << endl;
oil(texas);
cout << "In main(),texas = " << texas << ",&texas = ";
cout << &texas << endl;
system("pause");
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 = 113;
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>
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";
system("pause");
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 << "Update 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 address:\n";
cout << &tom << " = &tom," << &dick << " = &dick,";
cout << &harry << " = &harry\n";
remote_access();
system("pause");
return 0;
}实例9.8
#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,";
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";
system("pause");
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] = 1000 + 20.0*i;
cout << "Memory addresses:\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];
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 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;
system("pause");
return 0;
}实例9.11(头文件)
#pragma once
#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);
}实例9.12
#include<iostream>
#include"9.11namesp.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 &rp)
{
showPerson(rp.name);
std::cout << ":$" << rp.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;
}
}实例9.13
#include<iostream>
#include"9.11namesp.h"
void other(void);
void another(void);
int main(void)
{
using debts::Debt;
using debts::showDebt;
Debt golf = { {"Benny","Goatsniff"},120.0 };
showDebt(golf);
other();
another();
system("pause");
return 0;
}
void other(void)
{
using std::cout;
using std::endl;
using namespace debts;
Person dg = { "Doodles","Glister" };
showPerson(dg);
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;
}版权声明:本文为qq_46112413原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。