Guest User

Untitled

a guest
Nov 24th, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. //#include "stdafx.h"
  2. #include "stdio.h"
  3. #include "string.h"
  4. #include "stdlib.h"
  5. #include "windows.h"
  6. #include <cstring>
  7. #include <iostream>
  8. using namespace std;
  9. #define MAX 50
  10.  
  11.  
  12. //int anz, nr, i;
  13. //char bez[MAX], search[MAX];
  14. //FILE *datei;
  15.  
  16.  
  17. //Struct für das Listenelement
  18.  
  19. struct list_elem {
  20. struct list_elem *next; //Zeiger auf das nächste Listenelement
  21. int anz; //Anzahl
  22. int nr; //Teile-Nr.
  23. char bez[MAX]; //Bezeichnug
  24.  
  25. list_elem* getNext()
  26. {
  27. return next;
  28. }
  29.  
  30. void setNext(list_elem* next)
  31. {
  32. this->next = next;
  33. }
  34. };
  35.  
  36. //Struct für Liste
  37. struct List {
  38.  
  39. private:
  40. list_elem* head;
  41.  
  42. public:
  43. List() { //Konstruktor
  44. head = NULL;
  45. }
  46.  
  47. // Anhaengen eines Listenelements an das Ende
  48. // O(n)
  49. void append(list_elem* e) {
  50. if (head == NULL) { //Pruefen, ob Listenkopf auf NULL zeigt
  51. e->setNext(NULL);
  52. head = e;
  53. }
  54. else {
  55. list_elem* current = head;
  56. while (current->getNext() != NULL) {
  57. current = current->getNext();
  58. }
  59. current->setNext(e);
  60. e->setNext(NULL);
  61. }
  62.  
  63. }
  64.  
  65. list_elem* search_nr(int nr) {
  66. if (head == NULL) {
  67. return NULL;
  68. }
  69. else {
  70. list_elem* current = head;
  71. for (;current->getNext() != NULL; current = current->getNext()) {
  72. if(current->nr == nr) {
  73. return current;
  74. break;
  75. }
  76. }
  77. }
  78. }
  79. list_elem* search_bez(char bez[]) {
  80. if (head == NULL) {
  81. return NULL;
  82. }
  83. else {
  84. list_elem* current = head;
  85. for (;current != NULL; current = current->getNext()) {
  86. if (strcmp ( current->bez, bez ) == 0 ) {
  87. return current;
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. /*Ausgabe des Headelementes*/
  94. //O(1)
  95. list_elem* getHead () {
  96. return head;
  97. }
  98. };
  99.  
  100.  
  101.  
  102.  
  103. int main(){
  104. printf("Erstellen der zwei Objekte");
  105. List *liste1 = new List();
  106. list_elem *el1 = new list_elem();
  107. el1->nr=100;
  108. // char name[MAX]="Testobj";
  109. strcpy(el1->bez, "Testobj");
  110. el1->anz=1;
  111. liste1->append(el1);
  112. el1 = new list_elem();
  113. el1->nr=101;
  114. strcpy(el1->bez, "Testobj2");
  115. el1->anz=1;
  116. liste1->append(el1);
  117. printf("Erfolgreich erstellt\n\n");
  118. cout << "Test der Ausgabe" << endl;
  119. for(list_elem* current = liste1->getHead();current != NULL; current = current->getNext()) {
  120. printf("Art#: %i \t Bezeichnung: %s \t Anzahl: %i",current->nr, current->bez, current->anz);
  121. }
  122. printf("Test der Suchfunktion\nSuche Objekt mit Bezeichnung Testobj2 und gebe Art# aus.");
  123. el1 = new list_elem();
  124. char temp[MAX];
  125. el1 = liste1->search_bez(strcpy(temp, "Testobj2"));
  126. printf("Art#: %i",el1->nr);
  127. cout << "Test der Suchfunktion#2 \n Suche Objekt mit Art#: 100 und gebe Bez. aus." << endl;
  128. el1 = new list_elem();
  129. el1 = liste1->search_nr(100);
  130. printf("Bez#: %s",el1->bez);
  131. system ("pause");
  132. }
  133.  
  134. //liste, element0, element1, elementneu;
  135. /*
  136. //Eingabe von Teile-Nr., Bezeichnung, Anzahl
  137. void eingabe()
  138. {
  139. printf ("Teile-Nummer ... : ");
  140. scanf ("%s",liste.nr);
  141. printf ("Bezeichnug ..... : ");
  142. scanf ("%s",liste.bez);
  143. printf ("Anzahl ......... : ");
  144. scanf ("%s",liste.anz);
  145. }
  146.  
  147.  
  148. //in Datei schreiben
  149. void ausgabe_in_datei()
  150. {
  151. //Legt Datei mit "Bezeichnug" als Name an
  152. datei = fopen(("%s.txt",liste.bez), "a+");
  153.  
  154. if (datei != NULL)
  155. {
  156. fprintf (datei, "Teile-Nr: %10s | Bezeichnug: %15s | Anzahl: %10s\n", liste.nr, liste.bez, liste.anz);
  157. fclose (datei);
  158. }
  159.  
  160.  
  161. //Legt Datei mit "Teile-Nr." als Name an
  162. datei = fopen(("%s.txt", liste.nr), "a+");
  163.  
  164. if (datei != NULL)
  165. {
  166. fprintf (datei, "Teile-Nr: %8s | Bezeichnug: %13s | Anzahl: %4s\n", liste.nr, liste.bez, liste.anz);
  167. fclose (datei);
  168. }
  169. }
  170.  
  171.  
  172. //txt-Datei auslesen
  173. void lesen_aus_datei()
  174. {
  175. printf ("Bitte Bezeichnung ODER Teile-Nummer eingeben ... : ");
  176. scanf ("%s",search);
  177.  
  178. char text[200+1];
  179. datei = fopen (("%s.txt",search),"r");
  180. if (datei != NULL)
  181. {
  182. fscanf (datei, "%200c", text);
  183. text[200] = '\0';
  184. printf ("\n\n%s\n", text);
  185. fclose (datei);
  186. } else
  187. if (datei == NULL)
  188. {
  189. printf("\n Eintrag nicht vorhanden! \n");
  190. }
  191. }
  192.  
  193.  
  194.  
  195. //Hauptprogramm
  196. int main ()
  197. {
  198. printf("Teil eintragen ......... (1)\n");
  199. printf("Teil suchen ............ (2)\n");
  200. printf("Teileeintrag aendern ... (3) !!! funzt noch nicht !!! \n\n");
  201. printf("Ihre Auswahl .......... : ");
  202.  
  203. scanf("%i",&i);
  204. printf("\n\n");
  205.  
  206. if (i==1) {
  207. eingabe();
  208. ausgabe_in_datei();
  209. printf ("\n");
  210. } else
  211. if (i==2) {
  212. lesen_aus_datei();
  213. }
  214.  
  215. printf ("\n\n");
  216. system ("pause");
  217. return 1;
  218. }
  219.  
  220. */
Add Comment
Please, Sign In to add comment