4 in 1

Intrebari despre limbajul C++, standardul C++, STL, OOP in C++ sau alte subiecte nelegate de VisualC++
Post Reply
razvan
Junior
Junior
Posts: 1
Joined: 29 Sep 2014, 12:54
Judet: Buzău

4 in 1

Post by razvan » 29 Sep 2014, 13:03

salut! am 4 programe

produsul cartezian a n multimi.

Code: Select all

#include <iostream>
#include <conio.h>
using namespace std;
int st[50], nr[50], n;
void initializare()
{
int i;
cout<<"Dati numarul de multimi:"; cin>>n;
 for(i=1; i<=50; i++)
	st[i]=0;
 for(i=1; i<=n; i++)
	{
	 cout<<endl<<"numarul de elemente al multimii "<<i<<": ";
	 cin>>nr[i];
	 }
	}
 void tipar(int p)
 {
	int j;
	for(j=1; j<=p; j++)
	 cout<<st[j]<<" ";
	 cout<<endl;
	 }
 int valid()
 {
	return 1;
	}
	void bktr(int p)
	 {
		int pval;
		if(p==n+1)
		 tipar(p-1);
		else
		 for(pval=1; pval<=nr[p]; pval++)
			{
			 st[p]=pval;
				if(valid())
				 bktr(p+1);
				 }
			 }
		int main()
		{
		 initializare();
		 bktr(1);
		 getch();
     }
generarea combinarilor

Code: Select all

#include<iostream.h>
int n,m,s[100];
void init(int k){
if(k==1) s[k]=0;
	else s[k]=s[k-1];}
int succesor(int k)
{
if(s[k]<n-m+k)
	{s[k]=s[k]+1; return 1;}
		else return 0;}

int valid(){
return 1;}

int solutie(int k){
return k==m;}

void tipar(){
for(int i=1;i<=m;i++)
	cout<<s[i]<<" ";
	cout<<endl;}

void bt(int k){
init(k);
	while(succesor(k))
		if(valid())
			if(solutie(k)) tipar();
				else bt(k+1);}

void main()
{
cout<<"n=";cin>>n;
cout<<"m=";cin>>m;
bt(1);}
generarea aranjamentelor

Code: Select all

#include<iostream>
#include<cstdlib>
using namespace std;
int n,v[50],k,m;
void init()
{v[k]=0;}
int cond()
{int i,c=1;
for(i=1;i<=k-1;i++)
if(v[i]==v[k])c=0;
return c;}
int sol()
{return(k==m);}
void afisare()
{cout<<"(";
for(int i=1;i<=m;i++)
cout<<v[i]<<",";
cout<<"\b"<<")";
cout<<endl;}
void bt()
{k=1;
init();
while(k>0)
{while(v[k]<n)
{v[k]++;
if(cond())if(sol())afisare();
else{k++;
init();
}
}
k--;
}
}
int main()
{cout<<"Introduceti o valoare pentru n : ";cin>>n;
cout<<"Introduceti o valoare pentru m (m<n) : ";cin>>m;
cout<<endl;
cout<<"Aranjamentele multimii cu "<<n<<" elemente luate cate "<<m<<" sunt : "<<endl;
cout<<endl;
bt();
cout<<endl;
system("PAUSE");
}
generarea partitiilor unei multimi

Code: Select all

#include <iostream>
using namespace std;

char sp3[]="               ";

int x3[20], n3, nrsol3=0, max[20], maxim;

int DeterminareMaxim(int k)
{  int maxim=0,i;
    for(i=1;i<=k;i++)
        if (x3[i]>maxim) maxim=x3[i];
    return maxim;
}

void Afisare3()
{ int i,j;
  cout<<sp3;
  maxim=DeterminareMaxim(n3);
  for(j=1;j<=maxim;j++)
   {   cout<<"{";
       for(i=1;i<=n3;i++)
           if (x3[i]==j) cout<<i<<" ";
         cout<<"}  ";
   }
  cout<<endl;
  nrsol3++;
}

void BackRec3(int k)
{  int i;
   for(i=1;i<=DeterminareMaxim(k-1)+1;i++)
      { x3[k]=i;
    if (k==n3) Afisare3();
    else BackRec3(k+1);
     }
}

int main()
{ cout<<endl<<endl<<sp3<<"Partiile multimii {1,2,3.....,n}"<<endl;
  cout<<endl<<sp3<<" Dati valoarea lui n: "; cin>>n3;
  cout<<endl;
  BackRec3(1);
  cout<<endl<<sp3<<"Numar solutii: "<<nrsol3;
  return 0;
}

Ajutati-ma va rog. cum sa le adaug pe toate intr-un program, si la rularea acestuia sa aleg aparte ce program sa rulez ?



User avatar
MarianMMX
Junior
Junior
Posts: 16
Joined: 27 Mar 2014, 09:50
Judet: Galaţi

Re: 4 in 1

Post by MarianMMX » 01 Oct 2014, 15:35

Creezi un nou fisier, ex.: 4in1.cpp
In el copii codul din cele 4 fisiere (mai putin functiile main()).
Ordonezi codul si rezolvi conflictele de denumiri (daca e cazul).
Creezi o noua functie main, de genul:

Code: Select all

int main(int argc, char* argv[]) 
{ 
    if(argc == 2)
    {
        std::string optiune(argv[1]);
        if(optiune == "opt1")
        {
            //executa primul program
        }
    }
    else
    {
        cout << argv[0] << " optiune" << endl;
    }
return 0; 
}
Last edited by MarianMMX on 07 Oct 2014, 10:06, edited 1 time in total.
If you think C++ is not overly complicated, just what is a
protected abstract virtual base pure virtual private destructor
and when was the last time you needed one? — Tom Cargill (1990)

User avatar
black_atus
Membru
Membru
Posts: 139
Joined: 20 Jun 2012, 10:20
Judet: Prahova
Location: Ploiesti

Re: 4 in 1

Post by black_atus » 06 Oct 2014, 10:40

Sau poti folosi o instructiune switch cu 4 case-uri, cate unul pentru fiecare bucata de cod gen:
int nOptiune - pe care il iei de la tastatura
si
switch(nOptiune)
{
case 1:
functii 1..
break;
case 2:
functii 2..
break;
....
default:
cout << "Optiune invalida.."<<endl;
}
Cu stima,
Daniel Constantin

Post Reply