Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <vector>
- #include <stdlib.h>
- #include <time.h>
- using namespace std;
- //Liste von Zufallszahlen erzeugen
- void generate_func()
- {
- cout << "Wie viele zufällige Zahlen möchtest du erzeugen?" << endl;
- int anzahl_rand_numbers=0;
- cin >> anzahl_rand_numbers;
- cout << endl << "Zufallszahlen:" << endl;
- double random_number=0;
- //initialisiere random seed
- srand (time(NULL) );
- for (int i=0; i<anzahl_rand_numbers; i++)
- {
- random_number = ((double)(rand()%10000000))/100000;
- cout << random_number << endl;
- }
- }
- vector<double> input_func()
- {
- ifstream list ("list.txt");
- // Prüfe ob die Datei offen ist
- if (!list.is_open())
- {
- throw 404;
- }
- //Ausgabe des inputs
- cout << "Input:" << endl;
- string line;
- int anzahl=0;
- while (list.good())
- {
- getline (list,line);
- cout << line << ", ";
- anzahl++;
- }
- cout << endl;
- //Einlesen in Feld
- vector<double> unsort_feld(anzahl);
- int i=0;
- //An Anfang der Liste gehen
- list.seekg( ios_base::beg );
- //Fehlerstatus der Liste leeren
- list.clear();
- while (list.good())
- {
- list >> unsort_feld[i];
- ++i;
- }
- list.close();
- return unsort_feld;
- }
- int main()
- {
- cout << "Möchtest du eine Liste von Zufallszahlen erzeugen (1) oder einlesen (2)?" << endl;
- int generate_or_read=0;
- cin >> generate_or_read;
- try
- {
- if (generate_or_read == 1) generate_func();
- else if (generate_or_read == 2)
- {
- //Liste von Zahlen einlesen
- input_func();
- }
- else throw 1;
- }
- catch(int e)
- {
- if (e==404) cout << "Konnte Datei list.txt nicht öffnen!" << endl;
- if (e==1) cout << "Falsche Eingabe! Programm wird beendet." << endl;
- return 1;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement