etf2018

Z4Z4

Feb 5th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int strcmpi(const char *s1, const char *s2) {
  4. while (*s1 && *s2) {
  5. if (*s1>='A' && *s1<='Z' && *s2>='a' && *s2<='z') {
  6. if (*s1+32>*s2)
  7. return 1;
  8. else if (*s1+32<*s2)
  9. return -1;
  10. s1++; s2++;
  11. }
  12. if (*s2>='A' && *s2<='Z' && *s1>='a' && *s1<='z') {
  13. if (*s1>*s2+32)
  14. return 1;
  15. else if (*s1<*s2+32)
  16. return -1;
  17. s1++; s2++;
  18. }
  19. if (*s1>*s2)
  20. return 1;
  21. else if (*s1<*s2)
  22. return -1;
  23. s1++; s2++;
  24. }
  25. if (*s1)
  26. return 1;
  27. if (*s2)
  28. return -1;
  29. return 0;
  30. }
  31. int duzinataga(char* htmlkod) {
  32. int duzina= 0;
  33. while (*htmlkod!='>' && *htmlkod!=' ') {
  34. htmlkod++;
  35. duzina++;
  36. }
  37. return duzina;
  38. }
  39. int izbrojiotvorene(char* htmlkod) {
  40. int brojac = 0;
  41. while (*htmlkod) {
  42. if (*htmlkod=='<' && *(htmlkod+1)!='/') {
  43. while (*htmlkod!='>')
  44. htmlkod++;
  45. brojac++;
  46. }
  47. htmlkod++;
  48. }
  49. return brojac;
  50. }
  51. int izbrojizatvorene(char* htmlkod) {
  52. int brojac = 0;
  53. while (*htmlkod) {
  54. if (*htmlkod=='<' && *(htmlkod+1)=='/') {
  55. while (*htmlkod!='>')
  56. htmlkod++;
  57. brojac++;
  58. }
  59. htmlkod++;
  60. }
  61. return brojac;
  62.  
  63. }
  64. int provjeri(char* htmlkod) {
  65. int brojotvorenih, brojzatvorenih, i, duzinaTaga;
  66. brojotvorenih=izbrojiotvorene(htmlkod);
  67. brojzatvorenih=izbrojizatvorene(htmlkod);
  68. if (brojzatvorenih!=brojotvorenih)
  69. return 0;
  70.  
  71. char** otvorenitagovi=malloc(brojotvorenih*sizeof(char *));
  72. char** zatvorenitagovi=malloc(brojzatvorenih*sizeof(char *));
  73.  
  74. char* temp = htmlkod;
  75. char* temp2;
  76. i=0;
  77. while (*temp) {
  78. if (*temp=='<' && *(temp+1)!='/') {
  79. temp++;
  80. duzinaTaga=duzinataga(temp);
  81. otvorenitagovi[i]=(char *)malloc(duzinaTaga+1);
  82. temp2=otvorenitagovi[i];
  83. while (*temp!=' ' && *temp!='>')
  84. *temp2++=*temp++;
  85. *temp2='\0';
  86. i++;
  87. }
  88. temp++;
  89. }
  90.  
  91. i=0;
  92. temp=htmlkod;
  93.  
  94. while (*temp) {
  95. if (*temp=='<' && *(temp+1)=='/') {
  96. temp+=2;
  97. duzinaTaga=duzinataga(temp);
  98. zatvorenitagovi[i]=(char *)malloc(duzinaTaga+1);
  99. temp2=zatvorenitagovi[i];
  100. while (*temp!='>')
  101. *temp2++=*temp++;
  102. *temp2='\0';
  103. i++;
  104. }
  105. temp++;
  106. }
  107.  
  108. for (i=0;i<brojotvorenih;i++) {
  109. if (strcmpi(otvorenitagovi[i], zatvorenitagovi[brojzatvorenih-1-i])) {
  110. for (i=0;i<brojotvorenih;i++) {
  111. free(otvorenitagovi[i]);
  112. free(zatvorenitagovi[i]);
  113. }
  114. free(otvorenitagovi);
  115. free(zatvorenitagovi);
  116. return 0;
  117. }
  118.  
  119. }
  120.  
  121. for (i=0;i<brojotvorenih;i++) {
  122. free(otvorenitagovi[i]);
  123. free(zatvorenitagovi[i]);
  124. }
  125. free(otvorenitagovi);
  126. free(zatvorenitagovi);
  127. return 1;
  128. }
  129. int main() {
  130. char htmlkod[]={"<div id=""tekst""><h2>Naslov</h2><p>Ovo je <b>četvrti</b> i <i>posljednji</i> zadatak iz <a href=""zadaca4"">zadaće 4</a></p></div>"};
  131. int brojotvorenih=izbrojiotvorene(htmlkod);
  132. int brojzatvorenih=izbrojizatvorene(htmlkod);
  133. int provjera=provjeri(htmlkod);
  134. printf ("%d %d, %d", brojotvorenih, brojzatvorenih, provjera);
  135. return 0;
  136. }
Add Comment
Please, Sign In to add comment