【学习C++】C++ Primer Plus (第六版)第九章编程练习1-4
2016-04-27 16:44
555 查看
1.
2.#include <iostream>
#include <string>
using namespace std;
void strcount(const string);
int main()
{
string input;
cout << "Enter a line: \n";
getline(cin, input);
while (input != "\0"){
strcount(input);
cout << "Enter next line (empty line to quit) : \n";
getline(cin, input);
}
cout << "Bye\n";
return 0;
}
void strcount(const string str){
static int total = 0;
int count = str.size();
total += count;
cout << "\"" << str << "\"contains ";
cout << count << " characters\n";
cout << total << " charaters total\n";
}3.
//golf.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); //golf.cpp #include <iostream> #include "golf.h" void setgolf(golf & g, const char * name, int hc){ g.handicap = hc; \ int len = strlen(name); for (int i = 0; i < Len; i++){ g.fullname[i] = name[i]; } } int setgolf(golf & g){ std::cout << "Please enter the fullname : "; char name[Len]; std::cin.getline(name, Len); std::cout << "Please enter handicap : "; int han; std::cin >> han; std::cin.get(); setgolf(g, name, han); if (name[0] == '\0') return 0; else return 1; } void handicap(golf & g, int hc){ g.handicap = hc; } void showgolf(const golf & g){ std::cout << g.fullname << ", "<<g.handicap<<std::endl; } //main.cpp #include <iostream> #include "golf.h" int main(){ golf gl[3]; for (int i = 0; i < 3; i++){ if (setgolf(gl[i]) == 0) break; } for (int i = 0; i < 3; i++) showgolf(gl[i]); for (int i = 0; i < 3; i++){ handicap(gl[i], 1); showgolf(gl[i]); } std:: cin.get(); return 0; }
2.#include <iostream>
#include <string>
using namespace std;
void strcount(const string);
int main()
{
string input;
cout << "Enter a line: \n";
getline(cin, input);
while (input != "\0"){
strcount(input);
cout << "Enter next line (empty line to quit) : \n";
getline(cin, input);
}
cout << "Bye\n";
return 0;
}
void strcount(const string str){
static int total = 0;
int count = str.size();
total += count;
cout << "\"" << str << "\"contains ";
cout << count << " characters\n";
cout << total << " charaters total\n";
}3.
#include <iostream> struct chaff { char dross[20]; int slag; }; char buffer[60]; using namespace std; int main() { chaff *ch = new(buffer) chaff[2]; for (int i = 0; i < 2; i++){ char cha[20]; cin.get(cha, 20); while (cin.get() != '\n') continue; strcpy(ch[i].dross, cha); int a; cin >> a; ch[i].slag = a; cin.get(); } for (int i = 0; i < 2; i++){ cout << ch[i].dross << " ," << ch[i].slag; cout << " address : " << &ch[i] << endl; } cout << "buffer address : " << (void *) buffer << endl; cin.get(); return 0; }4.
//sales.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); } //sales.cpp #include <iostream> #include "sales.h" namespace SALES { void setSales(Sales & s, const double ar [], int n){ int times = QUARTERS < n ? QUARTERS : n; double total = 0; double ma=ar[0], mi=ar[0]; for (int i = 0; i < times; i++){ s.sales[i] = ar[i]; total += s.sales[i]; if (ma < ar[i]) ma = ar[i]; if (mi>ar[i]) mi = ar[i]; } for (int i = times; i < QUARTERS; i++){ s.sales[i] = 0; } s.average = total / times; s.max = ma; s.min = mi; } void setSales(Sales & s){ double total=0; for (int i = 0; i < QUARTERS; i++){ std::cin >> s.sales[i]; total += s.sales[i]; } double ma = s.sales[0],mi=s.sales[0]; for (int i = 0; i < QUARTERS; i++){ if (ma < s.sales[i]) ma = s.sales[i]; if (mi>s.sales[i]) mi = s.sales[i]; } s.average = total / 4; s.max = ma; s.min = mi; } void showSales(const Sales & s){ std::cout << "values:\n"; for (int i = 0; i < 4; i++) std::cout << s.sales[i]<<std::endl; std::cout <<"average: "<<s.average << std::endl; std::cout << "max: "<<s.max << std::endl; std::cout << "min: "<<s.min << std::endl; } } //main.cpp #include <iostream> #include "sales.h" int main() { SALES::Sales a, b; double ar[3] = { 0.1, 0.2, 0.3 }; SALES::setSales(a, ar, 3); SALES::showSales(a); std::cout << "Please input 4 numbers: \n"; SALES::setSales(b); SALES::showSales(b); std::cin.get(); std::cin.get(); return 0; }
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- 关于指针的一些事情
- c++ primer 第五版 笔记前言
- share_ptr的几个注意点
- Lua中调用C++函数示例
- Lua教程(一):在C++中嵌入Lua脚本
- Lua教程(二):C++和Lua相互传递数据示例
- C++联合体转换成C#结构的实现方法
- C++高级程序员成长之路
- C++编写简单的打靶游戏
- C++ 自定义控件的移植问题
- C++变位词问题分析
- C/C++数据对齐详细解析
- C++基于栈实现铁轨问题
- C++中引用的使用总结
- 使用Lua来扩展C++程序的方法
- C++中调用Lua函数实例
- Lua和C++的通信流程代码实例
- C与C++之间相互调用实例方法讲解
- 解析C++中派生的概念以及派生类成员的访问属性