Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- *Aufgabe H19
- * a) Das Programm ließt eine Datei Zeile für Zeile ein, speichert sich immer den höchsten Wert und schreibt die Anzahl der Zeilen mit.
- * Die Zeilenzahl entspricht der Variable m und der Höchstwert ist n.
- * Die Funktion sort() bekommt dann m und n übergeben und baut daraus das Array neu auf und gibt es zurück, anschließend wird es in die Datei out.txt geschrieben.
- * Das Programm überprüft NICHT ob das Eingabearray im richtigen Format war, weil dies meiner Meinung nach nicht in O(m) Schritten möglich ist.
- * Dafür "sortiert" das Programm das Array aber in O(n) Schritten und verwendet nur konstant viel Speicher zusätzlich nur Eingabe.
- *
- * b) array1.txt 0s
- * array2.txt 0s
- * array3.txt 0.003s
- * array4.txt 0.04s
- * array5.txt 0.512s
- */
- #include "stdafx.h"
- #include <iostream>
- #include <conio.h>
- #include <fstream>
- #include <string>
- #include <ctime>
- using namespace std;
- int* sort(int m, int n) {
- int* a = new int[m]{0};
- for (int i = 0; i < n; i++) {
- a[i + m - n] = i + 1;
- }
- return a;
- }
- int main(int argc, char** argv) {
- if (argc <= 1) {
- cout << "Error! No file specified!" << endl;
- _getch();
- return 1;
- }
- int lines{0}, max{0};
- string line;
- ifstream in(argv[1]);
- cout << "Reading file..." << endl;
- while (getline(in, line)) {
- if (atoi(line.c_str()) >= max) {
- max = atoi(line.c_str());
- }
- lines++;
- }
- in.close();
- cout << "Sorting...";
- clock_t begin = clock();
- int* a = sort(lines, max);
- clock_t end = clock();
- double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
- cout << " " << elapsed_secs << "s" << endl;
- cout << "Writing out.txt..." << endl;
- ofstream out("out.txt");
- for (int i = 0; i < lines; i++) {
- out << a[i] << endl;
- }
- out.close();
- cout << "Done!" << endl;
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment