programowanie_cwiczenia5i6.doc

(38 KB) Pobierz
5

5.1

#include <stdio.h>

#include <conio.h>

int main(void)

{

  char str[80], *p;

  printf("Wprowadz lancuch znakow: ");

  gets(str);

  p = str;

  while(*p && *p!=' ') p++;

  while(*p && *p==' ') p++;

  printf(p);

  getche();

  return 0;

}

 

 

6.1

#include <stdio.h>

#include <string.h>

#include <conio.h>

 

struct moja_struktura {

  short int i;

  char str[80];

} s, *p;

 

int main()

{

  p = &s;

  s.i = 10;  /* funkcjonalnie to jest równoważne */

  p->i = 10; /* temu */

  strcpy(p->str, "Bardzo lubie struktury");

 

  printf("%d %d", s.i, p->i);

  printf("\n%s", p->str);

  printf("\nZmienna typu short int ma wielkosc %d bajtow ", sizeof(short int));

  printf("\nZmienna typu moja_struktura ma wielkosc %d bajtow ",

                                                sizeof(struct moja_struktura));

  getche();

  return 0;

}

 

 

6.2

#include <stdio.h>

#include <conio.h>

 

int zaszyfruj(int i);

 

int main()

{

  short int i;

 

  i = zaszyfruj(10); /* zaszyfrowanie */

  printf("10 po zaszyfrowaniu to %d\n", i);

  i = zaszyfruj(i); /* odszyfrowanie */

  printf("2560 rozszyfrowane to %d", i);

 

  getche();

  return 0;

}

 

/* Szyfrowanie liczby całkowitej - odszyfrowanie liczby */

int zaszyfruj(int i)

{

  union crypt_type {

    int num;

    char c[2];

  } crypt;

  unsigned char ch;

 

  crypt.num = i;

 

  /* swap bytes */

  ch = crypt.c[0];

  crypt.c[0] = crypt.c[1];

  crypt.c[1] = ch;

 

  /* return encoded integer */

  return crypt.num;

}

 

6.3

/* TTen program wyświetla binarną reprezentację

znaku wprowadzanego z klawiatury

*/

#include <stdio.h>

#include <conio.h>

 

struct sample {

  unsigned a: 1;

  unsigned b: 1;

  unsigned c: 1;

  unsigned d: 1;

  unsigned e: 1;

  unsigned f: 1;

  unsigned g: 1;

  unsigned h: 1;

};

 

union key_type {

  char ch;

  struct sample bits;

} key;

 

int main(void)

{

  printf("Wprowadz znak: ");

 

  key.ch = getche();

  printf("\nKod binarny znaku to: ", key.ch);

 

  if(key.bits.h) printf("1 ");

  else printf("0 ");

  if(key.bits.g) printf("1 ");

  else printf("0 ");

  if(key.bits.f) printf("1 ");

  else printf("0 ");

  if(key.bits.e) printf("1 ");

  else printf("0 ");

  if(key.bits.d) printf("1 ");

  else printf("0 ");

  if(key.bits.c) printf("1 ");

  else printf("0 ");

  if(key.bits.b) printf("1 ");

  else printf("0 ");

  if(key.bits.a) printf("1 ");

  else printf("0 ");

 

  getche();

  return 0;

}

 

...
Zgłoś jeśli naruszono regulamin