Guest User

Untitled

a guest
May 27th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. using namespace std;
  7. struct student
  8. {
  9. string name;
  10. string surname;
  11. int grade[6];
  12. float avg;
  13. struct student *next;
  14. struct student *prev;
  15. };
  16.  
  17. typedef struct student StNode;
  18. typedef struct student *Stpt;
  19. Stpt findmax(Stpt first);
  20. void addNode(Stpt *first,Stpt *last,StNode s);
  21. int main()
  22. {
  23. Stpt first = NULL , last=NULL, stMax;
  24. StNode s;
  25. int n,sum,cnt,gr;
  26. cin>>n;
  27. for(int i=0; i<n; i++)
  28. {
  29. cin>>s.name>>s.surname;
  30. sum=0,cnt=0;
  31. while(1)
  32. {
  33. cin>>gr;
  34. if(gr==-1) break;
  35. else s.grade[cnt]=gr;
  36. sum+=gr;
  37. cnt++;
  38. }
  39. if(cnt<4)
  40. cnt=4;
  41. s.avg=(float)sum/cnt;
  42. addNode(&first,&last,s);
  43. }
  44.  
  45. stMax=findmax(first);
  46. cout<<stMax->name<<" "<<stMax->surname<<" "<<stMax->avg;
  47. return 0;
  48. }
  49. Stpt findmax(Stpt first)
  50. {
  51. Stpt Max= first;
  52. while(first!=NULL)
  53. {
  54. if(first->avg>Max->avg)
  55. Max=first;
  56. first=first->next;
  57. }
  58. return Max;
  59. }
  60.  
  61. void addNode(Stpt *first,Stpt *last,StNode s)
  62. {
  63. Stpt node =(Stpt)malloc(sizeof(StNode));
  64. (node->name=s.name);
  65. (node->surname=s.surname);
  66. node->avg=s.avg;
  67. node->next=NULL;
  68. node->prev=NULL;
  69. if((*first)==NULL)
  70. {
  71. (*first)=node;
  72. (*last)=node;
  73. }
  74. else
  75. {
  76. (*last)->next=node;
  77. node->prev=(*last);
  78. (*last)=node;
  79. }
  80. }
  81.  
  82. void printList(Stpt first)
  83. {
  84. while(first!=NULL)
  85. {
  86. cout<<first->name<<" "<<first->surname<<" "<<first->avg;
  87. first=first->next;
  88. }
  89.  
  90. }
Add Comment
Please, Sign In to add comment