Welcome, guest! Login / Register - Why register?
Psst.. new poll here.
Psst.. new forums here.
Microsoft is blocking us again (TY IP Reputation!) so just use oauth login instead. :)

Paste

Pasted as C# by dgvd ( 15 years ago )
// ----------- Дата у форматі день/місяць/рік --------------------
    class Date
    {

        private int day;
        private int month;
        private int year;

        public Date()
        {
            day = 1;
            month = 1;
            year = 1900;
        }

        public Date(int d, int m, int y)
        {
            day = d;
            month = m;
            year = y;
        }

        public int Day
        {
            get { return day; }
            set { day = value; }
        }

        public int Month
        {
            get { return month; }
            set { month = value; }
        }

        public int Year
        {
            get { return year; }
            set { year = value; }
        }

        public static bool operator == (Date d1, Date d2)
        {
            if (d1.day == d2.day && d1.month == d2.month && d1.year == d2.year)
                return true;
            else
                return false;
        }

        public static bool operator !=(Date d1, Date d2)
        {
            if (d1.year != d2.year)
                return true;
            else
                if (d1.month != d2.month)
                    return true;
                else
                    if (d1.day != d2.day)
                        return true;
                    else
                        return false;
        }

        public static bool operator < (Date d1, Date d2)
        {
            if (d1.year == d2.year)
                if (d1.month == d2.month)
                    if (d1.day < d2.day)
                        return true;
                    else
                        return false;
                else
                    if (d1.month < d2.month)
                        return true;

                    else
                        if (d1.year < d2.year)
                            return true;
                        else
                            return false;
            else
                if (d1.year < d2.year)
                    return true;
                else
                    return false;


        }

        public static bool operator > (Date d1, Date d2)
        {
            if (d1.year == d2.year)
                if (d1.month == d2.month)
                    if (d1.day > d2.day)
                        return true;
                    else
                        return false;
                else
                    if (d1.month > d2.month)
                        return true;

                    else
                        if (d1.year > d2.year)
                            return true;
                        else
                            return false;
            else
                if (d1.year > d2.year)
                    return true;
                else
                    return false;
        }

        public override string ToString()
        {
            if (day < 10)
            {
                if (month < 10)
                    return " " + day + "/ " + month + "/" + year;
                else
                    return " " + day + "/" + month + "/" + year;
            }
            else
            {
                if (month < 10)
                    return day + "/ " + month + "/" + year;
                else
                    return day + "/" + month + "/" + year;
            }
        }

    }

    class Information
    {

        private Date date;
        private double value_of_investments;
        private double transaction_amount;

        public Information()
        {
            date = new Date();
            value_of_investments = 0.00;
            transaction_amount = 0.00;
        }

        public Information(int day, int month, int year, double value_of_investments, double transaction_amount)
        {
            date = new Date(day, month, year);
            this.value_of_investments = value_of_investments;
            this.transaction_amount = transaction_amount;
        }


        public Date DATE
        {
            get { return date; }
            set { date = value; }
        }

        
        public double Val_Of_Invest
        {
            get { return value_of_investments; }
            set { value_of_investments = value; }
        }
        public double Trans_Am
        {
            get { return transaction_amount; }
            set { transaction_amount = value; }
        }

        public override string ToString()
        {
            return date.ToString() + "     " + value_of_investments + "      " + transaction_amount;
        }
    }


  
    class Program
    {  
        // Функція для визначення кількості днів між двома заданими датами
        static int TimePeriod(Date LastDate, Date FirstDate)
        {
            int result=0;
            int[] MonthesSize = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

            if (LastDate.Year - FirstDate.Year == 0)
            {
                int Subtraction_Of_Monthes = 0;
                Subtraction_Of_Monthes = LastDate.Month - FirstDate.Month;

                //=======================================================================
                if (Subtraction_Of_Monthes == 0)
                    result = LastDate.Day - FirstDate.Day;

                //=======================================================================

                if (Subtraction_Of_Monthes == 1)
                {
                    if (FirstDate.Month == 1 || FirstDate.Month == 3 || FirstDate.Month == 5 ||
                        FirstDate.Month == 7 || FirstDate.Month == 8 || FirstDate.Month == 10 || FirstDate.Month == 12)
                        result = 31 - FirstDate.Day + LastDate.Day;

                    else
                        if (FirstDate.Month == 2)
                        {
                            if (FirstDate.Year%4 == 0)
                            result = 29 - FirstDate.Day + LastDate.Day;
                            else
                                result = 28 - FirstDate.Day + LastDate.Day;
                        }
                        else
                            result = 30 - FirstDate.Day + LastDate.Day;
                }

                //=======================================================================

                if (Subtraction_Of_Monthes >= 2)
                {


                    int TimeRanges = 0; // к-ть днів між двома не сусідніми місяцями
                    int TempMonth = FirstDate.Month;
                    TempMonth++;
                    while (TempMonth < LastDate.Month)
                    {
                        TimeRanges += MonthesSize[TempMonth - 1];
                        TempMonth++;
                    }

                    result = MonthesSize[FirstDate.Month - 1] - FirstDate.Day + LastDate.Day + TimeRanges;
                }

            }

                // якщо різниця років більша за нуль
            else
            {
                int YearsSubtraction=0;

                int temp; // к-ть днів до кінця поточного року
                int index;
                temp = MonthesSize[FirstDate.Month - 1] - FirstDate.Day;
                index = FirstDate.Month;
                for (int i = index; i < 12; i++)
                    temp += MonthesSize[i];

                if (LastDate.Month == 1)
                {
                    temp += LastDate.Day;
                }

                else
                {
                    Date tempDate = new Date(1, 1, LastDate.Year);
                    int f = TimePeriod(LastDate, tempDate);
                    temp += f;
                    temp++;
                }

                YearsSubtraction = LastDate.Year - FirstDate.Year ;
                if (YearsSubtraction > 1)
                    temp += (YearsSubtraction - 1) * 365;


                result = temp;
            }



            return result;
        }
        static void Func(int k,double Eps, List<Information> DataList)
        {
            List<int> lDates_Subtraction = new List<int>();
            List<double> lCurrent_Amount = new List<double>();
            double A = 0;
            double Cj = 0, Dj = 0, Y0 = 0, Yj = 0;
            

            // ------ Поточна подiя -------------------------------------
            A = DataList[k].Val_Of_Invest + DataList[k].Trans_Am;
            // ----------------------------------------------------------

            for (int i = 0; i < k + 1; i++)
            {
                lCurrent_Amount.Add(DataList[i].Trans_Am);
                lDates_Subtraction.Add(TimePeriod(DataList[k].DATE, DataList[i].DATE));
            }

            // =============================== Реалiзацiя формул ==================================
            

            do
            {
                double Sum1 = 0;
                Y0 = Yj;

                for (int i = 0; i < lCurrent_Amount.Count; i++)
                    Sum1 += lCurrent_Amount[i] * Math.Exp(Convert.ToDouble(Y0 * lDates_Subtraction[i] / 365));
                Cj = A - Sum1;


                for (int i = 0; i < lCurrent_Amount.Count; i++)
                    Dj += lDates_Subtraction[i] * lCurrent_Amount[i] * Math.Exp(Convert.ToDouble(Y0 * lDates_Subtraction[i] / 365));

                Yj = Y0 + (Cj / Dj);


            }
            while (Math.Abs(Yj - Y0) > Eps);

            Console.WriteLine("  {0,10} {1,10:C2} {2,10:C2} {3,10:C2} {4,5} ", DataList[k].DATE, DataList[k].Val_Of_Invest, DataList[k].Trans_Am, DataList[k].Val_Of_Invest + DataList[k].Trans_Am, Yj);
            lCurrent_Amount.Clear();
            lDates_Subtraction.Clear();   

        }
        

        static void Main(string[] args)
        {

           
            string temp;
            
            List<Information> DataList = new List<Information>();
           

           
            StreamReader file_reader = new StreamReader("data.txt");
            Console.WriteLine("                   ---Вхiднi данi---\n ");
            try
            {
                while ((temp = file_reader.ReadLine()) != null)
                {

                    Information info = new Information();
                    string[] array_of_info = new string[3];
                    string[] array_of_date = new string[3];
                    array_of_info = temp.Split(' ');
                    array_of_date = (array_of_info[0]).Split('/');
                    Date d1 = new Date();
                    double Value_Of_Investments;
                    double Transaction_Amount;
                    try
                    {

                        d1.Day = Convert.ToInt32(array_of_date[0]);
                        d1.Month = Convert.ToInt32(array_of_date[1]);
                        d1.Year = Convert.ToInt32(array_of_date[2]);

                        Value_Of_Investments = Convert.ToDouble(array_of_info[1]);
                        Transaction_Amount = Convert.ToDouble(array_of_info[2]);

                        info.DATE = d1;
                        info.Val_Of_Invest = Value_Of_Investments;
                        info.Trans_Am = Transaction_Amount;
                        DataList.Add(info);
                       
                        
                    }
                    catch (Exception exc)
                    {
                        Console.WriteLine("Неправильнi данi !");
                        Console.WriteLine(exc.Message);
                    }
                   
                    Console.WriteLine("{0,15} {1,15:C2} {2,15:C2}",(info.DATE).ToString(),info.Val_Of_Invest,info.Trans_Am);
                    
                }
            }
            catch (IOException mes)
            {
                Console.WriteLine("Помилка читання з файлу !");
                Console.WriteLine(mes.Message);
            }

            file_reader.Close();

// ====================== Перевірка чи збережено хронологічний порядок ======================

            Information tempSort = new Information();
            for (int i = 0; i < DataList.Count - 1; i++)
                for (int j = i + 1; j < DataList.Count; j++)
                {
                    if (DataList[i].DATE > DataList[j].DATE)
                    {
                        tempSort = DataList[i];
                        DataList[i] = DataList[j];
                        DataList[j] = tempSort;
                    }
                }

           
// ==========================================================================================           

                    Console.WriteLine("\n\n    *******************************************\n\n");
                    
           

// =================== Перевірка коректності даних ================================
                    bool Err = false;
                    for (int i = 0; i < DataList.Count; i++ )
                    {
                        if (DataList[i].Trans_Am < 0)
                        {
                            if (DataList[i].Val_Of_Invest + DataList[i].Trans_Am < 0)
                            {

                                Console.WriteLine("\n    Некоректнi данi! В {0} -му рядку сума\n    знятих коштiв перевищує дозволену !!!", i + 1);
                                Err = true;
                                break;
                            }
                        }
                        
                    }
// ================================= Обчислення доходу ========================================
                    if (Err == false)
                    {                      
                        //Console.WriteLine("  Введiть дату для якої потрiбно обчислити дохiд: ");
                        //string choise;
                        //string[] cur_dat = new string[3];
                        //Date CurrentDate = new Date();
                        //try
                        //{
                        //    Console.Write("  ");
                        //    choise = Console.ReadLine();
                        //    cur_dat = choise.Split('/');
                        //    CurrentDate.Day = Convert.ToInt32(cur_dat[0]);
                        //    CurrentDate.Month = Convert.ToInt32(cur_dat[1]);
                        //    CurrentDate.Year = Convert.ToInt32(cur_dat[2]);

                        //}
                        //catch (Exception mes)
                        //{
                        //    Console.WriteLine("Bad argument !!!");
                        //    Console.WriteLine(mes.Message);
                        //}

                        Console.WriteLine("\n\n  Введiть точнiсть (Eps): ");
                        double Eps = 0;
                        Console.Write("  ");
                        try
                        {
                            Eps = Convert.ToDouble(Console.ReadLine());
                        }
                        catch (Exception exc)
                        {
                            Console.WriteLine("\n  Iде обробка даних.....\n");
                            if (exc.Source == Convert.ToString("."))
                                exc.Source = ",";

                        }
                        Console.WriteLine("\n\n");
                        for (int i = 0; i < DataList.Count; i++)
                            Func(i, Eps, DataList);
                        
                       
                          
       
                        
                    }
                    else
                        Console.Write("\n    Перезапустiть програму виправивши данi !!!");

            Console.ReadKey();
            
        }
    }
}

 

Revise this Paste

Your Name: Code Language: