Лекции по C++

Информация - Компьютеры, программирование

Другие материалы по предмету Компьютеры, программирование

/p>

cout << endl << "Введите первое целое число: ";

cin >> int1;

cout << "Введите второе целое число: ";

cin >> int2;

cout << endl;

long1 = int1 + int2;

long2 = int1 - int2;

long3 = int1 * int2;

long4 = int1 / int2;

long5 = int1 % int2;

cout << int1 << " + " << int2 << " = " << long1 << endl;

cout << int1 << " - " << int2 << " = " << long2 << endl;

cout << int1 << " * " << int2 << " = " << long3 << endl;

cout << int1 << " / " << int2 << " = " << long4 << endl;

cout << int1 << " % " << int2 << " = " << long5 << endl;

cout << endl << endl;

cout << "Веедите первое вещественное число: ";

cin >> x;

cout << "Введите второе вещественное число: ";

cin >> y;

cout << endl;

real1 = x + y;

real2 = x - y;

real3 = x * y;

real4 = x / y;

cout << x << " + " << y << " = " << real1 << endl;

cout << x << " - " << y << " = " << real2 << endl;

cout << x << " * " << y << " = " << real3 << endl;

cout << x << " / " << y << " = " << real4 << endl;

cout << endl << endl;

return 0;

}

/* Тест и результаты:

Введите первое целое число: 10

Введите второе целое число: 5

 

10 + 5 = 15

10 - 5 = 5

10 * 5 = 50

10 / 5 = 2

10 % 5 = 0

 

 

Введите первое вещественное число: 1.25

Введите второе вещественное число: 2.58

 

1.25 + 2.58 = 3.83

1.25 - 2.58 = -1.33

1.25 * 2.58 = 3.225

1.25 / 2.58 = 0.484496

 

*/

 

//Демонстрация операций инкремента и декремента см. в программе OPER2.CPP

 

// Программа SIZEOF.CPP, которая возвращает размеры данных, используя

// для этого операцию sizeof() с переменными и типами данных.

 

#include

 

int main()

{

short int aShort;

int anInt;

long aLong;

char aChar;

float aReal;

 

cout << "Таблица 1. Размеры памяти для переменных" << endl

<< endl;

cout << " Тип данных Используемая " << endl;

cout << " память (в байтах)" << endl;

cout << "------------------ -----------" << endl;

cout << " short int " << sizeof(aShort) << endl;

cout << " integer " << sizeof(anInt) << endl;

cout << " long integer " << sizeof(aLong) << endl;

cout << " character " << sizeof(aChar) << endl;

cout << " float " << sizeof(aReal) << endl;

cout << endl << endl << endl;

 

cout << "Таблица 2. Размеры памяти для типов данных" << endl

<< endl;

cout << " Тип данных Используемая" << endl;

cout << " память (в байтах)" << endl;

cout << "------------------ -----------" << endl;

cout << " short int " << sizeof(short int) << endl;

cout << " integer " << sizeof(int) << endl;

cout << " long integer " << sizeof(long) << endl;

cout << " character " << sizeof(char) << endl;

cout << " float " << sizeof(float) << endl;

cout << endl << endl << endl;

 

return 0;

}

 

 

/* Результаты:

 

Таблица 1. Размеры памяти для переменных"

Тип данных Используемая

память (в байтах)

------------------ -----------

short int 2

integer 2

long integer 4

character 1

float 4

 

Таблица 2. Размеры памяти для типов данных

Тип данных Используемая

память (в байтах)

------------------ -----------

short int 2

integer 2

long integer 4

character 1

float 4

*/

 

// Простая программа TYPECAST.CPP, демонстрирующая приведение типа

 

#include

 

int main()

{

short shortInt1, shortInt2;

unsigned short aByte;

int anInt;

long aLong;

char aChar;

float aReal;

 

// присваиваются значения

shortInt1 = 10;

shortInt2 = 6;

// действия выполняются без приведения типа

aByte = shortInt1 + shortInt2;

anInt = shortInt1 - shortInt2;

aLong = shortInt1 * shortInt2;

aChar = aLong + 5; // автоматическое преобразование

// в символьный тип

aReal = shortInt1 * shortInt2 + 0.5;

 

cout << "shortInt1 = " << shortInt1 << endl

<< "shortInt2 = " << shortInt2 << endl

<< "aByte = " << aByte << endl

<< "anInt = " << anInt << endl

<< "aLong = " << aLong << endl

<< "aChar is " << aChar << endl

<< "aReal = " << aReal << endl << endl << endl;

 

// дейтсвия выполняются с приведением типа

aByte = (unsigned short) (shortInt1 + shortInt2);

anInt = (int) (shortInt1 - shortInt2);

aLong = (long) (shortInt1 * shortInt2);

aChar = (unsigned char) (aLong + 5);

aReal = (float) (shortInt1 * shortInt2 + 0.5);

 

cout << "shortInt1 = " << shortInt1 << endl

<< "shortInt2 = " << shortInt2 << endl

<< "aByte = " << aByte << endl

<< "anInt = " << anInt << endl

<< "aLong = " << aLong << endl

<< "aChar is " << aChar << endl

<< "aReal = " << aReal << endl << endl << endl;

return 0;

}

/* Результаты:

shortInt1 = 10

shortInt2 = 6

aByte = 16

anInt = 4

aLong = 60

aChar is A

aReal = 60.5

 

shortInt1 = 10

shortInt2 = 6

aByte = 16

anInt = 4

aLong = 60

aChar is A

aReal = 60.5

*/

 

/* *** ВОПРОСЫ И ОТВЕТЫ ***

 

Существуют ли особые соглашения о присвоении имен идентификаторам?

 

Существует несколько стилей, которые стали популярными в последние

годы. Стиль, который используется в наших занятиях, требует начинать

имя переменной с символа, набранного в нижнем регистре. Если идентифи-

катор состоит из нескольки?/p>