Mam kod do perutacji zadanego ciagu (ponizej), jak podaje mu ilosc znakow do permutacji mniejsza niz 8 to kompilator sam dopisuje jakies dziwne znaki jak powyzej tej liczby to wszytko jest OK. Nie wiem dlaczego. Moglby ktos jeszcze zobaczyc czy obliczanie czasu dzialania programu jest dobrze zbobione.
Z gory dziakuje
[code]
#include<iostream>
#include<cstring>
#include <time.h>
using namespace std;
clock_t start, finish;
void char_permutation(char str[],char append[])
{
int length=strlen(str);
if(length)
{
double duration;
start = clock();
for(int i=0;i<length;++i)
{
char* str1=new char[length+1];
int cnt;
int cnt2;
for(cnt=0,cnt2=0;cnt<length;++cnt,++cnt2)
{
if(cnt==i)
{
str1[cnt]=str[++cnt2];
continue;
}
else
str1[cnt]=str[cnt2];
}
str1[cnt]='\0';
int alength=strlen(append);
char* append1=new char [alength+2];
strncpy(append1,append,alength);
append1[alength]=str[i];
append1[alength+1]='\0';
char_permutation(str1,append1);
delete [] str1;
delete [] append1;
}
finish = clock();
duration = (double)(finish - start) / CLOCKS_PER_SEC;
}
else
{
cout<<append<<endl;
}
}
int main()
{
int ile;
cout << "Podaj ile elementow chcesz permutowac? ";
cin >> ile;
char str[ile];
for (int i = 0; i < ile; i++){
cout << "Podaj znaki do permutacji: ";
cin >> str[i];
}
cout << "Tablica ma postac: \n";
for (int i = 0; i < ile; i++ ){
cout << str[i];
}
cout << endl;
char append[]="\0";
char_permutation(str,append);
double duration;
cout << "Czas dzialania programu: " << duration << endl;
system("PAUSE");
return EXIT_SUCCESS;
}
[/code]