植物生长问题

"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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值