probleme rezolvate siruri de caractère

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
davidush
Junior
Junior
Posts: 7
Joined: 12 Jan 2016, 23:32
Judet: Iaşi

probleme rezolvate siruri de caractère

Post by davidush » 21 Feb 2016, 03:15

Code: Select all

//
//  main.cpp
//  siruri de caractere
//
//  Created by motas david on 26.11.2015.
//  Copyright © 2015 motas david. All rights reserved.
//

#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <iostream>
#include <cstring>
#include <fstream>

using namespace std;

ifstream fin("date.in");
ofstream fout("date.out");

#define MAX 256

//ex1

void vocandcons(ifstream &f, ofstream &o)
{
    char s[MAX];
    unsigned int lg;
    unsigned  int voc, cons;
    
    voc = 0;
    cons = 0;
    while (!f.eof())
    {
        f.getline(s, MAX);
        lg =strlen(s);
        for (unsigned int i = 0; i < lg; i++)
        {
            if (strchr("aeiouAEIOU", s[i]))
                voc++;
            else
                if ((s[i] <= 'z' && s[i] >= 'a') || (s[i] <= 'Z' && s[i] >= 'A'))
                    cons++;
        }
        
    }
    o << voc << ' ' << cons;
}

//ex2

void semnesicuvinte (ifstream &f, ofstream &o)
{
    char s[MAX];
    unsigned int lg,cuv, semne;
    
    cuv=0;
    semne=0;
    
    while(!f.eof())
    {
        f.getline(s,MAX);
        lg=strlen(s);
        
        for(unsigned int i=0; i < lg; i++)
        {
            if(s[i] == ' ')
                cuv++;
            else
                if(strchr(".,;'/][=-!@#$%^&*(><?",s[i]))
                {
                semne++;
                    cuv++;
                }
        }
        
    }
    
    o << cuv << semne;
}

// ex 3


void ordinelexicografica(ifstream &f)
{
    char s[MAX], tab[MAX][MAX], sir[MAX];
    unsigned int  nr = 0 , i;
    char *p;
    int gasit;
    
    while (!f.eof())
    {
        f.getline(s, MAX);
        p = strtok(s, ".,?!;/|/ ");
        while (p != NULL)
        {
            strcpy(tab[nr], p);
            nr++;
            p = strtok(NULL, ".,?!;/|/ ");
        }
    }
    do
    {
        gasit = 0;
        for (i = 1; i < nr; i++)
        {
            if (strcmp(tab[i - 1], tab[i]) > 0)
            {
                strcpy(sir, tab[i - 1]);
                strcpy(tab[i - 1], tab[i]);
                strcpy(tab[i], sir);
                gasit = 1;
            }
        }
    } while (gasit != 0);
    
    unsigned int ap = 1;
    for (i = 1; i< nr; i++)
    {
        if (strcmp(tab[i - 1], tab[i]) == 0)
            ap++;
        else
        {
            cout << tab[i - 1] << "***" << ap << endl;
            ap = 1;
        }
    }
    if (strcmp(tab[i - 1], tab[i - 2]) == 0)
    {
        cout << tab[i - 2] << "***" << ap << endl;
    }
    else cout << tab[i - 1] << "***" << 1 << endl;
}


//ex 4

unsigned int punctpericulos (ifstream &f, unsigned int n )
{
    
    char s[MAX],sir1[MAX],sir[MAX], aux[MAX];
    unsigned int lg, lg1 , k;
    f.getline(s, MAX);
    
    
    for (unsigned int i = 0; i < n; i++)
    {
        for (unsigned int j = i + 1; j < n; j++)
        {
            lg = 0;
            for (k = 0; k <= i; k++)
                aux[lg++] = s[k];
            aux[lg] = '\0';
            
            strcpy(sir1, s + i);
            strcat(sir1, aux);
            
            lg1 = 0;
            for (k = 0; k <= j; k++)
                aux[lg1++] = s[k];
            aux[lg1] = '\0';
            
            strcpy(aux, s + j);
            strcat(sir, aux);
            
            if (strcmp(sir, sir1) > 0)
                return i;
        }
    }
    return 0;
}

//ex5

struct charTab
{
    char tab[256][256];
    int lg;
};

charTab PrefixSiSufix(ifstream &f)
{
    char s[MAX] , c[MAX];
    charTab sp;
    sp.lg = 0;
    
    f >> s;
    f >> c;
    
    for (int i = 0; i < strlen(s); i++)
    {
        if (strstr(c, s + i) == c)
        {
            strcpy(sp.tab[sp.lg], s + i);
            sp.lg++;
        }
    }
    return sp;
}


//ex6

bool rima(char s[], char v[] , unsigned int n)
{
    unsigned int i, j;
    
    i = 0;
    while (s[i] != 0){
        i++;
    }
    j = 0;
    while (v[j] != 0)
    {
        j++;
    }
    for (i = 1; i <= n; i++)
        if (s[strlen(s) - i] != v[strlen(v) - i])
            return 0;
    
    return 1;
}




enum rime
{
    monorima, rimaalba, rimaimbratisata, rimaimperecheata, rimaincrucisata
};




rime tiprima (ifstream &f,unsigned int n)
{
    unsigned int dim = 1;
    char v[5][MAX], s[MAX],*p,*d = NULL;
    while(!f.eof())
    {
        f.getline(s,MAX);
        p=strtok(s,",./?;:}{[] !");
        while(p!=NULL)
        {
            d=p;
            p=strtok(NULL,",./?;:}{[] !");
        }
        strcpy(v[dim],d);
        dim++;
        
    }
    
    if(rima(v[1],v[2],n) && rima(v[3],v[4],n))
        return rimaimperecheata;
        if(rima(v[1],v[3],n) && rima(v[2],v[4],n))
            return rimaincrucisata;
    if(rima(v[1],v[4],n) && rima(v[2],v[3],n))
        return rimaimbratisata;
    if(rima(v[1],v[2],n) && rima (v[1],v[3],n) && rima(v[1],v[4],n))
        return monorima;
    return rimaalba;
    
}



//ex7



bool anagrame(char a[100], char b[100], int n)
{
    if (n==-1)
        return 1;
    if (strchr(a,b[n]))
        return anagrame(a,b,n-1);
    else return 0;
}




    
    
        
       



sper sa fie de ajutor funtiile si daca sunt greseli astept cu placere corectarea/inmbunatatirea lor.
:whorship:



Post Reply