myGully.com

myGully.com (https://mygully.com/index.php)
-   Programmierung (https://mygully.com/forumdisplay.php?f=67)
-   -   Programm C# Zahlen sortieren (https://mygully.com/showthread.php?t=1941667)

Darren 18.11.09 20:32

Programm C# Zahlen sortieren
 
weiß einer von euch wie man ein programm im C# schreiben kann das man drei zahlen eingeben lässt und das sie dan sortiert werden

Darren 18.11.09 20:35

so habs ich gemacht geht aber nicht
/*
E: 3 Integerzahlen
V:Die 3 Zahlen absteigend sortieren
A: größte zahl: xxx
Mittlere Zahl: xxx
Kleinste Zahl: xxx
*/
using System;





class Program
{
static void Main()
{
int a;
int b;
int c;

Console.WriteLine("Bitte geben sie die erste Zahl ein (a)");
a = Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Bitte geben sie die erste Zahl ein (b)");
b= Convert.ToInt32(Console.ReadLine());

Console.WriteLine("Bitte geben sie die erste Zahl ein (c)");
c = Convert.ToInt32(Console.ReadLine());

if (a>b)
{
Console.Write("a{0},b{1}",a,b);
}

else

Console.Write("b{0},a{1}",b,a);
if (b>c)
{
Console.Write("b{0},c{1}",b,c);

}








if (c>b)
{
Console.Write("c{0},b{1}",c,b);
}










}

}

urga 19.11.09 18:29

aber ja doch :
der witz besteht darin, die ints in eine List<int> zu packen.
weiterhin gibt es List<T>.Sort(). somit sind alle probleme gelöst.
das sortieren an sich hat immer was mit listen (oder arrays) zu tun!

wenn du lernen möchtest, wie man selbst listen/array sortiert, google nach bubblesort und quicksort.
Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace simpleSort {
    class Program {
        static void Main(string[] args) {
            List<int> liste = new List<int>();
            Console.WriteLine("Zahlen eingeben, -1 für Ende\r\n");
            string s = "";
            while (s != "-1") {
                Console.Write("> ");
                s = Console.ReadLine();
                if (s != "-1") {
                    try {
                        liste.Add(Int32.Parse(s));
                    }
                    catch (Exception ex) {
                        Console.WriteLine("das war wohl keine zahl! (" + ex.ToString() + ")");
                    }
                }
            }
            liste.Sort();
            foreach (int i in liste) {
                Console.WriteLine(i);
            }
            Console.ReadLine();
        }
    }
}


xparet0209 28.11.09 23:02

einweitere interessanter sortier algorithmus ist "merge sort"

ich denke sich in solche dinge kurz einzuarbeiten kann nicht schaden, da mann sich nciht immer auf die vorprogrammierten dinge verlassen kann
und sowas einfaches ist auch eine ideale übung

flotti 09.12.09 12:43

hier mal der klassiker in C:

Code:

#include <stdio.h>
#include <stdlib.h>

void swap(int *, int *);
void sort(int *, int);

int main (){
       
        int values []={10,58,4,1,4,7,1,47,4,1,452,4,8,2,6,8,7,4};
        int n = sizeof(values)/sizeof(*values);
        sort(&values[0],n);
        for(int i=0; i<n; i++)
                printf("%i \n",values[i]);
        system("pause");
        return 1337;

}

void sort(int *sortme, int n){
        for (int i=n-1; i>=0; i--){
                for(int j=0; j<i; j++)
                {
                        if(sortme[j]>sortme[j+1])
                                swap(&sortme[j],&sortme[j+1]);
                }
        }

}


void swap(int *x, int *y)
{
        int temp;
        temp = *x;
        *x = *y;
        *y = temp;
}


Regade 21.12.09 15:03

Einfach alle Zahlen in ein Array schreiben und dann mit folgendem Befehl sortieren:

Array.Sort(arryname);

;-)

Hades 22.12.09 22:16

Trotzdem macht es Sinn sich mit den Grundlagen zu beschäftigen und zu wissen wie die einzelnen Algos funktionieren damit sie machen was sie machen.


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:28 Uhr.

Powered by vBulletin® (Deutsch)
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.