1 / 6

Příklady v jazyku C – část 5

Příklady v jazyku C – část 5. Nadefinujte znak EOLN pomocí makra: #include &lt;stdio.h&gt; #include &lt;ctype.h&gt; #define EOLN '<br>' int main(void) { int c, cislice = 0; printf(&quot;Zadej nejake znaky:<br>&quot;); while ((c = getchar()) != EOLN) { if (isdigit(c)) cislice++;

liang
Download Presentation

Příklady v jazyku C – část 5

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. Příklady v jazyku C – část 5

  2. Nadefinujte znak EOLN pomocí makra: #include <stdio.h> #include <ctype.h> #define EOLN '\n' int main(void) { int c, cislice = 0; printf("Zadej nejake znaky:\n"); while ((c = getchar()) != EOLN) { if (isdigit(c)) cislice++; if (isalpha(c)) putchar(toupper(c)); } printf("\nNa vstupni radce bylo %d cislic\n", cislice); while(!kbhit()); return 0; }

  3. Nadefinujte makro je_velke( c ) , kter0 rozpozna velke pismeno a makro taky_velke( c ) , ktere vyuzije podprogram isupper() z knihovny ctype.h a vyzkoušete je. #include <stdio.h> #include <ctype.h> #define je_velke(c) (((c) >= 'A' && (c) <= 'Z') ? 1 : 0) #define taky_velke(c) ((isupper(c)) ? 1 : 0) int main(void) {int c; c = 'B'; if (je_velke(c)) printf("Znak '%c' je velke pismeno\n", c); else printf("Znak '%c' neni velke pismeno\n", c); c = 'e'; if (je_velke(c)) printf("Znak '%c' je velke pismeno\n", c); else printf("Znak '%c' neni velke pismeno\n", c); c = 'B'; if (taky_velke(c)) printf("Znak '%c' je velke pismeno\n", c); else printf("Znak '%c' neni velke pismeno\n", c); c = 'e'; if (taky_velke(c)) printf("Znak '%c' je velke pismeno\n", c); else printf("Znak '%c' neni velke pismeno\n", c); while(!kbhit()); return 0; }

  4. Napište makro lze_tisknout(c ) , které otestuje tištitelnost znaku v ASCII-tabulce a nadefinujte konstantu , která omezí rozsah tabulky na 127 znaků: #include <stdio.h> #define HORNI_MEZ 127 #define lze_tisknout(c) (((c) >= ' ' && (c) < HORNI_MEZ) ? 1 : 0) int main(void) { int i; printf("ASCII tabulka :\n"); printf("===============\n\n"); for (i = 0; i < HORNI_MEZ; i++) { if (lze_tisknout(i)) printf("%3d %c ", i, i); else printf("%3d -?- ", i); } putchar('\n'); while(!kbhit()); return 0; }

  5. Nadefinujte makro cti_int(i) ,které umožní číst číslo formátovým vstupem scanf("%d", &i) Využijte operátor čárky: #include <stdio.h> #define cti_int(i) (scanf("%d", &i), i) int main(void) { int j, k; printf("Zadej cele cislo: "); if ((j = cti_int(k)) == 0) printf("Byla nactana nula: j = %d, k = %d\n", j, k); else printf("Nactena cisla: j = %d, k = %d\n", j, k); while(!kbhit()); return 0; }

  6. Nadefinujte konstanty počet a jméno_souborua upravne program pro přesměrování standadního vstupu: #include <stdio.h> #include <ctype.h> #define POCET 10 /* pocet ctenych cisel */ #define JMENO_SOUBORU "CISLA.TXT" int main(void) { FILE *fr; int i, c, cislo, pocet_sudych = 0; printf("Cteni ze Souboru nebo z Klavesnice [S/K]: "); while (1) { c = toupper(getchar()); if (c == 'S' || c == 'K') break; else putchar('\007'); } if (c == 'S') { if ((fr = fopen(JMENO_SOUBORU, "r")) == NULL) { printf("Soubor %s nelze otevrit\n", JMENO_SOUBORU); return; } } else fr = stdin; for (i = 1; i <= POCET; i++) { printf("Zadej %d. cislo: ", i); fscanf(fr, "%d", &cislo); if (cislo % 2 == 0) pocet_sudych++; /* sude cislo */ } printf("Precetlo se %d sudych cisel\n", pocet_sudych); if (fr != stdin) fclose(fr) while(!kbhit());return 0; }

More Related