Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.47 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. typedef struct{
  6. int cod;
  7. char nume[255];
  8. int suprafata;
  9. int loc;
  10. int venit;
  11. }Tara;
  12.  
  13. Tara t[100];
  14.  
  15.  
  16. void afisare1(Tara t[], int n)
  17. {
  18. Tara aux;
  19. printf("\nTarile in ordine alfabetica:");
  20. for(int i=0;i<n-1;i++)
  21. {
  22. for(int j=i+1;j<n;j++)
  23. if(strcmp(t[i].nume, t[j].nume)>0)
  24. {
  25. aux=t[i];
  26. t[i]=t[j];
  27. t[j]=aux;
  28. }
  29. }
  30.  
  31. for(int i=0;i<n;i++)
  32. {
  33. printf("\n%s, %g loc/kmp, %g venit/loc",t[i].nume,(float)t[i].loc/t[i].suprafata,(float)t[i].venit/t[i].loc);
  34. }
  35. printf("\n");
  36. }
  37.  
  38. void afisare2(Tara t[],int n)
  39. {
  40. Tara aux;
  41. float v1,v2;
  42. printf("\nTarile in ordinea crescatoare a nr de loc/kmp:");
  43. for(int i=0;i<n-1;i++)
  44. {
  45. for(int j=i+1;j<n;j++)
  46. {
  47. v1=(float)t[i].loc/t[i].suprafata;
  48. v2=(float)t[j].loc/t[j].suprafata;
  49. if(v1>v2)
  50. {
  51. aux=t[i];
  52. t[i]=t[j];
  53. t[j]=aux;
  54. }
  55. }
  56. }
  57.  
  58. for(int i=0;i<n;i++)
  59. {
  60. printf("\n%s, %g loc/kmp, %g venit/loc",t[i].nume,(float)t[i].loc/t[i].suprafata,(float)t[i].venit/t[i].loc);
  61. }
  62. printf("\n");
  63. }
  64.  
  65. void afisare3(Tara t[],int n)
  66. {
  67. Tara aux;
  68. float v1,v2;
  69. printf("\nTarile in ordinea descrescatoare a venit/loc:");
  70. for(int i=0;i<n-1;i++)
  71. {
  72. for(int j=i+1;j<n;j++)
  73. {
  74. v1=(float)t[i].venit/t[i].loc;
  75. v2=(float)t[j].venit/t[j].loc;
  76. if(v1<v2)
  77. {
  78. aux=t[i];
  79. t[i]=t[j];
  80. t[j]=aux;
  81. }
  82. }
  83. }
  84.  
  85. for(int i=0;i<n;i++)
  86. {
  87. printf("\n%s, %g loc/kmp, %g venit/loc",t[i].nume,(float)t[i].loc/t[i].suprafata,(float)t[i].venit/t[i].loc);
  88. }
  89. printf("\n");
  90. }
  91.  
  92. int main()
  93. {
  94. int n;
  95. char s[255];
  96. char *p=NULL;
  97. printf("\nCate tari? "); scanf("%d",&n); fflush(stdin);
  98. for(int i=0;i<n;i++)
  99. {
  100. gets(s);
  101. p=strtok(s,", ");
  102. t[i].cod=atoi(p);
  103. p=strtok(NULL,", ");
  104. strcpy(t[i].nume,p);
  105. p=strtok(NULL,", ");
  106. t[i].suprafata=atoi(p);
  107. p=strtok(NULL,", ");
  108. t[i].loc=atoi(p);
  109. p=strtok(NULL,", ");
  110. t[i].venit=atoi(p);
  111. }
  112. afisare1(t,n);
  113. afisare2(t,n);
  114. afisare3(t,n);
  115. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement