|
|
|
@ -3,6 +3,7 @@
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
#include "dag_data_split.h" // 包含数据处理的API
|
|
|
|
|
|
|
|
|
|
unsigned long int fib(unsigned long int n) {
|
|
|
|
|
if (n <= 1) return n;
|
|
|
|
@ -12,41 +13,32 @@ unsigned long int fib(unsigned long int n) {
|
|
|
|
|
int main() {
|
|
|
|
|
char buffer[1024]; // Buffer to store input data
|
|
|
|
|
|
|
|
|
|
ssize_t bytes_read = read(0, buffer, sizeof(buffer) - 1);
|
|
|
|
|
ssize_t bytes_read = read(0, buffer, sizeof(buffer));
|
|
|
|
|
if (bytes_read < 0) {
|
|
|
|
|
perror("Error reading input");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
buffer[bytes_read] = '\0'; // Null-terminate the string
|
|
|
|
|
|
|
|
|
|
// Parse the expected data length from the first 4 bytes for the first number
|
|
|
|
|
if (bytes_read < 4) {
|
|
|
|
|
fprintf(stderr, "Input is too short to contain valid data length.\n");
|
|
|
|
|
DataNode *dataList = splitData(buffer, bytes_read);
|
|
|
|
|
if (dataList == NULL) {
|
|
|
|
|
fprintf(stderr, "Failed to split data.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// First number length and number
|
|
|
|
|
uint32_t first_num_length;
|
|
|
|
|
memcpy(&first_num_length, buffer, sizeof(uint32_t));
|
|
|
|
|
char *first_num_start = buffer + 4;
|
|
|
|
|
if (bytes_read < 4 + first_num_length + 4) { // Check if there is enough data for the first number and the second length
|
|
|
|
|
fprintf(stderr, "Input does not contain enough data for the first number and the second length.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
// 读取解析出的第一个和第二个数字
|
|
|
|
|
unsigned long int num1, num2;
|
|
|
|
|
const char *firstdata = getDataNodeByIndex(dataList, 1);
|
|
|
|
|
const char *seconddata = getDataNodeByIndex(dataList, 2);
|
|
|
|
|
|
|
|
|
|
// Second number length and number
|
|
|
|
|
uint32_t second_num_length;
|
|
|
|
|
memcpy(&second_num_length, first_num_start + first_num_length, sizeof(uint32_t));
|
|
|
|
|
char *second_num_start = first_num_start + first_num_length + 4;
|
|
|
|
|
if (bytes_read < 4 + first_num_length + 4 + second_num_length) { // Check if there is enough data for the second number
|
|
|
|
|
fprintf(stderr, "Input does not contain enough data for the second number.\n");
|
|
|
|
|
if (firstdata == NULL || seconddata == NULL) {
|
|
|
|
|
fprintf(stderr, "Not enough data for two numbers.\n");
|
|
|
|
|
freeDataNodes(dataList);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read and calculate Fibonacci numbers
|
|
|
|
|
unsigned long int num1, num2;
|
|
|
|
|
if (sscanf(first_num_start, "%lu", &num1) != 1 || sscanf(second_num_start, "%lu", &num2) != 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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -61,5 +53,8 @@ int main() {
|
|
|
|
|
// Write to stdout
|
|
|
|
|
write(1, output, len);
|
|
|
|
|
|
|
|
|
|
// 清理内存
|
|
|
|
|
freeDataNodes(dataList);
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|