Last active
March 1, 2022 22:49
-
-
Save hburn7/58a4b556d88a2c8e35e6db6694d6fb29 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Text; | |
namespace IsEven; | |
internal class Program | |
{ | |
private const string PATH = ""; //Your path here | |
public static void Main(string[] args) { GenerateIf(); } | |
private static void GenerateIf() | |
{ | |
const int init = Int32.MinValue; | |
const int max = Int32.MaxValue; | |
// Delete the file if it exists. | |
if (File.Exists(PATH)) | |
{ | |
File.Delete(PATH); | |
} | |
using (var fs = new FileStream(File.OpenHandle(PATH, FileMode.OpenOrCreate, FileAccess.ReadWrite), FileAccess.ReadWrite)) | |
{ | |
// Includes | |
AddText(fs, "#include <stdlib.h>\n" + | |
"#include <stdio.h>\n" + | |
"#include <stdbool.h>\n\n"); | |
// isEven | |
AddText(fs, "bool isEven(int n) {\n"); | |
const double comp = (double) max; | |
for (int i = init; i < max; i++) | |
{ | |
double progress = (i / comp) * 100; | |
if ((i % 500000) == 0) | |
{ | |
Console.WriteLine($"{progress}%"); | |
} | |
if (i == init) | |
{ | |
AddText(fs, template_even(i, true) + "\n"); | |
} | |
else | |
{ | |
AddText(fs, (i % 2) == 0 ? template_even(i) + "\n" : template_odd(i) + "\n"); | |
} | |
} | |
// Close isEven | |
AddText(fs, "}\n\n"); | |
// Main | |
AddText(fs, "int main(int argc, char *argv[]) {\n\t" + | |
"int n = atoi(argv[1]);\n\t" + | |
"if(isEven(n)) {\n\t\t" + | |
"printf(\"%d is even!\\n\", n);\n\t}\n\t" + | |
"else {\n\t\t" + | |
"printf(\"%d is odd!\\n\", n);\n\t}\n}"); | |
} | |
} | |
private static string template_even(int n, bool use_if = false) | |
{ | |
string base_str = $"if (n == {n}) {{\n\t\treturn true;\n\t}} "; | |
return use_if ? "\t" + base_str : $"\telse {base_str}"; | |
} | |
private static string template_odd(int n) { return $"\telse if (n == {n}) {{\n\t\treturn false;\n\t}} "; } | |
private static void AddText(FileStream fs, string value) | |
{ | |
byte[] info = new UTF8Encoding(true).GetBytes(value); | |
fs.Write(info, 0, info.Length); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
👍 LGTM!