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

Printed on: April 25, 2024 at 18:04 o'clock

Mitglied: 129580
129580 Nov 09, 2018 at 17:11:31 (UTC)
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
Member: em-pie
em-pie Nov 09, 2018 at 21:39:51 (UTC)
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
Solution 137808 Nov 12, 2018 updated at 14:38:33 (UTC)
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
Member: Doktore1
Doktore1 Nov 19, 2018 at 12:35:08 (UTC)
Goto Top
Thank you
It was very helpful to see different ways.