C# :: Aufgabe #262 :: Lösung #7

8 Lösungen Lösungen öffentlich
#262

Array von Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln

Anfänger - C# von Gustl - 08.05.2019 um 20:08 Uhr
Schreibe ein Programm welches aus einem Array von Dezimalzahlen diese Zahlen in die nächstgelegene durch 5 teilbare Zahl umwandeln.

Etwa so:

Konsolenausgabe:

 7.1 => 5
8.4 => 10
-2.4 => 0
-2.6 => -5
-8.3 => -10
#7
vote_ok
von Developer# (50 Punkte) - 29.10.2019 um 17:51 Uhr
Quellcode ausblenden C#-Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _5_Divisble_Array
{
    class Program
    {
        static void Main(string[] args)
        {
            double[] arr = new double[5];
            arr[0] = 7.1;
            arr[1] = 8.4;
            arr[2] = -2.4;
            arr[3] = -2.6;
            arr[4] = -8.3;

            double[] newArr = DivisibleTo5(arr);
            PrintArr(arr, newArr);
            Console.Read();
        }

        static double[] DivisibleTo5(double[] arr)
        {
            double[] newArr = new double[arr.Length];
            int subNum = 0;
            int addNum = 0;
            bool isBreak = false;
            //Algorithmus: Zahl zu erst zu einer geraden zahl ohne komma machen und immer 
            //um 1 erhöhen und um 1 subtrahieren(dann um 2 erhöhen und subtrahieren,3...) 
            //und schauen ob die Zahl durch 5 tielbar ist

            for (int i = 0; i < arr.Length; i++)
            {
                int num = MakeKommaWeg(arr[i]); //7.1 --> 7
                int counter = 1;
                isBreak = false;
                while (!isBreak)
                {
                    addNum = num + counter; //8 dann 9 usw...
                    subNum = num - counter; //6 dann 5 usw...

                    if (addNum % 5 == 0)
                    {
                        newArr[i] = addNum;
                        isBreak = true;
                    }
                    else if (subNum % 5 == 0)
                    {
                        newArr[i] = subNum;
                        isBreak = true;
                    }

                    counter++;
                }
            }
            return newArr;
        }

        static int MakeKommaWeg(double number)
        {
            string str = Convert.ToString(number);  //7.1
            string afterKommaStelle = "";
            string vorKommaStelle = "";
            bool isBreak = false;
            int i = 0;

            //Nachkomma- und Vorkommastellen herausfiltern
            while(i < str.Length || !isBreak)
            {
                if (str[i] == ',')
                {
                    while (!isBreak)
                    {
                        afterKommaStelle += str[i + 1]; //7.1 --> 1
                        isBreak = true;
                        i++;
                    }
                }
                else
                {
                    vorKommaStelle += str[i];
                }
                i++;
            }

            if (int.Parse(afterKommaStelle) >= 0 && int.Parse(afterKommaStelle) <= 4)
            {
                //abgerundeten Wert returnen
                return int.Parse(vorKommaStelle);   //7
            }
            else //if (int.Parse(afterKommaStelle) >= 5 && int.Parse(afterKommaStelle) <= 9)
            {
                //aufgerundeten Wert returnen
                int strInt = int.Parse(vorKommaStelle);   //bsp -2
                if (strInt < 0)//-2 -->true
                {
                    return strInt - 1; //-2 - 1 --> -3 aufrunden
                }
                return strInt + 1;
            }
        }

        static void PrintArr(double[] arr, double[] newArr)
        {
            for (int i = 0; i < arr.Length; i++)
            {
                Console.WriteLine($"{arr[i],4} =>{newArr[i],3}");
            }
        }
    }
}

Zitat:

Es ist vielleicht bisschen kompliziert und uneffizient aber es funktioniert xd.

Kommentare:

Für diese Lösung gibt es noch keinen Kommentar

Bitte melden Sie sich an um eine Kommentar zu schreiben.
Kommentar schreiben