Содержание

Глава 3

Типы данных литералы и переменные

  1. Compute the distance from the Earth to the sun, in inches.
  2. Use byte.
  3. Find the radius of a circle given its area.
  4. Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
  5. Use the decimal type to compute a discount.
  6. Use the decimal type to compute the future value of an investment.
  7. Demonstrate bool values.
  8. Use format commands.
  9. Use the C format spedifier to output dollars and cents.
  10. Demonstrate escape sequences in strings.
  11. Demonstrate verbatim literal strings
  12. Demonstrate dynamic initialization.
  13. Demonstrate block scope.
  14. Demonstrate lifetime of a variable.
  15. This program attempts to declared a variable
    in an inner scope with the same name as one
    defined in an outer scope. *** This program will not compile. ***

  16. Demonstate automatic conversion from long to double.
  17. *** This program will not compile. ***
  18. Demonstrate casting.
  19. A promotion surprise!
  20. Using casts in an expression.

listing 1
// Compute the distance from the Earth to the sun, in inches.

using System;

class Inches {
public static void Main() {
long inches;
long miles;

miles = 93000000; // 93,000,000 miles to the sun

// 5,280 feet in a mile, 12 inches in a foot
inches = miles * 5280 * 12;

Console.WriteLine("Distance to the sun: " +
inches + " inches.");

}
}


Результат:

Distance to the sun: 5892480000000 inches.

Анализ:

Вверх


listing 2
// Use byte.

using System;

class Use_byte {
public static void Main() {
byte x;
int sum;

sum = 0;
for(x = 1; x <= 100; x++)
sum = sum + x;

Console.WriteLine("Summation of 100 is " + sum);

}
}


Результат:

Summation of 100 is 5050

Анализ:

Вверх


listing 3
// Find the radius of a circle given its area.

using System;

class FindRadius {
public static void Main() {
Double r;
Double area;

area = 10.0;

r = Math.Sqrt(area / 3.1416);

Console.WriteLine("Radius is " + r);

}
}


Результат:

Radius is 1,78412203012729

Анализ:

Вверх


listing 4
// Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().

using System;

class Trigonometry {
public static void Main() {
Double theta; // angle in radians

for(theta = 0.1; theta <= 1.0; theta = theta + 0.1) {
Console.WriteLine("Sine of " + theta + " is " + Math.Sin(theta));
Console.WriteLine("Cosine of " + theta + " is " + Math.Cos(theta));
Console.WriteLine("Tangent of " + theta + " is " + Math.Tan(theta));
Console.WriteLine();
}

}
}


Результат:

Sine of 0,1 is 0,0998334166468282
Cosine of 0,1 is 0,995004165278026
Tangent of 0,1 is 0,100334672085451

Sine of 0,2 is 0,198669330795061
Cosine of 0,2 is 0,980066577841242
Tangent of 0,2 is 0,202710035508673

Sine of 0,3 is 0,29552020666134
Cosine of 0,3 is 0,955336489125606
Tangent of 0,3 is 0,309336249609623

Sine of 0,4 is 0,389418342308651
Cosine of 0,4 is 0,921060994002885
Tangent of 0,4 is 0,422793218738162

Sine of 0,5 is 0,479425538604203
Cosine of 0,5 is 0,877582561890373
Tangent of 0,5 is 0,54630248984379

Sine of 0,6 is 0,564642473395035
Cosine of 0,6 is 0,825335614909678
Tangent of 0,6 is 0,684136808341692

Sine of 0,7 is 0,644217687237691
Cosine of 0,7 is 0,764842187284489
Tangent of 0,7 is 0,842288380463079

Sine of 0,8 is 0,717356090899523
Cosine of 0,8 is 0,696706709347166
Tangent of 0,8 is 1,02963855705036

Sine of 0,9 is 0,783326909627483
Cosine of 0,9 is 0,621609968270665
Tangent of 0,9 is 1,26015821755034

Sine of 1 is 0,841470984807896
Cosine of 1 is 0,54030230586814
Tangent of 1 is 1,5574077246549

Анализ:

Вверх


listing 5
// Use the decimal type to compute a discount.

using System;

class UseDecimal {
public static void Main() {
decimal price;
decimal discount;
decimal discounted_price;

// compute discounted price
price = 19.95m;
discount = 0.15m; // discount rate is 15%

discounted_price = price - ( price * discount);

Console.WriteLine("Discounted price: $" + discounted_price);
}
}


Результат:

Discounted price: $16,9575

Анализ:

Вверх


listing 6
/*
Use the decimal type to compute the future value
of an investment.
*/

using System;

class FutVal {
public static void Main() {
decimal amount;
decimal rate_of_return;
int years, i;

amount = 1000.0M;
rate_of_return = 0.07M;
years = 10;

Console.WriteLine("Original investment: $" + amount);
Console.WriteLine("Rate of return: " + rate_of_return);
Console.WriteLine("Over " + years + " years");

for(i = 0; i < years; i++)
amount = amount + (amount * rate_of_return);

Console.WriteLine("Future value is $" + amount);
}
}


Результат:

Original investment: $1000
Rate of return: 0,07
Over 10 years
Future value is $1967,15135728956532249

Анализ:

Вверх


listing 7
// Demonstrate bool values.

using System;

class BoolDemo {
public static void Main() {
bool b;

b = false;
Console.WriteLine("b is " + b);
b = true;
Console.WriteLine("b is " + b);

// a bool value can control the if statement
if(b) Console.WriteLine("This is executed.");

b = false;
if(b) Console.WriteLine("This is not executed.");

// outcome of a relational operator is a bool value
Console.WriteLine("10 > 9 is " + (10 > 9));

}
}


Результат:

b is False
b is True
This is executed.
10 > 9 is True

Анализ:

Вверх


listing 8
// Use format commands.

using System;

class DisplayOptions {
public static void Main() {
int i;

Console.WriteLine("Value\tSquared\tCubed");

for(i = 1; i < 10; i++)
Console.WriteLine(" {0}\t {1}\t {2}", i, i*i, i*i*i);

}
}


Результат:

Подсказка

Анализ:

Вверх


listing 9
/*
Use the C format spedifier to output dollars and cents.
*/

using System;

class UseDecimal {
public static void Main() {
decimal price;
decimal discount;
decimal discounted_price;

// compute discounted price
price = 19.95m;
discount = 0.15m; // discount rate is 15%

discounted_price = price - ( price * discount);

Console.WriteLine("Discounted price: {0:C}", discounted_price);
}
}


Результат:

Discounted price: 16,96р.

Анализ:

Вверх


listing 10
// Demonstrate escape sequences in strings.

using System;

class StrDemo {
public static void Main() {
Console.WriteLine("Line One\nLine Two\nLine Three");
Console.WriteLine("One\tTwo\tThree");
Console.WriteLine("Four\tFive\tSix");

// embed quotes
Console.WriteLine("\"Why?\", he asked.");
}
}


Результат:

Подсказка

Анализ:

Вверх


listing 11
// Demonstrate verbatim literal strings.

using System;

class Verbatim {
public static void Main() {
Console.WriteLine(@"This is a verbatim
string literal
that spans several lines.
");
Console.WriteLine(@"Here is some tabbed output:
1 2 3 4
5 6 7 8
");
Console.WriteLine(@"Programmers say, ""I like C#.""");
}
}


Результат:

This is a verbatim
string literal
that spans several lines.

Here is some tabbed output:
1 2 3 4
5 6 7 8

Programmers say, "I like C#."

Анализ:

Вверх


listing 12
// Demonstrate dynamic initialization.

using System;

class DynInit {
public static void Main() {
double s1 = 4.0, s2 = 5.0; // length of sides

// dynamically initialize hypot
double hypot = Math.Sqrt( (s1 * s1) + (s2 * s2) );

Console.Write("Hypotenuse of triangle with sides " +
s1 + " by " + s2 + " is ");

Console.WriteLine(" {0:#.###}.", hypot);
}
}


Результат:

Hypotenuse of triangle with sides 4 by 5 is 6,403 .

Анализ:

Вверх


listing 13
// Demonstrate block scope.

using System;

class ScopeDemo {
public static void Main() {
int x; // known to all code within Main()

x = 10;
if(x == 10) {
// start new scope
int y = 20; // known only to this block

// x and y both known here.
Console.WriteLine("x and y: " + x + " " + y);
x = y * 2;
}
// y = 100; // Error! y not known here

// x is still known here.
Console.WriteLine("x is " + x);
}
}


Результат:

x and y: 10 20
x is 40

Анализ:

Вверх


listing 14
// Demonstrate lifetime of a variable.

using System;

class VarInitDemo {
public static void Main() {
int x; for(x = 0; x < 3; x++) {
int y = -1; // y is initialized each time block is entered
Console.WriteLine("y is: " + y); // this always prints -1
y = 100;
Console.WriteLine("y is now: " + y);
}
}
}


Результат:

y is: -1
y is now: 100
y is: -1
y is now: 100
y is: -1
y is now: 100

Анализ:

Вверх


listing 15
/*
This program attempts to declared a variable
in an inner scope with the same name as one
defined in an outer scope.

*** This program will not compile. ***
*/

using System;

class NestVar {
public static void Main() {
int count;

for(count = 0; count < 10; count = count+1) {

Console.WriteLine("This is count: " + count);

int count; // illegal!!!
for(count = 0; count < 2; count++)
Console.WriteLine("This program is in error!");
}
}
}


Результат:

Анализ:

Вверх


listing 16
// Demonstate automatic conversion from long to double.

using System;

class LtoD {
public static void Main() {
long L;
double D;

L = 100123285L;
D = L; Console.WriteLine("L and D: " + L + " " + D);

}
}


Результат:

L and D: 100123285 100123285

Анализ:

Вверх


listing 17
// *** This program will not compile. ***

using System;

class LtoD {
public static void Main() {
long L;
double D;

D = 100123285.0;
L = D; // Illegal!!!

Console.WriteLine("L and D: " + L + " " + D);

}
}


Результат:

Анализ:

Вверх


listing 18
// Demonstrate casting.

using System;

class CastDemo {
public static void Main() {
double x, y;
byte b;
int i;
char ch;
uint u;
short s;
long l;

x = 10.0;
y = 3.0;

// cast an int into a double
i = (int) (x / y); // cast double to int, fractional component lost
Console.WriteLine("Integer outcome of x / y: " + i);
Console.WriteLine();

// cast an int into a byte, no data lost
i = 255;
b = (byte) i;
Console.WriteLine("b after assigning 255: " + b +
" -- no data lost.");

// cast an int into a byte, data lost
i = 257;
b = (byte) i;
Console.WriteLine("b after assigning 257: " + b +
" -- data lost.");
Console.WriteLine();

// cast a uint into a short, no data lost
u = 32000;
s = (short) u;
Console.WriteLine("s after assigning 32000: " + s +
" -- no data lost.");

// cast a uint into a short, data lost
u = 64000;
s = (short) u;
Console.WriteLine("s after assigning 64000: " + s +
" -- data lost.");
Console.WriteLine();

// cast a long into a uint, no data lost
l = 64000;
u = (uint) l;
Console.WriteLine("u after assigning 64000: " + u +
" -- no data lost.");

// cast a long into a uint, data lost
l = -12;
u = (uint) l;
Console.WriteLine("u after assigning -12: " + u +
" -- data lost.");
Console.WriteLine();

// cast an int into a char
b = 88; // ASCII code for X
ch = (char) b;
Console.WriteLine("ch after assigning 88: " + ch);

}
}


Результат:

Integer outcome of x / y: 3

b after assigning 255: 255 -- no data lost.
b after assigning 257: 1 -- data lost.

s after assigning 32000: 32000 -- no data lost.
s after assigning 64000: -1536 -- data lost.

u after assigning 64000: 64000 -- no data lost.
u after assigning -12: 4294967284 -- data lost.

ch after assigning 88: X

Анализ:

Вверх


listing 19
// A promotion surprise!

using System;

class PromDemo {
public static void Main() {
byte b;

b = 10;
b = (byte) (b * b); // cast needed!!

Console.WriteLine("b: "+ b);

}
}


Результат:

b: 100

Анализ:

Вверх


listing 20
// Using casts in an expression.

using System;

class CastExpr {
public static void Main() {
double n; for(n = 1.0; n <= 10; n++) {
Console.WriteLine("The square root of {0} is {1}", n, Math.Sqrt(n));

Console.WriteLine("Whole number part: {0}" , (int) Math.Sqrt(n));

Console.WriteLine("Fractional part: {0}", Math.Sqrt(n) - (int) Math.Sqrt(n) );
Console.WriteLine();

}

}
}


Результат:

The square root of 1 is 1
Whole number part: 1
Fractional part: 0

The square root of 2 is 1,4142135623731
Whole number part: 1
Fractional part: 0,414213562373095

The square root of 3 is 1,73205080756888
Whole number part: 1
Fractional part: 0,732050807568877

The square root of 4 is 2
Whole number part: 2
Fractional part: 0

The square root of 5 is 2,23606797749979
Whole number part: 2
Fractional part: 0,23606797749979

The square root of 6 is 2,44948974278318
Whole number part: 2
Fractional part: 0,449489742783178

The square root of 7 is 2,64575131106459
Whole number part: 2
Fractional part: 0,645751311064591

The square root of 8 is 2,82842712474619
Whole number part: 2
Fractional part: 0,82842712474619

The square root of 9 is 3
Whole number part: 3
Fractional part: 0

The square root of 10 is 3,16227766016838
Whole number part: 3
Fractional part: 0,16227766016838

Анализ:



Содержание

Hosted by uCoz