1 / 12

Informatik I (D-ITET) Group 7, 15:00-17:00, ETZ H91 Assistant: Ercan Ucan

Informatik I (D-ITET) Group 7, 15:00-17:00, ETZ H91 Assistant: Ercan Ucan. Slides at http://people.inf.ethz.ch/eucan/inf-1 /. Exercise 7 ( 12 /11/2012 ) . Series 6 – Exercise 1 – Stacks with Dynamic Arrays. #include < stdlib.h > #include < iostream > #include <string>

dalmar
Télécharger la présentation

Informatik I (D-ITET) Group 7, 15:00-17:00, ETZ H91 Assistant: Ercan Ucan

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Informatik I (D-ITET)Group 7, 15:00-17:00, ETZ H91Assistant: Ercan Ucan Slides at http://people.inf.ethz.ch/eucan/inf-1/ Exercise7 (12/11/2012)

  2. Series 6 – Exercise 1 – Stacks with Dynamic Arrays #include <stdlib.h> #include <iostream> #include <string> using namespace std; int* data; //Pointer auf das Array intnumberOfElements; //AnzahlElementeim Stack intsizeOfArray; //GegenwärtigeKapazität des Arrays void init() { numberOfElements = 0; sizeOfArray = 2; data = new int[sizeOfArray]; } void clear() { delete[] data; numberOfElements = 0; } int size() { return numberOfElements; } int pop(){ if (numberOfElements < 1){ cout << "Stack underrun: Stack is empty!" << endl; return -1; } else { numberOfElements--; return data[numberOfElements]; } return 0; } void push(int element){ // Falls Kapazitäterschöpft: Neues Array erstellen if ( sizeOfArray <= numberOfElements){ int* tempdata = new int[sizeOfArray]; for (inti = 0; i < sizeOfArray; i++){ tempdata[i] = data[i]; } sizeOfArray = 2* sizeOfArray; delete[] data; data = tempdata; delete[] tempdata; } data[numberOfElements] = element; numberOfElements++; }

  3. Series 6 – Exercise 1 – Stacks with Lists and Pointers #include <stdlib.h> #include <iostream> #include <string> using namespace std; struct item{ int key; //Value of the stack element item* next; //Pointer to the next item }; item* first; //Pointer auf oberstesStapelelement intnumberOfElements; void init() { first = NULL; numberOfElements = 0; } int size() { return numberOfElements; } void clear() { while (numberOfElements > 0) pop(); } int pop(){ int result; if (first != NULL){ item* temp = first; result = first->key; first = first -> next; delete temp; numberOfElements--; } else{ //Stack ist leer -> Fehler cout << "Stack underrun: Stack is empty!" << endl; result = -1; } return result; } void push(int element){ item* ni = new item; ni->key = element; ni->next = first; first = ni; numberOfElements++; }

  4. FILE INPUT/OUTPUT (I/O) • A C++ program views input or output as a stream of bytes. • The bytes in the input can come from the keyboard, a file in the hard disk … • The bytes in the output can flow to the display, a printer, a file … • A stream acts as an intermediary between the program and the stream’s source or destination. • This enables a C++ program to treat e.g. input from the keyboard in the same manner it treats input from a file. • You have already seen how the standard input cin and output coutobjects (defined in the iostream class library) work. • In the exercise you will learn a basic scheme to communicate with files either for input or for output. • In the same way you had to define: #include <iostream> you now have now to add at the beginning of your code: • #include <fstream>

  5. Simple File Input • Create an ifstreamobject to manage the input stream. • e.g. ifstream fin; • Associate that object with a particular file. • e.g. fin.open(“input.dat”); • Use the object in the same way you would use cin. • e.g. char ch; fin >> ch; Simple File Output • Create an ofstreamobject to manage the output stream. • e.g. ofstreamfout; • Associate that object with a particular file. • e.g. fout.open(“output.dat”); • Use the object in the same way you would use cout. • e.g. fout << “This is a message”; When you are done with the files, it’s good practice to close them: fout.close(); fin.close();

  6. FILE I/O – An example maximum and minimum temperature values for a specific day temperature difference for a specific day.

  7. FILE I/O – An example #include <iostream> #include <fstream> using namespace std; int main() { ifstreaminstream("input.dat"); ofstreamoutstream("output.txt"); double t_max; double t_min; double diff; string s; ... Header for file streams Input file stream Output file stream

  8. FILE I/O – An example #include <iostream> #include <fstream> using namespace std; int main() { ifstreaminstream("input.dat"); ofstreamoutstream("output.txt"); double t_max; double t_min; double diff; string s; ... Header for file streams Input file stream Output file stream

  9. Task 1: • Answer the theoretical questions: • What are binary files? What’s the opposite? • You want to store float 5.2. Give the C++ code and how much space it takes. • . • .

  10. Task 2: • Write a program that reads a text file and let’s the user search for a certain term in this file. • Gesuchter Ausdruck: „ein Beispiel eines Ausdrucks“ • Anzahl gefundene Stellen insgesamt: 32 • Ausdruck gefunden in Zeile: 3. Zeileninhalt: • „...Zeileninhalt von Zeile 3...“ • Ausdruck gefunden in Zeile 5. Zeileninhalt: • „...Zeileninhalt von Zeile 5...“ • Test your program with the provided file “story.txt”. • Take this file from the course web site. • Submit the 3 resulting text files with its search results as the solution for this part of the task.

  11. Task 3: • Write a program which does mathematical function computations: • Should run as ./program job.txt • job.txt contains 3 lines: • tan (can also be sin or cos) • input.txt • output.txt • Your task is to generate the output file: • It should contain both X and Y values! • You should make a column of x values and a column of y values.

  12. Task 3 (continued): • Handle the error cases properly: • When the file(s) cannot be found. • When an unexpected function is give to compute. • When an unexpected input is provided to the function. • The functions are present in <cmath> library • Optional: • Plot the output file using gnuplot • set terminal png • set out 'plot.png' • plot 'output.txt'

More Related