Created
October 18, 2015 18:56
-
-
Save tkarabela/b47ad6e59dfd57ec47ae to your computer and use it in GitHub Desktop.
Jednoduché početní programy v C
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> /* funkce scanf(), printf() */ | |
| #include <math.h> /* konstanta M_PI = 3.14159... */ | |
| int main() | |
| { | |
| /* polomer kruhu bude kladne realne cislo, proto pouzijeme double */ | |
| double r; | |
| printf("Zadejte polomer kruhu: "); | |
| /* pro nacitani typu double slouzi konverze %lf */ | |
| /* scanf vraci pocet uspesne nactenych polozek (zde 0 nebo 1) */ | |
| if (scanf("%lf", &r) != 1) { | |
| printf("Nepodarilo se precist cislo!\n"); | |
| /* predcasne ukonceni programu */ | |
| return 0; | |
| } else if (r <= 0) { | |
| printf("Polomer musi byt kladne cislo!\n"); | |
| return 0; | |
| } | |
| printf("Obvod: %f\n", 2*M_PI*r); | |
| printf("Obsah: %f\n", M_PI*r*r); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> /* funkce scanf(), printf() */ | |
| #include <math.h> /* funkce sqrt() - druha odmocnina, vraci hodnotu typu double */ | |
| int main() | |
| { | |
| /* | |
| deklarace promennych | |
| - budeme pracovat s realnymi cisly, proto pouzijeme typ double (plovouci radova carka) | |
| */ | |
| double a, b, c; /* vstupy - koeficienty kvadraticke rovnice */ | |
| double D; /* diskriminant */ | |
| double x1, x2; /* vystup - reseni rovnice */ | |
| /* | |
| nacteni vstupu | |
| */ | |
| printf("Zadejte koeficienty kvadraticke rovnice ve tvaru a*x^2 + b*x + c = 0.\n"); | |
| printf("a = "); | |
| /* pro nacitani typu double slouzi konverze %lf */ | |
| /* scanf vraci pocet uspesne nactenych polozek (zde 0 nebo 1) */ | |
| if (scanf("%lf", &a) != 1) { | |
| printf("Nepodarilo se precist cislo!\n"); | |
| /* predcasne ukonceni programu */ | |
| return 0; | |
| } else if (a == 0) { | |
| printf("Koeficient u kvadratickeho clenu nemuze byt nulovy!\n"); | |
| return 0; | |
| } | |
| printf("b = "); | |
| if (scanf("%lf", &b) != 1) { | |
| printf("Nepodarilo se precist cislo!\n"); | |
| return 0; | |
| } | |
| printf("c = "); | |
| if (scanf("%lf", &c) != 1) { | |
| printf("Nepodarilo se precist cislo!\n"); | |
| return 0; | |
| } | |
| /* | |
| vypocet reseni | |
| */ | |
| D = b*b - 4*a*c; | |
| if (D < 0) { | |
| printf("Rovnice nema reseni v R.\n"); | |
| return 0; | |
| } | |
| x1 = (-b + sqrt(D)) / (2*a); | |
| x2 = (-b - sqrt(D)) / (2*a); | |
| printf("Reseni:\n"); | |
| printf("x1 = %f\n", x1); | |
| printf("x2 = %f\n", x2); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> /* funkce scanf(), printf() */ | |
| /* | |
| uzitecne konstanty | |
| - na rozdil od promennych se zde neuvadi typ | |
| - #define funguje jako nahrazeni v textu - misto vsech vyskytu "PALEC_V_METRECH" se napise "0.025", nez se program prelozi | |
| */ | |
| #define PALEC_V_METRECH 0.025 | |
| #define MILE_V_METRECH 1609 | |
| #define SVETELNY_ROK_V_METRECH 9.46e15 /* 9,46 x 10^15 */ | |
| int main() | |
| { | |
| double delka_v_metrech, zadana_hodnota; | |
| int volba; | |
| /* | |
| nacteni vstupu | |
| */ | |
| printf("V jakych jednotkach chcete zadat vstup?\n"); | |
| printf("[1] metry\n"); | |
| printf("[2] palce\n"); | |
| printf("[3] mile\n"); | |
| printf("[4] svetelne roky\n"); | |
| printf("Zadejte cislo jednotky: "); | |
| if (scanf("%d", &volba) != 1) { | |
| printf("Nepodarilo se precist cislo!\n"); | |
| return 0; | |
| } else if (volba < 1 || volba > 4) { | |
| printf("Neznama volba (musi byt cislo od 1 do 4)!\n"); | |
| return 0; | |
| } | |
| printf("Zadejte delku (ve vami zvolenych jednotkach): "); | |
| if (scanf("%lf", &zadana_hodnota) != 1) { | |
| printf("Nepodarilo se precist cislo!\n"); | |
| return 0; | |
| } | |
| /* | |
| prevod vstupu na metry | |
| */ | |
| if (volba == 1) { | |
| /* metry */ | |
| delka_v_metrech = zadana_hodnota; | |
| } else if (volba == 2) { | |
| /* palce */ | |
| delka_v_metrech = zadana_hodnota * PALEC_V_METRECH; | |
| } else if (volba == 3) { | |
| /* mile */ | |
| delka_v_metrech = zadana_hodnota * MILE_V_METRECH; | |
| } else if (volba == 4) { | |
| /* svetelne roky */ | |
| delka_v_metrech = zadana_hodnota * SVETELNY_ROK_V_METRECH; | |
| } | |
| /* | |
| prevod metru na ostatni jednotky | |
| - konverze %g vypise double bud jako desetinne cislo, nebo v exponencialnim tvaru | |
| - kdybychom pouzili %f, u svetelnych roku bychom vetsinou videli jen 0.000000 (po zaokrouhleni) | |
| */ | |
| printf("Metry: %g\n", delka_v_metrech); | |
| printf("Centimetry: %g\n", delka_v_metrech * 100); | |
| printf("Palce: %g\n", delka_v_metrech / PALEC_V_METRECH); | |
| printf("Mile: %g\n", delka_v_metrech / MILE_V_METRECH); | |
| printf("Svetelne roky: %g\n", delka_v_metrech / SVETELNY_ROK_V_METRECH); | |
| return 0; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <stdlib.h> | |
| #include <stdio.h> /* funkce scanf(), printf() */ | |
| int main() | |
| { | |
| int cislo; | |
| printf("Zadejte kladne cele cislo: "); | |
| /* pro nacitani typu int slouzi konverze %d */ | |
| /* scanf vraci pocet uspesne nactenych polozek (zde 0 nebo 1) */ | |
| if (scanf("%d", &cislo) != 1) { | |
| printf("Nepodarilo se nacist cislo!\n"); | |
| /* predcasne ukonceni programu */ | |
| return 0; | |
| } else if (cislo < 1) { | |
| printf("Zadane cislo neni kladne!\n"); | |
| return 0; | |
| } | |
| if (cislo % 2 == 0) { | |
| /* zbytek po deleni dvojkou je nula */ | |
| printf("%d je sude.\n", cislo); | |
| } else { | |
| printf("%d je liche.\n", cislo); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment