Advertisement
Solomid

Untitled

Jan 16th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.04 KB | None | 0 0
  1. TUT 9
  2.  
  3. 5.
  4.  
  5. #include <stdio.h>
  6.  
  7. int prva_rijec(char *s){
  8.     int br=0;
  9.     char *p;
  10.     p = s;
  11.     if(*s == '\0') return 0;
  12.    
  13.     if(*s != ' ') br++;
  14.     while(*s != '\0'){
  15.         if(*s == ' ' && *(s+1) != ' ' && *(s+1) != '\0') {
  16.             br++;
  17.         }
  18.         s++;
  19.     }
  20.     while(*p != '\0'){
  21.         if(*p != ' ' && *(p+1) == ' ' && *(p+1) != '\0'){
  22.             *(p+1) = '\0';
  23.             break;
  24.         }
  25.         p++;
  26.     }
  27.     return br;
  28. }
  29.  
  30. int main() {
  31.     printf("Tutorijal 9, Zadatak 5");
  32.     return 0;
  33. }
  34.  
  35. 7.
  36.  
  37. #include <stdio.h>
  38. #include <string.h>
  39.  
  40. int slovo(char s)
  41. {
  42.     if(s >= 'A' && s <= 'Z')
  43.         return 1;
  44.     return 0;
  45. }
  46.  
  47. int uporedi(const char *s,const char *p)
  48. {
  49.     if(strcmp(s,p)>0) return 1;
  50.     if(strcmp(s,p)<0) return -1;
  51.     if(strcmp(s,p)==0) return 0;
  52. }
  53.  
  54. int strcmpi(const char *s,const char *p,int velikamala)
  55. {
  56.     char tmp1,tmp2;
  57.     if(velikamala==1) {
  58.         uporedi(s,p);
  59.     } else {
  60.         while(*s!='\0' && *p!='\0') {
  61.             tmp1=*s;
  62.             tmp2=*p;
  63.             if(slovo(tmp1)) tmp1 += 32;
  64.             if(slovo(tmp2)) tmp2 += 32;
  65.             if(tmp1<tmp2)return 1;
  66.             if(tmp1>tmp2)return -1;
  67.             s++;
  68.             p++;
  69.         }
  70.         if(*p !='\0')
  71.             return -1;
  72.         if(*s !='\0')
  73.             return 1;
  74.         return 0;
  75.     }
  76. }
  77. int main()
  78. {
  79.     printf("Tutorijal 9, Zadatak 7");
  80.  
  81.     return 0;
  82. }
  83.  
  84. TUT 10
  85.  
  86. 4.
  87.  
  88. #include <stdio.h>
  89. #include <string.h>
  90.  
  91. char* whitespace(char* s)
  92. {
  93.     char *p = s, *pocetak = s;
  94.     char *pom1, *pom2;
  95.  
  96.     while(*s != '\0') {
  97.         if(*s == '\n' || *s == '\t')
  98.             *s = ' ';
  99.         s++;
  100.     }
  101.    
  102.     while(*p != '\0') {        
  103.         if(*p == ' ') {
  104.             if(p != pocetak && *(p-1) != ' ' && *(p+1) != '\0'){
  105.                 p++;   
  106.             }
  107.            
  108.             pom1 = p;  //za brisanje razmaka
  109.             pom2 = p; // za vracanje pokazivaca nakon brisanja
  110.            
  111.             while(*pom1 == ' ') {
  112.                 pom1++;
  113.             }
  114.            
  115.             if(*pom1 == '\0') { // uslov za kraj stringa
  116.                 if(p != pocetak) p--;
  117.                 *p = *pom1;
  118.                 break;
  119.             }
  120.            
  121.             while(*p != '\0') {
  122.                 *p = *pom1;
  123.                 if(*p == '\0') break;
  124.                 p++;
  125.                 pom1++;
  126.             }
  127.             p = pom2;
  128.             p--; // da ne preskoci nijedan znak
  129.         }
  130.         p++;
  131.     }
  132.     return pocetak;
  133. }
  134.  
  135.  
  136. int main()
  137. {
  138.     char tekst[] = "Druga\nrecenica\t\n\t";
  139.     printf("'%s'", whitespace(tekst));
  140.     return 0;
  141. }
  142.  
  143.  
  144. TUT 11
  145.  
  146. 5.
  147.  
  148. #include <stdio.h>
  149. #include <math.h>
  150.  
  151. struct Vrijeme {
  152.     int sati,minute,sekunde;   
  153. };
  154.  
  155. struct Vrijeme unos_vremena(){
  156.     struct Vrijeme v;
  157.     scanf("%d %d %d",&v.sati,&v.minute,&v.sekunde);
  158.     return v;
  159. }
  160.  
  161. struct Vrijeme proteklo(struct Vrijeme v1, struct Vrijeme v2){
  162.     struct Vrijeme v3;
  163.     int v1_u_sek = 0, v2_u_sek = 0;
  164.     v1_u_sek = v1.sati*3600 + v1.minute*60 + v1.sekunde;
  165.     v2_u_sek = v2.sati*3600 + v2.minute*60 + v2.sekunde;
  166.     int razlika = fabs(v2_u_sek-v1_u_sek);
  167.     v3.sati = razlika/3600;
  168.     razlika %= 3600;
  169.     v3.minute = razlika/60;
  170.     razlika %= 60;
  171.     v3.sekunde = razlika;
  172.     return v3;
  173. }
  174.  
  175. int main() {
  176.     struct Vrijeme v1,v2,v3;
  177.     printf("Unesite prvo vrijeme (h m s): \n");
  178.     v1 = unos_vremena();
  179.     printf("Unesite drugo vrijeme (h m s): \n");
  180.     v2 = unos_vremena();
  181.     v3 = proteklo(v1,v2);
  182.     printf("Izmedju dva vremena je proteklo %d sati, %d minuta i %d sekundi.",v3.sati,v3.minute,v3.sekunde);
  183.     return 0;
  184. }
  185.  
  186. 6.
  187.  
  188. #include <stdio.h>
  189.  
  190. struct Osoba {
  191.     char ime[15];
  192.     char prezime[20];
  193.     int telefon;
  194. };
  195.  
  196. void unesi(char niz[], int velicina)
  197. {
  198.     char znak = getchar();
  199.     if (znak == '\n') znak = getchar();
  200.     int i = 0;
  201.     while(i < velicina - 1 && znak != '\n') {
  202.         niz[i] = znak;
  203.         i++;
  204.         znak = getchar();
  205.     }
  206.     niz[i]='\0';
  207. }
  208.  
  209. struct Osoba unos_osobe()
  210. {
  211.     struct Osoba o;
  212.     printf("Unesite ime: ");
  213.     unesi(o.ime,15);
  214.     printf("Unesite prezime: ");
  215.     unesi(o.prezime,20);
  216.     printf("Unesite broj telefona: ");
  217.     scanf("%d",&o.telefon);
  218.     return o;
  219. }
  220.  
  221. void ispis_osobe(struct Osoba o)
  222. {
  223.     printf("%s %s, Tel: %d",o.ime, o.prezime, o.telefon);
  224. }
  225.  
  226. int main()
  227. {
  228.     int k = 3,i = 0,j = 0;
  229.     struct Osoba imenik[100];
  230.     while(k) {
  231.         printf("Pritisnite 1 za unos, 2 za ispis, 0 za izlaz: ");
  232.         scanf("%d",&k);
  233.         if(k == 1) {
  234.             imenik[i] = unos_osobe();
  235.             i++;
  236.         } else if(k == 2) {
  237.             printf("\n");
  238.             for(j = 0; j < i; j++) {
  239.                 printf("%d. ",j+1);
  240.                 ispis_osobe(imenik[j]);
  241.                 printf("\n");
  242.             }
  243.         }
  244.     }
  245.     return 0;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement