C# tworzenie zbiorow losowych liczb

0

Witam
Dostałem od prowadzącego zadanie o następującej treści:
Stwórz klasę zbiór, która będzie przechowywać losowe liczby bez powtórzeń z możliwością dolosowania ich do zbioru oraz sprawdzenia, czy dana liczba jest już w zbiorze.
Przykładowe metody to: stwórz zbiór, dolosuj liczbę, znajdź liczbę, pokaż zbiór oraz za pomocą przeciążenia operatora wykonać dodawanie zbiorów.

Niestety mało co znam się na c#, oto co udało mi się jak na razie napisać:

#include<stdio.h>
#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<set>
#include <algorithm>
#include <iterator>
using namespace std;

         

class zbior
{
    public: int lelementow, zbiora[], zbiorb[], x, min, max, tab[];
      
      zbior(){
        min=1;
        max=99;
        lelementow=4;
        tab[lelementow];
        zbiora[lelementow]=zbior::tworz_zbior();
        zbior::pokaz_zbior(zbiora, lelementow);
        cout << endl;
        zbiorb[lelementow]=zbior::tworz_zbior();
        zbior::pokaz_zbior(zbiorb, lelementow);
    };

    int losuj_liczbe(){ 
       x=rand()%(max-min+1)+min;
    return x;
    }; 
    
    int tworz_zbior(){
        int i;
        for(i=0; i<= lelementow; i++){
            tab[i] = zbior::losuj_liczbe();      
        }
    };
    
    
    
    public: void pokaz_zbior(int zbior[], int rozmiar){
    int i;
        for(i=0; i<= rozmiar; i++){
            cout << zbior[i] << " ";    
        } 
    };
    

    // a = 1,2,4 
    // b = 3,4, 4, ,5;
    
    void dodaj(){
         int l=4;
         for(int i=0; i<lelementow; i++){
             for(int j=0; j<=lelementow; j++){
                 if(zbiora[j] > zbiorb[i]) break;    
                 if(zbiora[j] != zbiorb[i]){  
                    l=l+1;                
                    zbiorb[l]=zbiora[j]; 
                 }
             }
         }
    }  
};



main(){
srand(time(NULL));  
/*
std::sort(zbiora, zbiora + 5);
std::sort(zbiorb, zbiorb + 5);
      
zbiora[lelementow]=zbior::tworz_zbior();
zbiorb[lelementow]=zbior::tworz_zbior();
 
      
 zbior::pokaz_zbior(zbiora, lelementow);
 cout << endl;
 zbior::pokaz_zbior(zbiorb, lelementow);    
*/
zbior z=zbior();
//z.tworz_zbior();
system("pause");
return 0;
}

Na początek mam problem z poprawnym losowaniem zbiorów, ponieważ oba zbiory losuje mi zawsze z co najmniej 3 takimi samymi liczbami(nie ważne czy liczb w zbiorze jest 5 czy 50). Nie wiem również jak najlepiej rozwiązać problem z powtarzającymi się liczbami przy losowaniu i dodawaniu, których nie może być w zbiorze. Znalazłem coś takiego jak biblioteka "set", która jest zbiorem i w której elementy nie mogą się powtarzać, ale nie wiem jak ją poprawnie zastosować, a przede wszystkim jak dodać do siebie dwa "sety".
Proszę o jakieś wskazówki, lub sugestie w jaki sposób najlepiej można by rozwiązać to zadanie. Jak widać próbowałem dodawanie zbiorów rozwiązać za pomocą posortowania tablic, ale niestety nie działało to poprawnie. Przeciążenie operatora usunąłem ponieważ za każdym razem wyrzucało mi błąd.

0

Cos mi sie wydaje, ze mylisz C# z C++...

0

Tzn? Mam to napisać w c#, z tym, że do tej pory pisałem tylko w c lub c++ i szczerze powiedziawszy cały czas staram się zrozumieć różnice, także sorry za jakieś rażące błędy...

1

Tzn? Mam to napisać w c#, z tym, że do tej pory pisałem tylko w c lub c++ i szczerze powiedziawszy cały czas staram się zrozumieć różnice, także sorry za jakieś rażące błędy..

Problem w tym że obecnie w każdej linii twojego kodu jest błąd składniowy (tak dużo że nawet nie mam siły ich wymieniać). C# to wbrew pozorom nie jest C++, więc zobacz lepiej jakiś kurs żeby porównać różnice ;)

0

Tutaj masz fajny tutorial do c#: http://www.techotopia.com/index.php/C_Sharp_Essentials

0

HashSet<T> is in our latest CTP, and you can find it in the System.Collections.Generic namespace. The naming discussion over the last month has motivated me to recap some naming highlights for HashSet, so hang in til the end if you’re interested.

HashSet is an unordered collection containing unique elements. It has the standard collection operations Add, Remove, Contains, but since it uses a hash-based implementation, these operation are O(1). (As opposed to List<T> for example, which is O(n) for Contains and Remove.) HashSet also provides standard set operations such as union, intersection, and symmetric difference.

    HashSet<int> theSet1 = new HashSet<int>();
    theSet1.Add(1);
    theSet1.Add(2);
    theSet1.Add(2);
    // theSet1 contains 1,2

    HashSet<int> theSet2 = new HashSet<int>();
    theSet2.Add(1);
    theSet2.Add(3);
    theSet2.Add(4);
    // theSet2 contains 1,3,4

    theSet1.UnionWith(theSet2);
    // theSet1 contains 1,2,3,4

HashSet’s default Add operation returns a bool letting you know whether the item was added, so in the code sample above, you could check the return type to check whether the item was already in the set.

    bool added = theSet1.Add(2); // added is true
    added = theSet1.Add(2); // added is false

If you’re familiar with our ICollection<T> interface, notice that this means ICollection<T>.Add (returning void) has an explicit implementation, allowing HashSet<T> to introduce its own Add.

A note on uniqueness: HashSet determines equality according to the EqualityComparer you specify, or the default EqualityComparer for the type (if you didn’t specify). In the above example we didn’t specify an EqualityComparer so it will use the default for Int32. In the next example, we’ll use an OddEvenComparer, which considers items equal if they are both even or both odd.

    class OddEvenComparer : IEqualityComparer<int> {
        public OddEvenComparer() {}
        public bool Equals(int x, int y) {
            return (x & 1) == (y & 1);
        }

        public int GetHashCode(int x) {
            return (x & 1);
        }
    }

    ...

    // Now use the comparer
    HashSet<int> oddEvenSet = new HashSet<int>(new OddEvenComparer());
    oddEvenSet.Add(1);
    oddEvenSet.Add(3);
    oddEvenSet.Add(4);
    // oddEventSet contains 1,4; it considered 1 and 3 equal.

Notice the name UnionWith in the first example. UnionWith, as with the other set operations, modifies the set it’s called on and doesn’t create a new set. This distinction is important because the Linq operations Union, Intersect, etc on IEnumerable create a new set. So HashSet’s methods aren’t duplicating Linq; they’re provided in case you want to avoid creating a new set, and they’re distinguished by the With suffix.

Now for some naming fun, which will demonstrate some other framework guidelines. We would have liked to name this feature Set. This is because it’s preferred to use a common name rather than one that reveals details about the implementation. To borrow an example from Krzysztof Cwalina and Brad Abram’s book Framework Design Guidelines, a type used to submit print jobs to a print queue should be named Printer, and not PrintQueue. Applying this guideline to this class – HashSet, while more technically precise, isn’t as recognizable at Set. You can see this guideline in other class names in the System.Collections.Generic namespace: List<T> instead of ArrayList<T>, Dictionary<T> instead of Hashtable<T>.

This brings up the question of whether naming it Set would have been a bad idea if we add other sets in the future, such as an OrderedSet. However, a hash-based unordered set can reasonably be considered the “go-to” set because of its good performance, so distinguishing it with the name Set would still be acceptable.

Any guesses as to why we didn’t go with the name Set?

0

Korzystasz z funkcji "unionWith"

0

Dzięki za pomoc akrajka2

0 użytkowników online, w tym zalogowanych: 0, gości: 0