Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Ovdje pisete vas C kod.
- Potrebno je napisati Vasu funkciju bez upotrebe dodatnih funkcija,
- potrebno je napraviti testnu skelu "int main(void)" te
- testne slucajeve sa kojim cete testirati ispravan rad vase funkcije.
- */
- #include <stdio.h>
- // Prototip funkcije
- int model05(char *);
- int main(void)
- {
- const char *testskaf[20];
- //1 spefifikacija
- testskaf[0]="0";
- testskaf[1]="123456789-123456789-123456789";
- testskaf[2]="01234567891011";
- testskaf[3]="123456-123456-123456";
- //2 specifikacija
- testskaf[4]="1235981-293";
- testskaf[5]="abcdefgh-1123348";
- testskaf[6]="*///\%#";
- testskaf[7]="000000000";
- //3 specifikacija
- testskaf[8]="001-110-221";
- testskaf[9]="---";
- testskaf[10]="001-";
- testskaf[11]="--0283";
- //4 specifikacija
- testskaf[12]="132560";
- testskaf[13]="132560-122568";
- testskaf[14]="2803159";
- testskaf[15]="97217";
- //5 specifikacija
- testskaf[16]="1122334455667-8999988";
- testskaf[17]="22-1122334455667";
- testskaf[18]="22-22-1234567891011";
- testskaf[19]="97217";
- printf("Pogreska:%d",testskaf[0]);
- return 0;
- }
- int model05(char *pnb){
- //inicijalizacija
- int stringlength=0;
- int stringlengthpb1=0;
- int stringlengthpb2=0;
- int stringlengthpb3=0;
- int KBU=0,KBR=0;
- char string[100];
- char PB1[22],PB2[22],PB3[22];
- int indexcrtica[3];
- int brojac=0,brcrtica=0;
- //učitavanje stringa
- while((pnb[stringlength])!='\0')
- {
- string[stringlength]=pnb[stringlength];
- stringlength++;
- }
- string[stringlength]='\0';
- //prebrojavanje koliko elemenata stringa su "-" ili znamenka (0-9), te ukoliko ima crtica, koliko ih ima te na kojem su mjestu
- for (int i=0;i<stringlength;i++)
- {
- if((string[i]>=48 && string[i]<=57) || string[i]==45)
- {
- brojac++;
- }
- if(string[i]==45)
- {
- indexcrtica[brcrtica]=i; //sprema index crtice u string-u, u polje indexcrtica
- brcrtica++;
- }
- }
- //Slucajevi ovisno o broju crtica, sluze za podjelu stringa na P1, P2, P3
- switch(brcrtica)
- {
- //0 crtica P1 je cijeli string
- case 0:
- {
- int g=0;
- for(g;g<stringlength;g++)
- {
- PB1[g]=string[g];
- }
- PB1[g]='\0';
- break;
- }
- //1 crtica, string se dijeli na P1 i P2 gdje je P1 lijevo od crtice a P2 desno od crtice
- case 1:
- {
- int h=0;
- for(h;h<indexcrtica[0];h++)
- {
- PB1[h]=string[h];
- }
- PB1[h]='\0';
- int x=0;
- for(int k=(indexcrtica[0]+1);k<stringlength;k++)
- {
- PB2[x++]=string[k];
- }
- PB2[x]='\0';
- break;
- }
- //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
- case 2:
- {
- int h=0;
- for(h;h<indexcrtica[0];h++)
- {
- PB1[h]=string[h];
- }
- PB1[h]='\0';
- int z=0,y=0;
- for(int k=(indexcrtica[0]+1);k<indexcrtica[1];k++)
- {
- PB2[y++]=string[k];
- }
- PB2[y]='\0';
- for(int g=indexcrtica[1]+1;g<stringlength+1;g++)
- {
- PB3[z++]=string[g];
- }
- PB3[z]='\0';
- break;
- }
- default:
- {}
- }
- //provjera duljine p1,p2,p3
- if(brcrtica==0)
- {
- while(PB1[stringlengthpb1]!='\0')
- {
- stringlengthpb1++;
- }
- }
- else if(brcrtica==1)
- {
- while(PB1[stringlengthpb1]!='\0')
- {
- stringlengthpb1++;
- }
- while(PB2[stringlengthpb2]!='\0')
- {
- stringlengthpb2++;
- }
- }
- else if(brcrtica==2)
- {
- while(PB1[stringlengthpb1]!='\0')
- {
- stringlengthpb1++;
- }
- while(PB2[stringlengthpb2]!='\0')
- {
- stringlengthpb2++;
- }
- while(PB3[stringlengthpb3]!='\0')
- {
- stringlengthpb3++;
- }
- }
- else{}
- //provjera KB1
- KBU=PB1[stringlengthpb1-1]-48; //-48 zbog pretvorbe ASCII u INT
- int ponder=2;
- int ostatak=0;
- int suma=0;
- for(int i=stringlengthpb1-2;i>=0;i--)
- {
- suma+=((PB1[i]-48)*ponder);
- ponder++;
- }
- ostatak=suma%11;
- if(ostatak==0 || ostatak==1)
- {
- KBR=0;
- }
- else
- {
- KBR=11-ostatak;
- }
- //Kod koji vrši detekciju pogrešaka
- // Detekcija pogreške string izvan zeljenog opsega
- if(stringlength<2 || stringlength>22)
- {
- return -1;
- }
- // Detekcija pogreške string sadrži ne dozvoljene znakove
- else if (brojac!=stringlength)
- {
- return -2;
- }
- //Detekcija pogreške string sadrzi više od dvije crtice
- else if(brcrtica>2)
- {
- return -3;
- }
- //Detekcija pogreške duljina stringa P1,P2 ili P3 je veća od 12
- else if(brcrtica<=2 && (stringlengthpb1>12 ||stringlengthpb2>12 ||stringlengthpb3>12))
- {
- return -5;
- }
- else if(KBU!=KBR)
- {
- return -4;
- }
- else{
- return 0;
- }
- }// Tijelo funkcije
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement