Úvod do jazyka C
-
Upload
dolan-watkins -
Category
Documents
-
view
62 -
download
4
description
Transcript of Úvod do jazyka C
Úvod do jazyka C
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;
}
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;}
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;}
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);
}
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 );
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 );
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]);
}
Kopírovanie reťazcov
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]);}
Kopírovanie reťazcov - chyba
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]);}
Kopírovanie reťazcov
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);
}
Pripájanie reťazcov
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));
}
Porovnanie reťazcov
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");
}
Hľadanie podreťazca
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);
}
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 sustrcpy((char*)ptr2,(char*)&str1); // ekvivalentne
printf("\n\r Str1: '%s' Str2: '%s' ",str1,str2);
}
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);
}
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);
}
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);
}