Created
August 11, 2023 11:50
-
-
Save fvilante/7767c89adee7d0c8cb6614ea294d79ac to your computer and use it in GitHub Desktop.
C11 abstractions to emulate some Rust features
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
#include <stdio.h> | |
typedef double (*MathOperation)(double, double); | |
double add(double a, double b) { | |
return a + b; | |
} | |
double subtract(double a, double b) { | |
return a - b; | |
} | |
double multiply(double a, double b) { | |
return a * b; | |
} | |
double divide(double a, double b) { | |
if (b != 0.0) { | |
return a / b; | |
} else { | |
printf("Division by zero is not allowed.\n"); | |
return 0.0; | |
} | |
} | |
int main() { | |
MathOperation operations[] = {add, subtract, multiply, divide}; | |
printf("Select operation:\n"); | |
printf("1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n"); | |
int choice; | |
scanf("%d", &choice); | |
if (choice >= 1 && choice <= 4) { | |
double a, b; | |
printf("Enter two numbers: "); | |
scanf("%lf %lf", &a, &b); | |
double result = operations[choice - 1](a, b); | |
printf("Result: %lf\n", result); | |
} else { | |
printf("Invalid choice.\n"); | |
} | |
return 0; | |
} |
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
#include <stdio.h> | |
// Enum-like structure representing different variants | |
typedef enum { | |
Variant_Int, | |
Variant_Float, | |
Variant_String | |
} VariantType; | |
// Structure representing the variant | |
typedef struct { | |
VariantType type; | |
union { | |
int int_value; | |
float float_value; | |
char* string_value; | |
}; | |
} Variant; | |
// Function to print a variant's value based on its type | |
void printVariant(Variant* variant) { | |
switch (variant->type) { | |
case Variant_Int: | |
printf("Int: %d\n", variant->int_value); | |
break; | |
case Variant_Float: | |
printf("Float: %f\n", variant->float_value); | |
break; | |
case Variant_String: | |
printf("String: %s\n", variant->string_value); | |
break; | |
} | |
} | |
int main() { | |
Variant intVariant = {Variant_Int, .int_value = 42}; | |
Variant floatVariant = {Variant_Float, .float_value = 3.14}; | |
Variant stringVariant = {Variant_String, .string_value = "Hello"}; | |
printVariant(&intVariant); | |
printVariant(&floatVariant); | |
printVariant(&stringVariant); | |
return 0; | |
} |
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
´#include <stdio.h> | |
// Definição da estrutura | |
typedef struct { | |
int value; | |
} MyStruct; | |
// Método para inicializar a estrutura | |
void my_struct_init(MyStruct *instance, int value) { | |
instance->value = value; | |
} | |
// Método para imprimir o valor da estrutura | |
void my_struct_print(const MyStruct *instance) { | |
printf("Value: %d\n", instance->value); | |
} | |
// Método para dobrar o valor da estrutura | |
void my_struct_double(MyStruct *instance) { | |
instance->value *= 2; | |
} | |
int main() { | |
MyStruct my_instance; | |
my_struct_init(&my_instance, 5); | |
my_struct_print(&my_instance); | |
my_struct_double(&my_instance); | |
my_struct_print(&my_instance); | |
return 0; | |
} |
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
#include <stdio.h> | |
// Definição das estruturas para diferentes tipos de formas | |
typedef struct { | |
int width; | |
int height; | |
} Rectangle; | |
typedef struct { | |
int radius; | |
} Circle; | |
// Macro para selecionar o comportamento com base no tipo da forma | |
#define DrawShape(shape) _Generic((shape), Rectangle: drawRectangle, Circle: drawCircle)(shape) | |
// Função para desenhar um retângulo | |
void drawRectangle(Rectangle rect) { | |
printf("Drawing a rectangle with width %d and height %d...\n", rect.width, rect.height); | |
} | |
// Função para desenhar um círculo | |
void drawCircle(Circle circle) { | |
printf("Drawing a circle with radius %d...\n", circle.radius); | |
} | |
int main() { | |
Rectangle rect = {10, 5}; | |
Circle circle = {3}; | |
DrawShape(rect); // Chama drawRectangle | |
DrawShape(circle); // Chama drawCircle | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment