Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading.Tasks;
- namespace Nawiasy
- {
- class Program
- {
- static void Main(string[] args)
- {
- Console.WriteLine("Podaj ilość znaków nawiasowych:");
- int ilosc = Convert.ToInt32(Console.ReadLine());
- Console.WriteLine("Wpisz zdanie nawiasowe");
- char[] zdanienawiasowe;
- for(; ;)
- {
- string zdanie = Console.ReadLine();
- if(Regex.IsMatch(zdanie,@"[(]|[)]"))
- {
- if(zdanie.Length==ilosc)
- {
- zdanienawiasowe = zdanie.ToArray();
- break;
- }
- }
- Console.WriteLine("Złe zdanie nawiasowe, spróbuj ponownie.");
- }
- int i = 0, sumawyjsciowa=0;
- for(; ;)
- {
- if(zdanienawiasowe[i]==')')
- {
- for(int j=i;j<zdanienawiasowe.Length-1;j++)
- {
- if(zdanienawiasowe[j+1]=='(')
- {
- sumawyjsciowa++;
- zdanienawiasowe[i] = '0';
- zdanienawiasowe[j + 1] = '0';
- break;
- }
- }
- }
- else if (zdanienawiasowe[i] == '(')
- {
- for (int j = i; j < zdanienawiasowe.Length - 1; j++)
- {
- if (zdanienawiasowe[j + 1] == ')')
- {
- sumawyjsciowa++;
- zdanienawiasowe[i] = '0';
- zdanienawiasowe[j + 1] = '0';
- break;
- }
- }
- }
- i++;
- if(i==zdanienawiasowe.Length)
- {
- break;
- }
- }
- Console.WriteLine("Wynik: "+sumawyjsciowa);
- Console.Read();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement