C Exercises

download C Exercises

of 58

description

ΈτοιμεςΑσκήσεις σεC

Transcript of C Exercises

  • /

    C

    , 1997

  • / C

    2

    /* c01.c - c */ #include main() { int i; clrscr(); printf("\n c"); i = 156; printf("\n : %d", i); scanf("%d", &i); /* */ } /* end of main */ /* c02.c - , */ #include main() { int age; long misthos; char onoma[30]; clrscr(); printf("\n ; "); scanf("%s", onoma); /* & */ printf("\n ; "); scanf("%ld", &misthos); printf("\n ; "); scanf("%d", &age); printf("\n"); printf("\n . %s, %d %ld\

    ", onoma, age, misthos); scanf("%d", &age); } /* end of main */ /* c03.c - sizeof */ #include main() { char i; clrscr(); printf("\n = %d bytes", sizeof(int)); printf("\n = %d bytes", sizeof(long)); printf("\n = %d bytes", sizeof(double)); printf("\n = %d bytes", \

    sizeof(long double)); scanf("%c", &i); } /* end of main */

  • / C

    3

    /* c04.c - */ #include main() {

    int i; char ch;

    clrscr(); printf("\n : "); scanf("%d", &i); printf("\n : "); scanf("%c", &ch); printf("\n : %d : %c ", i, i); scanf("%d", &i); } /* end of main */ /* c05.c - , , (string) */ #include main() { int i; long li; char onoma[20]; float s; clrscr(); printf("\n : "); scanf("%d", &i); printf("\n : "); scanf("%ld", &li); printf("\n : "); scanf("%s", onoma); printf("\n : "); scanf("%f", &s); printf("/%d/\n", i); printf("/%2d/\n", i); printf("/%10d/\n", i); printf("/%-10d/\n", i); printf("/%2s/\n", onoma); printf("/%20s/\n", onoma); printf("/%20.5s/\n", onoma); printf("/%-23.5s/\n", onoma); printf("/%+10.2f/\n", s); printf("/%15.4f/\n", s); scanf("%d", &i); } /* end of main */

  • / C

    4

    /* c06.c - : \, %, ", beep */ #include main() { clrscr(); printf("\n\t\t \\"); printf("\n \% %%"); printf("\n \" "); printf("\n \a"); } /* end of main */ /* c07.c - - #define */ #include #define PI 3.1415926 /* */ main() { int radius = 10; long area, circum; clrscr(); circum = 2 * PI * radius; /* */ area = PI * radius * radius; /* */ printf("\n = %ld = %ld", circum, area); } /* end of main */ /* c08.c - */ #include main() { int a, b, c, max; clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%d", &b); printf("\n : "); scanf("%d", &c); max = a; if (b > max) max = b; if (c > max) max = c; printf("\n : %d ", max); scanf("%d", &a); } /* end of main */

  • / C

    5

    /* c09.c - - switch */ #include main() { int a, ypol; clrscr(); printf("\n : "); scanf("%d", &a); ypol = a % 3; /* 3 */ switch (ypol) { case 0 : printf("\n "); break; case 1 : printf("\n "); break; case 2 : printf("\n "); break; default : printf("\n "); } /* switch */ scanf("%d", &a); } /* end of main */ /* c10.c - */ #include main() { int a, ypol; clrscr(); printf("\n : "); scanf("%d", &a); ypol = a % 2; if (ypol == 1) printf("\n "); else printf("\n "); scanf("%d", &a); } /* end of main */

  • / C

    6

    /* c11.c - :

    5% 10% 20% + 5% */

    #include main() { long misthos, epidoma; int child; clrscr(); printf("\n : "); scanf("%ld", &misthos); printf("\n : "); scanf("%d", &child); switch (child) { case 0 : epidoma = 0; break; case 1 : epidoma = misthos * 0.05; break; case 2 : epidoma = misthos * 0.10; break; case 3 : epidoma = misthos * 0.20; break; default : epidoma = misthos * 0.05 * (child-3) + misthos * 0.20; } /* end of switch */ misthos = misthos + epidoma; printf("\n : %ld", epidoma); printf("\n : %ld", misthos); scanf("%d", &child); } /* end of main */

  • / C

    7

    /* c12.c - */ #include main() { int bathmos; clrscr();

    printf("\n : "); scanf("%d", &bathmos); if (bathmos >= 0 && bathmos

  • / C

    8

    /* c13.c - ? : */ #include main() { int koutia; char cans[6]; clrscr(); printf("\n : "); scanf("%d", &koutia); strcpy(cans, (koutia==1 ? "" : "")); printf("\n %d %s", koutia, cans); scanf("%d", &koutia); } /* end of main */ /* c14.c - while - 10 0 20, */ #include main() { int bathmos, i, sum; clrscr(); i = 1; while (i 20) printf("\n "); else {

    i++; sum += bathmos; /* */ } /* end of else */

    } /* end of while */ printf("\n = %.1f", sum/10.0); scanf("%d", &bathmos); } /* end of main */

  • / C

    9

    /* c15.c - , - - */ #include main() { int a, b, ypol, pil; float decad; clrscr(); prtinf("\n : "); scanf("%d", &a); prtinf("\n : "); scanf("%d", &b); pil = a / b; /* */ ypol = a % b; /* */ decad = (float) a / (float) b; /* */ printf("\n : %d", pil); printf("\n : %d", ypol); printf("\n : %10.2f", decad); scanf("%d", &a); } /* end of main */ /* c16.c - */ #include main() { float a, b, sum_f; int sum1, sum2; clrscr(); prtinf("\n : "); scanf("%f", &a); prtinf("\n : "); scanf("%f", &b); sum1 = (int) a + (int) b; sum2 = (int) (a + b); sum_f = a + b; printf("\n : %d", sum1); printf("\n : %d", sum2); printf("\n : %10.2f", sum_f); scanf("%f", &a); } /* end of main */

  • / C

    10

    /* c17.c - do while - */ #include main() { char ch = A; /* A */ clrscr(); printf("\n : \n"); do { printf("%c", ch); ch = ch + 1; } while (ch

  • / C

    11

    /* c19.c - getchar() putchar() */ #include main() { char ch; clrscr(); do { printf("\n : "); ch = getchar(); /* */ putchar(ch); /* */ } while (ch != #); /* # */ scanf("%c", %ch); } /* end of main */ /* c20.c - for */ #include main() { int a[10], i; long sum=0l; clrscr(); for (i=0; i

  • / C

    12

    /* c22.c - 10 - */ #include main() { int a[10], i, max, min, i_max, i_min; clrscr(); /* */ for (i=0; i 20); max = a[0]; min = a[0]; i_max = 0; i_min = 0; for (i=1; i max) { max = a[i]; i_max = i; } if (a[i] < min) { min = a[i]; i_min = i; } } /* end of for */ print("\n %d : %d", \

    i_max, max); print("\n %d : %d", \

    i_min, min); scanf("%d", &i); } /* end of main */

  • / C

    13

    /* c23.c - break continue */ #include main() { char ch; clrscr(); while (1) { printf("\n : "); ch = getchar(); putchar(); if (ch == #) break; /* */ if (ch < A || ch > Z) continue; /* */ else printf(" "); } /* end of while */ scanf("%c", &ch); } /* end of main */ /* c24.c - ++ */ #include main() { int i; clrscr(); i = 0; while (i++ == 0) { printf("\n ++"); printf("\ni=%d", ++i); } printf("\ni=%d", i); scanf("%d", &i); } /* end of main */

  • / C

    14

    /* c25.c - - - */ #include main() { char ch; clrscr(); ch = a while (ch- - == a) { printf("\n - -"); printf("\nch=%c", - -ch); } printf("\nch=%c", ch); scanf("%c", &ch); } /* end of main */ /* c26.c - - 20 - - - 0 100 */ #include main() { int pol[20], i; clrscr(); for (i=0; i 100); } for (i=1; i pol[i+1] && pol[i] > pol[i-1]) printf("\n %d ", i); scanf("%d", &i); } /* end of main */

  • / C

    15

    /* c27.c - 20 , - 0-20 a, b c - , */ #include main() { int bathmos, i; char taxi; int ia, ib, ic; /* */ int a[20], b[20], c[20]; /* */ ia=ib=ic=0; /* */

    clrscr(); for (i=0; i 20); do { printf("\n %d : ", i); scanf("%c", &taxi); } while (taxi != a && taxi!=b && taxi!=c); switch (taxi) { case a : a[ia] = bathmos; ia++; break; case b : b[ib] = bathmos; ib++; break; case c : c[ic] = bathmos; ic++; break; default : printf("\n "); } /* end of switch */ } /* end of for */ scanf("%d", &i); } /* end of main */

  • / C

    16

    /* c28.c - 10 , 0-100 1 () 2 () - , */ #include main() { int age, fylo, i; int i_man, i_woman; /* */ int sum_man, sum_woman; /* */ int man[10], woman[10]; /* */ float mo_man, mo_woman; /* */ i_man=i_woman=sum_man=sum_woman=0;

    clrscr(); for (i=0; i 100); switch (fylo) { case 1 : man[i_man] = age; i_man++; sum_man += age; break; case 2 : woman[i_woman] = age; i_woman++; sum_woman += age; break; default : printf("\n "); } /* end of switch */ } /* end of for */ mo_man = (float) sum_man / (float) i_man; mo_woman = (float) sum_woman / (float) i_woman; printf("\n%d %.1f ", i_man, mo_man); printf("\n%d %.1f", i_woman, mo_woman); scanf("%d", &i); } /* end of main */

  • / C

    17

    /* c29.c - (1=, 2=, 3=), 20 , - 17-40 140-220 - , */ #include main() { int age, oplo, ypsos, i; int i_str, i_naut, i_aer; /* */ int sum_str_age, sum_str_ypsos; /* */

    int sum_naut_age, sum_naut_ypsos; /* */ int sum_aer_age, sum_aer_ypsos;

    int str_age[20], str_ypsos[20]; /* */ int naut_age[20], naut_ypsos[20]; int aer_age[20], aer_ypsos[20]; float mo_str_age, mo_str_ypsos; /* */ float mo_naut_age, mo_naut_ypsos; float mo_aer_age, mo_aer_ypsos; i_str=i_naut=i_aer=sum_str_age=sum_str_ypsos=0; sum_naut_age=sum_naut_ypsos=sum_aer_age=sum_aer_naut=0;

    clrscr();

    for (i=0; i 40); do { printf("\n %d : ", i); scanf("%d", &ypsos); } while (ypsos < 140 || ypsos > 220);

  • / C

    18

    switch (oplo) { case 1 : str_age[i_str] = age; str_ypsos[i_str] = ypsos; i_str++; sum_str_age += age; sum_str_ypsos += ypsos; break; case 2 : naut_age[i_naut] = age; naut_ypsos[i_naut] = ypsos; i_naut++; sum_naut_age += age; sum_naut_ypsos += ypsos; break; case 3 : aer_age[i_aer] = age; aer_ypsos[i_aer] = ypsos; i_aer++; sum_aer_age += age; sum_aer_ypsos += ypsos; break; default : printf("\n "); } /* end of switch */ } /* end of for */ mo_str_age = (float) sum_str_age / (float) i_str; mo_str_ypsos = (float) sum_str_ypsos / (float) i_str; mo_naut_age = (float) sum_naut_age / (float) i_naut; mo_naut_ypsos = (float) sum_naut_ypsos / (float) i_naut; mo_aer_age = (float) sum_aer_age / (float) i_aer; mo_aer_ypsos = (float) sum_aer_ypsos / (float) i_aer; printf("\n%d %.1f %.1f",\

    i_str, mo_str_age, mo_str_ypsos); printf("\n%d %.1f %.1f",\

    i_naut, mo_naut_age, mo_naut_ypsos); printf("\n%d %.1f \

    %.1f", i_aer, mo_aer_age, mo_aer_ypsos); scanf("%d", &i); } /* end of main */

  • / C

    19

    /* c30.c - 10 Bubble-Sort - , 1 flag - flag 0 1 - 0 - - */ #include main() { int i, flag, temp, a[10]; clrscr(); /* */ for (i=0; i

  • / C

    20

    /* c31.c - - . 30 - - , l1 l2, - - , , l2 l1 - l1>l2, - */ #include main() { int i, l1, l2, a[30]; int value; /* */ int found; /* 1 */ int middle; /* */ clrscr(); /* */ for (i=0; i

  • / C

    21

    /* c32.c - 10 - - */ #include main() { int i, a[10], max, min; clrscr(); /* */ for (i=0; i

  • / C

    22

    /* c33.c - (struct) - - : , , 5 - - */ #include struct ypalilos { char eponymo[15]; char onoma[10]; int age; long misthos; }; /* end of struct */ /* main() - - , - C */ main() { struct ypalilos ypal;

    /* ypal ypalilos, . (struct) */

    int i, max_age, max_misthos; char max_age_eponymo[15];

    /* */ char max_age_onoma[10]; /* */

    char max_misthos_eponymo[15]; /* */ char max_misthos_onoma[10]; /* */

    clrscr(); max_age=0; max_misthos=0; for (i=0; i

  • / C

    23

    if (ypal.age > max_age) { max_age = ypal.age; strcpy(max_age_eponymo, ypal.eponymo); strcpy(max_age_onoma, ypal.onoma); } /* strcpy() , , - - */ if (ypal.misthos > max_misthos) { max_misthos = ypal.misthos; strcpy(max_misthos_eponymo, ypal.eponymo); strcpy(max_misthos_onoma, ypal.onoma); } } /* end of for */ printf("\n %s %s : %d", \

    max_age_eponymo, max_age_onoma, max_age); printf("\n %s %s : %d", \

    max_misthos_eponymo, max_misthos_onoma, max_misthos); scanf("%d", &i); } /* end of main */

  • / C

    24

    /* c34.c - (struct) - : , , , , 10 - - - 100000 */ #include struct pelates { int code;

    char eponymo[15]; char onoma[10]; char address[20]; char poli[10]; long ypoloipo; }; /* end of struct */ main() { int i; long sum=0l; struct pelates pel[10]; clrscr(); for (i=0; i 100000) printf("\n %s %s %ld",\

    pel[i].eponymo, pel[i].onoma, pel[i].ypoloipo); } /* end of for */ printf("\n : %ld", sum); scanf("%d", &i); } /* end of main */

  • / C

    25

    /* c35.c - (struct) - : , , , , , 10 - 20% - 5% - - */ #include struct misthotos { char eponymia[25]; int imerom; int meres; long mikta; long kratisis; long foros; long kathara; }; /* end of struct */ main() { int i; long sum=0l; struct misthotos mist[10]; clrscr(); for (i=0; i

  • / C

    26

    /* c36.c - 10 */ #include main() { int i, max, max2, a[10]; clrscr(); for (i=0; i

  • / C

    27

    /* c38.c - - - - - - */ #include main() { int i, max_fortio, fortio;

    int paketa; /* */ int synolo=0; /* */ int flight=0; /* */ clrscr(); printf("\n : "); scanf("%d", &paketa); printf("\n : "); scanf("%d", &max_fortio); for (i=0; i max_fortio) { flight++; synolo -= fortio; printf("\n %d %d : ",\

    flight, synolo); synolo = fortio; } if (synolo == max_fortio) { flight++; printf("\n %d %d : ",\

    flight, synolo); synolo = 0; } } /* end of for */ if (synolo > 0) { flight++; printf("\n %d %d : ", flight, synolo); } } /* end of main */ /* , */

  • / C

    28

    /* c39.c - - */ #include main() { int a, b, c; int athroisma; int sum();

    /* - - , , . */ clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%d", &b); printf("\n : "); scanf("%d", &c); athroisma = sum(a, b, c); /* */ printf("\n : ", athroisma); scanf("%d", &a); } /* end of main */ /* */ int sum(a1, b1, c1) int a1, b1, c1; /* a1, b1 c1 , */ { int sum1; /* */ sum1=a1+b1+c1; return sum1;

    /* return */ } /* end of sum() */

  • / C

    29

    /* c40.c - - */ #include main() { int a, b, c; int max; int maximum();

    /* - - , , . */ clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%d", &b); printf("\n : "); scanf("%d", &c); max = maximum(a, b, c); /* */ printf("\n : ", max); scanf("%d", &a); } /* end of main */ /* */ int maximum(a1, b1, c1) int a1, b1, c1; /* a1, b1 c1 , */ { int max1; /* */ max1=a1; if (b1>max1) max=b1; if (c1>max1) max=c1; return max1;

    /* return */ } /* end of sum() */

  • / C

    30

    /* c41.c - void - void */ #include main() { int a; char ch;

    void ektyp(); /* void

    , void */ clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%c", &ch); ektyp(a, ch); /* void */ scanf("%d", &a); } /* end of main */ void ektyp(a1, ch1) int a1; char ch1; { int i; for (i=1; i

  • / C

    31

    /* c42.c - 20 , 1 *, 2 *, 3 * 20 * - void * - - void */ #include main() { int i;

    void ektyp(); /* void

    , void */ clrscr(); for (i=1; i

  • / C

    32

    /* c43.c - void - 10 - 1 1 *, 2 3 *, 3 5 * 19 * */

    * ***

    ***** *******

    ********* ***********

    ************* ***************

    ***************** *******************

    #include main() { int i;

    void tree(); clrscr(); a=9; /* */ b=1; /* * */ for (i=1; i

  • / C

    33

    /* c44.c - , , , , - */ #include main() { int a, b; int *pa, *pb; /* */ clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%d", &b); pa = &a; /* &a a */ pb = &b; printf("\n a b : %d %d", a, b); printf("\n a b : %p %p", &a, &b); printf("\n a b : %p %p", pa, pb); printf("\n : %p %p", &pa, &pb); printf("\n : %d %d",*pa,*pb); scanf("%d", &a); } /* end of main */

  • / C

    34

    /* c45.c - integer, long, float, double char - - - - ; */ #include main() { int i, *pi; long li, *pli; float fi, *pfi; double di, *pdi; char ch, *pch; clrscr(); printf("\n : "); scanf("%d", &i); printf("\n : "); scanf("%ld", &li); printf("\n : "); scanf("%f", &fi); printf("\n : "); scanf("%lf", &di); printf("\n : "); scanf("%c", &ch); pi = &i; pli = &li; pfi = &fi; pdi = &di; pch = &ch; printf("\n : %p %p %p %p %p", \

    pi, pli, pfi, pdi, pch); pi++; pli++; pfi++; pdi++; pch++; printf("\n : %p %p %p %p %p", \

    pi, pli, pfi, pdi, pch); scanf("%d", &a); } /* end of main */ /* , pch , pi 2, pdi 8 pli pfi 4 - bytes */

  • / C

    35

    /* c46.c - void - C */ #include main() { int a, b; void change(); clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%d", &b); change(&a, &b); /* */ printf("\n a b : %d %d", a, b); scanf("%d", &a); } /* end of main */ void change() int *a1, *b1; /* a1 b1 */ { int temp; temp = *a1; /* *a1 *b1 */ *a1 = *b1; /* . a b */ *b1 = temp; /* a b - change(), a1 b1 */ } /* end of change() */

  • / C

    36

    /* c47.c - void - - C */ #include main() { int a, b; void abplus(); clrscr(); printf("\n : "); scanf("%d", &a); printf("\n : "); scanf("%d", &b); abplus(&a, &b); printf("\n a b : %d %d", a, b); scanf("%d", &a); } /* end of main */ void abplus() int *a1, *b1; { int sum, diff; sum = *a1 + *b1; diff = *a1 - *b1; *a1 = sum; *b1 = diff; } /* end of abplus() */

  • / C

    37

    /* c48.c - - 6 8 , . 48 , - */ #include main() { int a[6][8]; /* 6 8 */ int max, athlete, trial, i, j; clrscr(); max=0; /* */ athlete=0; /* */ trial=0; /* */ for (i=0; i

  • / C

    38

    /* c49.c - 10 , - - - */ #include main() { int a[10]; /* 1 , . a == &a[0] a+i == &a[i] */ int max, i_max, i; clrscr(); for (i=0; i

  • / C

    39

    /* c50.c - 10 , - void - - - */ #include main() { int a[10]; /* 1 , . a == &a[0] a+i == &a[i] */ int i; void maximum(); clrscr(); for (i=0; i

  • / C

    40

    /* c51.c - a, b, c, 10 -, void - a b void */ #include main() { int a[10], b[10], c[10], i; void store_pinakas(); void add_pinakas(); clrscr(); store_pinakas(a); store_pinakas(b); add_pinakas(a, b, c);

    printf("\n 1 : "); for (i=0; i

  • / C

    41

    /* c52.c - - C : strlen(), strcat(), strcpy() strcmp() - - */ #include main() { char name1[20], name2[20], name3[40]; int mikos; clrscr();

    printf("\n : "); scanf("%s", name1);

    printf("\n : "); scanf("%s", name2);

    printf("\n : "); scanf("%s", name3); /* strlen() string */ mikos = strlen(name1);

    printf("\n %d : ", mikos); mikos = strlen(name2);

    printf("\n %d : ", mikos); mikos = strlen(name3);

    printf("\n %d : ", mikos); /* strcat() strings string */ strcat(name3, name1);

    printf("\n \ %s : ", name3);

    /* strcpy() string */ strcpy(name3, name2);

    printf("\n %s %s : ", \ name3, name2);

    /* strcmp() strings , 0 */ if (strcmp(name1, name2) == 0) printf("\n "); else printf("\n "); scanf("%d", &mikos); } /* end of main */

  • / C

    42

    /* c53.c - a, b, c, 10, 10 20 - void a b, void a b void a b c - (sorting by merging) */ #include main() { int a[10], b[10], c[20], i; void store_pinakas(); /* */ void sort_pinakas(); /* */ void merge_pinakas(); /* */ clrscr(); store_pinakas(a); store_pinakas(b); sort_pinakas(a); sort_pinakas(b); merge_pinakas(a, b, c);

    printf("\n 1 : "); for (i=0; i

  • / C

    43

    void sort_pinakas(a1); int *a1; { int i, flag, temp; do { flag = 0;

    for (i=0; i *(a1+i+1))

    { temp = *(a1+i); *(a1+i) = *(a1+i+1); *(a1+i+1)=temp; flag = 1;

    } /* end of if */ } /* end of for */ } while (flag==1); } /* end of sort_pinakas() */ /* Bubble-Sort */ void merge_pinakas(a1, b1, c1); int *a1, *b1, *c1; { int i, ia=0, ib=0, ic=0; /* ia, ib ic */ /* */

    while (ia

  • / C

    44

    /* while, - c, a b*/ if (ia

  • / C

    45

    /* c54.c - i a b test() void - - 0 a b - test() a b - a b */ #include int i; main() { extern int i; /* i */ void test(); clrscr(); for(i=0; i

  • / C

    46

    /* c55.c - a 8 16 */ #include main() {

    int a; clrscr(); printf("\n "); scanf("%d", &a); printf("\n 8 : %o", a); printf("\n 16 : %x", a); printf("\n 16 : %X", a); scanf("%d", &a); } /* end of main */ /* % 8 , %x - 16 - (a f) , - % 16 - (A F) - */ /* c56.c - a b, - 8 c AND (&) OR (|) a b */ #include main() {

    int a, b, c; clrscr(); printf("\n a"); scanf("%d", &a); printf("\n b"); scanf("%d", &b); printf("\n 8 a : %o", a); printf("\n 8 b : %o", b); c = a & b; printf("\n 8 a AND b : %o", c); c = a | b; printf("\n 8 a OR b : %o", c); scanf("%d", &a); } /* end of main */ /* 8 - 2 - - , . 1 0 0 1 - - & a b 1 1, | a b 0 0 */

  • / C

    47

    /* c57.c - 8 2 (0 1) */ #include main() {

    int a[i], i , sum; clrscr(); for(i=0; i

  • / C

    48

    /* c59.c - */ #include main() { FILE *fp; /* */ char ch; int i; clrscr(); fp=fopen("C:\IEK.BAT", "r"); /* */ while(!feof(fp)) /* */ { ch=fgetc(fp); /* */ printf("%c", ch); } /* end of while */ fclose(fp); /* */ scanf("%d", &i); } /* end of main */ /* c60.c - , - - * */ #include main() { FILE *fp; char word[20]; /* */ int i; clrscr(); fp=fopen("C:\IEK.DAT", "w"); /* */ do { printf(" : "); scanf("%s", word); fputs(word, fp); /* */ fputs("\n", fp); /* */ } while (strcmp(word, "*")); fclose(fp); /* */ pfb=fopen("C:\IEK.DAT", "r"); /* */ while(!feof(fp)) /* */ { fgets(word, 20, fp); /* */ printf("%s", word); } /* end of while */ fclose(fp); /* */ scanf("%d", &i); } /* end of main */

  • / C

    49

    /* c61.c - , , 5 pelates.dat */ #include #include main() { FILE *fp; char eponymo[15], onoma[10], address[15], ypolipo[10]; int i; clrscr(); pfb=fopen("C:\PELATES.DAT", "w"); for (i=0; i

  • / C

    50

    /* c62.c - : , , , stock - 10 - - - - */ #include struct apothiki { int kodikos; char perigrafi[20]; int timi; int posotita; int stock; }; /* end struct */ main() {

    int i; long sum=0l; /* */ struct apothiki apoth[10]; /* */ struct apothiki *p_apoth; /* */

    clrscr(); for(i=0; ikodikos); printf("\n : "); scanf("%s", p_apoth->perigrafi); printf("\n : "); scanf("%d", &p_apoth->timi); printf("\n : "); scanf("%d", &p_apoth->posotita); printf("\n stock : "); scanf("%d", &p_apoth->stock); if ((p_apoth->posotita) < (p_apoth->stock)) printf("\n : %s", p_apoth->perigrafi); sum += (long)(p_apoth->timi)*(long)(p_apoth->posotita);

    } /* end of for */ printf("\n : %ld", sum);

    scanf("%d", &i); } /* end of main */ /* , - : p_apoth->kodikos : (*p_apoth).kodikos, scanf(), : scanf("%d", &p_apoth->kodikos); scanf("%d", &(*p_apoth).kodikos); */

  • / C

    51

    /* c63.c - 6 - - - : ) -, ) , ) - ) , */ #include main() {

    int a[i][j]; /* */ int i, j, max, i_max, j_max; int sum=0, sum_ypok, sum_efim;

    clrscr(); /* nested-for - i j */ for(i=0; i

  • / C

    52

    /* - (max), (j_max) - (i_max) */ max = a[0][0]; i_max = 0; j_max = 0; for(i=0; i

  • / C

    53

    /* c64.c - htoi() - 16 */ long htoi(s) char *s; /* s string */ { int length, timh, i=1; long sum = 0L;

    length = strlen(s); /* s */ while (*s != '\0') /* '\0' */ { switch (*s) /* */ { case '0' : timh = 0; break; case '1' : timh = 1; break; case '2' : timh = 2; break; case '3' : timh = 3; break; case '4' : timh = 4; break; case '5' : timh = 5; break; case '6' : timh = 6; break; case '7' : timh = 7; break; case '8' : timh = 8; break; case '9' : timh = 9; break; case 'a' : case 'A' : timh = 10; break; case 'b' : case 'B' : timh = 11; break; case 'c' : case 'C' : timh = 12; break; case 'd' : case 'D' : timh = 13; break; case 'e' : case 'E' : timh = 14; break; case 'f' : case 'F' : timh = 15; break; } /* end of switch */ sum = sum + power(16, length-i)*timh; i++; *s++; /* */ } /* end of while */ return sum;

    /* sum 16 string - */ } /* end of htoi() */

  • / C

    54

    /* 16 : - 16. 16 0 - 16 1 . 16 0 - 9 0 - 9 : a, A : 10 b, B : 11 c, C : 12 d, D : 13 e, E : 14 f, F : 15 ..: 6F0d = 6 X 16

    3 + 15 X 16

    2 + 0 X 16

    1 + 13 X 16

    0 */ /* htoi() long - string 16 . - power(16, length-i), 16 length-i, i 1 1 0. - timh, , sum, return */ /* c65.c - reverse() - */ void reverse(s) char *s; { int length;

    char *t; length = strlen(s); for (i=0; i

  • / C

    55

    /* c67.c - strcmp() 0 , -1 1 */ int strcmp(s, t) char *s, *t; { int timh, i=0; while (*s++ == *t++) /* s t */ i++; /* i */ if (i==strlen(s) && i==strlen(t)) /* strings */ timh = 0; else { /* *(s+i) *(t+i) strings strings */ if (*(s+i) < *(t+i)) /* s t */ timh = -1; if (*(s+i) > *(t+i)) /* s t */ timh = 1; } /* end of if-else */ return timh; /* timh */ } /* end of strcmp() */ /* : i - strings - - , while if i - strings - , strings timh = 0 - , - strings timh = 1 timh = -1 */ : *s = ""; *s = ""; *s = ""; *t = ""; *t = ""; *t = ""; i = 5 timh = -1 i = 0 timh = 1 i = 7 timh = 0

  • / C

    56

    /* c68.c - - (.. Jan, Feb, ) , - 4, - 100 400 - . 1996 , 1900 2000 */ int hmeres_mhna(mhnas, etos) char *mhnas; int etos; { int hmeres, ypol4, ypol100, ypol400, disekto; ypol4 = etos % 4; /* 4 */ ypol100 = etos % 100; /* 100 */ ypol400 = etos % 400; /* 400 */ if (ypol4 != 0) disekto = 0; /* */ if (ypol4 == 0 && ypol100 != 0) disekto = 1; /* */ if (ypol100 == 0 && ypol400 != 0) disekto = 0; /* */ if (ypol400 == 0) disekto = 1; /* */ if (strcmp(mhnas, Jan) == 0)

    hmeres = 31; if (strcmp(mhnas, 'Feb') == 0) { if (disekto)

    hmeres = 29; else

    hmeres=28; } /* end of if */ if (strcmp(mhnas, 'Mar') == 0)

    hmeres = 31; if (strcmp(mhnas, 'Apr') == 0)

    hmeres = 30; if (strcmp(mhnas, 'May') == 0)

    hmeres = 31; if (strcmp(mhnas, 'Jun') == 0)

    hmeres = 30; if (strcmp(mhnas, 'Jul') == 0)

    hmeres = 31; if (strcmp(mhnas, 'Aug') == 0)

    hmeres = 31; if (strcmp(mhnas, 'Sep') == 0)

    hmeres = 30;

  • / C

    57

    if (strcmp(mhnas, 'Oct') == 0) hmeres = 31;

    if (strcmp(mhnas, 'Nov') == 0) hmeres = 30;

    if (strcmp(mhnas, 'Dec') == 0) hmeres = 31;

    return hmeres; /* */ } /* end of hmeres_mhna() */

  • / C

    58

    /* c69.c - :

    1.355.000 0 0 1.355.000 0 1.282.500 5 64.125 2.637.500 64.125 1.582.500 15 237.375 4.220.000 301.500 3.165.000 30 949.500 7.385.000 1.251.000 8.440.000 40 3.376.000 15.825.000 4.627.000

    45 1.355.000 , 1.282.500 5%, . 64.125 - 2.800.000, 1.355.000, 1.282.500 5%, . 64.125 2.800.000 - 1.282.500 - 1.355.000 = 162.500 15%, . 24.375 - : 64.125 + 24.375 = 88.500 . - */ main() {

    long foros, eisodhma;

    printf("\n : "); scanf("%ld", &eisodhma); if (eisodhma < 1355000)

    foros = 0; if (eisodhma >= 1355000 && eisodhma < 2637500)

    foros = int((eisodhma-1355000)*0.05+0.5); if (eisodhma >= 2637500 && eisodhma < 4220000)

    foros = int((eisodhma-2637500)*0.15+64125+0.5); if (eisodhma >= 4220000 && eisodhma < 7385000)

    foros = int((eisodhma-4220000)*0.30+301500+0.5); if (eisodhma >= 7385000 && eisodhma < 15825000)

    foros = int((eisodhma-7385000)*0.40+1251000+0.5); if (eisodhma >= 15825000)

    foros = int((eisodhma-15825000)*0.45+4627000+0.5);

    printf("\n : %ld", foros); scanf("%ld", &eisodhma);

    } /* end of main */