ログイン
編集不可のページディスカッション情報添付ファイル
solanumaple/makecpp

MMA

学習がてら,初めて使えるものを作れたので自慢
"改善"してご自由にお使いください
このコードを使用したことによる一切の責任は持てません。

ディレクトリ内に a.cpp~z.cppが存在しなければそのファイル名で,
全て存在すればその時の時刻をファイル名にして以下を出力します

   1 #include<iostream>
   2 //#include<iomanip>
   3 //#include<cstdlib>
   4 //#include<cstdio>
   5 //#include<cmath>
   6 //#include<cstring>
   7 //#include<vector>
   8 //#include<algorithm>
   9 //#include<string>
  10 //#include<sstream>
  11 //#include<ncurses.h>
  12 
  13 using namespace std;
  14 
  15 int main(){
  16   ios::sync_with_stdio(false);
  17 
  18   return 0;
  19 }

   1 #include<iostream>
   2 #include<fstream>
   3 #include<ctime>
   4 #include<sstream>
   5 #include<string>
   6 #include<vector>
   7 #include<unistd.h>
   8 #include<dirent.h>
   9 
  10 using namespace std;
  11 void out(const char*);
  12 string name(void);
  13 
  14 int main(){
  15   out( name().c_str() );
  16   return 0;
  17 }
  18 
  19 string name(){
  20   // a.cpp^z.cppまで探して全て存在していたら現在時刻.cpp のパスを返す
  21   char path[2000];//パス名全体の長さの上限1023バイト
  22   stringstream ss;
  23   getcwd(path,1030);
  24   ss.str("");
  25   ss.clear(stringstream::goodbit);
  26   ss << path << '/';
  27 
  28   //a-zまで探す
  29   struct dirent* dirst;
  30   DIR* dir = opendir(path);
  31   vector<string> used;
  32   vector<string>::iterator itnm;
  33   stringstream tmp;
  34   bool safe=false;//ディレクトリ読めたかフラグ
  35   bool nexist;//存在するとき0,しないとき1
  36 
  37   while( (dirst = readdir(dir)) != NULL ){
  38     used.push_back( dirst->d_name );
  39     safe = true;
  40   }
  41   closedir(dir);
  42 
  43   for(char i='a'; i<='z'; i++){
  44     nexist = 1;
  45     tmp.clear(stringstream::goodbit); tmp.str("");
  46     tmp << i << ".cpp";
  47     for( itnm = used.begin(); itnm != used.end(); itnm++){
  48       if( *itnm != tmp.str() )
  49         nexist *= 1;
  50       else
  51         nexist *= 0;
  52     }
  53     if( nexist && safe){//ファイルが存在しないとき
  54       ss << tmp.str();
  55       return ss.str();
  56     }
  57   }
  58   //a-zまで探す
  59 
  60   //しかたないから時刻をファイル名に
  61   time_t timer = time(NULL);
  62   struct tm* str = localtime( &timer );
  63   ss << str->tm_year+1900 << str->tm_mon+1 << str->tm_mday;
  64   ss << str->tm_hour << str->tm_min << str->tm_sec << ".cpp";
  65   return ss.str();
  66   //時刻
  67 }
  68 
  69 void out(const char* filename){
  70 
  71   ofstream ofs( filename, ios::app);
  72   if(ofs){
  73     cout << filename << "を生成" << endl;
  74     ofs << "#include<iostream>" << endl;
  75     ofs << "//#include<iomanip>" << endl;
  76 
  77     ofs << "//#include<cstdlib>" << endl;
  78     ofs << "//#include<cstdio>" << endl;
  79     ofs << "//#include<cmath>" << endl;
  80     ofs << "//#include<cstring>" << endl;
  81 
  82     ofs << "//#include<vector>" << endl;
  83     ofs << "//#include<algorithm>" << endl;
  84     ofs << "//#include<string>" << endl;
  85     ofs << "//#include<sstream>" << endl;
  86     ofs << "//#include<ncurses.h>" << endl << endl;
  87     ofs << "using namespace std;" << endl << endl;
  88     ofs << "int main(){" << endl;
  89     ofs << "  ios::sync_with_stdio(false);" << endl << endl;
  90     ofs << "  return 0;" << endl;
  91     ofs << "}" << endl;
  92   }else{
  93     cerr << "出力ファイルを開けませんでした" << endl;
  94   }
  95 
  96 }

※一部ヘッダが環境依存 自分はsolで使っている

solanumaple/makecpp (最終更新日時 2014-07-17 13:55:28 更新者 solanumaple)