Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.70 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>             // Für die Füllenden Nullen
  4. #include <time.h>              // Für Messung von Rechenzeit!!
  5. #include <fstream>
  6.  
  7.  
  8. using namespace std;
  9.  
  10. int programm();
  11. // long long isPrim_alt(long long zahl); alte Funktion
  12. long long isPrim(long long zahl);
  13.  
  14. void searchPrim(long long start, long long ziel, int flag1, int flag2, long long zaehler=1);
  15.  
  16. int main()
  17. {
  18.     programm();
  19.     system("pause");
  20.     return 0;
  21. }
  22.  
  23. void searchPrim(long long start,long long ziel, int flag1, int flag2, long long zaehler){
  24.                           float time1=0.0f, time2=0.0f, tstart1, tstart2;      // Zeitmessungsvariablen
  25.                           long long count = zaehler;
  26.                           if ((ziel-start) > 20000000) cout << "Der Zahlenraum ist zu gross!" << endl; // anpassbar, je nachdem wie viel Zeitdauer man dem User zumuten kann ;) Der Zahlenraum 1 - 1Mio dauert ca. 3 1/2 h ...
  27.                           else {
  28.                           ofstream primzahlen_s;
  29.                          
  30.                          
  31.                           if (flag1) {
  32.                                      if (flag2) {
  33.                                                 primzahlen_s.open("Primzahlen.txt", ios::out /*,ios::_Openprot*/);
  34.                                                 }
  35.                                      else {
  36.                                           primzahlen_s.open("Primzahlen.txt", ios::out | ios::app /*,ios::_Openprot*/);
  37.                                          
  38.                                           }
  39.                                           }
  40.                           if (!primzahlen_s) {
  41.                                            cerr << "Fehler beim Öffnen der Datei!";
  42.                                            }
  43.                           else {
  44.                          
  45.  
  46.                          
  47.                           tstart1 = clock();
  48.                           tstart2 = clock();                    // Zeitmessugn Start
  49.                           for (long long i=start; i<ziel; i++){
  50.                                                   time2 = 0.0f;
  51.                                                                                            
  52.                                                    if (isPrim(i)) {
  53.                                                                    if (count==1){
  54.                                                                                  cout  << count << ".\t" << i << "\t";
  55.                                                                                  //primzahlen_s  << count << ".\t" << i << "\t";
  56.                                                                                  primzahlen_s << i;
  57.                                                                                  }
  58.                                                                    else {
  59.                                                                         cout << endl << count << ".\t" << i << "\t";
  60.                                                                         //primzahlen_s << endl << count << ".\t" << i << "\t";
  61.                                                                         primzahlen_s << endl << i;
  62.                                                                         }
  63.                                                                    count++;
  64.                                                                    time2 += clock() - tstart2;     // Zeitmessung Ende
  65.                                                                    time2 = (time2/CLOCKS_PER_SEC)*1000;  // Umrechnung in MilliSekunden
  66.                                                                    if (time2<1){
  67.                                                                                  cout <<  "<1 ms";
  68.                                                                                  //primzahlen_s <<  "<1 ms";
  69.                                                                                  }
  70.                                                                    else {
  71.                                                                         cout <<  " " << time2 << " ms";
  72.                                                                         //primzahlen_s <<  " " << time2 << " ms";
  73.                                                                         }
  74.                                                                    tstart2 = clock();                    // Zeitmessugn Start
  75.                                                                    }
  76.                                                    }
  77.                           time1 += clock() - tstart1;     // Zeitmessung Ende
  78.                           time1 = (time1/CLOCKS_PER_SEC)*1000;  // Umrechnung in MilliSekunden
  79.  
  80.                           int std=0,min=0,sec=0,msec;
  81.                           msec = (int)time1;
  82.                           while ( msec >= 1000 ){
  83.                                                  msec-=1000;
  84.                                                  sec++;
  85.                                                  }
  86.                           while ( sec >= 60 ){
  87.                                               sec-=60;
  88.                                               min++;
  89.                                               }
  90.                           while ( min >= 60 ){
  91.                                               min-=60;
  92.                                               std++;
  93.                                               }
  94.                           cout << endl  << endl << "Gesamtzeit der Primzahlensuche von " << start << " bis " << ziel << ": " << setfill('0') << setw(2) << std << "h:" << setfill('0') << setw(2) << min << "min:" << setfill('0') << setw(2) << sec << "s:" << msec << "ms" << endl;
  95.      
  96.                           }
  97.                           primzahlen_s.close();
  98.                           }
  99.      }
  100.  
  101. /*long long isPrim_alt(long long zahl){                                                Alte, langsame Funktion.
  102.                      for (long long double i=2; i<(zahl/2); i++){                      die neue Funktion (unten) prüft zunächst die Teilbarkeit durch 2
  103.                                                 if (!(zahl%i)) return 0;               und lässt im Erfolgsfalle jeden zweiten Teiler weg.
  104.                                                
  105.                                                 }
  106.                      return 1;                              
  107.     }*/
  108.    
  109. long long isPrim(long long zahl){
  110.                      if (!(zahl%2)) return 0;
  111.                      else {
  112.                            for (long long double i=3; i<(zahl/2); i=i+2){
  113.                                                                        if (!(zahl%i)) return 0;
  114.                                                                        }
  115.                            return 1;
  116.                            }
  117.                                                    
  118.     }
  119.  
  120. int programm(){
  121.                    
  122.           char frage1[80],frage2[80],frage3[80],buffer_a[1000],buffer_b[1000],buffer[1000];
  123.           long long start,ziel,zaehler;
  124.           int flag1, flag2;
  125.          
  126.           do {
  127.                        
  128.           system("cls");        
  129.           cout << "\t\tPrimzahlengenerator v1.1 (c) by Grownz" << endl << endl << endl;
  130.          
  131.           ifstream primzahlen_l;
  132.           primzahlen_l.open("Primzahlen.txt", ios::in);
  133.           if (primzahlen_l) {
  134.                             do{
  135.                             primzahlen_l.close();
  136.                             ifstream primzahlen_l;
  137.                             primzahlen_l.open("Primzahlen.txt", ios::in);
  138.                             system("cls");        
  139.                             cout << "\t\tPrimzahlensucher v1.1 (c) by Severus" << endl << endl << endl;
  140.                             cout << "Primzahlen.txt vorhanden!" << endl << endl;
  141.                             zaehler=0;
  142.                             do {
  143.                                 zaehler++;
  144.                                 if (!(zaehler%2)) {
  145.                                                   primzahlen_l.getline(buffer_a,1000,'\n');
  146.                                                   //cout << "Letzte Zeile: " << buffer_a  << " Counter: " << zaehler << endl;
  147.                                                   }
  148.                                 else {
  149.                                      primzahlen_l.getline(buffer_b,1000,'\n');
  150.                                      //cout << "Letzte Zeile: " << buffer_b  << " Counter: " << zaehler << endl;
  151.                                      }
  152.                                
  153.                                 } while (primzahlen_l);
  154.                                
  155.                             if (!(zaehler%2)) {
  156.                                                      cout << "Letzte gefundene Primzahl: " << buffer_b << "\tNummer: " << zaehler << endl;
  157.                                                      for (int i=0;i<1000;i++){
  158.                                                                               buffer[i]=buffer_b[i];
  159.                                                                               }
  160.                                                     }
  161.                             else {
  162.                                  cout << "Letzte gefundene Primzahl: " << buffer_a  << "\tNummer: " << zaehler << endl;
  163.                                  for (int i=0;i<1000;i++){
  164.                                                           buffer[i]=buffer_a[i];
  165.                                                           }
  166.                                  }
  167.                             primzahlen_l.close();
  168.                            
  169.                            
  170.                             long zw_zaehler=atol(buffer);
  171.                             cout << endl << "Soll ab Primzahl Nr. " << zaehler << ", Wert " << zw_zaehler << " weiter gemacht werden? [Y(J)/N]";
  172.                             cin.getline(frage3,80);
  173.                            
  174.                             if ((frage3[0]=='Y') || (frage3[0]=='y') || (frage3[0]=='J') || (frage3[0]=='j')){
  175.                                                                                                               long zahlen;
  176.                                                                                                               cout << endl << endl << "Wie viel weitere Zahlen sollen ueberprueft werden?" << endl;
  177.                                                                                                               cin >> zahlen;
  178.                                                                                                               cout << endl << endl;
  179.                                                                                                              
  180.                                                                                                               searchPrim(zw_zaehler, (zw_zaehler+zahlen), true, false, zaehler);
  181.                                                                                                               system("pause");
  182.                                                                                                               return 0;
  183.                                                                                                               }
  184.                             else if ((frage3[0]=='N') || (frage3[0]=='n')){
  185.                                                                            flag1=0;
  186.                                                                            cout << endl <<"Die Suche nach Primzahlen wird nicht unmittelbar fortgesetzt!" << endl << endl;
  187.                                                                            system("pause");
  188.                                                                            }
  189.                             else {
  190.                             cerr << endl <<"Fehleingabe! Bitte neu eingeben!" << endl<< endl;
  191.                             system("pause");
  192.                             //exit(1);
  193.                             }
  194.                             } while ((frage3[0]!='Y') && (frage3[0]!='y') && (frage3[0]!='J') && (frage3[0]!='j') && (frage3[0]!='N') && (frage3[0]!='n'));  
  195.                             }
  196.                            
  197.           //else
  198.          
  199.           cout << "Sollen die Daten gespeichert werden? [Y(J)/N]";
  200.           cin.getline(frage1,80);
  201.          
  202.           if ((frage1[0]=='Y') || (frage1[0]=='y') || (frage1[0]=='J') || (frage1[0]=='j')){
  203.                                                                                         flag1=1;
  204.                                                                                         cout << endl << "Ergebnisdaten werden in der Datei Primzahlen.txt\nim Programmverzeichnis gespeichert!" << endl << endl;
  205.                                                                                         system("pause");
  206.                                                                                         }
  207.           else if ((frage1[0]=='N') || (frage1[0]=='n')){
  208.                                                        flag1=0;
  209.                                                        cout << endl <<"Ergebnisdaten werden nicht gespeichert!" << endl << endl;
  210.                                                        system("pause");
  211.                                                        }
  212.           else {
  213.                cerr << endl <<"Fehleingabe! Bitte neu eingeben!" << endl<< endl;
  214.                system("pause");
  215.                //exit(1);
  216.                }
  217.               } while ((frage1[0]!='Y') && (frage1[0]!='y') && (frage1[0]!='J') && (frage1[0]!='j') && (frage1[0]!='N') && (frage1[0]!='n'));
  218.          
  219.           if (flag1) {
  220.           do {
  221.               system("cls");        
  222.               cout << "\t\tPrimzahlengenerator v1.1 (c) by Severus" << endl << endl << endl;
  223.               cout << "Soll die Datei ueberschrieben werden (Y) oder fortgesetzt werden (N)?" << endl << "WARNUNG!! Alte Daten gehen beim ueberschreiben unwiderruflich verloren!" << endl;
  224.               cin.getline(frage2,80);
  225.               if ((frage2[0]=='Y') || (frage2[0]=='y') || (frage2[0]=='J') || (frage2[0]=='j')){
  226.                                                                                         flag2=1;
  227.                                                                                         cout << endl << "Ergebnisdaten werden neu in der Datei Primzahlen.txt gespeichert!" << endl << endl;
  228.                                                                                         system("pause");
  229.                                                                                         }
  230.               else if ((frage2[0]=='N') || (frage2[0]=='n')){
  231.                                                        flag2=0;
  232.                                                        cout << endl <<"Ergebnisdaten werden an bestehende angehängt!" << endl << endl;
  233.                                                        system("pause");
  234.                                                        }
  235.               else {
  236.               cerr << endl <<"Fehleingabe! Bitte neu eingeben!" << endl<< endl;
  237.               system("pause");
  238.               //exit(1);
  239.                }
  240.               } while ((frage2[0]!='Y') && (frage2[0]!='y') && (frage2[0]!='J') && (frage2[0]!='j') && (frage2[0]!='N') && (frage2[0]!='n'));
  241.           }
  242.              
  243.          
  244.                  
  245.           cout << endl << endl << "In welchem Zahlenraum sollen Primzahlen gesucht werden?" << endl << "Start: \t";
  246.           cin >> start;
  247.           if (start==1) start++;
  248.           cout << "Ziel: \t";
  249.           cin >> ziel;
  250.           cout << endl << endl;
  251.  
  252.           searchPrim(start, ziel, flag1, flag2);
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement