Skip to Content

series

/* Series Generator
generates a string with a numeric series in the middle,
like foo01.gif foo02.gif .... foo99.gif
programs output may be redirected as its input
allowing the creation of very complex series
like /year1981/foo01.gif .... /year2002/foo90.gif
useful in creating download lists
Alaa The Great
3/1/2002
*/

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <string>
#include <cstdio>
using namespace std;

void generate(int start, int end, int pad, char *postfix, string prefix ) ;

/* program should have at least 4 arguments
  1-first and 2-last values of the series
  3-number of digits (needed to pad leading zeros)
  4-string after the series
  any arguments that follows will be assumed to be
  a string to generate a series for (string at the begining of the series)
  if only 4 arguments are given the program asks for input of string
  to put in the begining of the series (terminates when EOF is enetered) */
 
int main( int argc, char *argv[]) {
int start, end, pad;
char *postfix;
char a;
string prefix;
prefix="";
if (argc<5) {
cout<<endl<<"need more arguments"<<endl;
return 1;
}

start=atoi(argv[1]);
end=atoi(argv[2]);
pad=atoi(argv[3]);
postfix=argv[4];
if (argc==5) {
do {
a=cin.get();
if (a=='\n'||a==EOF&&prefix!="") {
generate(start,end,pad,postfix,prefix);
prefix="";
}
if (a!='\n'&&a!=EOF) prefix=prefix+a;
} while (a!=EOF);
}
else {
for (int i=5; i<argc; i++)
generate(start, end, pad, postfix, argv[i]);
}
return 0;
}

void generate(int start, int end, int pad, char *postfix, string prefix) { 
for (int count=start; count<=end; count++) {
cout<<prefix;
if (count!=0) for(int k=pad; pow(10.0,(k-1))>count; k--) cout<<0;
else for (int k=pad; k>1; k--) cout<<0;
cout<<count<<postfix<<endl;
}
}


Dr. Radut | book