9.5 复习题
1.a:利用自动存储
b:多个文件共享,仅需在一个文件进行定义,存储形式是静态外部链接。
c:一个文件中所有函数可以使用,其他文件无法使用,使用内部静态链接
d:利用本地静态变量。
3.
#include<iostream>
int main() {
double x;
std::cout << "Enter value: ";
while (!(std::cin >> x)) {
std::cout << "Bad input.Please enter a number: ";
std::cin.clear();
while (std::cin.get() != '\n')
continue;
}
std::cout << "Value = " << x << std::endl;
system("pause");
return 0;
}
4.
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
int main() {
double x;
cout << "Enter value: ";
while (!(cin >> x)) {
cout << "Bad input.Please enter a number: ";
cin.clear();
while (cin.get() != '\n')
continue;
}
cout << "Value = " << x << endl;
system("pause");
return 0;
}
6.
7.
9.6 编程练习
1.
//头文件“demo1.h” #pragma once #ifndef COORDIN_H_ #define COORDIN_H_ const int Len = 40; struct golf { char fullname[Len]; int handicap; }; 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 // !COORDIN_H_
//golf.cpp #include<iostream> #include<string> #include"demo1.h" using namespace std; void setgolf(golf& g, const char* name, int hc) { for (int i = 0; i < Len; i++) { g.fullname[i] = *name; name++; } g.handicap = hc; } int setgolf(golf& g) { char name[Len]; int hc; cout << "Please enter the name: "; cin.get(name, Len); cin.ignore(); cout << "and its hc: "; cin >> hc; if (name[0] != '\0') { setgolf(g,name,hc); return 1; } else { return 0; } } void handicap(golf& g, int hc) { g.handicap = hc; } void showgolf(const golf& g) { cout << g.fullname << endl; cout << g.handicap << endl; }
//main函数 #include<iostream> #include"demo1.h" using namespace std; int main() { golf g; setgolf(g, "sdhsu", 100); showgolf(g); setgolf(g); showgolf(g); system("pause"); return 0; }
2.
#include<iostream>
#include<string>
using namespace std;
int main() {
//2.
void strcount(const string str);
string str;
string str_test;
cout << "Enter a line:" << endl;;
getline(cin, str);
while (cin) {
str_test = str;
while (str_test == "") {
cout << "It's Null" << endl;
cout << "Bye!" << endl;
return 0;
}
strcount(str);
cout << "Enter next line (empty line to quit):\n ";
getline(cin, str);
}
system("pause");
return 0;
}
void strcount(const string str) {
static int total = 0;
int count = 0;
cout << "\"" << str << "\" contains ";
count = str.length();
total += count;
cout << count << " characters\n";
cout << total << " characters total\n";
}
4.
//头文件namespace4.h #pragma once #ifndef COORDIN_H_ #define COORDIN_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 // !COORDIN_H_
//函数定义func.cpp #include<iostream> #include"namespace4.h" using namespace SALES; //注意这里的“SALES::”必须写,因为这个函数声明在名称空间“SALES”中 void SALES::setSales(Sales& s, const double ar[], int n) { using namespace std; double sum = 0; double max, min; if (n >= QUARTERS) { for (int i = 0; i < QUARTERS; i++) { s.sales[i] = ar[i]; } } else { for (int i = 0; i < n; i++) { s.sales[i] = ar[i]; } } max = min = s.sales[0]; cout << "Data at a glance:" << endl; for (int i = 0; i < QUARTERS; i++) { cout << "NO." << i + 1 << "'s sales:" << s.sales[i] << endl; sum += s.sales[i]; if (max < s.sales[i]) max = s.sales[i]; if (min > s.sales[i]) min = s.sales[i]; } s.average = sum / QUARTERS; s.max = max; s.min = min; } void SALES::setSales(Sales& s) { using namespace std; double ar[QUARTERS]; for (int i = 0; i < QUARTERS; i++) { cout << "Please enter the NO." << i + 1 << " 's sales:"; cin >> ar[i]; } setSales(s, ar, 4); } void SALES::showSales(const Sales& s) { using namespace std; cout << "The average: " << s.average << endl; cout << "The max: "<< s.max << endl; cout << "The min: "<< s.min << endl; }
//主函数main #include<iostream> #include"namespace4.h" using namespace std; using namespace SALES; int main() { Sales s; setSales(s); showSales(s); system("pause"); return 0; }