class AvgNums {
static void Main() { string str; int n;
double sum = 0.0; double avg, t;
Console.Write(" : "); str = Console.ReadLine (); try {
n = Int32.Parse(str);
} catch(FormatException exc) {
Console.WriteLine(exc.Message); return;
} catch(OverflowException exc) {
Console.WriteLine(exc.Message); return;
}
Console.WriteLine(" " + n + " ."); for (int i=0; i < n; i++) {
Console.Write(": "); str = Console.ReadLine (); try {
t = Double.Parse(str);
} catch(FormatException exc) {
Console.WriteLine(exc.Message); t = 0.0;
} catch(OverflowException exc) {
Console.WriteLine(exc.Message); t = 0;
}
sum += t;
}
avg = sum / n;
Console.WriteLine(" " + avg);
, , .
: 5 5 .
: 1.1: 2.2: 3.3: 4.4: 5.5
3.3
, . , , , Int32. Parse (), , .. , .
, Parse () . , TryParse (), . TryParseO, Int 32:
static bool TryParse(string s, out int )
s , , . ( .) , , s , TryParse () false. true. , , , , ( ) .
15 , -
#: , -. , , . , . , #. - , , , . - , -. ( , - LINQ, 19.) , , .
|
|
. , , . , , , .
, . , , . , .
, , , . , , , , . , , .
C/C++, , C# C/C++.
delegate. :
delegate _ (_);
_ , , ; ; _ , , . , , .
, . , , , , . : , .
, .
// .
Using System;
// , delegate string StrMod(string str);
class DelegateTest {
// .
static string ReplaceSpaces(string s) {
Console.WriteLine(" ."); return s.Replace(' ', '-');
}
// .
static string RemoveSpaces(string s) { string temp = ""; int i;
Console.WriteLine(" ."); for(i=0; i < s.Length; i++) if(s[i]!= ' ') temp += s[i];
Return temp;
}
// ^. static string Reverse(string s) { string temp = ""; int i, j;
Console.WriteLine(" . "); for(j=0, i=s.Length-1; i >= 0; i, j++) temp += s[i];
Return temp;
}
static void Main() {
// .
StrMod strOp = new StrMod(ReplaceSpaces); string str;
|
|
// , str = strOp(" .");
Console.WriteLine(" : " + str);
Console.WriteLine();
strOp = new StrMod(RemoveSpaces); str = strOp(" .");
Console.WriteLine(" : " + str);
Console.WriteLine();
strOp = new StrMod(Reverse); str = strOp(" .");
Console.WriteLine(" : " + str);
}
}