Advertisement
Guest User

Metafactorial

a guest
Jan 16th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.43 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. /*
  6. this c++ program will create another c++ program with "metafactorial.cpp" name
  7. which is going to compute factorial of some number n and display it
  8. */
  9.  
  10. std::string buildMetafactorial(int factorial)
  11. {
  12.     if (factorial == 0 || factorial == 1)
  13.         return "metafactorial = 1;";
  14.     else
  15.     {
  16.         std::string s = "metafactorial = ";
  17.         for (int i = 1; i <= factorial; i++)
  18.         {
  19.             if (i == factorial)
  20.             {
  21.                 s += std::to_string(i) + ";";
  22.                 break;
  23.             }
  24.             s += std::to_string(i) + " * ";
  25.         }
  26.         return s;
  27.     }
  28. }
  29.  
  30. int main()
  31. {
  32.     int factorial = 0;
  33.     std::cin >> factorial;
  34.  
  35.     if (factorial >= 0)
  36.     {
  37.         std::fstream file("metafactorial.cpp", std::ios::out);
  38.  
  39.         file << "#include <iostream>" << std::endl << std::endl;
  40.         file << "int main()" << std::endl;
  41.         file << "{" << std::endl;
  42.  
  43.         file << "\t" << "int metafactorial = 0;" << std::endl;
  44.         file << "\t" << buildMetafactorial(factorial) << std::endl;
  45.         file << "\t" << "std::cout << metafactorial << std::endl;" << std::endl;
  46.  
  47.         file << "}" << std::endl;
  48.        
  49.         file.close();
  50.         std::cout << "Metafactorial generated!" << std::endl;
  51.         return 0;
  52.     }
  53.  
  54.     std::cout << "Metafactorial wasn't generated!" << std::endl;
  55.     return 1;
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement