植物生长问题
来源:互联网 发布:php开发的博客 编辑:程序博客网 时间:2024/04/28 09:41
"head.h"#include <iostream>#include <termios.h>using namespace std;class Tree{public: double Grow(double len); Tree(double len=0); double Get(){return len;}private: double len;};class Bamboo{public: double Grow(double len); Bamboo(double len=0); double Get(){return len;}private: double len;};class PlantGrow{private: Tree tree[5]; Bamboo bam[4]; double totalTreeLength,totalBambooLength;public: PlantGrow(); ~PlantGrow(); void Grow(int day); void Compare(); void GetResult();};"PlantGrow.cpp"#include "head.h"PlantGrow::PlantGrow(){ int ii,jj; for(ii=0;ii<2;ii++){ tree[ii] = Tree(2); } for(ii=2;ii<5;ii++){ tree[ii] = Tree(3); } bam[0] = Bamboo(0.5); for(jj=1;jj<4;jj++){ bam[jj] = Bamboo(1); } int cc; totalTreeLength = totalBambooLength = 0; for(cc=0;cc<5;cc++) { totalTreeLength+=tree[cc].Get(); } for(cc=0;cc<4;cc++) { totalBambooLength+=bam[cc].Get(); }}/*void PlantGrow::Len(){ int ii,jj; for(ii = 0;ii<5;ii++){ totalTreeLength += this->tree[ii].Get(); } for(jj = 0;jj<4;jj++){ totalBambooLength += this->tree[ii].Get(); }}*/PlantGrow::~PlantGrow(){}Tree::Tree(double len){ this->len = len;}Bamboo::Bamboo(double len){ this->len = len;}void PlantGrow::GetResult(){}void PlantGrow::Grow(int day){ int ii,jj; for(ii = 0;ii<5;ii++){ totalTreeLength += tree[ii].Grow(tree[ii].Get()); } cout<<"\t\tcurrent totalTreeLength is "<<totalTreeLength<<endl; for(jj = 0;jj<4;jj++){ totalBambooLength += bam[jj].Grow(bam[jj].Get()); } cout<<"\t\tcurrent totalBambooLength is "<<totalBambooLength<<endl;}double Tree::Grow(double len){ double tlen = len; if(tlen<5){ tlen=tlen*0.02; } else { tlen=tlen*0.01; } return tlen;}double Bamboo::Grow(double len){ double blen = len; if(blen<2){ blen=blen*0.05; } else { blen=blen*0.02; } return blen;}void PlantGrow::Compare(){//totalTreeLength = totalBambooLength if(totalTreeLength > totalBambooLength){ cout<<"\t\tTrees are more taller " <<totalTreeLength-totalBambooLength<<endl; } else if(totalTreeLength < totalBambooLength){ cout<<"\t\tBamboo are more taller "<<totalBambooLength-totalTreeLength<<endl; } else { cout<<"Trees are the same tall as bamboos"<<endl; }}int main(){ PlantGrow pg; cout<<"当前:\n\t\t5棵树,两棵2米高,3棵3米高"<<endl; cout<<"\t\t4棵毛竹1棵高0.5米,3棵高1米"<<endl; while(1) { //system("clear"); cout<<"\t\t输入观察的天数:"; int day; cin>>day; while(day>0) { pg.Grow(day); day--; } pg.Compare(); } return 0;}