Re: Пеpебоp чисел
- From
- Dmitriy Gerasimenko ()
- To
- Alexander Hritonenkov
- Date
- 2002-07-23T14:51:09Z
- Area
- RU.ALGORITHMS
From: "Dmitriy Gerasimenko" <gerasimenko@karelia.ru>
> DG> Резyльтат вывода для числа:
> DG> [1234]
>
> DG> 1 : 2134
> DG> 2 : 2314
> DG> 3 : 2341
> DG> 4 : 3241
> DG> 5 : 3421
> DG> 6 : 3412
> DG> 7 : 4312
> DG> 8 : 4132
> DG> 9 : 4123
> DG> 10 : 1423
> DG> 11 : 1243
> DG> 12 : 1234
AH> А где же вот это?
AH> 2143
AH> 3214
AH> 4321
AH> 1432
AH> 1324
AH> 1342
AH> 2413
AH> 2431
AH> 3124
AH> 3142
AH> 4213
AH> 4231
AH> Или это yже не пеpестановки?
Я знаю что она выводит не всё. Она была показана для сравнения.
А программу пусть доделывают те кому это интересно или (и) надо,
а правильная программа (один из её вариантов), если угодно,
находится ниже, (кстати она уже печаталась ранее, сдесь же.)
#include <dir.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <process.h>
#include <string.h>
#include <ctype.h>
enum TOperator {PRINT, NEXT_SYMBOL, NEXT_POSITION};
char *sOperation[]= {"PRINT", "NEXT_SYMBOL", "NEXT_POSITION"};
int main(int c, char **argv)
{
char *bytes;
char *pbytes;
char *pargv;
char *trigger;
char filename[MAXPATH];
TOperator op;
int position;
int count_position;
FILE *stream;
unsigned long count;
//--------------------------------------------------------------
if (c <= 1) {
fprintf(stderr, "\nBad command line argument.");
exit(1);
}
//--------------------------------------------------------------
if (c == 3) {
count_position= atoi(argv[2]);
if (count_position == 0) {
fprintf(stderr, "\nBad command line argument.");
exit(1);
}
} else {
count_position= strlen(argv[1]);
}
//--------------------------------------------------------------
bytes= strdup(argv[1]);
if (!bytes) {
fprintf(stderr,"Not enough memory to allocate buffer for bytes.\n");
exit(1);
}
//--------------------------------------------------------------
trigger= (char*)malloc(count_position);
if (!trigger) {
fprintf(stderr,"Not enough memory to allocate buffer for
trigger.\n");
free(bytes);
exit(1);
}
for (int cc= 0; cc < count_position; cc++) trigger[cc]= 0;
//--------------------------------------------------------------
bytes[0]= '\0';
pbytes= bytes;
pargv= argv[1];
while (*pargv) {
if (strchr(bytes, *pargv) == 0)
{
*pbytes++= *pargv;
*pbytes= '\0';
}
pargv++;
}
//--------------------------------------------------------------
// open a file for output
sprintf(filename, "%s_%d.txt", argv[1], count_position);
stream= fopen(filename, "wt+");
if (!stream)
{
fprintf(stderr,"\nError open/create file: %s", filename);
exit(1);
}
fprintf(stream, "\nNumber= %s", argv[1]);
fprintf(stream, "\nCount position= %d", count_position);
//--------------------------------------------------------------
position= count_position - 1;
count= 1;
op= PRINT;
while(1)
{
switch(op)
{
case PRINT:
// write some data to the file
fprintf(stream, "\n");
fprintf(stream, "%u :", count);
for (int j = 0; j < count_position; j++)
{
fprintf(stream,"%c", bytes[trigger[j]]);
}
if (position != (count_position - 1))
position= (count_position- 1);
count++;
op= NEXT_SYMBOL;
break;
case NEXT_SYMBOL:
trigger[position]++;
if (trigger[position] == strlen(bytes))
{ // last symbol
trigger[position]= 0;
op= NEXT_POSITION;
} else { // ok
op= PRINT;
}
break;
case NEXT_POSITION:
position--;
if (position < 0)
{ // exit to DOS
goto EXIT_TO_DOS;
} else { // ok
op= NEXT_SYMBOL;
}
break;
}// switch
} // while(1)
//-----------------------------------------------------------------
EXIT_TO_DOS:
free(bytes);
free(trigger);
// close the file
fclose(stream);
//-----------------------------------------------------------------
fprintf(stdout,
"\n-----------------------------------------------------------------");
fprintf(stdout, "\nProgramm: %s %s is succefull!", argv[0], argv[1]);
fprintf(stdout, "\nResult programm save to: [%s]", filename);
fprintf(stdout, "\nCount= [%d]", count - 1);
fprintf(stdout,
"\n-----------------------------------------------------------------");
//-----------------------------------------------------------------
return 0;
}
Будьте здоровы!
--- ifmail v.2.15dev5
* Origin: UNKNOWN (2:5020/400)