1 / 24

Úvod do jazyka C

Úvod do jazyka C. Algoritmizácia úloh. Návrat číselnej hodnoty 1. #include &lt;stdio.h&gt; int funkcia(int x) { return x*x; } main() { int x; int y; x=3; y=funkcia(x); printf(&quot;<br> Mocnina %d je %d &quot;, x, y); return 0; }. Návrat číselnej hodnoty 2. #include &lt;stdio.h&gt; i nt y ;

Download Presentation

Úvod do jazyka C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Úvod do jazyka C Algoritmizácia úloh

  2. Návrat číselnej hodnoty 1. #include <stdio.h> int funkcia(int x) { return x*x; } main() { int x; int y; x=3; y=funkcia(x); printf("\n\r Mocnina %d je %d ",x,y); return 0; }

  3. Návrat číselnej hodnoty 2. #include <stdio.h> int y; int funkcia(int x) { return x*x; } main() { int x; x=3; y=funkcia(x); printf("\n\r Mocnina %d je %d ",x,y); return 0; }

  4. Návrat číselnej hodnoty 3. #include <stdio.h> void funkcia(int x, int *mocnina) { *mocnina = (x * x); // -- ulozime na adresu, kde ukazuje “mocnina” return; } main() { int x,y; x=3; funkcia(x, (int*)&y); printf("\n\r Mocnina %d je %d ",x,y); return 0; }

  5. Reťazce a ukazovatele #include <stdio.h> #include <string.h> main() { char str1[30], *ptr1; ptr1=(char*)&str1; strcpy((char*)&str1,"pokus1234"); printf("\n\r Str1 obsahuje: '%s' ",str1); strcpy((char*)ptr1,"pokus5678"); printf("\n\r Str1 obsahuje: '%s' ",str1); }

  6. Základne funkcie pre spracovanie textu // vracia dlzku retazca int strlen (char * str ); // kopiruje retazec *str do retazec *dest strcpy (char * dest, const char * src); // kopiruje retazec *str do retazec *dest, ale maximalne num znakov strncpy (char * dest, const char * src, size_t num );

  7. Základne funkcie pre spracovanie textu // pripoji retazec *src za retazec *dest strcat ( char * dest, const char * src); // najde podretazec *str2 na *str1 char *strstr ( const char * str1, const char * str2 ); // porovna dva retazce, ak su zhodne vrati 0 int strcmp ( const char * str1, const char * str2 );

  8. Kopírovanie reťazcov #include <stdio.h> #include <string.h> main() { int i; char str1[10]; strcpy((char*)&str1,"pokus"); printf("\n\r Dlzka retazca '%s' : %d ",str1,strlen(str1)); for(i=0; i<strlen(str1)+1; i++) printf("\n\r %d - %c ma hodnotu %d ",i,str1[i],str1[i]); }

  9. Kopírovanie reťazcov

  10. Kopírovanie reťazcov - chyba #include <stdio.h> #include <string.h> main() { int i, pocet=4; char str1[30]; strncpy((char*)&str1,"velmi dlhy retazec",pocet); printf("\n\r Dlzka retazca '%s' : %d ",str1,strlen(str1)); for(i=0; i<strlen(str1)+1; i++) printf("\n\r %d - '%c' ma hodnotu %d ",i,str1[i],str1[i]); }

  11. Kopírovanie reťazcov - chyba

  12. Kopírovanie reťazcov #include <stdio.h> #include <string.h> main() { int i, pocet=4; char str1[30]; strncpy((char*)&str1,"velmi dlhy retazec",pocet); str1[pocet]=0; printf("\n\r Dlzka retazca '%s' : %d ",str1,strlen(str1)); for(i=0; i<strlen(str1)+1; i++) printf("\n\r %d - '%c' ma hodnotu %d ",i,str1[i],str1[i]); }

  13. Kopírovanie reťazcov

  14. Pripájanie reťazcov #include <stdio.h> #include <string.h> main() { int i, pocet=4; char str1[30], str2[30]; strcpy((char*)&str1,"Jan"); strcpy((char*)&str2,"Novak"); printf("\n\r Str1: '%s' ",str1); printf("\n\r Str2: '%s' ",str2); strcat((char*)&str1,(char*)&str2); printf("\n\r Str1: '%s' ",str1); printf("\n\r Str2: '%s' ",str2); }

  15. Pripájanie reťazcov

  16. Porovnanie reťazcov #include <stdio.h> #include <string.h> main() { int i, pocet=4; char str1[30], str2[30]; strcpy((char*)&str1,"Jan"); strcpy((char*)&str2,"Jan"); printf("\n\r Porovnanie1: %d ", strcmp((char*)&str1,(char*)&str2)); strcpy((char*)&str1,"Jan"); strcpy((char*)&str2,"jan"); printf("\n\r Porovnanie2: %d ", strcmp((char*)&str1,(char*)&str2)); }

  17. Porovnanie reťazcov

  18. Hľadanie podreťazca #include <stdio.h> #include <string.h> main() { int i, pocet=4; char str1[30], str2[30], *ptr; strcpy((char*)&str1,"Toto je nejaky text"); strcpy((char*)&str2,"je"); ptr=strstr((char*)&str1,(char*)&str2); if(ptr!=NULL) printf("\n\r Nasli sme '%s' ", ptr); else printf("\n\r Takyto retazec sa tu nenachadza"); }

  19. Hľadanie podreťazca

  20. Kopírovanie reťazcov #include <stdio.h> #include <string.h> main() { char str1[30], str2[30], *ptr1, *ptr2; strcpy((char*)&str1,"Jan"); strcpy((char*)&str2,(char*)&str1); printf("\n\r Str1: '%s' Str2: '%s' ",str1,str2); }

  21. Kopírovanie reťazcov – použitie ukazovateľov #include <stdio.h> #include <string.h> main() { char str1[30], str2[30], *ptr1, *ptr2; ptr1=(char*)&str1; ptr2=(char*)&str2; strcpy((char*)ptr1,"Jan"); strcpy((char*)ptr2,(char*)ptr1); // nasledujuce riadky su strcpy((char*)ptr2,(char*)&str1); // ekvivalentne printf("\n\r Str1: '%s' Str2: '%s' ",str1,str2); }

  22. Vrátenie reťazcov z funkcií #include <stdio.h> #include <string.h> void funkcia(char *str) { strcpy((char*)str,"Nejaky vysledny text"); } main() { char str1[30], str2[30], *ptr1, *ptr2; funkcia((char*)&str1); printf("\n\r Str1: '%s' ",str1); }

  23. Vrátenie reťazcov z funkcií #include <stdio.h> #include <string.h> int funkcia(char *hladane_meno) { FILE *fp; char meno[30]; int pocet=0; fp=fopen("mena.txt","r"); while(!feof(fp)) { fscanf(fp,"%s\n",&meno); if(strcmp((char*)&meno,(char*)hladane_meno)==0) pocet++; } fclose(fp); return pocet; } main() { int pocet; pocet=funkcia("novak"); printf("\n\r V subore je '%d' Janov",pocet); }

  24. Vrátenie reťazcov z funkcií #include <stdio.h> #include <string.h> void funkcia(int poradie, char *meno) { FILE *fp; char meno_tmp[30]; int i=0; fp=fopen("mena.txt","r"); while(!feof(fp)) { fscanf(fp,"%s\n",&meno_tmp); if(poradie==i) strcpy((char*)meno,(char*)&meno_tmp); i++; } fclose(fp); } main() { int riadok=3; char meno[30]; funkcia(riadok,(char*)meno); printf("\n\r Na riadku %d je meno %s ",riadok,meno); }

More Related