cx-language / cx

C* is a hybrid low-level/high-level systems programming language focused on performance and productivity.
https://cx-language.github.io/
MIT License
130 stars 9 forks source link

Add more examples #76

Open emlai opened 1 year ago

emlai commented 1 year ago

Here are a few examples generated by ChatGPT:

// Import the standard math library.
import "math.h";

// Define a generic function that calculates the average of two values.
T average<T>(T a, T b) {
    return (a + b) / 2;
}

// Define a struct that represents a 2D point.
struct Point {
    x: f32;
    y: f32;
}

// Define a function that calculates the distance between two points.
f32 distance(Point a, Point b) {
    var dx = a.x - b.x;
    var dy = a.y - b.y;
    return sqrt(dx * dx + dy * dy);
}

// Define a class that represents a circle.
class Circle {
    // Define a private field to store the circle's radius.
    private radius: f32;

    // Define a constructor that initializes the circle's radius.
    constructor(radius: f32) {
        this.radius = radius;
    }

    // Define a method that calculates the area of the circle.
    area(): f32 {
        return this.radius * this.radius * PI;
    }

    // Define a method that calculates the circumference of the circle.
    circumference(): f32 {
        return 2 * this.radius * PI;
    }
}

void main() {
    // Use the `average` function to calculate the average of two numbers.
    var a = average(3, 7);
    var b = average(5.5, 8.5);

    // Print the results of the averages to the console.
    println(a); // 5
    println(b); // 7

    // Create two points and calculate their distance.
    var p1 = Point { x: 0.0, y: 0.0 };
    var p2 = Point { x: 3.0, y: 4.0 };
    var d = distance(p1, p2);

    // Print the distance to the console.
    println(d); // 5

    // Create a circle with radius 2.0 and calculate its area and circumference.
    var circle = Circle(2.0);
    var area = circle.area();
    var circumference = circle.circumference();

    // Print the area and circumference to the console.
    println(area);         // 12.566371
    println(circumference); // 12.566371
}
// Import the standard input/output library.
import "stdio.h";

// Define a function that calculates the factorial of a number.
int factorial(int n) {
    if (n <= 1) {
        return 1;
    }
    return n * factorial(n - 1);
}

// Define a function that checks if a number is prime.
bool isPrime(int n) {
    if (n < 2) {
        return false;
    }
    for (var i = 2; i < n; i++) {
        if (n % i == 0) {
            return false;
        }
    }
    return true;
}

void main() {
    // Read a number from the user.
    print("Enter a number: ");
    var n = 0;
    scanf("%d", &n);

    // Calculate and print the factorial of the number.
    var fact = factorial(n);
    printf("%d! = %d\n", n, fact);

    // Check if the number is prime and print the result.
    var prime = isPrime(n);
    printf("%d is%s prime.\n", n, prime ? "" : " not");
}
Spoiledpay commented 11 months ago

Hello, Can you help answer some questions about PATH, Environment and compilation outside the CX compiler directory?