Difference between revisions of "C типове данни int, char, float, array, string, struct"

From Ilianko
Line 22: Line 22:
 
array, struct
 
array, struct
  
 +
 +
[[Image:square.png|200px|right|frame|Квадрат]]
 +
'''Задача 2.''' Да се създаде програма за намиране на лицето на правоъгълник по дадени координати на точки на два срещуположни върха. Координатите на точките да се въвеждат от клавиатурата, след стартиране на програмата. Първо х и у за първа точка, после x и y за втората точка.
 +
 +
''' ''Упътване:''' Напишете псевдо код на програмата на хартия, после я реализирайте.
 +
 +
== structures ==
 +
'''Разгледайте и пробвайте следните примери'''
 +
 +
=== Едноизмерна точка ===
 +
[[Image:1Dpoint.png|none]]
 +
<code><pre>
 +
/* 1D Point */
 +
#include <stdio.h>
 +
 +
int main()
 +
{
 +
  int x;
 +
  puts("Enter point coordinate \n");
 +
  scanf("%d",&x);
 +
  printf("The point coordinate is %i! \n", x);
 +
  return 0;
 +
}
 +
</pre></code>
 +
 +
=== Едноизмерна отсечка ===
 +
[[Image:1Dline.png|none]]
 +
<code><pre>
 +
/* 1D Line */
 +
 +
#include <stdio.h>
 +
 +
int main()
 +
{
 +
  int x1;
 +
  int x2;
 +
     
 +
  puts("Enter start point of line \n");
 +
  scanf("%d",&x1);
 +
   
 +
  puts("Enter end point of line \n");
 +
  scanf("%d",&x2);
 +
 +
  printf("The line is %i units long! \n", x2 - x1);
 +
  return 0;
 +
}
 +
</pre></code>
 +
=== Едноизмерна отсечка със struct ===
 +
[[Image:1Dline.png|none]]
 +
<code><pre>
 +
/* 1D Line struct */
 +
 +
#include <stdio.h>
 +
 +
struct line
 +
{
 +
  int x1;
 +
  int x2;
 +
};
 +
 +
int main()
 +
{
 +
  struct line ln;
 +
 +
  puts("Enter start point of line \n");
 +
  scanf("%d", &ln.x1);
 +
   
 +
  puts("Enter end point of line \n");
 +
  scanf("%d", &ln.x2);
 +
 +
  printf("The line is %i units long! \n", ln.x2 - ln.x1);
 +
  return 0;
 +
}
 +
</pre></code>
 +
 +
=== Двуизмерна точка със struct ===
 +
[[Image:2Dpoint.png|none]]
 +
<code><pre>
 +
/* 2D point struct*/
 +
 +
#include <stdio.h>
 +
 +
struct point
 +
{
 +
  int x;
 +
  int y;
 +
};
 +
 +
int main()
 +
{
 +
  struct point pt;
 +
   
 +
  puts("Enter the point X coordinate \n");
 +
  scanf("%d", &pt.x);
 +
 
 +
  puts("Enter the point Y coordinate \n");
 +
  scanf("%d", &pt.y);
 +
 +
  printf("The point coordinates are P( %i ; %i )! \n", pt.x,pt.y);
 +
  return 0;
 +
}
 +
</pre></code>
 +
 +
=== Двуизмерна отсечка със struct ===
 +
[[Image:2Dline.png|none]]
 +
<code><pre>
 +
/* 2D Line struct */
 +
 +
#include <stdio.h>
 +
#include <math.h>
 +
 +
struct point
 +
{
 +
  int x;
 +
  int y;
 +
};
 +
 +
struct line
 +
{
 +
  struct point x1;
 +
  struct point x2;
 +
};
 +
 +
int main()
 +
{
 +
 +
  struct line ln;
 +
   
 +
  //fisrt point
 +
  puts("Enter the start point of line X coordinate!\n");
 +
  scanf("%d", &ln.x1.y);
 +
   
 +
  puts("Enter the start point of line Y coordinate! \n");
 +
  scanf("%d", &ln.x1.x);
 +
   
 +
  //second point
 +
  puts("Enter the end point of line X coordinate! \n");
 +
  scanf("%d", &ln.x2.x);
 +
   
 +
  puts("Enter the end point of line Y coordinate! \n");
 +
  scanf("%d", &ln.x2.y);
 +
   
 +
  printf("The line is %f units long! \n", sqrt( pow(ln.x1.x - ln.x2.x, 2)+pow(ln.x1.y - ln.x2.y, 2)));
 +
  return 0;
 +
}
 +
</pre></code>
 +
 +
=== Двуизмерна точка със struct и функция ===
 +
 +
<code><pre>
 +
/* 2D Line struct function */
 +
 +
#include <stdio.h>
 +
#include <math.h>
 +
 +
struct point
 +
{
 +
  int x;
 +
  int y;
 +
};
 +
 +
struct line
 +
{
 +
  struct point x1;
 +
  struct point x2;
 +
};
 +
 +
struct point makepoint()
 +
{
 +
  struct point temp;
 +
  puts("Enter the point of line X coordinate!\n");
 +
  scanf("%d", &temp.x);
 +
  puts("Enter the point of line Y coordinate! \n");
 +
  scanf("%d", &temp.y);
 +
  return temp;
 +
}
 +
   
 +
int main()
 +
{
 +
  struct line ln;
 +
 +
  ln.x1 = makepoint();
 +
  ln.x2 = makepoint();
 +
 +
  printf("The line is %f units long! \n", sqrt( pow(ln.x1.x - ln.x2.x, 2)+pow(ln.x1.y - ln.x2.y, 2)));
 +
  return 0;
 +
}
 +
</pre></code>
 
[[Category:C Справочник]]
 
[[Category:C Справочник]]

Revision as of 08:01, 9 April 2011

Типове данни

int, Integers - цели числа

 int
 short int
 long  int
 unsigned int
 signed int
 size_t <=> unsigned int
 

char, Characters - символи

 char - минимум 8 бита
 wchar_t- Wide character ~ unsigned int (Unicode)

float, Floating-point numbers - числа с плаваща запетая

 float - single precision
 double - double precision
 long double - extended precision

Преобразуване на типове данни CAST

Съставни типове данни

array, struct


Квадрат

Задача 2. Да се създаде програма за намиране на лицето на правоъгълник по дадени координати на точки на два срещуположни върха. Координатите на точките да се въвеждат от клавиатурата, след стартиране на програмата. Първо х и у за първа точка, после x и y за втората точка.

Упътване: Напишете псевдо код на програмата на хартия, после я реализирайте.

structures

Разгледайте и пробвайте следните примери

Едноизмерна точка

1Dpoint.png
/* 1D Point */
#include <stdio.h>

int main()
{
  int x;
  puts("Enter point coordinate \n");
  scanf("%d",&x); 
  printf("The point coordinate is %i! \n", x);
  return 0;
}

Едноизмерна отсечка

1Dline.png
/* 1D Line */

#include <stdio.h>

int main()
{
  int x1;
  int x2;
       
  puts("Enter start point of line \n");
  scanf("%d",&x1); 
    
  puts("Enter end point of line \n");
  scanf("%d",&x2);

  printf("The line is %i units long! \n", x2 - x1);
  return 0;
}

Едноизмерна отсечка със struct

1Dline.png
/* 1D Line struct */

#include <stdio.h>

struct line
{
  int x1;
  int x2;
};

int main()
{
  struct line ln;

  puts("Enter start point of line \n");
  scanf("%d", &ln.x1); 
    
  puts("Enter end point of line \n");
  scanf("%d", &ln.x2);

  printf("The line is %i units long! \n", ln.x2 - ln.x1);
  return 0;
}

Двуизмерна точка със struct

2Dpoint.png
/* 2D point struct*/

#include <stdio.h>

struct point
{
  int x;
  int y;
};

int main()
{
  struct point pt;
    
  puts("Enter the point X coordinate \n");
  scanf("%d", &pt.x); 
  
  puts("Enter the point Y coordinate \n");
  scanf("%d", &pt.y);

  printf("The point coordinates are P( %i ; %i )! \n", pt.x,pt.y);
  return 0;
}

Двуизмерна отсечка със struct

2Dline.png
/* 2D Line struct */

#include <stdio.h>
#include <math.h>

struct point
{
  int x;
  int y;
};

struct line
{
  struct point x1;
  struct point x2;
};

int main()
{
	
  struct line ln;
    
  //fisrt point
  puts("Enter the start point of line X coordinate!\n");
  scanf("%d", &ln.x1.y); 
    
  puts("Enter the start point of line Y coordinate! \n");
  scanf("%d", &ln.x1.x);
    
  //second point
  puts("Enter the end point of line X coordinate! \n");
  scanf("%d", &ln.x2.x); 
    
  puts("Enter the end point of line Y coordinate! \n");
  scanf("%d", &ln.x2.y);
    
  printf("The line is %f units long! \n", sqrt( pow(ln.x1.x - ln.x2.x, 2)+pow(ln.x1.y - ln.x2.y, 2)));
  return 0;
}

Двуизмерна точка със struct и функция

/* 2D Line struct function */

#include <stdio.h>
#include <math.h>

struct point
{
  int x;
  int y;
};

struct line
{
  struct point x1;
  struct point x2;
};

struct point makepoint()
{
  struct point temp;
  puts("Enter the point of line X coordinate!\n");
  scanf("%d", &temp.x); 
  puts("Enter the point of line Y coordinate! \n");
  scanf("%d", &temp.y);
  return temp;
}
    
int main()
{
  struct line ln;

  ln.x1 = makepoint();
  ln.x2 = makepoint();

  printf("The line is %f units long! \n", sqrt( pow(ln.x1.x - ln.x2.x, 2)+pow(ln.x1.y - ln.x2.y, 2)));
  return 0;
}