forked from 11.IB/for_loop
My solution
parent
d1e40ee119
commit
7ac0a5884e
44
Program.cs
44
Program.cs
|
@ -1,18 +1,60 @@
|
|||
using System;
|
||||
|
||||
class Program {
|
||||
static void Main(string[] args) {
|
||||
static int Main(string[] args) {
|
||||
// 1.
|
||||
{
|
||||
Console.Write("E1: Enter string [+-0]: ");
|
||||
string str = Console.ReadLine();
|
||||
int x = 1;
|
||||
foreach (char c in str) switch (c) {
|
||||
case '+': ++x; break;
|
||||
case '-': --x; break;
|
||||
case '0': x = 0; break;
|
||||
default:
|
||||
Console.WriteLine("Invalid character: '"+c+"'!");
|
||||
return 1;
|
||||
}
|
||||
Console.WriteLine(x);
|
||||
}
|
||||
// 2.
|
||||
{
|
||||
Console.Write("E2: Enter string [+-0!]: ");
|
||||
string str = Console.ReadLine();
|
||||
int x = 1;
|
||||
foreach (char c in str) switch (c) {
|
||||
case '+': ++x; break;
|
||||
case '-': --x; break;
|
||||
case '0': x = 0; break;
|
||||
case '!':
|
||||
if (x == 0) break;
|
||||
int s = x < 0 ? 1 : -1;
|
||||
for (int i = x+s; i != 0; i += s) x *= i;
|
||||
break;
|
||||
default:
|
||||
Console.WriteLine("Invalid character: '"+c+"'!");
|
||||
return 1;
|
||||
}
|
||||
Console.WriteLine(x);
|
||||
}
|
||||
// 3.
|
||||
{
|
||||
Console.Write("E3: Enter a word: ");
|
||||
string str = Console.ReadLine();
|
||||
for (int i = 0; i < str.Length; i += 2)
|
||||
Console.Write(str[i]);
|
||||
Console.WriteLine();
|
||||
}
|
||||
// 4.
|
||||
{
|
||||
Console.Write("E4: Enter words: ");
|
||||
string str = Console.ReadLine();
|
||||
string[] split = str.Split(' ');
|
||||
for (int i = 0; i < split.Length; i += 2)
|
||||
Console.Write(split[i] + ' ');
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue