fork download
  1. #include <stdio.h>
  2. #include <stdlib.h> // για atoi()
  3.  
  4. // Macros
  5.  
  6. #define MAXSLEN (254 + 1) // MAXimum String LENgth
  7.  
  8. // Πρόσθετοι Τύποι Δεδομένων
  9.  
  10. typedef struct person // ορισμός δομής person ως πρόσθετο τύπο
  11. { // με το όνομα Person (λόγω του typedef)
  12. char name[ MAXSLEN ]; // ... 1ο πεδίο της δομής
  13. char surname[ MAXSLEN ]; // ... 2ο πεδίο της δομής
  14. int age; // ... 3ο πεδίο της δομής
  15. } Person; // το όνομα του πρόσθετου τύπου
  16.  
  17. // Πρότυπα Συναρτήσεων
  18.  
  19. char *s_getflushed(char *s, size_t len); // αντί fgets, για να μην κρατάει το '\n'
  20. void read_person( Person *someone ); // για διάβασμα των πεδίων της someone
  21. void print_person( Person someone ); // για τύπωμα των πεδίων της someone
  22.  
  23. // -------------------------------------------------------------------------------
  24. int main( void )
  25. {
  26. Person someone; // η someone είναι μεταβλητή τύπου Person
  27.  
  28. read_person( &someone ); // πέρασμα της someone by reference
  29. print_person( someone ); // πέρασμα της someone by value
  30.  
  31. return 0;
  32. }
  33. // -------------------------------------------------------------------------------
  34. char *s_getflushed(char *s, size_t len)
  35. {
  36. char *cp;
  37. for (cp=s; (*cp=getc(stdin)) != '\n' && (cp-s) < len-1; cp++ )
  38. ;
  39.  
  40. if ( *cp != '\n') { // len reached withoutn '\n'
  41. *cp = '\0'; // null terminate s and
  42. while (getchar() != '\n') // flush remaining chars
  43. ;
  44. }
  45. else // '\n' found
  46. *cp = '\0'; // null-terminate
  47.  
  48. return s;
  49. }
  50. // -------------------------------------------------------------------------------
  51. void read_person( Person *someone ) // by reference πέρασμα της someone
  52. { // άρα χρήση του -> για τα πεδία της
  53. char inbuf[ 254+1 ] = {'\0'};
  54.  
  55. printf("Dwse to onoma sou: ");
  56. s_getflushed( someone->name, MAXSLEN );
  57.  
  58. printf("Dwse to epwnumo sou: ");
  59. s_getflushed( someone->surname, MAXSLEN );
  60.  
  61. printf("Dwse tin ilikia sou: ");
  62. s_getflushed( inbuf, MAXSLEN );
  63. someone->age = atoi( inbuf );
  64.  
  65. return;
  66. }
  67. // -------------------------------------------------------------------------------
  68. void print_person( Person someone ) // by value πέρασμα της someone
  69. { // άρα χρήση τελείας για τα πεδία της
  70. puts("========================================");
  71.  
  72. printf("To onoma pou edwses einai: %s\n", someone.name);
  73. printf("To epwnimo pou edwses einai: %s\n", someone.surname);
  74. printf("H ilikia pou edwses einai: %d\n", someone.age);
  75.  
  76. putchar('\n');
  77.  
  78. return;
  79. }
  80.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty