Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include <string>
- /*
- this c++ program will create another c++ program with "metafactorial.cpp" name
- which is going to compute factorial of some number n and display it
- */
- std::string buildMetafactorial(int factorial)
- {
- if (factorial == 0 || factorial == 1)
- return "metafactorial = 1;";
- else
- {
- std::string s = "metafactorial = ";
- for (int i = 1; i <= factorial; i++)
- {
- if (i == factorial)
- {
- s += std::to_string(i) + ";";
- break;
- }
- s += std::to_string(i) + " * ";
- }
- return s;
- }
- }
- int main()
- {
- int factorial = 0;
- std::cin >> factorial;
- if (factorial >= 0)
- {
- std::fstream file("metafactorial.cpp", std::ios::out);
- file << "#include <iostream>" << std::endl << std::endl;
- file << "int main()" << std::endl;
- file << "{" << std::endl;
- file << "\t" << "int metafactorial = 0;" << std::endl;
- file << "\t" << buildMetafactorial(factorial) << std::endl;
- file << "\t" << "std::cout << metafactorial << std::endl;" << std::endl;
- file << "}" << std::endl;
- file.close();
- std::cout << "Metafactorial generated!" << std::endl;
- return 0;
- }
- std::cout << "Metafactorial wasn't generated!" << std::endl;
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement