2.
/*2014. 01. 15 by Arby*/ /*c++ primer 第九章编程练习*/ // static.cpp -- using a static local variable #include <iostream> #include <string> #include <cstring> using namespace std; // constants const int ArSize = 10; // function prototype void strcount(const string str); int main() { /*初始化*/ string input; /*操作*/ cout << "Enter a line:\n"; getline(cin, input); while (cin) { strcount(input); cout << "Enter next line (empty line to quit):\n"; getline(cin, input); } cout << "Bye\n"; cin.get(); cin.get(); return 0; } void strcount(const string str) { static int total = 0; // static local variable int count = 0; // automatic local variable cout << "\"" << str <<"\" contains "; count = str.length(); total += count; cout << count << " characters\n"; cout << total << " characters total\n"; }
3.
/*2014. 01. 15 by Arby*/ /*c++ primer 第九章编程练习*/ // static.cpp -- using a static local variable #include <iostream> using namespace std; struct chaff { char dross[20]; int slag; }; char buffer[100]; int main() { /*初始化*/ chaff *member = new (buffer) chaff[2]; /*操作*/ strcpy(member[0].dross, "xiaoke"); member[0].slag = 10; strcpy(member[1].dross, "xiaochui"); member[1].slag = 20; cout << (void*)buffer << endl; cout << &member[0].dross << endl; cin.get(); cin.get(); return 0; }
4.
#ifndef GOLF_H #define GOLF_H namespace SALES { const int QUARTERS = 4; struct Sales { double sales[QUARTERS]; double average; double max; double min; }; void setSales(Sales &s, const double ar[], int n); void setSales(Sales &s); void showSales(const Sales &s); }; #endif
#include <iostream> /*在系统中查找*/ using namespace std; #include "golf.h" /*在文件中查找*/ namespace SALES { void setSales(Sales &s, const double ar[], int n) { int i = 0; int totle = 0; s.max = 0; if(n > 4) //n > 4 { for(i = 0; i < 4; i++) { s.sales[i]= ar[i]; totle = totle + s.sales[i]; if(s.sales[i] > s.max) s.max = s.sales[i]; } s.average = totle / 4; s.min = s.sales[0]; for(i = 0; i < 4; i++) { if(s.min > s.sales[i]) s.min = s.sales[i]; } } //end if else //n < 4 { for(i = 0; i < n; i++) { s.sales[i]= ar[i]; totle = totle + s.sales[i]; if(s.sales[i] > s.max) s.max = s.sales[i]; } s.average = totle / n; s.min = s.sales[0]; for(i = 0; i < n; i++) { if(s.min > s.sales[i]) s.min = s.sales[i]; } } // end else } void setSales(Sales &s) { int i = 0; int totle = 0; s.max = 0; cout << "enter the 4 sales: " << endl; for(i = 0; i < 4; i++) { cin >> s.sales[i]; totle = totle + s.sales[i]; if(s.sales[i] > s.max) s.max = s.sales[i]; } s.min = s.sales[0]; for(i = 0; i < 4; i++) { if(s.min > s.sales[i]) s.min = s.sales[i]; } } void showSales(const Sales &s) { for(int i = 0; i < 4; i++) { cout << "the " << i << " is: "<< s.sales[i] << endl; } cout << "the average is: "<< s.average<< endl; cout << "the max is: "<< s.max << endl; cout << "the min is: " << s.min <<endl; } }
/*2014. 01. 15 by Arby*/ /*c++ primer 第九章编程练习*/ // 名称空间 #include <iostream> using namespace std; #include "golf.h" int main() { /*初始化*/ using SALES::Sales; using SALES::setSales; using SALES::showSales; Sales sale1; Sales sale2; double ar[5] = {43, 45.5, 345, 545, 3}; /*操作*/ setSales(sale1, ar, 5); setSales(sale2); showSales(sale1); showSales(sale2); cin.get(); cin.get(); return 0; }