CHAPTER 04:EX 07
2008-05-17 23:48
471 查看
//7. Make a Stash that holds doubles. Fill it with 25 double
//values, then print them out to the console.
#ifndef SOLUTION07_H
#define SOLUTION07_H
struct Stash {
int quantity;
int index;
double *storage;
void initialize ();
void inflate (int increase);
int add (double n);
double fetch(int index);
void cleanup ();
};
#endif //SOLUTION07_H
//7. Make a Stash that holds doubles. Fill it with 25 double
//values, then print them out to the console.
#include <iostream>
#include "solution07.h"
using namespace std;
const int increase = 25;
int main () ...{
double t = 25.1;
Stash number;
number.initialize();
for (int i = 0; i < 25; i++)
number.add(t + i);
for (i = 0; i < 25; i++)
cout << number.fetch (i) << ' ';
cout << endl;
number.cleanup();
}
//7. Make a Stash that holds doubles. Fill it with 25 double
//values, then print them out to the console.
#include <iostream>
#include "solution07.h"
using namespace std;
const int increase = 5;
void Stash::initialize () {
quantity = 0;
index = 0;
storage = 0;
}
void Stash::inflate (int increase) {
int oldquantity = quantity;
int newquantity = oldquantity + increase;
double *old = storage;
storage = new double[newquantity];
for (int i = 0; i < oldquantity; i++)
storage[i] = old[i];
delete []old;
quantity = newquantity;
}
int Stash::add (double n) {
if (index >= quantity)
inflate(increase);
storage[index] = n;
index++;
return index;
}
double Stash::fetch(int index) {
return storage[index];
}
void Stash::cleanup () {
cout << "freeing storage..." <<endl;
int quantity = 0;
int index = 0;
delete []storage;
}
//values, then print them out to the console.
#ifndef SOLUTION07_H
#define SOLUTION07_H
struct Stash {
int quantity;
int index;
double *storage;
void initialize ();
void inflate (int increase);
int add (double n);
double fetch(int index);
void cleanup ();
};
#endif //SOLUTION07_H
//7. Make a Stash that holds doubles. Fill it with 25 double
//values, then print them out to the console.
#include <iostream>
#include "solution07.h"
using namespace std;
const int increase = 25;
int main () ...{
double t = 25.1;
Stash number;
number.initialize();
for (int i = 0; i < 25; i++)
number.add(t + i);
for (i = 0; i < 25; i++)
cout << number.fetch (i) << ' ';
cout << endl;
number.cleanup();
}
//7. Make a Stash that holds doubles. Fill it with 25 double
//values, then print them out to the console.
#include <iostream>
#include "solution07.h"
using namespace std;
const int increase = 5;
void Stash::initialize () {
quantity = 0;
index = 0;
storage = 0;
}
void Stash::inflate (int increase) {
int oldquantity = quantity;
int newquantity = oldquantity + increase;
double *old = storage;
storage = new double[newquantity];
for (int i = 0; i < oldquantity; i++)
storage[i] = old[i];
delete []old;
quantity = newquantity;
}
int Stash::add (double n) {
if (index >= quantity)
inflate(increase);
storage[index] = n;
index++;
return index;
}
double Stash::fetch(int index) {
return storage[index];
}
void Stash::cleanup () {
cout << "freeing storage..." <<endl;
int quantity = 0;
int index = 0;
delete []storage;
}
相关文章推荐
- CHAPTER 04:EX 18
- CHAPTER 04:EX 24
- CHAPTER 04:EX 25
- CHAPTER 05:EX 04
- CHAPTER 07:EX 01
- Chapter 5-04
- 第13、14讲:Chapter 04--Creating a Database
- Chapter 07 - Pivot, Unpivot and Grouping Sets - Solutions
- 2016—04—07 北京
- 专线维护--07/04
- 学习笔记 07-01-04
- CHAPTER 03:EX 33
- CHAPTER 10:EX 33
- 04/07/09
- head first JavaScript chapter 04 & 05 & 06
- Inside C#_Chapter1(04)
- Chapter 04-Tuning the shared Pool
- Chapter 04-Using Conversion Functions and Conditional Expressions(00)
- 3 视频里weekend05、06、07的可靠性 + HA原理、分析、机制 + weekend01、02、03、04、05、06、07的分布式集群搭建
- [bbk2344] 第49集 - Chapter 12-Optimizing Sore Perations(04)