forked from 11.IB/for_loop
61 lines
1.4 KiB
C#
61 lines
1.4 KiB
C#
using System;
|
|
|
|
class Program {
|
|
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;
|
|
}
|
|
}
|