You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6 KiB

#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include "dag_data_split.h"
unsigned long int fib(unsigned long int n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}
int main() {
char buffer[1024]; // Buffer to store input data
ssize_t bytes_read = read(0, buffer, sizeof(buffer));
if (bytes_read < 0) {
perror("Error reading input");
return 1;
}
DataNode *dataList = splitData(buffer, bytes_read);
if (dataList == NULL) {
fprintf(stderr, "Failed to split data.\n");
return 1;
}
unsigned long int num1, num2;
const char *firstdata = getDataNodeByIndex(dataList, 1);
const char *seconddata = getDataNodeByIndex(dataList, 2);
if (firstdata == NULL || seconddata == NULL) {
fprintf(stderr, "Not enough data for two numbers.\n");
freeDataNodes(dataList);
return 1;
}
if (sscanf(firstdata, "%lu", &num1) != 1 || sscanf(seconddata, "%lu", &num2) != 1) {
fprintf(stderr, "Failed to parse the numbers correctly.\n");
freeDataNodes(dataList);
return 1;
}
unsigned long int fib1 = fib(num1);
unsigned long int fib2 = fib(num2);
unsigned long int sum = fib1 + fib2;
// Prepare output string
char output[1024];
int len = snprintf(output, sizeof(output), "Fibonacci(%lu) + Fibonacci(%lu) = %lu + %lu = %lu\n", num1, num2, fib1, fib2, sum);
// Write to stdout
write(1, output, len);
// <20><><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
freeDataNodes(dataList);
return 0;
}