您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: