1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| // Exemple avec MinGw
// Attention : j'ai écrit ce code en vitesse. Normalement, il faudrait faire du contrôle d'erreurs.
#include "Windows.h"
#include <fstream>
#include <iostream>
void executerEtAttendreFinExecution(const char* programme, const char* parametres)
{
// Code copié-collé sans réfléchir depuis
// https://www.codeproject.com/Articles/1842/A-newbie-s-elementary-guide-to-spawning-processes
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = programme;
ShExecInfo.lpParameters = parametres;
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
}
int main()
{
std::string equation("rien"); // initialisation d'une variable "equation"
std::cout << "Quel est votre équation ?" << std::endl;
std::cin >> equation; // prends la valeur donné, pour l'exemple, disons : x*x == 4
// Écrire le code source d'un programme. Ce code source dépend de la valeur de "equation".
{
std::ofstream fichierCpp("CalculEquation.cpp");
fichierCpp <<
"#include <iostream>\n"
"int main()"
"{"
"for (int x; x < 50; x++)"
"{"
"if (" << equation << ")"
"{"
"std::cout << \"x peut valoir \" << x << std::endl;"
"}"
"else"
"{"
"std::cout << \"ERREUR: \" << x << \" ne fonctionne pas. \" << std::endl;"
"}"
"}"
"system(\"pause\");"
"return 0;"
"}";
}
// Compiler le code source.
executerEtAttendreFinExecution("g++", "CalculEquation.cpp -o CalculEquation.exe");
// Exécuter le programme créé.
executerEtAttendreFinExecution("CalculEquation.exe", "");
return 0;
} |