joi, 22 octombrie 2015

C# 6 Aplicatie la siruri de caractere



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CuvinteCareRimeaza
{
    class Program
    {
        /* se citeste un text, sa se scrie perechile de cuvintele care rimeaza (au ultimele 2 litere identice)
        apoi sa se scrie toate literele distincte si frecventele de aparitie
        metoda de rezolvare: intr-o unica parcurgere a sirului caracterele se numara intr-un tablou de structuri
        textul se reține intr-o variabila string apoi cuvintele se separa intr-un tablou de structuri
        care se parcurge apoi in 2 cicluri
        */
        struct caractere
        {
            public char caracter;
            public int contor;
        }
        struct cuvinte
        {
            public string cuvant;
            public bool folosit;
        }
        static caractere[] aparitii = new caractere[30];
        static cuvinte[] fraza = new cuvinte[300];
        static void Main(string[] args)
        {
            Console.Write("Introdu textul:");
            string text = Console.ReadLine(); //citeste text cu spatii pana la ENTER
            //intr-un singur ciclu numara caracterele distincte si separa cuvintele
            int l = text.Length;
            Console.WriteLine("lungime text={0}", l);
            int i = 0; // indice general in text
            int nrcar = 0; //contor in array aparitii
            int nrcuv = 0; //contor cuvinte in fraza
            int inceputCuvant = 0; //indice inceput cuvant
            bool spatii = true; //indicator ciclare spatii
            bool gasit = false; //indicator caracter gasit in vectorul aparitii
            while(i<l)
            {
                if (text[i] == ' ')
                {
                    if (!spatii)
                    {
                        spatii = true;
                        fraza[nrcuv].cuvant = text.Substring(inceputCuvant, i - inceputCuvant);
                        fraza[nrcuv].folosit = false;
                        nrcuv++;
                    }
                }
                else
                {
                    for (int k = 0; k <= nrcar; k++)
                    {
                        if (aparitii[k].caracter == text[i])
                        {
                            aparitii[k].contor++;
                            gasit = true;
                        }
                    }
                    if (!gasit)
                    {
                        aparitii[nrcar].caracter = text[i];
                        aparitii[nrcar].contor = 1;
                        nrcar++;
                    }
                    gasit = false;
                    if (spatii)
                    {
                        spatii = false;
                        inceputCuvant = i;
                    }
                }
                i++;
            }
            //trateaza ultima litera  (pentru  i=l-1)
            i--;
            for (int k = 0; k <= nrcar; k++)
            {
                if (aparitii[k].caracter == text[i])
                {
                    aparitii[k].contor++;
                    gasit = true;
                }
            }
            if (!gasit)
            {
                aparitii[nrcar].caracter = text[i];
                aparitii[nrcar].contor = 1;
                nrcar++;
            }
            i++;
            fraza[nrcuv].cuvant = text.Substring(inceputCuvant, i - inceputCuvant);
            fraza[nrcuv].folosit = false;
            nrcuv++;
            //afiseaza numarul de aparitii
            for (int j = 0; j <= nrcar-1; j++)
                Console.WriteLine("{0} de {1} ori", aparitii[j].caracter, aparitii[j].contor);
            //afiseaza cuvintele
            for (int j = 0; j < nrcuv; j++)
                Console.WriteLine(fraza[j].cuvant);
            //cauta cuvintele pereche in 2 cicluri imbricate
            Console.WriteLine("perechi de cuvinte care rimeaza");
            for(int u=0;u<=nrcuv-1;u++)
            {
                for(int v=u+1;v<nrcuv;v++)
                {
                    bool b1 = fraza[u].folosit;
                    bool b2 = fraza[v].folosit;
                    string s1 = fraza[u].cuvant;
                    string s2 = fraza[v].cuvant;
                    int lg1 = s1.Length;
                    int lg2 = s2.Length;
                    if (lg1>=2&&lg2>=2&&s1[lg1-2]==s2[lg2-2]&&s1[lg1-1]==s2[lg2-1]&&!b1&&!b2)
                    {
                        b1 = true;
                        b2 = true;
                        Console.WriteLine("{0} {1}",fraza[u].cuvant, fraza[v].cuvant);
                    }
                }
            }
            Console.ReadKey();
        }
    }
}

Niciun comentariu:

Trimiteți un comentariu