Hilfe bei einem Programm

  • Hilfe bei einem Programm

    Hi ich muss ein Programm schreiben. Hier die Aufgabe in Kurzform

    Schreiben Sie ein Programm zur Längen-Berechnung eines Polygons.
    Zunächst soll die Anzahl der Punkte eingegeben werden.
    Danach sind in einer Schleife die Koordinaten der einzelnen Punkte einzulesen.
    Unabhängig von der vg. Anzahl sind mindestens 3 Punkte zu ermitteln. Anschließend ist bis zum Erreichen der eingegebenen Anzahl nach jedem Punkte-Paar die Frage: noch mal (j/n)
    zu stellen. Es werden nur die Buchstaben J, j, N, n akzeptiert. Werden andere Zeichen
    eingegeben, ist die Aufforderung beliebig lange zu wiederholen.



    Ich weiss nicht genau wie ich abfragen soll das die Variable auf ja (J/j) steht. Helft mir bitte. Danke
  • Meine Version der Lösung deiner Aufgabe.
    Wenn jemand etwas zu verbessern hat, immer schön posten...

    Quellcode

    1. /* Includefiles */
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include <ctype.h>
    5. /* Defines */
    6. //#define MAX_NR_LENGTH 2
    7. #define MAXPOINTS 10
    8. #define MINPOINTS 3
    9. /* Declarations of Structures */
    10. struct Polygon
    11. {
    12. int i_nr_points;
    13. int *ptr_x;
    14. int *ptr_y;
    15. };
    16. /* Enum */
    17. enum _MyBool_
    18. {
    19. FALSE = 0,
    20. TRUE = 1
    21. };
    22. typedef enum _MyBool_ mybool;
    23. /* Declarations of Functions */
    24. struct Polygon *CreatePolygon(int aNrPoints);
    25. void DeletePolygon(struct Polygon *aPolygon);
    26. mybool SetValue(struct Polygon *aPolygon, int aPoint, int aX, int aY);
    27. int GetNrPoints(struct Polygon *aPolygon);
    28. void GetPointValue(struct Polygon *aPolygon, int aPoint, int *aX, int *aY);
    29. int ReadInNumber();
    30. mybool JaNeinCheck();
    31. int ConvASCII(int aASCII);
    32. void errorNotEnoughPoints();
    33. void errorOutOfMemory();
    34. void errorPolygonNotExist();
    35. /* Main-Function */
    36. int main(int argc, char *argv[])
    37. {
    38. int i_points;
    39. int i_element;
    40. int i_x, i_y;
    41. mybool b_wrong_points;
    42. struct Polygon *ptr_struct_polygon;
    43. ptr_struct_polygon = NULL;
    44. /********************************************
    45. Greeting
    46. ********************************************/
    47. printf("\n\nSie haben das Programm %s gestartet.\n\tWillkommen.\n", argv[0]);
    48. printf("___________________________________________________________________\n\n");
    49. /********************************************
    50. Menu
    51. ********************************************/
    52. printf("Bitte geben Sie die gewuenschte Anzahl (%d - %d) an Punkten ein,\ndie Polygon aufweisen soll:\n", MINPOINTS, MAXPOINTS);
    53. b_wrong_points = TRUE;
    54. i_points = 0;
    55. do
    56. {
    57. i_points = ReadInNumber();
    58. if( i_points > MAXPOINTS ) printf("Das Polygon darf nicht mehr als %d Punkte haben.\nErneute Eingabe:\n", MAXPOINTS);
    59. else if( i_points < MINPOINTS ) printf("Das Polygon muss mindestens %d Punkte haben.\nErneute Eingabe:\n", MINPOINTS);
    60. else b_wrong_points = FALSE;
    61. }while(b_wrong_points);
    62. printf("\nDas Polygon erhaelt %d Punkte\n", i_points);
    63. ptr_struct_polygon = CreatePolygon(i_points);
    64. i_element = i_x = i_y = 0;
    65. while(i_points--)
    66. {
    67. printf("Bitte geben Sie die X-Koordinate fuer den %d. Punkt ein:\n", (i_element+1));
    68. i_x = ReadInNumber();
    69. printf("Bitte geben Sie die Y-Koordinate fuer den %d. Punkt ein:\n", (i_element+1));
    70. i_y = ReadInNumber();
    71. if( SetValue(ptr_struct_polygon, i_element, i_x, i_y) ) i_element++;
    72. else break;
    73. if(i_points > 0)
    74. {
    75. printf("\nWollen Sie noch einen Punkt eingeben?\n\t\tJa (j)/ Nein (n)\n");
    76. if(!JaNeinCheck()) i_points = 0;
    77. }
    78. }
    79. printf("\n___________________________________________________________________\n\n");
    80. printf("Wollen Sie die Polygondaten ausgeben lassen?\n\t\tJa (j)/ Nein (n)\n");
    81. if(JaNeinCheck())
    82. {
    83. printf("Ausgabe der Polygondaten:\n");
    84. printf("\t\tDas Polygon hat %d Punkte\n", GetNrPoints(ptr_struct_polygon));
    85. for(i_points = 0; i_points < GetNrPoints(ptr_struct_polygon); i_points++)
    86. {
    87. GetPointValue(ptr_struct_polygon, i_points, &i_x, &i_y);
    88. printf("Punkt Nr. %d hat die Koordinaten: X = %d\tY = %d\n", (i_points+1), i_x, i_y);
    89. }
    90. }
    91. printf("\nDas Programm wird nun beendet.\n");
    92. /********************************************
    93. Passing
    94. ********************************************/
    95. DeletePolygon(ptr_struct_polygon);
    96. ptr_struct_polygon = NULL;
    97. printf("\n___________________________________________________________________\n\n");
    98. }
    99. /********************************************
    100. Funktionsimplementationen
    101. ********************************************/
    102. /* SetValue-> Koordinaten des Polygons einfügen */
    103. mybool SetValue(struct Polygon *aPtrPolygon, int aPoint, int aX, int aY)
    104. {
    105. if(!aPtrPolygon) errorPolygonNotExist();
    106. if(aPoint >= aPtrPolygon->i_nr_points) return(FALSE);
    107. *(aPtrPolygon->ptr_x + aPoint) = aX;
    108. *(aPtrPolygon->ptr_y + aPoint) = aY;
    109. return(TRUE);
    110. }
    111. /* GetNrPoints-> Anzahl der Punkte des Polygons ausgeben */
    112. int GetNrPoints(struct Polygon *aPolygon)
    113. {
    114. return(aPolygon->i_nr_points);
    115. }
    116. /* GetPointValue-> Koordinaten des Polygons ausgeben */
    117. void GetPointValue(struct Polygon *aPolygon, int aPoint, int *aX, int *aY)
    118. {
    119. *aX = *(aPolygon->ptr_x + aPoint);
    120. *aY = *(aPolygon->ptr_y + aPoint);
    121. }
    122. /* CreatePolygon-> Erstellen des Polygons */
    123. struct Polygon *CreatePolygon(int aNrPoints)
    124. {
    125. struct Polygon *polygon;
    126. int i;
    127. if(aNrPoints < 3) errorNotEnoughPoints();
    128. polygon = malloc(sizeof(struct Polygon));
    129. if(!polygon) errorOutOfMemory();
    130. polygon->ptr_x = malloc(aNrPoints * sizeof(int));
    131. if(!polygon->ptr_x) errorOutOfMemory();
    132. polygon->ptr_y = malloc(aNrPoints * sizeof(int));
    133. if(!polygon->ptr_y) errorOutOfMemory();
    134. polygon->i_nr_points = aNrPoints;
    135. for(i = 0; i < aNrPoints; i++, SetValue(polygon, i, 0, 0));
    136. return(polygon);
    137. }
    138. /* ReadInNumber-> Zahlen einlesen */
    139. int ReadInNumber()
    140. {
    141. int i_ascii;
    142. int i_number;
    143. i_ascii = i_number = 0;
    144. while( (i_ascii = getchar()) != '\n' )
    145. {
    146. if( !((i_ascii < 48) || (i_ascii > 57)) )
    147. {
    148. i_number *= 10;
    149. i_number += ConvASCII(i_ascii);
    150. }
    151. }
    152. return i_number;
    153. }
    154. /* JaNeinCheck-> Ja/Nein-Abfrage */
    155. mybool JaNeinCheck()
    156. {
    157. char *str_check;
    158. mybool b_result;
    159. mybool b_loop;
    160. b_result = FALSE;
    161. b_loop = TRUE;
    162. str_check = malloc(sizeof(char));
    163. do
    164. {
    165. switch(tolower(*gets(str_check)))
    166. {
    167. case 'j': b_result = TRUE;
    168. b_loop = FALSE;
    169. break;
    170. case 'n': b_result = FALSE;
    171. b_loop = FALSE;
    172. break;
    173. default: printf("Bitte j fuer Ja oder n fuer Nein druecken.\n");
    174. break;
    175. }
    176. }while(b_loop);
    177. return b_result;
    178. }
    179. /* ConvASCII-> ASCII-Werte in Zahlen konvertieren */
    180. int ConvASCII(int aASCII)
    181. {
    182. int result;
    183. result = 0;
    184. switch(aASCII)
    185. {
    186. case 48: result = 0;
    187. break;
    188. case 49: result = 1;
    189. break;
    190. case 50: result = 2;
    191. break;
    192. case 51: result = 3;
    193. break;
    194. case 52: result = 4;
    195. break;
    196. case 53: result = 5;
    197. break;
    198. case 54: result = 6;
    199. break;
    200. case 55: result = 7;
    201. break;
    202. case 56: result = 8;
    203. break;
    204. case 57: result = 9;
    205. break;
    206. default: break;
    207. }
    208. return result;
    209. }
    210. /* DeletePolygon-> Speicher des Polygons freigeben */
    211. void DeletePolygon(struct Polygon *aPtrPolygon)
    212. {
    213. if(!aPtrPolygon) return;
    214. if(aPtrPolygon->ptr_x) free(aPtrPolygon->ptr_x);
    215. if(aPtrPolygon->ptr_y) free(aPtrPolygon->ptr_y);
    216. }
    217. /********************************************
    218. Fehlerbehandlung
    219. ********************************************/
    220. /* errorNotEnoughPoints-> Polygon sollte mind. 3 Punkte haben */
    221. void errorNotEnoughPoints()
    222. {
    223. printf("Error: A Polygon needs more than 2 points.\n");
    224. exit(-1);
    225. }
    226. /* errorOutOfMemory-> Falls zuwenig Speicher */
    227. void errorOutOfMemory()
    228. {
    229. printf("Fatal Error! Out Of Memory.\n");
    230. exit(-1);
    231. }
    232. /* errorPolygonNotExist-> Falls Polygon nicht created wurde */
    233. void errorPolygonNotExist()
    234. {
    235. printf("Das Polygon exestiert nicht.\n");
    236. exit(-1);
    237. }
    Alles anzeigen


    gruss
    chris
  • Mach dir eine Funktion zum allokieren des Speichers die das Error-Handling kapselt:

    Quellcode

    1. void* my_malloc (size_t nSize)
    2. {
    3. void* p = malloc(nSize);
    4. if (!p)
    5. errorOutOfMemory ();
    6. return p;
    7. }


    ConvASCII könnte auch so ausschauen:

    Quellcode

    1. int ConvASCII(int aASCII)
    2. {
    3. if (aASCII >= 48 && aASCII <= 57)
    4. return aASCII - 48;
    5. else
    6. return 0;
    7. }


    Ein malloc (sizeof (char)) macht keinen Sinn.
    Stattdessen machst du einfach

    Quellcode

    1. switch (tolower (getc ()))


    hth