Программирование на языке C#. Методическое пособие по курсу «Программирование на языках высокого уровня»

  • Upload
    iko1402

  • View
    250

  • Download
    0

Embed Size (px)

Citation preview

. .

C#

-

2007

621.398

-25

..

C#. . .: , 2007. 94 .

C#, , - . Microsoft Visual Studio 2005 - .

, . .., 2007

, 2007

6

71 . C#

71.1.

81.2.

101.3.

111.4.

121.5. /

141.6.

141.7.

161.8.

181.9.

19

202.

202.1.

212.2. C#

24

253. - C#

253.1.

253.2.

283.3.

313.4.

343.5.

363.6.

393.7.

413.8.

433.9.

443.10.

463.11.

483.12.

493.13.

513.14.

583.15.

61

614. Microsoft Visual Studio 2005

614.1.

624.2.

634.3.

644.4. /

654.4.1. / -

664.4.2. / -

674.4.3. /

684.4.4. /

714.5.

724.5.1. SDI

754.5.2. MDI

80

815. - Microsoft Visual Studio 2005

815.1.

845.2.

875.3.

895.4.

91

91

92

++ . # . , . , , # .

# ( Microsoft .NET Framework) . # , Windows, Internet, .

. , . " " , , . . #. # "" . ( C++) Pascal, , Delphi, .

# , Microsoft .Net Framework Microsoft. C# , , C++, Java, Delphi, Modula-2 Smalltalk. , Turbo Pascal Borland Delphi, . , # , , ; , , - .

, . ++ Delphi 1 . C#

1.1.

Microsoft Studio 2005. , , , . C# , . Studio 2005 :

File New Project;

Visual C#, Windows, Console Application;

(Location) (Name), ConsApp.

:

using System; // using System.Collections.Generic;

using System.Text;

namespace ConsApp

{

//

class Program

{

//

static void Main(string[] args)

{

// ,

Console.WriteLine(" );

Console.ReadLine();

}

}

}

( ), (Program), (Main). . . Console.ReadLine(); . , / Pascal. .

1.2.

C#

sbyte1 , -128 127

byte 1, 0 255

char2 , Unicode0000 FFFF

short2 , -32768 32767

ushort2 , 0 65535

int4 , -2 147 483 646 2 147 483 647

uint4 , 0 4 294 967 295

long8 , -9 223 372 036 854 775 808 9 223 372 036 854 775 807

ulong8 , 0 18 446 744 073 709 551 615

decimal28, 29 (10e-28 (7.9e+28

float7 1.5e-45 3.4e38

double15-16 5.0e-324 1.7e308

booltrue false

string

C#, ++ , , .

.

int i, k;

float x, y;

decimal d1;

short n;

string s1;

char c1;

bool b1;

i = 3;

n=9;

x = -6.7f; // x = -456.43F;

double z;

z = 5.76; // z = 5.34D; z = -76.45d;

d1 = 123.43m; // d1 = 154.65M;

s1 = "ABCDEF";

c1 = '?';

b1=true;

, -6.7 double x = -6.7; ! d1 = 123.43; float, double, decimal. decimal Currency (): ( ).

.

: , . bool, double, decimal . float double; int long, float, double, decimal; long float, double, decimal.

(__)

( - . ).

x=(float)56.3; // double // float

d1 = (decimal)25.6; // double // decimal

i = (int)8.6; // i=8

. , :

x=(float)56.6e+300; , float. . , short n=123456789; n=( short)123456789; .

C# , . !

: ; , int, double, bool, string.

1.3.

+, -, *, /, %. / , % . . . ,

i = 14;

k = 4;

x = (float)i / k;

i++ i-- ++i --i. i = 14 k=(++i)+4; i=15 k=19; k=(i++)+4 i=15 k=18. x+=z; x=x+z; + .

() . i = 1478; :

k = i >> 3; //k=184

i = i k; b1=!(i>k);

, Math. , :

x=4598.3f;

z = Math.Pow(x, 0.25);

z = Math.Log(x); Math : . , double .

1.4.

, 1:

if (_) ;

if (_)

{1; 2; . . . .}

, 2: if ( _ )

1;

else 2 ;

if (_)

{ 1; 2; }

else

{; ; . . . .}

, ++. , if (). () . , ++: == && || ! .

. :

switch ( )

{

case _:

break;

case _:

break;

. . . . . . . . . . .

default:

break;

} ++ switch . break; . .

static void Main(string[] args)

{

string s1;

s1 = "ABC";

switch (s1)

{

case"ABC":

Console.WriteLine("Variant 1");

break;

case "DEF":

Console.WriteLine("Variant 2");

break;

default:

Console.WriteLine("Unknown ");

break;

}

Console.ReadLine();

}

1.5. /

/ Console: Write( ), WriteLine( ), ReadLine() . : ReadLine string, . Write, WriteLine . , . Write WriteLine , WriteLine , Write .

Convert. , ToInt32 int; ToDouble double; ToString string. Convert : , .

: :

namespace ConsApp

{

class Program

{

static void Main(string[] args)

{

int i;

double x,y,z;

string s;

Console.Write("i="); // s = Console.ReadLine(); // i = Convert.ToInt32(s); //

// Console.Write("x=");

x = Convert.ToDouble(Console.ReadLine());

//,

y = i * x;

z = 2 * i - x;

Console.WriteLine("y=" + y);

//

Console.WriteLine(Convert.ToString(z));

//

Console.ReadLine();

} } }

WriteLine , . , , +y. . decimal.

namespace Console5

{

class Class1

{

static void Main(string[] args)

{

decimal d1,d2,d3;

string s;

s=Console.ReadLine();

d1=Convert.ToDecimal(s);

d2=4.5m; //m M decimal

d3=d1*d2;

Console.WriteLine("Answer is :{0:###.##}",d3);

Console.ReadLine();

} } }

{0 : ###.##} : .

Console.ReadLine(); .

1.6.

C# :

//

/*

*/

/// XML-

C# XML- XML-. XML-. XML- .

1.7. C# . , .

- [] _

_ [,] _

, .. .

:

static void Main(string[] args)

{int[] arr1; // 1

int[] arr2=new int[66]; // 2

int[] arr3 = {2, 5, 55, -6, 8}; // 3

string s;

int n;

Console.Write("Count of Elements ");

s=Console.ReadLine();

n=Convert.ToInt32(s);

arr1=new int[n]; . . . . // 4

// 1 , // 4, n. // 2 . // 3 , . , int[]a,b,c; , ( ). C# , 1.

C# . . arr1.Length arr1.

:

static void Main(string[] args)

{

int[,] a;

int[,] b ={ { 1, 2, 3 }, { 4, 5, 6 } };

//

int n, m;

a = new int[6, 12]; // n = b.GetLength(0); //

//

Console.WriteLine("n=" + n);

m = b.GetLength(1); //

// Console.WriteLine("n=" + m);

n = a.GetUpperBound(0); //

// Console.WriteLine("n=" + n);

m = a.GetUpperBound(1); //

// Console.WriteLine("n=" + m);

m = a.Length; // Console.ReadLine(); . . . .

} GetLength(1) GetUpperBound(1)!

. : . . , :

int[] mas2={1,2,3,4};

int[]mas1=new int[4];

mas1=mas2 , , , , . , , mas2.CopyTo(mas1, i); mas2 mas1 i; mas1 . mas1 . mas1 mas2 .

, , C# : (, , ) : .

: double [ ] [ ] q;

, . .

1.8.

C# :

while,

do . . . while,

for,

foreach.

.

while( ___ )

{

//

}

do

{

//

} while( ___);

foreach ( ):

foreach ( in _)

{

//

}

break; ( ) for Continue; ( ).

for foreach : . .

static void Main(string[] args)

{

double[,] a;

int n, m,kolpol=0;

Console.Write("Rows ? ");

n = Convert.ToInt32(Console.ReadLine());

Console.Write("Columns ? ");

m = Convert.ToInt32(Console.ReadLine());

a = new double[n, m];

for(int i=0;i