Глава 3
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.
Анализ:
Вверх
sum = 0;
Summation of 100 is 5050
Анализ:
Вверх
area = 10.0;
r = Math.Sqrt(area / 3.1416);
Radius is 1,78412203012729
Анализ:
Вверх
Sine of 0,1 is 0,0998334166468282
Sine of 0,2 is 0,198669330795061
Sine of 0,3 is 0,29552020666134
Sine of 0,4 is 0,389418342308651
Sine of 0,5 is 0,479425538604203
Sine of 0,6 is 0,564642473395035
Sine of 0,7 is 0,644217687237691
Sine of 0,8 is 0,717356090899523
Sine of 0,9 is 0,783326909627483
Sine of 1 is 0,841470984807896
Анализ:
Вверх
Discounted price: $16,9575
Анализ:
Вверх
Original investment: $1000
Анализ:
Вверх
b = false;
b is False
Анализ:
Вверх
Console.WriteLine("Value\tSquared\tCubed");
Анализ:
Вверх
Discounted price: 16,96р.
Анализ:
Вверх
Анализ:
Вверх
This is a verbatim
Here is some tabbed output:
Programmers say, "I like C#."
Анализ:
Вверх
Hypotenuse of triangle with sides 4 by 5 is 6,403 .
Анализ:
Вверх
x and y: 10 20
Анализ:
Вверх
y is: -1
Анализ:
Вверх
for(count = 0; count < 10; count = count+1)
{
Анализ:
Вверх
L = 100123285L;
L and D: 100123285 100123285
Анализ:
Вверх
D = 100123285.0;
Console.WriteLine("L and D: " + L + " " + D);
Анализ:
Вверх
x = 10.0;
// cast an int into a double
Integer outcome of x / y: 3
b after assigning 255: 255 -- no data lost.
s after assigning 32000: 32000 -- no data lost.
u after assigning 64000: 64000 -- no data lost.
ch after assigning 88: X
Анализ:
Вверх
b = 10;
b: 100
Анализ:
Вверх
Console.WriteLine("Whole number part:
{0}" ,
(int) Math.Sqrt(n));
The square root of 1 is 1
The square root of 2 is 1,4142135623731
The square root of 3 is 1,73205080756888
The square root of 4 is 2
The square root of 5 is 2,23606797749979
The square root of 6 is 2,44948974278318
The square root of 7 is 2,64575131106459
The square root of 8 is 2,82842712474619
The square root of 9 is 3
The square root of 10 is 3,16227766016838
Анализ:
listing 2
// Use byte.
using System;
class Use_byte
{
int sum;
for(x = 1; x <= 100; x++)
sum = sum + x;
Console.WriteLine("Summation of 100 is " + sum);
Результат:
listing 3
// Find the radius of a circle given its area.
using System;
class FindRadius
{
Double area;
Console.WriteLine("Radius is " + r);
Результат:
listing 4
// Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
using System;
class Trigonometry
{
for(theta = 0.1; theta <= 1.0; theta = theta + 0.1)
{
Console.WriteLine("Cosine of " + theta + " is " +
Math.Cos(theta));
Console.WriteLine("Tangent of " + theta + " is " +
Math.Tan(theta));
Console.WriteLine();
Результат:
Cosine of 0,1 is 0,995004165278026
Tangent of 0,1 is 0,100334672085451
Cosine of 0,2 is 0,980066577841242
Tangent of 0,2 is 0,202710035508673
Cosine of 0,3 is 0,955336489125606
Tangent of 0,3 is 0,309336249609623
Cosine of 0,4 is 0,921060994002885
Tangent of 0,4 is 0,422793218738162
Cosine of 0,5 is 0,877582561890373
Tangent of 0,5 is 0,54630248984379
Cosine of 0,6 is 0,825335614909678
Tangent of 0,6 is 0,684136808341692
Cosine of 0,7 is 0,764842187284489
Tangent of 0,7 is 0,842288380463079
Cosine of 0,8 is 0,696706709347166
Tangent of 0,8 is 1,02963855705036
Cosine of 0,9 is 0,621609968270665
Tangent of 0,9 is 1,26015821755034
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
{
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);
Результат:
listing 6
/*
Use the decimal type to compute the future value
of an investment.
*/
using System;
class FutVal
{
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);
Результат:
Rate of return: 0,07
Over 10 years
Future value is $1967,15135728956532249
listing 7
// Demonstrate bool values.
using System;
class BoolDemo
{
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 True
This is executed.
10 > 9 is True
listing 8
// Use format commands.
using System;
class DisplayOptions
{
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
{
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);
Результат:
listing 10
// Demonstrate escape sequences in strings.
using System;
class StrDemo
{
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
{
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#.""");
Результат:
string literal
that spans several lines.
1 2 3 4
5 6 7 8
listing 12
// Demonstrate dynamic initialization.
using System;
class DynInit
{
// 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);
Результат:
listing 13
// Demonstrate block scope.
using System;
class ScopeDemo
{
x = 10;
if(x == 10)
{
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 is 40
listing 14
// Demonstrate lifetime of a variable.
using System;
class VarInitDemo
{
Console.WriteLine("y is: " + y); // this always prints -1
y = 100;
Console.WriteLine("y is now: " + y);
Результат:
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
{
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
{
double D;
D = L;
Console.WriteLine("L and D: " + L + " " + D);
Результат:
listing 17
// *** This program will not compile. ***
using System;
class LtoD
{
double D;
L = D; // Illegal!!!
Результат:
listing 18
// Demonstrate casting.
using System;
class CastDemo
{
byte b;
int i;
char ch;
uint u;
short s;
long l;
y = 3.0;
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);
Результат:
b after assigning 257: 1 -- data lost.
s after assigning 64000: -1536 -- data lost.
u after assigning -12: 4294967284 -- data lost.
listing 19
// A promotion surprise!
using System;
class PromDemo
{
b = (byte) (b * b); // cast needed!!
Console.WriteLine("b: "+ b);
Результат:
listing 20
// Using casts in an expression.
using System;
class CastExpr
{
Console.WriteLine("Fractional part:
{0}",
Math.Sqrt(n) - (int) Math.Sqrt(n) );
Console.WriteLine();
Результат:
Whole number part: 1
Fractional part: 0
Whole number part: 1
Fractional part: 0,414213562373095
Whole number part: 1
Fractional part: 0,732050807568877
Whole number part: 2
Fractional part: 0
Whole number part: 2
Fractional part: 0,23606797749979
Whole number part: 2
Fractional part: 0,449489742783178
Whole number part: 2
Fractional part: 0,645751311064591
Whole number part: 2
Fractional part: 0,82842712474619
Whole number part: 3
Fractional part: 0
Whole number part: 3
Fractional part: 0,16227766016838