public int ComputeSequence(int toNumber, ComputeMethod sum)
        {
            int result = 1;

            if (toNumber <= 0)
            {
                throw new ArgumentException();
            }

            if (sum == ComputeMethod.Sum)
            {
                for (int i = 2; i <= toNumber; i++)
                {
                    result += i;
                }
            }

            else if (sum == ComputeMethod.Product)
            {
                for (int i = 2; i <= toNumber; i++)
                {
                    result *= i;
                }
            }

            return(result);
        }
        public int ComputeSequence(int toNumber, ComputeMethod sum)
        {
            if (toNumber <= 0)
            {
                throw new ArgumentException();
            }

            var range = Enumerable.Range(1, toNumber).ToList();

            switch (sum)
            {
            case ComputeMethod.Sum:
            {
                return(range.Sum());
            }

            case ComputeMethod.Product:
            {
                return(range.Aggregate((a, b) => a * b));
            }

            default:
            {
                throw new NotImplementedException();
            }
            }
        }
    public void ChangeComputeMethod(int idx)
    {
        computeMethod = (ComputeMethod)idx;
        float start = Time.realtimeSinceStartup;

        Reconstruct();
        CanvasUI.RecoTime.text = String.Format("Reconstruction Time: {0}", Time.realtimeSinceStartup - start);
    }
        //---------------------------------------------------------------------

        /// <summary>
        /// Grows the cohorts during spin-up
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static ISiteCohorts MakeBiomassCohorts(List <Landis.Library.AgeOnlyCohorts.ICohort> ageCohorts,
                                                      ActiveSite site,
                                                      ComputeMethod initialBiomassMethod)
        {
            IEcoregion ecoregion = PlugIn.ModelCore.Ecoregion[site];

            //if (Climate.Spinup_AllData.Count >= ageCohorts[0].Age)
            //try
            //{
            //PlugIn.ModelCore.UI.WriteLine("Making Biomass Cohorts using spin-up climate data");

            SiteVars.Cohorts[site] = new Library.LeafBiomassCohorts.SiteCohorts();
            if (ageCohorts.Count == 0)
            {
                return(SiteVars.Cohorts[site]);
            }

            int indexNextAgeCohort = 0;

            //  The index in the list of sorted age cohorts of the next
            //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.

            //int endTime = (successionTimestep == 1) ? -1 : -1;
            //PlugIn.ModelCore.UI.WriteLine("  Ageing initial cohorts.  Oldest cohorts={0} yrs, succession timestep={1}, endTime={2}.", ageCohorts[0].Age, successionTimestep, endTime);
            for (int time = -(ageCohorts[0].Age); time <= -1; time += successionTimestep)
            {
                //PlugIn.ModelCore.UI.WriteLine("  Ageing initial cohorts.  Oldest cohorts={0} yrs, succession timestep={1}.", ageCohorts[0].Age, successionTimestep);
                EcoregionData.SetSingleAnnualClimate(ecoregion, time + ageCohorts[0].Age, Climate.Phase.SpinUp_Climate);     //the spinup climate array is sorted from oldest to newest years

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count && ageCohorts[indexNextAgeCohort].Age == -time)
                {
                    ISpecies species = ageCohorts[indexNextAgeCohort].Species;

                    float[] initialBiomass = initialBiomassMethod(species, SiteVars.Cohorts[site], site);

                    SiteVars.Cohorts[site].AddNewCohort(ageCohorts[indexNextAgeCohort].Species, 1,
                                                        initialBiomass[0], initialBiomass[1]);
                    indexNextAgeCohort++;
                }

                Main.Run(site, successionTimestep, true);
            }
            return(SiteVars.Cohorts[site]);
        }
        //---------------------------------------------------------------------

        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List <Landis.Library.AgeOnlyCohorts.ICohort> ageCohorts,
                                                     ActiveSite site,
                                                     ComputeMethod initialBiomassMethod)
        {
            SiteVars.Cohorts[site] = new Library.BiomassCohorts.SiteCohorts();

            if (ageCohorts.Count == 0)
            {
                return(SiteVars.Cohorts[site]);
            }

            int indexNextAgeCohort = 0;
            //  The index in the list of sorted age cohorts of the next
            //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;

            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep)
            {
                //  Grow current biomass cohorts.
                PlugIn.GrowCohorts(site, successionTimestep, true);

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time)
                {
                    ISpecies species = ageCohorts[indexNextAgeCohort].Species;

                    int initialBiomass = initialBiomassMethod(species, SiteVars.Cohorts[site], site);

                    SiteVars.Cohorts[site].AddNewCohort(ageCohorts[indexNextAgeCohort].Species, 1,
                                                        initialBiomass);


                    //foreach (ISpeciesCohorts spp in SiteVars.Cohorts[site])
                    //    foreach (ICohort co in spp)
                    //        PlugIn.ModelCore.Log.WriteLine("I'm born!  My name is {0}.", co.Species.Name);
                    indexNextAgeCohort++;
                }
            }

            //PlugIn.ModelCore.Log.WriteLine("Initial Community = {0}.", SiteVars.Cohorts[site].Write());
            return(SiteVars.Cohorts[site]);
        }
Exemplo n.º 6
0
        public int ComputeSequence(int toNumber, ComputeMethod sum)

        {
            if (toNumber <= 0) //att input ska vara RÄTT (inte null) input mindre eller likamed
            {
                throw new ArgumentException();
            }

            if (sum == ComputeMethod.Sum)
            {
                return(ComputeSequenceSumOrProduct(toNumber, true));
            }
            else // (sum == ComputeMethod.Product)
            {
                return(ComputeSequenceSumOrProduct(toNumber, false)); //VAD BETYDER DETTA?! NÄR ANVÄNDER MAN DETTA?!
            }
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static ISiteCohorts MakeBiomassCohorts(List<Landis.Library.AgeOnlyCohorts.ICohort> ageCohorts,
            ActiveSite site,
            ComputeMethod initialBiomassMethod)
        {
            SiteVars.Cohorts[site] = new Library.LeafBiomassCohorts.SiteCohorts();
            if (ageCohorts.Count == 0)
              return SiteVars.Cohorts[site];

            int indexNextAgeCohort = 0;
            //  The index in the list of sorted age cohorts of the next
            //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            //PlugIn.ModelCore.Log.WriteLine("  Adding new cohorts.  Oldest cohorts={0}, timestep={1}.", ageCohorts[0].Age, successionTimestep);
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep)
            {
                EcoregionData.GenerateNewClimate(0, successionTimestep);

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time)
                {
                    ISpecies species = ageCohorts[indexNextAgeCohort].Species;

                    float[] initialBiomass = initialBiomassMethod(species, SiteVars.Cohorts[site], site);

                    SiteVars.Cohorts[site].AddNewCohort(ageCohorts[indexNextAgeCohort].Species, 1,
                                                initialBiomass[0], initialBiomass[1]);
                    indexNextAgeCohort++;
                }

                Century.Run(site, successionTimestep, true);

            }

            return SiteVars.Cohorts[site];
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List<AgeCohort.ICohort> ageCohorts,
            ActiveSite site,
            ComputeMethod initialBiomassMethod)
        {
            //SiteCohorts biomassCohorts = new SiteCohorts();
            if (ageCohorts.Count == 0)
                //return biomassCohorts;
                return SiteVars.SiteCohorts[site];

            int indexNextAgeCohort = 0;
            //  The index in the list of sorted age cohorts of the next
            //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep)
            {
                EcoregionData.GenerateNewClimate(0, successionTimestep);

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time)
                {
                    float[] initialBiomass = initialBiomassMethod(SiteVars.SiteCohorts[site],
                                                                 site, ageCohorts[indexNextAgeCohort].Species);
                    SiteVars.SiteCohorts[site].AddNewCohort(ageCohorts[indexNextAgeCohort].Species,
                                                initialBiomass[0], initialBiomass[1]);
                    indexNextAgeCohort++;
                }

                Century.Run(SiteVars.SiteCohorts[site], site.Location, successionTimestep, true);

                //if(time % fire.frequency == 0)
                // reduce cohorts by X%
                // FireEffects.ReduceLayers(site);

            }

            return SiteVars.SiteCohorts[site];
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List<AgeCohort.ICohort> ageCohorts,
            ActiveSite              site,
            ComputeMethod           initialBiomassMethod)
        {
            SiteCohorts biomassCohorts = new SiteCohorts();
            if (ageCohorts.Count == 0)
                return biomassCohorts;

            int indexNextAgeCohort = 0;
                //  The index in the list of sorted age cohorts of the next
                //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep) {
                //  Grow current biomass cohorts.
                Util.GrowCohorts(biomassCohorts, site, successionTimestep, true);

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time) {
                    ushort initialBiomass = initialBiomassMethod(biomassCohorts,
                                                                 site,
                                                                 ageCohorts[indexNextAgeCohort].Species);
                    biomassCohorts.AddNewCohort(ageCohorts[indexNextAgeCohort].Species,
                                                initialBiomass);
                    indexNextAgeCohort++;
                }
            }

            return biomassCohorts;
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List<Landis.Library.AgeOnlyCohorts.ICohort> ageCohorts,
            ActiveSite site,
            ComputeMethod initialBiomassMethod)
        {
            // ISiteCohorts biomassCohorts = new SiteCohorts();
            //SiteCohorts biomassCohorts = new SiteCohorts();

            SiteVars.Cohorts[site] = new SiteCohorts(); //biomassCohorts;

            // if (ageCohorts.Count == 0)
            //    return SiteVars.Cohorts[site]; //biomassCohorts;

            if (SiteVars.Cohorts == null)
                PlugIn.ModelCore.Log.WriteLine("Damn BioSuccession !!");

            //if (ageCohorts.Count == 0)
            //    return biomassCohorts;

            int indexNextAgeCohort = 0;
            //  The index in the list of sorted age cohorts of the next
            //  cohort to be considered

            //  Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep)
            {
                //  Grow current biomass cohorts.
                PlugIn.GrowCohorts(SiteVars.Cohorts[site], site, successionTimestep, true);
                //PlugIn.GrowCohorts(site, successionTimestep, true);

                //  Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time)
                {

                    ISpecies species = ageCohorts[indexNextAgeCohort].Species;

                    int initialBiomass = initialBiomassMethod(species, SiteVars.Cohorts[site], site);

                    SiteVars.Cohorts[site].AddNewCohort(ageCohorts[indexNextAgeCohort].Species,
                                                initialBiomass);

                    //PlugIn.ModelCore.Log.WriteLine("I'm born!");

                    //foreach (ISpeciesCohorts spp in SiteVars.Cohorts[site])
                    //    foreach (ICohort co in spp)
                    //        PlugIn.ModelCore.Log.WriteLine("I'm born!  My name is {0}.", co.Species.Name);
                    indexNextAgeCohort++;
                }
            }

            // return biomassCohorts;
            return SiteVars.Cohorts[site];
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        ///   at the site, using a specified method for computing a cohort's
        ///   initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static SiteCohorts MakeBiomassCohorts(List<AgeCohort.ICohort> ageCohorts,
                                                     ActiveSite site,
                                                     ComputeMethod initialBiomassMethod)
        {
            // Fix to keep initial mineral N and P as paramterized for year 1.
            double minN = SiteVars.MineralSoil[site].ContentN;
            double minP = SiteVars.MineralSoil[site].ContentP;

            SiteCohorts biomassCohorts = new SiteCohorts();
            if (ageCohorts.Count == 0)
                return biomassCohorts;

            int indexNextAgeCohort = 0;
            //The index in the list of sorted age cohorts of the next
            //  cohort to be considered.

            //Loop through time from -N to 0 where N is the oldest cohort.
            //  So we're going from the time when the oldest cohort was "born"
            //  to the present time (= 0).  Because the age of any age cohort
            //  is a multiple of the succession timestep, we go from -N to 0
            //  by that timestep.  NOTE: the case where timestep = 1 requires
            //  special treatment because if we start at time = -N with a
            //  cohort with age = 1, then at time = 0, its age will N+1 not N.
            //  Therefore, when timestep = 1, the ending time is -1.
            int endTime = (successionTimestep == 1) ? -1 : 0;
            for (int time = -(ageCohorts[0].Age); time <= endTime; time += successionTimestep)
            {
                //Grow current biomass cohorts.
                NutrientSuccession.InitGrowCohorts(biomassCohorts, site, successionTimestep, true);

                //Add those cohorts that were born at the current year
                while (indexNextAgeCohort < ageCohorts.Count &&
                       ageCohorts[indexNextAgeCohort].Age == -time)
                {
                    float[] initialBiomass = initialBiomassMethod(biomassCohorts,
                                                                 site, ageCohorts[indexNextAgeCohort].Species);
                    float initialWoodBiomass = initialBiomass[0];
                    float initialLeafBiomass = initialBiomass[1];

                    biomassCohorts.AddNewCohort(ageCohorts[indexNextAgeCohort].Species,
                        initialWoodBiomass, initialLeafBiomass);
                    indexNextAgeCohort++;
                }
            }

            // Reset mineral nutrients
            SiteVars.MineralSoil[site].ContentN = minN;
            SiteVars.MineralSoil[site].ContentP = minP;

            return biomassCohorts;
        }
        //---------------------------------------------------------------------
        /// <summary>
        /// Grows the cohorts during spin-up
        /// Makes the set of biomass cohorts at a site based on the age cohorts
        /// at the site, using a specified method for computing a cohort's
        /// initial biomass.
        /// </summary>
        /// <param name="ageCohorts">
        /// A sorted list of age cohorts, from oldest to youngest.
        /// </param>
        /// <param name="site">
        /// Site where cohorts are located.
        /// </param>
        /// <param name="initialBiomassMethod">
        /// The method for computing the initial biomass for a new cohort.
        /// </param>
        public static ISiteCohorts MakeBiomassCohorts(List<Landis.Library.AgeOnlyCohorts.ICohort> ageCohorts,   
            ActiveSite site,
            ComputeMethod initialBiomassMethod)
        {
            IEcoregion ecoregion = PlugIn.ModelCore.Ecoregion[site];
            //if (Climate.Spinup_AllData.Count >= ageCohorts[0].Age)
            //try
            //{
                //PlugIn.ModelCore.UI.WriteLine("Making Biomass Cohorts using spin-up climate data");

                SiteVars.Cohorts[site] = new Library.LeafBiomassCohorts.SiteCohorts();
                if (ageCohorts.Count == 0)
                  return SiteVars.Cohorts[site];

                int indexNextAgeCohort = 0;
                //  The index in the list of sorted age cohorts of the next
                //  cohort to be considered

                //  Loop through time from -N to 0 where N is the oldest cohort.
                //  So we're going from the time when the oldest cohort was "born"
                //  to the present time (= 0).  Because the age of any age cohort
                //  is a multiple of the succession timestep, we go from -N to 0
                //  by that timestep.  NOTE: the case where timestep = 1 requires
                //  special treatment because if we start at time = -N with a
                //  cohort with age = 1, then at time = 0, its age will N+1 not N.
                //  Therefore, when timestep = 1, the ending time is -1.

                //int endTime = (successionTimestep == 1) ? -1 : -1;
                //PlugIn.ModelCore.UI.WriteLine("  Ageing initial cohorts.  Oldest cohorts={0} yrs, succession timestep={1}, endTime={2}.", ageCohorts[0].Age, successionTimestep, endTime);
                for (int time = -(ageCohorts[0].Age); time <= -1; time += successionTimestep)
                {
                     //PlugIn.ModelCore.UI.WriteLine("  Ageing initial cohorts.  Oldest cohorts={0} yrs, succession timestep={1}.", ageCohorts[0].Age, successionTimestep);
                    EcoregionData.SetSingleAnnualClimate(ecoregion, time + ageCohorts[0].Age, Climate.Phase.SpinUp_Climate); //the spinup climate array is sorted from oldest to newest years

                     //  Add those cohorts that were born at the current year
                     while (indexNextAgeCohort < ageCohorts.Count && ageCohorts[indexNextAgeCohort].Age == -time)
                        {
                            ISpecies species = ageCohorts[indexNextAgeCohort].Species;

                            float[] initialBiomass = initialBiomassMethod(species, SiteVars.Cohorts[site], site);

                            SiteVars.Cohorts[site].AddNewCohort(ageCohorts[indexNextAgeCohort].Species, 1,
                                                        initialBiomass[0], initialBiomass[1]);
                            indexNextAgeCohort++;
                        }

                        Century.Run(site, successionTimestep, true);

                }
            //}
            //catch (ClimateDataOutOfRangeException ex)
            //{
            //    throw ex;   //do nothing
            //}
            return SiteVars.Cohorts[site];
        }
Exemplo n.º 13
0
        public decimal?Compute(List <CourseScore> courseScoreList, ComputeMethod method)
        {
            if (method == ComputeMethod.加權平均)
            {
                decimal sum     = decimal.Zero;
                decimal credits = decimal.Zero;

                foreach (var cs in courseScoreList)
                {
                    JHCourseRecord course = Courses[cs.CourseID];
                    if (!course.Credit.HasValue && course.Credit.Value <= 0)
                    {
                        continue;
                    }
                    if (!cs.Score.HasValue)
                    {
                        continue;
                    }

                    sum     += course.Credit.Value * cs.Score.Value;
                    credits += course.Credit.Value;
                }

                if (credits > 0)
                {
                    return(sum / credits);
                }
                else
                {
                    return(null);
                }
            }
            else if (method == ComputeMethod.加權總分)
            {
                decimal sum = decimal.Zero;

                foreach (var cs in courseScoreList)
                {
                    JHCourseRecord course = Courses[cs.CourseID];
                    if (!course.Credit.HasValue && course.Credit.Value <= 0)
                    {
                        continue;
                    }
                    if (!cs.Score.HasValue)
                    {
                        continue;
                    }

                    sum += course.Credit.Value * cs.Score.Value;
                }

                return(sum);
            }
            else if (method == ComputeMethod.合計總分)
            {
                decimal sum = decimal.Zero;

                foreach (var cs in courseScoreList)
                {
                    if (!cs.Score.HasValue)
                    {
                        continue;
                    }
                    sum += cs.Score.Value;
                }

                return(sum);
            }
            else if (method == ComputeMethod.算術平均)
            {
                decimal sum   = decimal.Zero;
                decimal count = 0;

                foreach (var cs in courseScoreList)
                {
                    if (!cs.Score.HasValue)
                    {
                        continue;
                    }

                    sum += cs.Score.Value;
                    count++;
                }

                if (count > 0)
                {
                    return(sum / count);
                }
                else
                {
                    return(null);
                }
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 14
0
 public ThreadWorker(ComputeMethod method)
 {
     run = method;
 }