Programozás‎ > ‎Feladatok‎ > ‎Telefonszámok‎ > ‎Megoldás‎ > ‎

og_phone.cpp

Letöltés: og_phone.cpp

#include <stdio.h>
#include <iostream>
#include <string>
 
using namespace std;
 
void beolvas(char*);
void atir();
void keres(int, char*,int*, int, int);
bool egyezik(int,int);
 
 
long long sbe;
char* s;
int sl;
 
int n;
 
char** b;
int* bl;
char** a;
 
char c;
 
int main()
{
    beolvas("PHONE.I4");
    atir();
    int f[100];
 
    keres(0,"", f,0,-1);
 //   cout <<"\n" <<egyezik(3,0);
}
 
void beolvas(char* file)
{
    FILE* be = fopen(file, "r");
    s = new char[101];
    for(int i=0; i<101; i++)
    {
        fscanf(be, "%c", &s[i]);
        if (s[i]=='\n') {sl=i-1; break;}
 
    //    cout << s[i];
 
    }
    fscanf(be, "%d", &n);
   // cout << "\n" <<sl <<"\n"<<n<<"\n";
 
    b=new char*[n];
    bl = new int[n];
    fscanf(be, "%c", &c);
    for (int i=0; i<n; i++)
    {
        b[i] = new char[51];
    //    cout << i<< "\n";
        for(int j=0; j<101; j++)
        {
            fscanf(be, "%c", &b[i][j]);
 
            if ((b[i][j]=='\n') || (b[i][j]=='\r'))  {bl[i] = j; b[i][j]='\0'; break;}
 
        }
 
    }
 
    for(int i=0; i<n; i++)
    {
  //      cout << b[i] <<" " << bl[i] << "\n";
    }
 
    fclose(be);
}
 
 
void atir()
{
    a = new char*[n];
    for (int i=0; i<n; i++)
    {
     //   cout << i << "\n";
        a[i] = new char[bl[i]+1];
        for (int j=0 ; j<bl[i];j++)
        {
            if ((b[i][j]=='i') || (b[i][j]=='j')) {a[i][j]='1';}
            else if ((b[i][j]=='a') || (b[i][j]=='b') || (b[i][j]=='c')) {a[i][j]='2';}
            else if ((b[i][j]=='d') || (b[i][j]=='e') || (b[i][j]=='f')) {a[i][j]='3';}
            else if ((b[i][j]=='g') || (b[i][j]=='h'))  {a[i][j]='4';}
            else if ((b[i][j]=='k') || (b[i][j]=='l'))  {a[i][j]='5';}
            else if ((b[i][j]=='m') || (b[i][j]=='n'))  {a[i][j]='6';}
            else if ((b[i][j]=='p') || (b[i][j]=='r') || (b[i][j]=='s')) {a[i][j]='7';}
            else if ((b[i][j]=='t') || (b[i][j]=='u') || (b[i][j]=='v')) {a[i][j]='8';}
            else if ((b[i][j]=='w') || (b[i][j]=='x') || (b[i][j]=='y')) {a[i][j]='9';}
            else if ((b[i][j]=='o') || (b[i][j]=='q') || (b[i][j]=='z')) {a[i][j]='0';}
//            cout << a[i][j];
        }
        a[i][bl[i]]='\0';
      //  cout << "\n";
 //       cout << a[i] <<" " << bl[i] << "\n";
    }
}
 
 
void keres (int at, char* eddig, int* k,int kl,int last)
{
    if (last!=-1){
    k[kl]=last;
    kl++;
    }
 
    if (at==sl+1)
    {
          for(int i=0; i<kl; i++)
          {
              cout << b[k[i]] << "\n";
          }
          cout << "\n";
        return;
    }
   //cout << "Doing " << at <<"\n";
    for (int i=0; i<n; i++)
    {
        if (egyezik(i, at))
        {
            char str[at+bl[i]];
            for (int j=0; j<at; j++)
            {
                str[j]=s[j];
            }
            for (int j=0; j<bl[i]; j++)
            {
                str[j+at]=a[i][j];
            }
          //  cout << str;
 
            keres(at+bl[i], str, k ,kl,i);
        }
    }
 
}
 
bool egyezik(int i, int at)
{
 
    if(bl[i]>(sl-at+1)) {return false;}
    for(int j=0; j<bl[i]; j++)
    {
        if(a[i][j]!=s[at+j]) { return false;}
    }
 
    return true;
}