doktore1
Goto Top

C Sharp. jede zweite Zahl einlesen

Hallo liebe Gemeinde.

Ich möchte gerne mit einer Schleife eine Zeile einlesen.
Danach jede zweite Position ausgeben.
Als delimiter das Komma.

Bsp. Input
(-21.22,11.11,55.55,33.33,44.44,-66.66,99.99,77.77,100))

Output
11.11,33.33,-66.66,77.77

Vielen Dank für die Mühe

Grüße

Content-Key: 392187

Url: https://administrator.de/contentid/392187

Ausgedruckt am: 28.03.2024 um 20:03 Uhr

Mitglied: 129580
129580 09.11.2018 um 18:11:31 Uhr
Goto Top
Guten Abend,

Ich möchte gerne mit einer Schleife eine Zeile einlesen.
Danach jede zweite Position ausgeben.
Als delimiter das Komma.

Das ist schön. Warum machst du das dann nicht einfach?
Oder brauchst du für diese Aufgabe hilfe? Wenn ja, dann musst du uns schon mitteilen, wo's hängt.
Poste dann auch deinen aktuellen Code.

Viele Grüße,
Exception
Mitglied: em-pie
em-pie 09.11.2018 um 22:39:51 Uhr
Goto Top
Moin,
Stimme @129580 zu :
Mach doch, was du vorhast.

Zum logischen Ansatz:
  • Zeile anhand des Delimiters in ein Array schreiben
  • For-Next-Schleife einsetzen
  • Den Schleifenzähler nicht mit i++ hochzuholen, sondern mit i+2 und die zählervariable als Variable für das Arrayindex verwenden.

Gruß
em-pie
Mitglied: 137808
Lösung 137808 12.11.2018 aktualisiert um 15:38:33 Uhr
Goto Top
There are a few methods which can be used here are some of them (ex. are using c# console application project):

LINQ variant
 // file
 const string FILE = @"D:\test.txt";  
 // read file
 string strINPUT = System.IO.File.ReadAllLines(FILE);
 // remove parentheses, convert all array elements to real numbers and only leave every second item of the array
 IEnumerable<double> numbers = Array.ConvertAll(strINPUT.Trim(new char { '(', ')' }).Split(new char { ',' }), x => double.Parse(x, System.Globalization.CultureInfo.GetCultureInfo("en-us"))).Where((x,i) => (i+1) % 2 == 0);  
 // FOR-Schleife über die Zahlen
 foreach(double num in numbers) {
     Console.WriteLine(num);
 }
 Console.ReadLine();
for-loop with step
 // file
 const string FILE = @"D:\test.txt";  
 // read file
string strINPUT = System.IO.File.ReadAllLines(FILE);
// read first line of file trim parenthesis and split with comma
string numbers = strINPUT.Trim(new char { '(', ')' }).Split(new char { ',' });  
// loop through array starting with second item and 2 step incrementor
for (int i = 1; i < numbers.Length; i+=2) {
    Console.WriteLine(numbers[i]);
}
Console.ReadLine();
for-loop with if check of remainder of division
 // file
 const string FILE = @"D:\test.txt";  
 // read file
string strINPUT = System.IO.File.ReadAllLines(FILE);
// read first line of file trim parenthesis and split with comma
string numbers = strINPUT.Trim(new char { '(', ')' }).Split(new char { ',' });  
// loop through array starting with second item and 2 step incrementor
for (int i = 0; i < numbers.Length; i++) {
    if ((i+1) % 2 == 0) {
        Console.WriteLine(numbers[i]);
    }
}
Console.ReadLine();
Regards
Mitglied: Doktore1
Doktore1 19.11.2018 um 13:35:08 Uhr
Goto Top
Thank you
It was very helpful to see different ways.