Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <string>
  5. #include <vector>
  6. #include <stdlib.h>
  7. #include <time.h>
  8. using namespace std;
  9.  
  10. //Liste von Zufallszahlen erzeugen
  11. void generate_func()
  12. {
  13.     cout << "Wie viele zufällige Zahlen möchtest du erzeugen?" << endl;
  14.     int anzahl_rand_numbers=0;
  15.     cin >> anzahl_rand_numbers;
  16.     cout << endl << "Zufallszahlen:" << endl;
  17.    
  18.     double random_number=0;
  19.     //initialisiere random seed
  20.     srand (time(NULL) );
  21.  
  22.     for (int i=0; i<anzahl_rand_numbers; i++)
  23.     {
  24.         random_number = ((double)(rand()%10000000))/100000;
  25.         cout << random_number << endl;
  26.     }
  27. }
  28.  
  29. vector<double> input_func()
  30. {
  31.     ifstream list ("list.txt");
  32.     // Prüfe ob die Datei offen ist
  33.     if (!list.is_open())
  34.     {
  35.         throw 404;
  36.     }
  37.  
  38.     //Ausgabe des inputs
  39.     cout << "Input:" << endl;
  40.     string line;
  41.     int anzahl=0;
  42.     while (list.good())
  43.     {
  44.         getline (list,line);
  45.         cout << line << ", ";
  46.         anzahl++;
  47.     }
  48.     cout << endl;
  49.  
  50.     //Einlesen in Feld
  51.     vector<double> unsort_feld(anzahl);
  52.     int i=0;
  53.     //An Anfang der Liste gehen
  54.     list.seekg( ios_base::beg );
  55.     //Fehlerstatus der Liste leeren
  56.     list.clear();
  57.     while (list.good())
  58.     {
  59.         list >> unsort_feld[i];
  60.         ++i;
  61.     }
  62.     list.close();
  63.    
  64.     return unsort_feld;
  65. }
  66.  
  67. int main()
  68. {
  69.     cout << "Möchtest du eine Liste von Zufallszahlen erzeugen (1) oder einlesen (2)?" << endl;
  70.     int generate_or_read=0;
  71.     cin >> generate_or_read;
  72.     try
  73.     {
  74.         if (generate_or_read == 1) generate_func();
  75.         else if (generate_or_read == 2)
  76.         {  
  77.             //Liste von Zahlen einlesen
  78.             input_func();
  79.         }
  80.         else throw 1;
  81.     }
  82.     catch(int e)
  83.     {
  84.         if (e==404) cout << "Konnte Datei list.txt nicht öffnen!" << endl;
  85.         if (e==1) cout << "Falsche Eingabe! Programm wird beendet." << endl;
  86.         return 1;
  87.     }
  88.  
  89.     return 0;
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement