Advertisement
Guest User

Untitled

a guest
Jan 21st, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.43 KB | None | 0 0
  1. /* Ovdje pisete vas C kod.
  2. Potrebno je napisati Vasu funkciju bez upotrebe dodatnih funkcija,
  3. potrebno je napraviti testnu skelu "int main(void)" te
  4. testne slucajeve sa kojim cete testirati ispravan rad vase funkcije.
  5. */
  6.  
  7. #include <stdio.h>
  8.  
  9. // Prototip funkcije
  10. int model05(char *);
  11. int main(void)
  12. {
  13. const char *testskaf[20];
  14. //1 spefifikacija
  15. testskaf[0]="0";
  16. testskaf[1]="123456789-123456789-123456789";
  17. testskaf[2]="01234567891011";
  18. testskaf[3]="123456-123456-123456";
  19. //2 specifikacija
  20. testskaf[4]="1235981-293";
  21. testskaf[5]="abcdefgh-1123348";
  22. testskaf[6]="*///\%#";
  23. testskaf[7]="000000000";
  24. //3 specifikacija
  25. testskaf[8]="001-110-221";
  26. testskaf[9]="---";
  27. testskaf[10]="001-";
  28. testskaf[11]="--0283";
  29. //4 specifikacija
  30. testskaf[12]="132560";
  31. testskaf[13]="132560-122568";
  32. testskaf[14]="2803159";
  33. testskaf[15]="97217";
  34. //5 specifikacija
  35. testskaf[16]="1122334455667-8999988";
  36. testskaf[17]="22-1122334455667";
  37. testskaf[18]="22-22-1234567891011";
  38. testskaf[19]="97217";
  39. printf("Pogreska:%d",testskaf[0]);
  40. return 0;
  41. }
  42.  
  43. int model05(char *pnb){
  44. //inicijalizacija
  45. int stringlength=0;
  46. int stringlengthpb1=0;
  47. int stringlengthpb2=0;
  48. int stringlengthpb3=0;
  49. int KBU=0,KBR=0;
  50. char string[100];
  51. char PB1[22],PB2[22],PB3[22];
  52. int indexcrtica[3];
  53. int brojac=0,brcrtica=0;
  54.  
  55. //učitavanje stringa
  56. while((pnb[stringlength])!='\0')
  57. {
  58. string[stringlength]=pnb[stringlength];
  59. stringlength++;
  60. }
  61. string[stringlength]='\0';
  62.  
  63.  
  64. //prebrojavanje koliko elemenata stringa su "-" ili znamenka (0-9), te ukoliko ima crtica, koliko ih ima te na kojem su mjestu
  65. for (int i=0;i<stringlength;i++)
  66. {
  67. if((string[i]>=48 && string[i]<=57) || string[i]==45)
  68. {
  69. brojac++;
  70. }
  71. if(string[i]==45)
  72. {
  73. indexcrtica[brcrtica]=i; //sprema index crtice u string-u, u polje indexcrtica
  74. brcrtica++;
  75.  
  76. }
  77. }
  78.  
  79. //Slucajevi ovisno o broju crtica, sluze za podjelu stringa na P1, P2, P3
  80. switch(brcrtica)
  81. {
  82. //0 crtica P1 je cijeli string
  83. case 0:
  84. {
  85. int g=0;
  86. for(g;g<stringlength;g++)
  87. {
  88. PB1[g]=string[g];
  89. }
  90. PB1[g]='\0';
  91.  
  92. break;
  93. }
  94. //1 crtica, string se dijeli na P1 i P2 gdje je P1 lijevo od crtice a P2 desno od crtice
  95. case 1:
  96. {
  97. int h=0;
  98. for(h;h<indexcrtica[0];h++)
  99. {
  100. PB1[h]=string[h];
  101. }
  102.  
  103. PB1[h]='\0';
  104. int x=0;
  105. for(int k=(indexcrtica[0]+1);k<stringlength;k++)
  106. {
  107. PB2[x++]=string[k];
  108. }
  109. PB2[x]='\0';
  110.  
  111. break;
  112. }
  113. //2 crtice string se dijeli na P1,P2,P3, gdje je p1 lijevo od prve crtive, p2 izmedu 2 i 3 crtice, a p3 desno od druge crtice
  114.  
  115. case 2:
  116. {
  117. int h=0;
  118. for(h;h<indexcrtica[0];h++)
  119. {
  120. PB1[h]=string[h];
  121. }
  122. PB1[h]='\0';
  123. int z=0,y=0;
  124. for(int k=(indexcrtica[0]+1);k<indexcrtica[1];k++)
  125. {
  126. PB2[y++]=string[k];
  127. }
  128. PB2[y]='\0';
  129. for(int g=indexcrtica[1]+1;g<stringlength+1;g++)
  130. {
  131. PB3[z++]=string[g];
  132. }
  133. PB3[z]='\0';
  134. break;
  135. }
  136. default:
  137. {}
  138.  
  139.  
  140. }
  141.  
  142. //provjera duljine p1,p2,p3
  143. if(brcrtica==0)
  144. {
  145.  
  146. while(PB1[stringlengthpb1]!='\0')
  147. {
  148. stringlengthpb1++;
  149. }
  150. }
  151. else if(brcrtica==1)
  152. {
  153. while(PB1[stringlengthpb1]!='\0')
  154. {
  155. stringlengthpb1++;
  156. }
  157. while(PB2[stringlengthpb2]!='\0')
  158. {
  159. stringlengthpb2++;
  160. }
  161.  
  162. }
  163. else if(brcrtica==2)
  164. {
  165. while(PB1[stringlengthpb1]!='\0')
  166. {
  167. stringlengthpb1++;
  168. }
  169. while(PB2[stringlengthpb2]!='\0')
  170. {
  171. stringlengthpb2++;
  172. }
  173. while(PB3[stringlengthpb3]!='\0')
  174. {
  175. stringlengthpb3++;
  176. }
  177. }
  178. else{}
  179. //provjera KB1
  180. KBU=PB1[stringlengthpb1-1]-48; //-48 zbog pretvorbe ASCII u INT
  181. int ponder=2;
  182. int ostatak=0;
  183. int suma=0;
  184. for(int i=stringlengthpb1-2;i>=0;i--)
  185. {
  186. suma+=((PB1[i]-48)*ponder);
  187. ponder++;
  188. }
  189. ostatak=suma%11;
  190. if(ostatak==0 || ostatak==1)
  191. {
  192. KBR=0;
  193. }
  194. else
  195. {
  196. KBR=11-ostatak;
  197. }
  198.  
  199. //Kod koji vrši detekciju pogrešaka
  200.  
  201. // Detekcija pogreške string izvan zeljenog opsega
  202. if(stringlength<2 || stringlength>22)
  203. {
  204. return -1;
  205. }
  206. // Detekcija pogreške string sadrži ne dozvoljene znakove
  207. else if (brojac!=stringlength)
  208. {
  209. return -2;
  210. }
  211. //Detekcija pogreške string sadrzi više od dvije crtice
  212. else if(brcrtica>2)
  213. {
  214. return -3;
  215. }
  216. //Detekcija pogreške duljina stringa P1,P2 ili P3 je veća od 12
  217. else if(brcrtica<=2 && (stringlengthpb1>12 ||stringlengthpb2>12 ||stringlengthpb3>12))
  218. {
  219. return -5;
  220. }
  221. else if(KBU!=KBR)
  222. {
  223. return -4;
  224. }
  225. else{
  226. return 0;
  227. }
  228.  
  229. }// Tijelo funkcije
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement