Professional Documents
Culture Documents
(.)
. .
.
.
.
................................................................................................................................................................2
............................................................................................................................................................3
1 C#..........................................................................................................................6
1.1 C# ...................................................................................................................8
1.2 ...............................................................12
1.1 MS Visual C# .......................................................................14
1.2 C# ........................................................................16
1.3 C# ............................18
1.4 ....................................................................................................29
2 .........................................................................32
2.1 .....................................................................................................34
2.2 ............................................................................................................36
2.3 ........................................................................................................37
2.1 .................................................................................................38
2.2 .......................................................................................39
3 ..................................................................................................................41
3.1 ..................................................................................................44
3.2 .......................................................................................................................45
3.3 ......................................................................................................................47
3.4 ..........................................................................................................................49
3.5 ...........................................................................................................51
3.1 ..................................................................................................52
3.2 if if...else ..............................................................................................53
3.3 if ...................................................................................................57
3.4 switch...case.................................................................................................59
4 ................................................................................................................................63
4.1 ......................................................................................................................66
4.2 ................................................................................................................67
4.3 ........................................................................................................................70
4.4 ......................................................................................................................71
4.5 ............................................................................................................73
4.1 while ......................................................................................................74
4.2 do...while ...............................................................................................77
4.3 for ..........................................................................................................78
5 ..........................................................................................................................81
5.1 .........................................................................................................84
5.2 ...................................................................................................85
5.3...........................................................................................................86
5.4 ............................................................................................................87
5.5 ................................................................................................................88
5.6 .......................................................................................89
5.1 ......................................................................................91
5.2 ....................................................................................................95
5.3 ...........................................................................................................96
6 .........................................................................................................................................99
6.1.....................................................................................................101
6.2 ......................................................................................................102
6.3 ..................................................................................................103
6.4 ................................................................................................................105
6.5 ..........................................................................................................106
6.6 ......................................................................................................................108
6.1 ...................................................................................................110
6.2 ...............................................................................................114
6.3 foreach ...............................................................................................................115
6.4 .............................................................................................116
6.5 ...........................................................................117
7 .........................................................................................................................118
7.1 .........................................................................................................120
7.2 .....................................................................................................121
7.3 .......................................................................................................123
7.4 .............................................................................................125
7.5 ..............................................................................................................126
4
7.1 ..............................................................................................................128
7.2 .................................................................................................131
.........................................................................................................132
8 ......................................................................133
8.1 ....................................................................................135
8.2 .....................................................................................136
8.1 ..................................................................................................138
8.2 Windows................................................................................145
9 ......................................................................................................................159
9.1 Windows Application...................................................................161
....................................................................................................................................................163
Microsoft Visual C# 2008 Express...........................................................................164
10 ...........................................................................................177
10.1 .........................................................................................179
10.2 ..........................................................................................................................181
10.3 ..........................................................................................................184
10.1 .........................................................................................186
10.2 ............................................................................189
.......................................................................................191
1
C#
1.
1.1 Console Application MS Visual C#
1.2
1.3 C#
2.
MS Visual C#
)solution) (project)
(library)
C# (identifier)
Write WriteLine Console
System WriteLine
3.
3.1
1.1
1.2
C#
3.2
- 1.1 MS Visual C#
()
25
30
- 1.2 C#
- 1.3 C#
- 1.4
3.3
-
- (MS Visual C# Express)
4.
4.1
4.1.1 2
4.1.2 1.1 - 1.2 1.1 - 1.3
4.2
4.2.1 C# MS Visual C#
4.2.2 1.1 MS Visual C#
1.2 C# 1.3
C# 1.4
4.2.3 1.1 C#
4.2.4 1.2
4.2.5 MS Visual C#
C#
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
7.1 MS Visual C# Express
1.1
C#
.
1. 2. .
3. 4. .
1.1 1.2 1.3
1. MS Visual C#
1.4 Console Application First
1.5 MS Visual C#
namespace First
{
class First
{
static void Main()
{
System.Console.WriteLine("Hello teacher!");
System.Console.ReadLine();
}
}
}
1.6
1.7 System.Console.ReadLine() ;
ReadLine
1.8
using
2.
2.1 Second C#
o MyNameSpace
o MyClass
o Enter
Console
Live as if you were to die tomorrow.
Learn as if you were to live forever.
-Mahatma Gandhi
2.2
3.
3.1 ( )
XXX
$$$
_Y
string
i_j
Student ID
HelloWorld!
first-time
null
123Class
Section3
w*h
do
3.2 x float
3.5 C#
MY_AGE
10
income
temp_c
temp_k
NAME
3.6 12.5
using System;
class CircleArea
{
static void Main()
{
const double PI = 3.1415926535;
radius = 12.5;
area = PI * radius * radius;
Console.WriteLine("Circle area = {0}", area);
}
}
Circle
3.7
11
1.2
.
1. 2. .
3. 4. .
1.
1.1 MS Visual C# Expression
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
using System;
class Test
{
static void Main()
{
double x = 4.0, y = 2.0;
int a = 10, b = 4;
Console.WriteLine(________);
Console.ReadLine();
}
}
x+a
a/b
a/x
y/x
(a+b)/b%a
9.0/5.0*(a-x)
x+y-x*y
57%50/25
2.
using System;
class SayHi
{
static void Main()
{
string yourName = ___(a)___;
uint yourAge = ___(b)___;
Console.WriteLine("Hello {1}. You are {0} years old.",
___(c)___, ___(d)___);
}
}
(a) (d)
Arthur 18
Hello Arthur.
__(a)__
__(b)__
__(c)__
__(d)__
13
1.1
MS Visual C#
MS Visual C#
(solution) (project)
(library)
MS Visual C#
1.1
14
Console Application
1.2
Run
1.3 MS Visual C#
15
1.2
C#
C# (object-oriented programming language)
C#
C#
(source code)
C# .cs
prog1.cs
(compiler)
.exe executable
C#
C#
C#
Microsoft Visual C# 2008
Express Edition ( MS Visual C#)
MS Visual C#
// C#
16
(Code Completion)
(Console Application)
1.4
(Windows Application)
1.5
17
1.3
C#
C#
(subroutine)
namespace ___(A)___
{
class ___(B)___
{
static void Main()
{
___(C)___
}
}
}
18
C#
class HelloClass
{
static void Main()
{
System.Console.WriteLine("Hello World!");
System.Console.ReadLine();
}
}
C#
C# (identifier)
C#
(A-Z,a-z) (0-9)
(_)
63
(reserved word) class, namespace, int, void,
static
19
(Data Types) C#
C#
1
1 C#
char
bool
byte
int
uint
long
ulong
float
double
string
a
true false
0 255
- 2,147,483,648 2,147,483,647
0 4,294,967,295
- 9,223,372,036,854,775,808 9,223,372,036,854,775,807
0 and 18,446,744,073,709,551,615
() 3.14159
() Hello
(Variables)
(variable)
C#
DataType variableName;
variableName DataType
( 1)
DataType variableName = initialValue;
= initialValue
variableName
20
distance uint
uint distance;
(Constants)
C#
const
const DataType constantName = value;
constantName DataType
value
myconst double 2.71828
const double myconst = 2.71828;
Harry
5
1: using System;
2:
3: class Intro {
4:
static void Main() {
5:
const string MY_NAME = "Harry";
6:
7:
Console.WriteLine("Hello {0}, how are you?", MY_NAME);
8:
Console.WriteLine("And how can I help you, {0}?",
9:
MY_NAME);
10:
Console.ReadLine();
11:
}
12: }
21
) (
(Arithmetic Expressions)
(expression) C#
3000, 1.414
"Hello,
World"
true false
myName, salary
(arithmetic
expression)
C# 2
2
x = 20
(add)
x+4
24
(subtract)
32-x
12
(multiply)
x*2
40
(divide)
x/2
10
(modulo)
x%6
22
(negate)
x = 20
-x
-20
( )
N/A
(x+2)*3
66
C#
( )
*, / %
+
Write WriteLine
Console System
WriteLine
Write WriteLine
class Hello
{
static void Main()
{
System.Console.Write("Hello, ");
System.Console.WriteLine("everybody");
}
}
23
class Hello
{
static void Main()
{
Console.Write("Hello, ");
Console.WriteLine("everybody");
}
}
Write WriteLine
WriteLine
{0} {1} {2}
C# width, height width*height
24
(Formatting String)
Write WriteLine
) (
C#
C# (formatting string)
{index [,alignment] [:formatSpecifier]}
Two
index
index 0 index
index 1
index alignment
alignment
alignment formatSpecifier
(format specifier)
[ ]
25
(Format Character)
Exponential ()
Fixed-point
()
General (
)
Number ( Fixedpoint comma 3 )
Percentage (
)
Hexadecimal ()
Write WriteLine
using System;
class MainClass
{
public static void Main(string[] args)
{
Console.WriteLine("x = {0} y = {1}",123,456);
Console.WriteLine("123456789");
Console.WriteLine("{0,9}", 123);
Console.WriteLine("123456789");
Console.WriteLine("{0,-9}", 123);
Console.WriteLine("123456789123456789");
Console.WriteLine("{0,-9}{0,9}", 123,456);
}
}
26
Write WriteLine
using System;
class MainClass
{
public static void Main(string[] args)
{
int n = 123456789;
Console.WriteLine("{0:E}", n);
Console.WriteLine("{0:F}", n);
Console.WriteLine("{0:G}", n);
Console.WriteLine("{0:N}", n);
Console.WriteLine("{0:P}", n);
Console.WriteLine("{0:X}", n);
}
}
Write WriteLine
using System;
class MainClass
{
public static void Main(string[] args)
{
int n = 123456789;
Console.WriteLine("{0,20:E}", n);
Console.WriteLine("{0,20:F}", n);
Console.WriteLine("{0,20:G}", n);
Console.WriteLine("{0,20:N}", n);
Console.WriteLine("{0,20:P}", n);
Console.WriteLine("{0,20:X}", n);
}
}
27
20
Console.WriteLine("{0:F2}",
123.4500); 2 F 123.4500
123.45
28
1.4
29
MSDN Library
Search 1.8
Math
1.8 Math
30
F1
1.9 F1
31
1.
1.1
1.2
1.3
2.
Math System
C#
Console.ReadLine (String)
C# Parse
Parse
<numeric_datatype> <string_expression>
Parse
3.
3.1
2.1
2.2
2.3
32
()
20
20
20
3.2
- 2.1
- 2.2
3.3
-
-
4.
4.1
4.1.1 2
4.1.2 2.1 - 2.3 2.1 - 2.2
4.2
4.2.1 Class Math
4.2.2 2.1 2.2
4.2.3 2.1
4.2.4 2.2
4.2.5 2.3
4.2.6
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
7.1
33
2.1
.
1. 2. .
3. 4. .
2.1 2.2
1.
Math.Abs(-1)
|-1|
Math.Sqrt(5)
Math.Abs(9.5)
Math.Pow(5,2)
Math.Pow(2,-1)
Math.Pow(5,0.5)
Math.Pow(Math.Sqrt(2),8)
Math.Log(10)
Math.Sin(Math.PI/6)
Math.Log10(100)
2. C#
xy+z
cos 2 + ln x
|x| + |y|
C#
Math.Pow(x,y) + z
x2 + y2 + z2
sin2x + cos2x
5
a+b
x ln y
34
3. 2.2 (x,y,z)
Enter x: 1.5
Enter y: 7
Enter z: 12.25
Size of the vector (1.5,7,12.25) is 14.19.
35
2.2
.
1. 2. .
3. 4. .
(Quadratic equation)
ax2 + bx + c = 0
x=
b b 2 4ac
2a
a, b c
x x
Enter a:
Enter b:
Enter c:
x = 2.5,
2
3
-20
-4
36
2.3
.
1. 2. .
3. 4. .
r r2 2 r
r
37
2.1
(+ - * / ) C# Math
1
Math System
Console
Math.Abs(x)
Math.Ceiling(x)
Math.Floor(x)
Math.Log(x)
Math.Log10(x)
Math.Round(x)
Math.Pow(x,y)
Math.Sqrt(x)
Math.Max(x)
Math.Min(x)
Math.Sin(x)
Math.Cos(x)
Math.PI
Math.E
x (|x|)
x
x
x
x
x
x y (xy)
x ( x )
x y
x y
x (x )}
x (x )}
3.14159265358979323846
e 2.7182818284590452354
38
2.2
C#
Console.ReadLine
()
2.1
using System;
class SayHello
{
static void Main()
{
string name;
Console.Write("What is your name? ");
name = Console.ReadLine();
Console.WriteLine("Hello {0}, how are you?", name);
Console.ReadLine();
}
}
C#
Parse
Parse
<numeric_datatype>.Parse(<string_expression>)
Parse
<numeric_datatype> <string_expression>
Parse
39
2.2 (x,y)
( {2:f2}
{2} )
using System;
class Vector2D
{
static void Main()
{
double x, y, size;
Console.Write("Enter x: ");
x = double.Parse(Console.ReadLine());
Console.Write("Enter y: ");
y = double.Parse(Console.ReadLine());
size = Math.Sqrt(x*x + y*y);
Console.WriteLine("Size of the vector ({0},{1}) is {2:f2}.",
x, y, size);
Console.ReadLine();
}
}
40
1.
1.1
1.2 if ifelse
1.3 if
1.4 switch...case
2.
(condition)
C#
if if..else switch..case
(boolean
expressions) true ()
false ()
if (condition)
if statement
condition
if...else statement1 condition
statement2
if if ( if...else)
if
C# switch...case
if expression
case constant-expression
expression case
break
41
3.
3.1
3.1
3.2
3.3
3.4
3.5
()
10
15
15
30
30
3.2
- 3.1
- 3.2 if if...else
- 3.3 if
- 3.4 switch...case
3.3
-
-
4.
4.1
4.1.1 2
4.1.2 3.1 - 3.5 3.1 3.3
4.2
4.2.1 2
4.2.2
4.2.3 3.1
3.1
42
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
-
43
3.1
.
1. 2. .
3. 4. .
3.1
x, y z int,
c char
s string
x 2
x
x > 2
x%2 == 0
(x%5 == 0)
(x%y == 0)
((x%y == 0) && (z%y == 0))
ch == 'a'
((c >= 'a') && (c <= 'z'))
((c >= 'A') && (c <= 'Z'))
((c >= '0') && (c <= '9'))
c '0' '9'
(s != "Hello")
!(s != "Arthur")
44
3.2
.
1. 2. .
3. 4. .
3.2
(quadrant)
Y
Q2
Q1
X
Q3
Q4
() (x,y)
(quadrant) x y
I don't know.
using System;
class Quadrant {
static void Main() {
Console.Write("Enter X: ");
int x = int.Parse(Console.ReadLine());
Console.Write("Enter Y: ");
int y = int.Parse(Console.ReadLine());
if (___(a)___)
Console.WriteLine("({0},{1}) is in Q1.",
if (___(b)___)
Console.WriteLine("({0},{1}) is in Q2.",
if (___(c)___)
Console.WriteLine("({0},{1}) is in Q3.",
if (___(d)___)
Console.WriteLine("({0},{1}) is in Q4.",
if (___(e)___)
Console.WriteLine("I don't know.");
x, y);
x, y);
x, y);
x, y);
}
}
45
Please input X: -50
Please input Y: 10
(-50, 10) is in Q2.
Please input X: 0
Please input Y: 50
I don't know.
__(a)__
__(b)__
__(c)__
__(d)__
__(e)__
46
3.3
.
1. 2. .
3. 4. .
3.3
(BMI : Body mass index)
()
()
18.5
18.5 25
25 30
30
(Underweight)
(Normal)
(Overweight)
(Obese)
using System;
class BMICalc {
static void Main() {
Console.Write("Enter your weight (in kg): ");
double w = double.Parse(Console.ReadLine());
Console.Write("Enter your height (in m): ");
double h = double.Parse(Console.ReadLine());
double bmi = w/(h*h);
Console.WriteLine("Your BMI is {0:f2}.", bmi);
if (___(a)___)
Console.WriteLine("You are underweight.");
else if (___(b)___)
Console.WriteLine("You are normal.");
else if (___(c)___)
Console.WriteLine("You are overweight.");
else
Console.WriteLine("You are obese.");
}
}
47
Enter your weight: 65
Enter your height: 1.75
Your BMI is 21.22.
You are normal.
__(a)__
__(b)__
__(c)__
48
3.4
.
1. 2. .
3. 4. .
(Regular)
(Regular)
(Express)
(/)
2000
0.25
2000
0.35
50
4.5 .
49
50
3.5
.
1. 2. .
3. 4. .
1.
3.4
3.6 *,
y
/, ^ (x y) (x y) (x )
Enter
Enter
Enter
5.2^3
51
3.1
(boolean expressions)
true () false ()
C#
()
C#
==
x == y
!=
x != y
<
<
x < y
<=
>
>
>=
,
char, string
,
char, string
,
char
x <= y
,
char
x > y
,
char
x >= y
,
char
x y
x y
x y
x y
x y
x y
52
3.2
if if...else
if (condition)
1: if
statement
condition
if (condition)
statement; // executed if the condition is true
({...})
if (condition)
statement1;
statement2;
statement3;
:
}
{
// executed if the condition is true
// executed if the condition is true
// executed if the condition is true
2: if...else
statement1 condition
statement2
if (condition)
statement1; //executed if the condition is true
else
statement2; //executed if the condition is false
if...else
if (condition)
statementT1;
statementT2;
}
else {
statementF1;
statementF2;
}
{
//executed if the condition is true
//executed if the condition is true
53
3.1 (pseudo-code)
Passed 60 Failed
if student's score is greater than or equal to 60
Print "Passed"
otherwise
Print "Failed"
C# ()
if (score >= 60)
Console.WriteLine("Passed");
else
Console.WriteLine("Failed");
60
3.2 (even)
(odd)
if
using System;
class OddOrEven {
static void Main() {
int N;
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
if (N%2 == 0)
Console.WriteLine("{0} is even", N); //true
if (N%2 != 0)
Console.WriteLine("{0} is odd", N); //true
}
}
54
if...else
using System;
class OddOrEven {
static void Main() {
int N;
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
if (N%2 == 0)
Console.WriteLine("{0} is even", N); //true
else
Console.WriteLine("{0} is odd", N); //false
}
}
3.3
using System;
class Cellphone {
static void Main() {
// Step 1: Take the number of minutes input
Console.Write("Enter the number of minutes: ");
int minutes = int.Parse(Console.ReadLine());
// Step 2: Split the number of minutes into two parts,
// the first two and the remaining
int first, remaining;
if (minutes > 2) {
// Step 2.1: If the call takes more than two minutes,
// then the first two minutes is used entirely and the
// remaining is minutes subtracted by two
first = 2;
remaining = minutes - 2;
}
else {
// Step 2.2: If the call takes less than 2 minutes,
// these minutes are considered part of the first two
first = minutes;
remaining = 0;
}
// Step 3: Compute the fee based on the number of minutes
// during the first two minutes, and the number of minutes
// after the first two minutes
int fee = (first*5) + (remaining*2);
Console.WriteLine("The air time fee is {0} baht.", fee);
}
}
55
Enter the number of minutes: 1
The air time fee is 5 baht.
56
3.3
if
if ( if...else)
if
if (condition1)
statement1;
else if (condition2)
statement2;
else if (condition3)
statement3;
:
else
statementN;
statement1 condition1
statement2 condition2 else
if statement2 condition2
condition1 condition3
condition1 condition2 condition3
statementN
3.4
50
50 60
60 70
70 80
80
F
D
C
B
A
57
if
if (point < 50)
Console.WriteLine("Grade
else if (point < 60)
Console.WriteLine("Grade
else if (point < 70)
Console.WriteLine("Grade
else if (point < 80)
Console.WriteLine("Grade
else
Console.WriteLine("Grade
58
F");
D");
C");
B");
A");
3.4
switch...case
if C# switch...case
switch...case
switch (expression)
{
case constant-expression-1:
statements;
break;
case constant-expression-2:
statements;
break;
case constant-expression-3:
statements;
break;
:
default:
statements;
break;
}
C# expression (integer)
(char) (string) expression
case constant-expression
expression case
break
switch...case
constantexpression expression
default
59
3.5 (A,B,C,D,F)
(grade)
A
B
C
D
F
(grade point)
4.0
3.0
2.0
1.0
0.0
using System;
class GradePoint {
static void Main() {
string grade;
Console.Write("Please input your grade: ");
grade = Console.ReadLine();
switch (grade) {
case "A" : Console.WriteLine("Your point is 4.0."); break;
case "a" : Console.WriteLine("Your point is 4.0."); break;
case "B" : Console.WriteLine("Your point is 3.0."); break;
case "b" : Console.WriteLine("Your point is 3.0."); break;
case "C" : Console.WriteLine("Your point is 2.0."); break;
case "c" : Console.WriteLine("Your point is 2.0."); break;
case "D" : Console.WriteLine("Your point is 1.0."); break;
case "d" : Console.WriteLine("Your point is 1.0."); break;
case "F" : Console.WriteLine("Your point is 0.0."); break;
case "f" : Console.WriteLine("Your point is 0.0."); break;
default: Console.WriteLine("Invalid input!!"); break;
}
}
}
60
C# case
using System;
class GradePoint {
static void Main() {
string grade;
Console.Write("Please input your grade: ");
grade = Console.ReadLine();
switch (grade) {
case "A" :
case "a" : Console.WriteLine("Your point is 4.0."); break;
case "B" :
case "b" : Console.WriteLine("Your point is 3.0."); break;
case "C" :
case "c" : Console.WriteLine("Your point is 2.0."); break;
case "D" :
case "d" : Console.WriteLine("Your point is 1.0."); break;
case "F" :
case "f" : Console.WriteLine("Your point is 0.0."); break;
default: Console.WriteLine("Invalid input!!"); break;
}
}
}
Console.WriteLine
using System;
class GradePoint {
static void Main() {
string grade;
double point = -1;
Console.Write("Please input your grade: ");
grade = Console.ReadLine();
switch (grade) {
case "A" : case "a" : point = 4.0; break;
case "B" : case "b" : point = 3.0; break;
case "C" : case "c" : point = 2.0; break;
case "D" : case "d" : point = 1.0; break;
case "F" : case "f" : point = 0.0; break;
default: Console.WriteLine("Invalid input!!"); break;
}
if (point >= 0)
Console.WriteLine("Your point is {0:f1}.", point);
}
}
61
3.6
+
- +
1: using System;
2: class Calculator {
3:
static void Main() {
4:
double n1, n2, ans = 0;
5:
char op;
6:
Console.Write("Enter the first number: ");
7:
n1 = double.Parse(Console.ReadLine());
8:
Console.Write("Enter the second number: ");
9:
n2 = double.Parse(Console.ReadLine());
10:
Console.Write("Enter the operator: ");
11:
op = char.Parse(Console.ReadLine());
12:
switch(op) {
13:
case '+': ans = n1+n2; break;
14:
case '-': ans = n1-n2; break;
15:
default: op = ' '; break;
16:
}
17:
if (op == ' ')
18:
Console.WriteLine("Invalid operator!");
19:
else
20:
Console.WriteLine("{0}{1}{2} = {3}", n1, op, n2, ans);
21:
}
22: }
15 + -
op (' ')
62
1.
1.1
1.2 while
1.3 do..while
1.4 for
2.
while
) (
do..while
while do..while
C# (
(
for
63
3.
3.1
4.1
4.2
4.3
4.4
4.5
()
20
30
20
20
20
3.2
- 4 .1 While
- 4.2 dowhile
- 4.3 for
3.3
-
-
4.
4.1
4.1.1 2
4.1.2 4.1 - 4.5 4.1 4.3
4.2
4.2.1 While, dowhile for
4.2.2 4.1 While 4.1
4.2.3 4.1
4.2.4 4.2 dowhile 4.2
4.2.5 4.3 for 4.3
4.2.6
64
4.2.7 4.4
4.2.8 4.5
65
4.1
.
1. 2. .
3. 4. .
4.1
4.3
(
)
using System;
class While5 {
static void Main() {
int N = 0, sum;
___(a)___;
while (N >= 0) { //Exit while loop when N is negative
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
____________(b)____________;
}
____________(c)____________;
Console.WriteLine("Bye Bye!!!");
}
}
Please input
Please input
Please input
Please input
Please input
Sum = 604
Bye Bye!!!
N:
N:
N:
N:
N:
3
2
599
0
-5
__(a)__
__(b)__
__(c)__
66
4.2
.
1. 2. .
3. 4. .
4.1 4.2
1.
C#
-1
()
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Number of students is 6
Average score is 77.23
quit:
quit:
quit:
quit:
quit:
quit:
quit:
76
56.7
87.4
53.5
90.8
99
-1
67
2.
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Enter a score, or -1 to
Number of students is 6
Average score is 77.23
Min score is 53.50
Max score is 99.00
68
quit:
quit:
quit:
quit:
quit:
quit:
quit:
76
56.7
87.4
53.5
90.8
99
-1
69
4.3
.
1. 2. .
3. 4. .
4.3
1. 0, -1, -2, ..., -56 (
)
class ForEx {
static void Main() {
int k;
for ( __(a)__ ; __(b)__ ; __(c)__ ) {
____(d)____;
}
}
}
__(a)__
__(b)__
__(c)__
__(d)__
2. 1 (a),...,(d) 7, 14,
21,...,70
__(a)__
__(b)__
__(c)__
__(d)__
70
4.4
.
1. 2. .
3. 4. .
Enter N: 10
10 is not a prime
Enter N: 19
19 is a prime
Enter N: 1
1 is not a prime
71
72
4.5
.
1. 2. .
3. 4. .
(*)
:
)
73
4.1
while
while
() while
while (condition)
statement;
if
({...})
while (condition) {
statement1;
statement2;
:
statementN;
}
4.1 1 N N
1: using System;
2: class OneToN {
3:
static void Main() {
4:
int i, N;
5:
Console.Write("Enter N: ");
6:
N = int.Parse(Console.ReadLine());
7:
i = 1; // initialize variable i to 1
8:
while (i <= N) {
9:
Console.WriteLine(i);
10:
i++;
11:
}
12:
}
13: }
Please input N: 5
1
2
3
4
5
N 6 1 i
while 9 10 i <=
74
N i i
1 ( i) N ( i
i <= N )
4.2 1 N
4.1
using System;
class Summing {
static void Main() {
int N, i, sum;
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
i = 1; sum = 0;
while (i <= N) {
sum = sum + i;
i++;
}
Console.WriteLine("Sum from 1 to {0} = {1} ", N, sum);
}
}
Please input N: 4
1
2
3
4
Sum from 1 to 4 = 10
(counter)
(counting loop)
()
(sentinel loop)
75
4.3
using System;
class While4 {
static void Main() {
int N = 0;
while (N >= 0) { // stop when N is negative
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
}
Console.WriteLine("Bye Bye!!!");
}
}
Please input
Please input
Please input
Please input
Please input
Bye Bye!!!
76
N:
N:
N:
N:
N:
3
2
3000
9999
-50
4.2
do...while
do..while
while do..while
do..while
do..while
do statement; while (condition);
do {
statement1;
statement2;
:
statementN;
} while (condition);
4.4 do..while
4.3
using System;
class DoWhile1 {
static void Main() {
int N = 0;
do {
Console.Write("Please input N: ");
N = int.Parse(Console.ReadLine());
} while (N >= 0);
Console.WriteLine("Bye Bye!!!");
}
}
77
4.3
for
while
do..while (counting loop)
while do..while
(
)
using System;
class CountDown {
static void Main() {
int i;
i = 10;
while (i >= 0) {
Console.WriteLine(i);
i--;
}
}
}
//
//
//
//
(1)
(3)
(2)
(4)
C# (
)
for
for (init_stmt; condition; update_stmt) {
statement1;
statement2;
:
statementN;
}
78
for
for
while do..while
4.5 1,2,3,...,20
using System;
class Counting {
static void Main() {
int i;
for (i = 1; i <= 20; i++)
Console.WriteLine(i);
}
}
79
4.6 N
N ( N )
using System;
class Divisors {
static void Main() {
int i, N;
Console.Write("Enter N: ");
N = int.Parse(Console.ReadLine());
for (i = 1; i <= N; i++) {
if (N%i == 0) Console.WriteLine(i);
}
}
}
Enter N: 100
1
2
4
5
10
20
25
50
100
80
1.
1.1
1.2
1.3
2.
(method)
5.1
5.2
5.3
5.4
5.5
5.6
()
20
20
20
20
30
30
81
3.2
- 5 .1
- 5.2
- 5.3
3.3
-
-
4.
4.1
4.1.1 2
4.1.2 5.1 5.6 5.1 5.3
4.2
4.2.1
4.2.2 5.1
5.1
4.2.3 5.1
4.2.4 5.2 5.2
5.3
4.2.6 5.6
4.2.7
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
82
7.
-
83
5.1
.
1. 2. .
3. 4. .
5.1
1. 5.1
1.1 PrintLongerLine
PrintLine 20 10
PrintLongerLine
1.3 Main
84
5.2
.
1. 2. .
3. 4. .
5.1 5.2
1. 5.2
1.1
xxxxxxxxxx
********************
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
85
5.3
.
1. 2. .
3. 4. .
5.1 5.2
PlotChar
c char dist int
PrintCharLine PlotChar dist-1
c
4--7
Main
1: using System;
2: class Stars {
3:
static void PlotChar(char c, int dist) {
4:
________________________________________________________
5:
________________________________________________________
6:
________________________________________________________
7:
________________________________________________________
8:
}
9:
10:
static void Main() {
11:
PlotChar('x', 1);
12:
PlotChar('-', 2);
13:
PlotChar('+', 3);
14:
PlotChar('o', 6);
15:
}
16: }
x
+
o
86
5.4
.
1. 2. .
3. 4. .
5.3
5.3
CircleArea
using System;
class Circle {
_____(a)_____
static void Main() {
Console.Write("Enter radius: ");
double radius = double.Parse(Console.ReadLine());
Console.WriteLine("The area of the circle is {0:f2}.",
CircleArea(radius));
}
}
Enter radius: 32
The area of the circle is 3216.99.
CircleArea (a)
87
5.5
.
1. 2. .
3. 4. .
5.3
5.4 f n
f(n)
n
1
i =1 i
f ( n) =
n | f(n)
---+-----1 | 1.000
2 | 1.500
3 | 1.833
4 | 2.083
5 | 2.283
6 | 2.450
7 | 2.593
8 | 2.718
9 | 2.829
10 | 2.929
11 | 3.020
12 | 3.103
13 | 3.180
14 | 3.252
15 | 3.318
88
5.6
.
1. 2. .
3. 4. .
x
f ( x) = 20 + 10 sin
x 0 20 sin(x) x
x|
--+---------------------------------------> f(x)
0|
*
1|
*
2|
*
3|
*
4|
*
5|
*
6|
*
7|
*
8|
*
9|
*
10|
*
11|
*
12|
*
13|
*
14|
*
15|
*
16|
*
17|
*
18|
*
19|
*
20|
*
89
Math.Sin C#
Math.Round
double int
type casting (int)
int a = (int) Math.Round(10.3293);
90
5.1
Main
(subroutine)
(subprogram) (function)
C# (method)
:
Main
Console.WriteLine Math.Sqrt
C#
1.
C# Main
C#
91
namespace ()
class
Main
statement;
...
Main Main
namespace ()
class
Main
statement;
...
MyMethod_1
statement;
...
MyMethod_2
statement;
...
...
MyMethod_N
statement;
...
Main
92
Main
(non-value returning
methods) (value-returning methods)
2.
( subroutine procedure)
void return_type
10--11
ClassName.MethodName ClassName MyClass
( 2) PrintLine
C#
Console.WriteLine WriteLine Console
93
C# Main
94
5.2
(argument)
(parameter) parameter_list (
)
1: using System;
2: class ParamNoRet {
3:
static void PrintCharLine(char c, int len) {
4:
for (int i = 0; i < len; i++)
5:
Console.Write(c);
6:
Console.WriteLine();
7:
}
8:
9:
static void Main() {
10:
PrintCharLine(o, 10);
11:
PrintCharLine(x, 20);
12:
}
13: }
PrintCharLine Main
10 'o' 10 PrintCharLine
c len 4 5
o 10 PrintCharLine
11 x 20
95
5.3
Console.ReadLine
int.Parse
return_type void
return
return
return expression;
expression
true
false
( 0
1 2 )
(function)
Math
96
5.3 TriangleArea
Main
TriangleArea
1: using System;
2: class Triangle {
3:
static double TriangleArea(double w, double h) {
4:
double area = w*h/2.0;
5:
return area;
6:
}
7:
8:
static void Main() {
9:
Console.Write("Enter width: ");
10:
double width = double.Parse(Console.ReadLine());
11:
Console.Write("Enter height: ");
12:
double height = double.Parse(Console.ReadLine());
13:
Console.WriteLine("The area of the triangle is {0:f2}.",
14:
TriangleArea(width, height));
15:
}
16: }
TriangleArea double
3 double
Console.WriteLine 13-14
5.4
n
f ( n) = i
i =1
Main
n 1 15
f (n)
1: using System;
2: class Function {
3:
static double f(int n) {
4:
double sum = 0.0;
5:
for (int i = 1; i <= n; i++)
6:
sum += Math.Sqrt(i);
7:
8:
return sum;
9:
}
10:
11:
static void Main() {
12:
Console.WriteLine(" n | f(n)");
13:
Console.WriteLine("---+------");
14:
for (int n = 1; n <= 15; n++) {
15:
Console.WriteLine("{0,2} | {1:f3}", n, f(n));
16:
}
17:
}
18: }
97
n | f(n)
---+-----1 | 1.000
2 | 2.414
3 | 4.146
4 | 6.146
5 | 8.382
6 | 10.832
7 | 13.478
8 | 16.306
9 | 19.306
10 | 22.468
11 | 25.785
12 | 29.249
13 | 32.855
14 | 36.596
15 | 40.469
: {0,2} Console.WriteLine 15
( n) n
3 ' 3' (
') n 15 '15'
15
98
1.
1.1
1.2
1.3 foreach
1.4
2.
(arrays)
(elements)
[ ] x[0] y[0]
(index) (0) 1
n n-1
3.
3.1
6.1
6.2
6.3
6.4
6.5
6.6
3.2
- 6.1
- 6.2
- 6.3 foreach
()
10
5
10
10
20
20
99
- 6.4
- 6.5
3.3
4.
4.1
4.1.1 2
4.1.2 6.1 6.6 6.1 6.5
4.2
4.2.1
4.2.2 6.1 6.1
100
6.1
.
1. 2. .
3. 4. .
5.1
1. stdScores
double 6 78.0, 51.2, 25.1, 62.6,
27.6 18.0
2
using System;
class ScoreSum {
static void Main() {
double[] stdScores =
____________________________________________________;
double sum = 0.0;
for (int i=0; i<6; i++) {
___________________________;
}
Console.WriteLine("Sum = {0:f2}", sum);
}
}
101
6.2
.
1. 2. .
3. 4. .
1. 6.2
2. 6.2
Length 6
102
6.3
.
1. 2. .
3. 4. .
6.3 6.4 6.4
6.7 6.4
ArrayAverage
double
(: ArraySum )
using System;
class ArrayTest {
static double ArraySum(double[] data) {
double sum = 0;
foreach (double x in data)
sum += x;
return sum;
}
static double ArrayAverage(double[] data) {
____________________________________________________________
____________________________________________________________
}
static void Main() {
double[] myData;
int n;
Console.Write("Enter number of items: ");
n = int.Parse(Console.ReadLine());
myData = new double[n];
for (int i = 0; i < n; i++) {
Console.Write("Enter item#{0}: ", i+1);
myData[i] = double.Parse(Console.ReadLine());
}
Console.WriteLine("Average = {0:f2}", ArrayAverage(myData));
}
}
103
Enter number of items: 4
Enter item#1: 10.5
Enter item#2: 67
Enter item#3: 3
Enter item#4: 0.6
Average = 20.28
104
6.4
.
1. 2. .
3. 4. .
6.5
('
0'.. '9') ('A'..'Z')
('a'..'z')
using System;
class CountAll {
static void Main() {
Console.Write("Enter a string: ");
string s = Console.ReadLine();
int cntDigit = 0, cntUpper = ______, cntLower = ______;
foreach (char c in s) {
if (c >= '0' && __________)
cntDigit++;
if (_____________ && c <= 'Z')
cntUpper++;
if (_______________________________)
_________________________;
}
Console.WriteLine("There are {0} digits.", cntDigit);
Console.WriteLine("There are {0} uppercase letters.",
cntUpper);
Console.WriteLine("There are {0} lowercase letters.",
cntLower);
}
}
Enter
There
There
There
105
6.5
.
1. 2. .
3. 4. .
(
)
'R' 'E'
'W' 'R'
'E' 'W'
C#
Enter text: SNOPAWR WEOM DWWN
NEED MORE WEAPONS
106
LUFITUAWB SI WFIL
GNIMMAEGOEP WVOL I
107
6.6
.
1. 2. .
3. 4. .
How many students? 6
Student#1 score: 25
Student#2 score: 39
Student#3 score: 48
Student#4 score: 12
Student#5 score: 20
Student#6 score: 30
ID| Score
--+-----------------------------------------------1|*************************(25)
2|***************************************(39)
3|************************************************(48)
4|************(12)
5|********************(20)
6|******************************(30)
108
109
6.1
C#
(array)
1.
DataType[] ArrayName;
ArrayName DataType
[]
ArrayName DataType[]
DataType
6.1 score, grade
Name
int[] score;
// score refers to an array of integers
char[] grade; // grade refers to an array of characters
string[] name; // Name refers to an array of strings
110
int s;
char c;
string n;
// s refers to an integer
// c refers to a character
// n refers to a string
2.
new
new DataType[num_elements]
DataType num_elements
new
new
ArrayName
6.2
5 scores
int[] scores;
scores = new int[5];
10 names
string[] names;
names = new string[10];
111
new num_elements
value0 value1
C#
ArrayName = new DataType[] {value0, value1, ..., valueN-1};
num_elements
new
DataType[] ArrayName = {value0, value1, ..., valueN-1};
3.
(index)
0
1 N 0 N-1
C# idx ArrayName
ArrayName[idx]
112
6.4 scores 5
52
scores[0] = 52;
Console.WriteLine(scores[4]);
3
for (int i = 0; i < 5; i++)
scores[i] = 3;
int sum = 0;
for (int i = 0; i < 5; i++)
sum = sum + scores[i];
for (int i = 0; i < 5; i++)
Console.WriteLine("Student {0}: {1}", i+1, scores[i]);
113
6.2
6.1
for
(property) C#
Length
ArrayName.Length
ArrayName Length
ArrayName
6.5 5
weights
using System;
class AverageWeight {
static void Main() {
double[] weights = {65.5, 44.8, 70.0, 54.2, 77.6};
double sum = 0.0;
for (int i = 0; i < weights.Length; i++)
sum += weights[i];
Console.WriteLine("Average weight is {0:f2}",
sum/weights.Length);
}
}
114
6.3
foreach
C# foreach
foreach (DataType var in ArrayName)
statement;
DataType var
ArrayName statement
6.6 6.5
foreach for 6 7
1: using System;
2: class AverageWeight {
3:
static void Main() {
4:
double[] weights = {65.5, 44.8, 70.0, 54.2, 77.6};
5:
double sum = 0.0;
6:
foreach (double x in weights)
7:
sum += x;
8:
Console.WriteLine("Average weight is {0:f2}",
9:
sum/weights.Length);
10:
}
11: }
115
6.4
6.7 ArraySum
double
double[] 3 foreach
1: using System;
2: class ArrayTest {
3:
static double ArraySum(double[] data) {
4:
double sum = 0.0;
5:
foreach (double x in data)
6:
sum += x;
7:
return sum;
8:
}
9:
10:
static void Main() {
11:
double[] myData = {1.0, 2.4, 3.6, 4.8};
12:
Console.WriteLine("Sum = {0}", ArraySum(myData));
13:
}
14: }
116
6.5
C# (string)
[]
foreach Length
6.8 'E'
using System;
class CountE {
static void Main() {
Console.Write("Enter a string: ");
string s = Console.ReadLine();
int count = 0;
for (int i = 0; i < s.Length; i++) {
if (s[i] == 'E') count++;
}
Console.WriteLine("There are {0} E's in the string", count);
}
}
6.9
foreach for
using System;
class CountE {
static void Main() {
Console.Write("Enter a string: ");
string s = Console.ReadLine();
int count = 0;
foreach (char c in s) {
if (c == 'E') count++;
}
Console.WriteLine("There are {0} E's in the string", count);
}
}
117
1.
1.1
1.2
1.3
2.
(multi-dimensional array)
0
( ( C#
GetLength
3.
3.1
7.1
7.2
7.3
7.4
7.5
3.2
118
()
20
20
20
30
30
- 7 .1
- 7.2
3.3
-
-
4.
4.1
4.1.1 2
4.1.2 7.1 7.5 7.1 7.2
4.2
4.2.1
4.2.2 7.1 7.2
7.1 7.2
7.3
4.2.3 7.1-7.3
4.2.4 7.4 7.5
4.2.5
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
-
119
7.1
.
1. 2. .
3. 4. .
7.1 7.2
ShowMatrix
using System;
class Matrix {
static void ShowMatrix(int[,] m) {
for (int i = 0; i < ___________________; i++) {
for (int j = 0; j < ___________________; j++) {
Console.Write("{0,4}", _________________);
}
Console.WriteLine();
}
}
static void Main() {
int[,] A = {
{ 5, 3, 8},
{ 2, 6, 10},
{ 1, 8, 25},
{12, 3, 30}
};
ShowMatrix(A);
}
}
5
2
1
12
120
3
6
8
3
8
10
25
30
7.2
.
1. 2. .
3. 4. .
7.2
ReadMatrix
ShowMatrix 7.1
using System;
class Matrix {
// ShowMatrix
//
static int[,] ReadMatrix(int nrows, int ncols) {
int[,] m = new int[_______,_______];
for (int i = 0; i < nrows; i++) {
for (int j = 0; j < ncols; j++) {
Console.Write("Enter element[{0},{1}]: ", i+1, j+1);
_______________ = int.Parse(Console.ReadLine());
}
}
return m;
}
static void Main() {
int num_rows, num_cols;
int[,] A;
Console.Write("How many rows? ");
num_rows = int.Parse(Console.ReadLine());
Console.Write("How many columns? ");
num_cols = int.Parse(Console.ReadLine());
A = ReadMatrix(num_rows, num_cols);
Console.WriteLine("Matrix A is");
ShowMatrix(A);
}
}
121
How many rows? 2
How many columns? 3
Enter element[1,1]:
Enter element[1,2]:
Enter element[1,3]:
Enter element[2,1]:
Enter element[2,2]:
Enter element[2,3]:
Matrix A is
9
8
7
6
5
4
122
9
8
7
6
5
4
7.3
.
1. 2. .
3. 4. .
7.2
TransposeMatrix
( )
using System;
class Matrix {
// ShowMatrix ReadMatrix
//
static int[,] TransposeMatrix(int[,] m) {
int[,] mt = new int[____________________,____________________];
for (int i = 0; i < m.GetLength(0); i++)
for (int j = 0; j < m.GetLength(1); j++)
______________________________________________________;
return mt;
}
static void Main() {
int num_rows, num_cols;
int[,] A, At;
Console.Write("How many rows? ");
num_rows = int.Parse(Console.ReadLine());
Console.Write("How many columns? ");
num_cols = int.Parse(Console.ReadLine());
A = ReadMatrix(num_rows, num_cols);
Console.WriteLine("Matrix A is");
ShowMatrix(A);
Console.WriteLine("Transpose of Matrix A is");
At = TransposeMatrix(A);
ShowMatrix(At);
}
}
123
How many rows? 2
How many columns? 3
Enter element[1,1]:
Enter element[1,2]:
Enter element[1,3]:
Enter element[2,1]:
Enter element[2,2]:
Enter element[2,3]:
Matrix A is
1
2
3
4
5
6
Transpose of Matrix
1
4
2
5
3
6
124
1
2
3
4
5
6
A is
7.4
.
1. 2. .
3. 4. .
(determinant) 22
det(A) = (a11a22 a12a21)
22
Enter matrix A
Enter element[1,1]: 1
Enter element[1,2]: 2
Enter element[2,1]: 3
Enter element[2,2]: 4
The determenant of A is -2
ShowMatrix /
ReadMatrix
125
7.5
.
1. 2. .
3. 4. .
Amn
Bn p C m p
n
22
Enter
Enter
Enter
Enter
Enter
matrix A
element[1,1]:
element[1,2]:
element[2,1]:
element[2,2]:
1
2
3
4
Enter
Enter
Enter
Enter
Enter
matrix B
element[1,1]:
element[1,2]:
element[2,1]:
element[2,2]:
5
6
7
8
Matrix A*B is
19 22
43 50
126
ShowMatrix
ReadMatrix
127
7.1
C#
(multi-dimensional array)
2
57
(1,3) 10 (2,4) 21
1.
ArrayName DataType
new
new DataType[nrows,ncols]
128
new DataType
nrows ncols new
7.1 students
(,) 5 3
string[,] students;
students = new string[5,3];
2.
new
DataType[,] ArrayName
{ value(0,0), value
{ value(1,0), value
:
:
{ value(nrows-1,0),
};
= {
(0,1), ..., value(0,ncols-1) },
(1,1), ..., value(1,ncols-1) },
:
value(nrows-1,1), ..., value(nrows-1,ncols-1) }
129
7.2 A 43
int[,]
{ 5,
{ 2,
{ 1,
{12,
}
A = {
3, 8},
6, 10},
8, 25},
3, 30}
3.
0
ri ci ArrayName
ArrayName[ri,ci]
7.3 A 7.2
A[3,2] ( a3, 2 (
Console.WriteLine(A[2,1]);
: A[2,1] A[3,2]
C# 0
1
33 a 4,3
A[3,2] = 33;
130
7.2
Length
(
) C# GetLength
ArrayName.GetLength(dim_idx)
dim_idx 0
1
ArrayName.GetLength(0)
ArrayName.GetLength(1)
7.4 A 7.2
A 1
for (int i = 0; i < A.GetLength(0); i++)
A[i,0] = 1;
A 5
for (int i = 0; i < A.GetLength(1); i++)
A[1,i] = 5;
A
for (int i = 0; i < A.GetLength(0); i++)
for (int j = 0; i < A.GetLength(1); j++)
A[i,j] = 0;
131
1. 2
1
5 6
3 4
5 6 4
+
3 2
1 3
2.
3. 1
132
1.
1.1
1.2 Windows Application MS Visual C#
2.
(object)
(button)
3.
3.1
8.1
8.2
()
30
30
3.2
- 8 .1
- 8.2 Windows
3.3
-
-
133
4.
4.1
4.1.1 2
4.1.2 8.1 8.2 8.1 8.2
4.2
4.2.1
4.2.2 8.1 8.1
4.2.3 8.1
4.2.4 8.2 Windows
8.2
4.2.5
Windows
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
-
134
8.1
.
1. 2. .
3. 4. .
8.1
135
8.2
.
1. 2. .
3. 4. .
8.2
ADD
1.
1.1
1.2 ADD
2.
136
1 ..
2 ..
3 ..
4 ..
Parse
...................................
.
.
137
8.1
2 in-house development
industry development
in-house
138
8.1
2
1. (Function)
2. (Object oriented)
Algorithmic decomposition
C++ , C# , SmallTalk
139
(Paradigms)
(Functional)
Pascal
(Object Oriented)
Java
C++
140
C#
(message)
8.2
(message) 8.1
8.1
1 3
1. State
2. Behavior
3. Identify
State
(Stage)
Behavior
(State)
Identity
8.3
(class) (Abstract)
141
(instance)
A
5
B
10
8.2
2B
142
A
r = 20
(x,y) = (5,5)
(T)
Circle
(r)
(x,y)
(T,F)
B
r = 15
(x,y) = (2,3)
(F)
A B A
B
C#
(.)
. [. .]
143
C# Console
WriteLine()
ReadLine()
System.Console.WriteLine(Hello World! );
System.Console.ReadLine();
C#
(.)
144
8.2
Windows
C# (Object-Oriented Programming Language)
.cs prog.cs (compiler)
.exe executable
MS Visual Studio 2008
MS Visual C#
Windows
1. Microsoft Visual C# 2008 Create Project.. 8.3
145
146
8.6 Form1.cs[Design]
4.
4.1 Save All 8.7
147
Forms Designer
Output window
Solution Explorer
8.9 Design
148
2. (Toolbox)
X Toolbox Toolbox
Y Auto Hide
8.10
Z
8.11
149
8.12
3. Toolbox
3.1 Button X
Form1 Y 8.13
Y
8.13
150
3.2 - Toolbox
8.14
8.14 -
4.
Visual Programming
4.1 button1
8.15
151
4.2 Form1.cs X
Y 8.16
8.16 Form1.cs
button1
Close(); button1_Click() 8.17
8.17
152
Form1.cs[Design] 8.18
8.18 Form1.cs[Design]
5.
5.1 Start Debugging F5 8.19
153
5.2 8.20
8.20
Properties
Properties
1. button2 8.21
8.21
154
2. Properties
2.1 View -> Properties Window 8.22
8.23 Properties
155
Z
8.24 Properties
156
Message Box
Show Message Message Box
Hello My First Windows Program 8.25
8.25
X
8.26 Properties button2
157
1.
Windows Application MS Visual C#
2.
MS
Visual C# (Windows
Application)
3.
3.1
9.1
Windows Application
()
360
3.2
3.3
-
-
-
-
4.
4.1
4.1.1 4
4.1.2 9.1
4.2
159
4.2.1
4.2.2
9.1
4.2.3 ()
4.2.4
4.2.5 3
4.2.6
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
-
160
9.1
Windows Application
.
1. 2. .
3. 4. .
MS Visual C#
..
..
..
..
..
..
..
..
..
..
..
..
..
161
.........................
. /
.
.
.
.
1
2
3
4
5
6
7
8
9
10
162
B
4
C
3
D
2
E
1
F
0
163
1
2. Microsoft Visual C# 2008 Express Edition 2
X Y Z
\
164
Console Application
Console Application
165
2. New Project 4
X Console Application
o Name
Z OK
X
Z
4 New Project
3. Program.cs
3.1 Editor Program.cs
C#
3.2
3.3 Solution Explorer
166
5 Program.cs
4.
4.1 Tool > Options
167
4.2 Options 7
7 Options
4.3 Show all settings
168
o
9 Line numbers
4.5 Editor 10
10
169
5.
MS Visual C#
5.1 Save All
5.2 Save Project
5.3 Location Browse
X
o
11
5.4 Project Location OK
12
12 Project Location
170
13
5.6
14 consolePrj1
171
6.
6.1
6.2
6.3
6.4 Debug
o
q
p
15
7. Editor
MS Visual C# .cs Default
Program.cs C#
n using namespace class
o namespace class
class Program
p class Program
namespace class class
Main
q Main
Main
172
n
Y
p
[
16 Editor
17
173
8.
Start Debugging F5 18
18
19
19
174
9.
MS Visual C# .exe
20
20 .exe
10.
Error List
10.1 view -> Error List
175
22 Error List
23
176
10
1.
1.1.
1.2.
1.3.
2.
C# (structure)
3.
3.1
10.1
10.2
10.3
()
30
30
60
3.2
- 10.1
- 10.2
3.3
-
-
4.
4.1
177
4.1.1 2
4.1.2 10.1-10.3 10.1-10.2
4.2
4.2.1
4.2.2 10.1
10.1 10.2
4.2.3 10.1 10.2
4.2.4 10.2
10.3
4.2.5
5.
5.1
6.
6.1 http://msdn.microsoft.com/th-th/vcsharp/default(en-us).aspx
7.
-
178
10.1
.
1. 2. .
3. 4. .
10.1
1.
1.1 VehicleInfo
o
o
o
o
make
plate
color
year
struct ______________________ {
_________________________________________;
_________________________________________;
_________________________________________;
_________________________________________;
}
1.2
ReadVector
Vector PrintVector
Vector
using System;
class VectorEx {
struct Vector {
public double x,y,z;
}
static Vector ReadVector() {
Vector v;
Console.Write("X element: ");
__________ = double.Parse(Console.ReadLine());
Console.Write("Y element: ");
179
_____________________________________________________;
_____________________________________________________;
_____________________________________________________;
______________________________;
}
static void PrintVector(Vector v) {
Console.Write("({0},{1},{2})",
________, ________, ________);
}
static void Main() {
Console.WriteLine("Enter a vector");
Vector a = ReadVector();
Console.Write("You justed enter a vector ");
PrintVector(a);
Console.WriteLine();
}
}
Enter a vector
X element: 6
Y element: 13
Z element: 1
You just entered a vector (6,13,1)
180
10.2
.
1. 2. .
3. 4. .
10.1
1.
ReadVector Vector
PrintVector Vector
using System;
class VectorEx {
struct Vector {
public double x,y,z;
}
static Vector ReadVector() {
Vector v;
Console.Write("X element: ");
__________ = double.Parse(Console.ReadLine());
Console.Write("Y element: ");
_____________________________________________________;
_____________________________________________________;
_____________________________________________________;
______________________________;
}
static void PrintVector(Vector v) {
Console.Write("({0},{1},{2})",
________, ________, ________);
}
static void Main() {
Console.WriteLine("Enter a vector");
Vector a = ReadVector();
Console.Write("You justed enter a vector ");
PrintVector(a);
Console.WriteLine();
}
}
181
Enter a vector
X element: 6
Y element: 13
Z element: 1
You just entered a vector (6,13,1)
2.
v = (vx,vy,vz) v |v|
v = v x2 + v y2 + v z2
VectorSize
ReadVector PrintVector
( Vector
) VectorSize
using System;
class VectorEx {
// Vector
// PrintVector ReadVector
//
static double VectorSize(Vector v) {
return __________________________________________________;
}
static void Main() {
Console.WriteLine("Enter a vector");
Vector v = ReadVector();
Console.Write("The size of the vector ");
PrintVector(v);
Console.WriteLine(" is {0}", VectorSize(v));
}
}
Enter a vector
X element: 3
Y element: 7
Z element: -2
The size of the vector (3,7,-2) is 7.87400787401181
182
3.
u = (ux,uy,uz) v = (vx,vy,vz) (dot product) u v
uv
uv = uxvx+ uyvy+ uzvz
using System;
class VectorEx {
// Vector
// ReadVector
static double DotVectors(Vector u, Vector v) {
_______________________________________________;
}
static void Main() {
Console.WriteLine("Enter vector u");
Vector u = ReadVector();
___________________________________________;
___________________________________________;
Console.WriteLine("u * v = {0}", DotVectors(u,v));
}
}
Enter vector u
X element: -1.2
Y element: 3
Z element: 0.5
Enter vector v
X element: 4
Y element: 2
Z element: 1.8
u * v = 2.1
183
10.3
.
1. 2. .
3. 4. .
10.2
1 999
-
-1
184
185
10.1
C#
(structure)
1.
(member)
C#
struct ( structure)
struct StructName {
public DataType1 var1;
public DataType2 var2;
:
public DataTypeN varN;
}
StructName N
DataType1 var1
DataType2 var2 public
(protection level)
186
10.1 StdInfo
id
name
double score
struct StdInfo {
public int id;
public string name;
public double score;
}
class MyClass {
struct StdInfo {
public int id;
public string name;
public double score;
}
static void Main() {
:
}
}
2.
struct
StructName structVar;
StructName structVar
struct
187
(.)
structVar.memberName
10.2 Vector
x y z double
Vector
(3,4,5)
1: using System;
2: class VectorEx {
3:
struct Vector {
4:
public double x;
5:
public double y;
6:
public double z;
7:
}
8:
9:
static void Main() {
10:
Vector v1;
11:
v1.x = 3; v1.y = 4; v1.z = 5;
12:
Console.WriteLine("Vector v1 = ({0},{1},{2})",
13:
v1.x, v1.y, v1.z);
14:
}
15: }
Vector double
C#
3-7
struct Vector {
public double x,y,z;
}
188
10.2
students StdInfo
10.1
StdInfo[] students;
new
students = new StdInfo[30];
StdInfo 30 students
%
students[5]
5 ( 6 )
students[5].name
189
StdInfo students[5].name
students[5].name = "Arthur";
Console.WriteLine("The student's name is {0}", students[5].name);
190
1. .
2. .
3. .
4.
5.
6.
7.
8.
9.
10.
11.
12.
.
.
.
.
.
.
.
191