Ad
Code
Diff
  • #include <stdint.h>
    
    uint32_t sum(uint32_t a, uint32_t b)
    {
    	if(a == 0u)
    	{
    		return b;
    	}
    	return sum(a-1, b+1);
    }
    • function sum(a,b) {
    • return a+b; // wrong returning
    • #include <stdint.h>
    • uint32_t sum(uint32_t a, uint32_t b)
    • {
    • if(a == 0u)
    • {
    • return b;
    • }
    • return sum(a-1, b+1);
    • }

Some additional robustness

Code
Diff
  • #include <stddef.h>
    
    const char* digit_to_text(unsigned digit)
    {
    	static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
      
    	if(digit > 9u)
    	{
    		return NULL;
    	}
      
    	return nums[digit];
    }
    • const char *nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    • #include <stddef.h>
    • const char* digit_to_text(int digit) {
    • return nums[digit];
    • const char* digit_to_text(unsigned digit)
    • {
    • static const char* nums[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
    • if(digit > 9u)
    • {
    • return NULL;
    • }
    • return nums[digit];
    • }

Now in C!

Code
Diff
  • #include <stdint.h>
    #include <stdbool.h>
    
    #define MAX_ANGLE 180u
    
    typedef enum
    {
    	ACUTE,
    	RIGHT,
    	OBTUSE,
    } AngleType;
    
    typedef enum
    {
    	EQUILATERAL,
    	ISOSCELES,
    	SCALENE,
    } SideType;
    
    typedef struct
    {
    	union
    	{
    		struct
    		{
    			uint32_t a;
    			uint32_t b;
    			uint32_t c;
    		};
    		uint32_t angles[3];
    	};
    	
    } Triangle;
    
    bool TriangleIsValid(const Triangle* triangle);
    AngleType TriangleGetAngleType(const Triangle* triangle);
    SideType TriangleGetSideType(const Triangle* triangle);
    uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
    
    bool TriangleIsValid(const Triangle* triangle)
    {
        return triangle->a + triangle->b + triangle->c == 180u;
    }
    
    AngleType TriangleGetAngleType(const Triangle* triangle)
    {
    	for(unsigned i = 0u; i < 3u; i++)
    	{
    		if(triangle->angles[i] > 90u)
    		{
    			return OBTUSE;
    		}
    		if(triangle->angles[i] == 90u)
    		{
    			return RIGHT;
    		}
    	}
    	return ACUTE;
    }
    
    SideType TriangleGetSideType(const Triangle* triangle)
    {
    	uint32_t a = triangle->a;
    	uint32_t b = triangle->b;
    	uint32_t c = triangle->c;
    	
    	if(a == b && b == c)
    	{
    		return EQUILATERAL;
    	}
    	if(a == b || b == c || c == a)
    	{
    		return ISOSCELES;
    	}
    	return SCALENE;
    }
    
    uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2)
    {
    	return 180u - angle1 - angle2;
    }
    • use std::marker::PhantomData;
    • #include <stdint.h>
    • #include <stdbool.h>
    • #[derive(Debug, PartialEq, Eq)]
    • enum AngleType {
    • Acute,
    • Right,
    • Obtuse
    • }
    • #[derive(Debug, PartialEq, Eq)]
    • enum SideType {
    • Equilateral,
    • Isoceles,
    • Scalene
    • }
    • struct Complete;
    • struct Incomplete;
    • #define MAX_ANGLE 180u
    • struct Triangle<T> {
    • angles: [u32; 3],
    • phantom: PhantomData<T>
    • typedef enum
    • {
    • ACUTE,
    • RIGHT,
    • OBTUSE,
    • } AngleType;
    • typedef enum
    • {
    • EQUILATERAL,
    • ISOSCELES,
    • SCALENE,
    • } SideType;
    • typedef struct
    • {
    • union
    • {
    • struct
    • {
    • uint32_t a;
    • uint32_t b;
    • uint32_t c;
    • };
    • uint32_t angles[3];
    • };
    • } Triangle;
    • bool TriangleIsValid(const Triangle* triangle);
    • AngleType TriangleGetAngleType(const Triangle* triangle);
    • SideType TriangleGetSideType(const Triangle* triangle);
    • uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2);
    • bool TriangleIsValid(const Triangle* triangle)
    • {
    • return triangle->a + triangle->b + triangle->c == 180u;
    • }
    • impl<T> Triangle<T> {
    • fn angle_type(&self) -> AngleType {
    • for angle in self.angles {
    • if angle > 90 {
    • return AngleType::Obtuse;
    • }
    • if angle == 90 {
    • return AngleType::Right;
    • }
    • }
    • AngleType::Acute
    • }
    • fn side_type(&self) -> SideType {
    • let [a, b, c] = self.angles;
    • if a == b && b == c {
    • SideType::Equilateral
    • } else if a == b || b == c || c == a {
    • SideType::Isoceles
    • } else {
    • SideType::Scalene
    • }
    • }
    • AngleType TriangleGetAngleType(const Triangle* triangle)
    • {
    • for(unsigned i = 0u; i < 3u; i++)
    • {
    • if(triangle->angles[i] > 90u)
    • {
    • return OBTUSE;
    • }
    • if(triangle->angles[i] == 90u)
    • {
    • return RIGHT;
    • }
    • }
    • return ACUTE;
    • }
    • impl Triangle<Complete> {
    • fn new(a: u32, b: u32, c: u32) -> Self {
    • Self { angles: [a, b, c], phantom: PhantomData::<Complete> }
    • }
    • SideType TriangleGetSideType(const Triangle* triangle)
    • {
    • uint32_t a = triangle->a;
    • uint32_t b = triangle->b;
    • uint32_t c = triangle->c;
    • if(a == b && b == c)
    • {
    • return EQUILATERAL;
    • }
    • if(a == b || b == c || c == a)
    • {
    • return ISOSCELES;
    • }
    • return SCALENE;
    • }
    • impl Triangle<Incomplete> {
    • fn new(a: u32, b: u32) -> Self {
    • Self { angles: [a, b, 180 - a - b], phantom: PhantomData::<Incomplete> }
    • }
    • fn other_angle(&self) -> u32 {
    • self.angles[2]
    • }
    • uint32_t TriangleGetThirdAngle(uint32_t angle1, uint32_t angle2)
    • {
    • return 180u - angle1 - angle2;
    • }