Advertisement
Guest User

Untitled

a guest
Nov 24th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.03 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "student.h"
  5. #define LEN 1000
  6. int student::init (const char *n,int v)
  7. {
  8. value=v;
  9. if(n)
  10. {
  11. name = new char[strlen(n)+1];
  12. if(!name) return -1;
  13. strcpy(name,n);
  14. return 0;
  15. }
  16. name=0;
  17. return 0;
  18. }
  19.  
  20. student::student(const char *n,int v)
  21. {
  22. init(n,v);
  23. }
  24.  
  25. student::student(const student &x)
  26. {
  27. init (x.name,x.value);
  28. }
  29.  
  30. student::~student()
  31. {
  32. if(name)
  33. {
  34. delete[]name;
  35. name=0;
  36. }
  37. value=0;
  38. }
  39.  
  40. student & student:: operator=(const student &x)
  41. {
  42. this -> ~student();
  43. init(x.name,x.value);
  44. return *this;
  45. }
  46.  
  47. int student :: operator < (const student & b)
  48. {
  49. if (strcmp(name,b.name)<0) return 1;
  50. else
  51. {
  52. if (strcmp(name,b.name)==0)
  53. {
  54. if (value<b.value) return 1;
  55. }
  56. }
  57. return 0;
  58. }
  59.  
  60. int student :: operator > (const student & b)
  61. {
  62. if (strcmp(name,b.name)>0) return 1;
  63. else
  64. {
  65. if (strcmp(name,b.name)==0)
  66. {
  67. if (value>b.value) return 1;
  68. }
  69. }
  70. return 0;
  71. }
  72.  
  73. int student :: operator == (const student & b)
  74. {
  75. if (strcmp(name,b.name)==0 && value==b.value) return 1;
  76. return 0;
  77. }
  78.  
  79. int student:: read(FILE *fp)
  80. {
  81. char buf[LEN];
  82. this -> ~student();
  83. if(fscanf(fp,"%s%d",buf,&value)!=2)
  84. {
  85. if(feof(fp)) return -1;
  86. return -2;
  87. }
  88. return init (buf,value);
  89. }
  90.  
  91. void student:: print(FILE *fp,int level)
  92. {
  93. int j;
  94. for(j=0;j<level*2;j++) printf(" ");
  95. fprintf(fp,"%s-%d\n",name,value);
  96. }
  97.  
  98. void student:: menu()
  99. {
  100. char *p;
  101. char s[LEN];
  102. int k;
  103. print_menu_student();
  104. while(fgets(s,LEN,stdin))
  105. {
  106.  
  107. k=strtol(s,&p,10);
  108. if(p==s) continue;
  109. switch(k)
  110. {
  111. case -1: return ;
  112. case 1: { print(stdout,0);break;}
  113. default: { printf("Error number\n");}
  114. }
  115. print_menu_student();
  116. }
  117. return;
  118. }
  119.  
  120. void print_menu_student()
  121. {
  122. printf("В классе STUDENT доступны следующие функции:\n");
  123. printf("-1-выйти\n");
  124. printf("1-печать\n");
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement