Question: Can someone help to write this c program on C++ #define RETURN_TO_ORIGIN_POINT 0 // @Program Includes #include #include #include #include // @Useful Typedefs/Defines typedef int8_t
Can someone help to write this c program on C++
#define RETURN_TO_ORIGIN_POINT 0 // @Program Includes #include #include #include #include // @Useful Typedefs/Defines typedef int8_t i8; typedef int16_t i16; typedef int32_t i32; typedef int64_t i64; typedef uint8_t u8; typedef uint16_t u16; typedef uint32_t u32; typedef uint64_t u64; typedef float r32; typedef double r64; typedef u64 Count; #define foreach(i, lim) for(u64 (i) = 0; (i) < (lim); ++(i)) #define forrng(i, l, h) for(i64 (i) = (l); (i) < (h); ++(i)) #define print(text, ...) printf((text), __VA_ARGS__) #define error(text, ...) fprintf(stderr, (text), __VA_ARGS__) #define heap_alloc(size) (malloc(size)) #define heap_alloc_and_zero(size) (calloc(size, 1)) // @Vector Helpers typedef struct Vector3 { r64 x, y, z; } Vector3; Vector3 init_vector3(r64 x, r64 y, r64 z) { Vector3 v = { x, y, z }; return v; } r64 vector3_distance_squared(Vector3 a, Vector3 b) { return (a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y) + (a.z - b.z)*(a.z - b.z); } typedef Vector3 v3; #define v3(x, y, z) init_vector3(x, y, z) #define v3_distance_squared(a, b) vector3_distance_squared(a, b) // @Star Structs/Functions typedef struct Star { v3 position; char *name; } Star; typedef struct StarSet { Count count; Star *stars; } StarSet; void load_star_set(StarSet *s, const char *filename, r64 maximum_distance) { // NOTE(Ryan): maximum_distance will set the distance at which a star is not // stored. For example, if maximum_distance == 10.0, any stars // greater than 10.0 parsecs away from Sol will not be loaded. // // If no limit is desired, set maximum_distance to be less than // zero. FILE *f = fopen(filename, "r"); if(f) { char *buffer = 0; fseek(SEEK_END, f); u64 size = ftell(f); rewind(f); buffer = heap_alloc_and_zero(size); if(buffer) { fread(buffer, 1, size, f); { struct Row { }; u64 read_pos = 0; foreach(i, size) { if(buffer[i] == ',') { } else if(buffer == ' ') { } } } free(buffer); } fclose(f); } else { error("ERROR: \"%s\" could not be loaded. ", filename); } } void clean_up_star_set(StarSet *s) { if(s->count && s->stars) { free(s->stars); s->count = 0; } } // @Main int main(int argc, char **argv) { // create and load a star set, limiting to all stars within // 10.0 parsecs from Sol r64 distance_limit = 10.0; // parsecs StarSet star_set = {0}; load_star_set(&star_set, "", distance_limit); { // calculate a trip to all stars using a greedy approach print("Calculating trip to all stars within %f parsecs of Sol... " "-------------------------------------------------------------- ", distance_limit); i8 *visited_star = heap_alloc_and_zero(star_set.count); Count index_of_star_with_minimum_distance; Count index_of_current_star = 0; r64 minimum_distance_squared; r64 distance_squared; r64 distance; r64 total_distance_travelled = 0.0; while(1) { index_of_star_with_minimum_distance = 0; minimum_distance_squared = -1.0; // find the next star that: // 1. has not been visited // 2. is closest from the current position foreach(i, star_set.count) { if(!visited_star[i]) { distance_squared = v3_distance_squared( star_set.stars[index_of_current_star].position, star_set.stars[i].position ); if(distance_squared < minimum_distance_squared || minimum_distance_squared < 0.0) { index_of_star_with_minimum_distance = i; minimum_distance_squared = distance_squared; } } } if(minimum_distance_squared >= 0.0 && index_of_star_with_minimum_distance >= 0 && index_of_star_with_minimum_distance < star_set.count) { // if a star was found, then we'll go to it... distance = sqrt(minimum_distance_squared); total_distance_travelled += distance; visited_star[index_of_star_with_minimum_distance] = 1; print("%s -> %s (%f parsecs) ", star_set.stars[index_of_current_star].name, star_set.stars[index_of_star_with_minimum_distance].name, distance); index_of_current_star = index_of_star_with_minimum_distance; } else { //... otherwise, trip is done #if RETURN_TO_ORIGIN_POINT // if we want to return to the origin point, we'll // travel back there distance = sqrt(v3_distance_squared(current_position, v3(0, 0, 0))); total_distance_travelled += distance; print("%s -> Sol (%f parsecs) ", star_set.stars[index_of_current_star], distance); index_of_current_star = 0; #endif // then, in either case, the loop should be done now. print(" Ending trip calculation... "); break; } } print("TRIP STATISTICS " "--------------- " " " "Total Distance Travelled: %f parsecs " " ", total_distance_travelled); } clean_up_star_set(&star_set); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
