Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Руководство_по_C++_CLI.doc
Скачиваний:
4
Добавлен:
01.07.2025
Размер:
8 Мб
Скачать
☆

3.7.2Practical Learning: Using a Single-Precision Variables

  1. To declare a single-precision variable, change the file as follows:  

using namespace System;

Int main()

{

long propertyNumber = 490724;

__wchar_t propertyType = 'S';

Byte stories = 2;

unsigned bedrooms = 5;

float bathrooms = 3.5;

unsigned yearBuilt = 1962;

Console::WriteLine("=//= Altair Realty =//=");

Console::WriteLine("-=- Properties Inventory -=-");

Console::Write("Property #: ");

Console::WriteLine(propertyNumber);

Console::Write("Property Type: ");

Console::WriteLine(propertyType);

Console::Write("Stories: ");

Console::WriteLine(stories);

Console::Write("Bedrooms: ");

Console::WriteLine(bedrooms);

Console::Write("Bathrooms: ");

Console::WriteLine(bathrooms);

Console::Write("Year Built: ");

Console::WriteLine(yearBuilt);

return 0;

}

  1. Execute the project to see the result:  

=//= Altair Realty =//=

-=- Properties Inventory -=-

Property #: 490724

Property Type: S

Stories: 2

Bedrooms: 5

Bathrooms: 3.5

Year Built: 1962

  1. Close the DOS window

3.7.3Double-Precision Variables

One of the limitations of the float data type is that it provides less precision than required sometimes (in fact, the Microsoft C++ compiler, always displays a warning when you use the float data type to declare a variable). The alternative is to use the double keyword or the Double data type to declare a variable that would hold decimal numbers. The Double data type is defined in the System namespace.

A double-precision variable declared with double or Double can hold a decimal or fractional number that ranges from 1.7 x 10-308 to 1.7 x 10308. It is used for numbers that and/or are very large.

using namespace System;

Int main()

{

System::Double Fraction = 12.35;

Console::WriteLine(Fraction);

return 0;

}

This would produce:

12.35

Press any key to continue

 

3.7.4Practical Learning: Using a Single-Precision Variables

  1. To use a double-precision variable, change the file as follows:  

using namespace System;

Int main()

{

long propertyNumber = 490724;

__wchar_t propertyType = 'S';

Byte stories = 2;

unsigned bedrooms = 5;

float bathrooms = 3.5;

unsigned yearBuilt = 1962;

double marketValue = 652540;

Console::WriteLine("=//= Altair Realty =//=");

Console::WriteLine("-=- Properties Inventory -=-");

Console::Write("Property #: ");

Console::WriteLine(propertyNumber);

Console::Write("Property Type: ");

Console::WriteLine(propertyType);

Console::Write("Stories: ");

Console::WriteLine(stories);

Console::Write("Bedrooms: ");

Console::WriteLine(bedrooms);

Console::Write("Bathrooms: ");

Console::WriteLine(bathrooms);

Console::Write("Year Built: ");

Console::WriteLine(yearBuilt);

Console::Write("Market Value: ");

Console::WriteLine(marketValue);

return 0;

}

  1. Execute the project to see the result:  

=//= Altair Realty =//=

-=- Properties Inventory -=-

Property #: 490724

Property Type: S

Stories: 2

Bedrooms: 5

Bathrooms: 3.5

Year Built: 1962

Market Value: 652540

  1. Close the DOS window