.


:




:

































 

 

 

 


Define




. -. , , .

:

  • ;
  • , ;
  • , .

, , . . , .

C++ . C++ , , . . , , , , , , .

C++

() , . :

  1. int ();
  2. char ();
  3. wchar_t ( );
  4. bool ();
  5. float ();
  6. double ( ).

(), . , , .

, :

  • short ();
  • long ();
  • signed ();
  • unsigned ().

(int)

int , . 16- 2 , 32- 4 .

short , 2 . long , 4 . , 16- int short int, 32- int long int.

. signed (0 , 1 ). unsigned , . , int . IBM PC- .

, signed .

, , . - , L, l (long) U, u (unsigned). , 32L long 4 . L U , , 0x22UL 05Lu.

short int, long int, signed int unsigned int short, long, signed unsigned .

(char)

, , . , 1 . char, , . -128 127. unsigned 255. 256- ASCII. char , .

(wchar_t)

wchar_t , 1 , , Unicode. ; , short. wchar_t L, , LGates.

(bool)

true false, . false 0 (). true. true 1.

(float, double long double)

C++ : float, double long double.

, . . IBM PC- float 4 , , 8 23 . , 1.0, 2.0. 1, .

double, 8 , 11 52 . , . , , float long int, - .

long double , 10 .

double. F, f (float) L, l (long). , 2E+6L long double, 1.82f float.

int. sizeof, . , MS-DOS sizeof (int) 2, Windows 98 OS/2 4.

ANSI , , :

sizeof(float) ≤ slzeof(double) ≤ sizeof(long double)
sizeof(char) ≤ slzeof(short) ≤ sizeof(int) ≤ sizeof(long)

<limits.h> (<climits>), <float.h> (<cfloat>), numeric_limits

void

, void, . , , , .

C++ IBM PC-

Q: IBM PC- ?
A: IBM PC- (. IBM PC compatible) , IBM PC, XT AT. IBM PC- , Intel 8086 (, , Intel 8086). .

, , , , . - , .

()
bool true false  
signed char -128 127  
unsigned char 0 255  
signed short int -32 768 32 767  
unsigned short int 0 65 535  
signed long int -2 147 483 648 2 147 483 647  
unsigned long int 0 4 294 967 295  
float 3.4e-38 3.4e+38  
double 1.7e-308 1.7C+308  
long double 3.4e-4932 3.4e+4932  

.

2: C. . .

1) C

, .

, . . , , , - , . , , , " " , . , .

:

;

;

;

;

.

( ), .

, . . , .

.

, (, ), .

, , .

, , .

, , , ( ) .

, . , . .

:

;

, ;

.

, , :

, , ;

.

. , , . , , . , , , (, , ..).

:

1. ( );

2. ;

3. ( ).

 

18

1: C.

 

. -. , .

-, FILE, stdio.h. FILE .

fopen(), FILE, .

FILE *fopen(name, type);

name ( ),
type - , :

"r" ( );

"w" ; , ;

"a" ( ); , ;

"r+" ( );

"w+" ; , ;

"a+" , , .

- . , NULL.

fclose() , fopen() . fclose().

: 0, ; EOF, .

#include <stdio.h>
int main() {

FILE *fp;

char name[]="my.txt";

if(fp = fopen(name, "r")!=NULL) { // ?
... //

} else printf(" ");

fclose(fp);

return 0;
}

:

char fgetc();

FILE. . , EOF.
:

fputc(,);

FILE. .

fscanf() fprintf() scanf() printf(), , .

fscanf(, "", );
fprintf(, "", );

fgets() fputs() - , gets() puts() .

fgets(,,);

, '\n', , EOF . - '\0'. .

fputs(,);

. - .
s1.txt. s1.txt, 3 s2.txt.

#include <stdio.h>
#include <stdlib.h>
int main() {

FILE *S1, *S2;

int x, y;

system("chcp 1251");

system("cls");

printf(" : ");

scanf("%d", &x);

S1 = fopen("S1.txt", "w");

fprintf(S1, "%d", x);

fclose(S1);

S1 = fopen("S1.txt", "r");

S2 = fopen("S2.txt", "w");

fscanf(S1, "%d", &y);

y += 3;

fclose(S1);

fprintf(S2, "%d\n", y);

fclose(S2);

return 0;
}

- 2

 

19

1: C.

 

(enum), .

?

enum <> { < 1>, < 2>, ... < N> }; // ;!

?

  #include <conio.h> #include <stdio.h>   enum Gender { MALE, FEMALE };   void main() { enum Gender a, b; a = MALE; b = FEMALE; printf("a = %d\n", a); printf("b = %d\n", b); getch(); }

Gender. enum Gender MALE FEMALE.

, 0, 1, 2 .. :

?

  #include <conio.h> #include <stdio.h>   enum Token { SYMBOL, //0 NUMBER, //1 EXPRESSION = 0, //0 OPERATOR, //1 UNDEFINED //2 };   void main() { enum Token a, b, c, d, e; a = SYMBOL; b = NUMBER; c = EXPRESSION; d = OPERATOR; e = UNDEFINED; printf("a = %d\n", a); printf("b = %d\n", b); printf("c = %d\n", c); printf("d = %d\n", d); printf("e = %d\n", e); getch(); }

0 1 0 1 2. , SYMBOL EXPRESSION, NUMBER OPERATOR.

?

  enum Token { SYMBOL, //0 NUMBER, //1 EXPRESSION = 10, //10 OPERATOR, //11 UNDEFINED //12 };

SYMBOL 0, NUMBER 1, EXPRESSION 10, OPERATOR 11, UNDEFINED 12.

, , . , switch.

, Token . Token . , .

. - , .

?

  #include <conio.h> #include <stdio.h> #include <stdlib.h>   static char *ErrorNames[] = { "Index Out Of Bounds", "Stack Overflow", "Stack Underflow", "Out of Memory" };   enum Errors { INDEX_OUT_OF_BOUNDS = 1, STACK_OVERFLOW, STACK_UNDERFLOW, OUT_OF_MEMORY };   void main() { // printf(ErrorNames[INDEX_OUT_OF_BOUNDS-1]); exit(INDEX_OUT_OF_BOUNDS); }

, . exit(N) , , 0 - . .

. , enum , . , .

?

typedef enum enumName { FIELD1, FIELD2 } Name;

?

typedef enum Bool { FALSE, TRUE } Bool;

 

2:/ C++.

++, , -.

stdio.h.
++ - iostream, - :

#include <iostream>

iostream :

cin (stdin )

cout (stdout )

cerr (stderr )

Microsoft Visual Studio :

using namespace std;

- :

>>

<<

cout << ;

:

cout << n;

:
cout << '1' << '2' <<... << ' n';

int n;
char j;
cin >> n >> j;
cout << " n " << n << "j=" << j;

cin >> ;

, , :

int n;
cin >> n;

:

cin >> 1 >> 2 >>...>> n;

(, \n, \t).

int n;
char j;
cin >> n >> j;

. cin , .

#include <iostream>
using namespace std;

int main() {

char s[80];

cin >> s;

cout << s << endl;

system("pause");

return 0;
}

getline():

#include <iostream>
using namespace std;

int main() {

char s[80];

cin.getline(s,80);

cout << s << endl;

system("pause");

return 0;
}


- (<<, >>).
++ . :

endl '\n'
dec 10-
oct 8-
hex 16-
setbase
width()
fill('')
precision() ( ) fixed
fixed ,
showpos +
scientific
get()
getline(, ) .

- C++

#include <iostream>
using namespace std;
int main() {

int n;

cout << " n:";

cin >> n;

cout << " n : " << n << endl;

cin.get(); cin.get();

return 0;
}

,

#include <stdio.h>
int main() {

int n;

printf(" n:");

scanf("%d",&n);

printf(" n : %d\n",n);

getchar(); getchar();

return 0;
}

#include <iostream>
using namespace std;
int main()
{

double a = -112.234;

double b = 4.3981;

int c = 18;

cout << endl << "double number:" << endl;

cout <<"width(10)" << endl;

cout.width(10);

cout << a << endl << b << endl;

cout <<"fill('0')" << endl;

cout.fill('0');

cout.width(10);

cout << a << endl << b << endl;

cout.precision(5);

cout <<"precision(5)" << endl << a << endl << b << endl;

cout << "fixed" << endl << fixed << a << endl << b << endl;

cout << "showpos" << endl << showpos << a << endl << b << endl;

cout << "scientific" <<endl << scientific << a << endl << b << endl;

cout << endl << "int number:" << endl;

cout << showbase << hex << c <<" " << showbase << oct << c << " ";

cout << showbase << dec << c << endl;

cin.get();

return 0;
}



: t∈[0;3] 0,5 y=cos(t).

#include <iostream>
using namespace std;
int main() {

cout<<fixed;

for (double t = 0; t <= 3; t += 0.5) {

cout.width(3);

cout.precision(1);

cout << t;

cout.width(8);

cout.precision(3);

cout << cos(t) << endl;

}

system("pause");

return 0;
}


 

20

1: . .

, . . .
(). #. , , .


#include
#define ()
#undef
#if
#ifdef
#ifndef
#else
#elif , else if
#endif
#line
#error
#pragma , .

 

#include

#include . , . , . , .

#include <stdio.h>
#include "func.c"

#define

#define .

define

. #define , , , . , , .

#include <stdio.h>
#define A 3
int main(){

printf("%d + %d = %d", A, A, A+A); // 3 + 3 = 6

getchar();

return 0;
}

. :

U u (unsigned);

F ( f) float;

L/span> ( l) 8 (long int);

L/span> ( l) long double

#define A 280U // unsigned int
#define B 280LU // unsigned long int
#define C 280 // int (long int)
#define D 280L // long int
#define K 28.0 // double
#define L 28.0F // float
#define M 28.0L // long double

, , . , .

(1,..., n)

, .

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
#define SIN(x) sin(PI*x/180)
int main() {

int c;

system("chcp 1251");

system("cls");

printf(" : ");

scanf("%d", &c);

printf("sin(%d)=%lf", c, SIN(c));

getchar(); getchar();

return 0;
}



, . , , . 1 , , .
#undef.

,

#include <stdio.h>
#define sum(A,B) A+B
int main(){

int a,b,c,d;

a=3; b=5;

c = (a + b)*2; // c = (a + b)*2

d = sum(a, b) * 2; // d = a + b*2;

printf(" a = %d\n b = %d\n", a, b);

printf(" c = %d \n d = %d \n", c, d);

getchar();

return 0;
}

:

#if #ifdef/#ifndef #elif, #else #endif . #if , , #if. :





:


: 2017-02-25; !; : 434 |


:

:

, , .
==> ...

1552 - | 1452 -


© 2015-2024 lektsii.org - -

: 0.183 .