|
|
|
@ -2,8 +2,8 @@
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
|
|
// 假设的 Fibonacci 函数实现
|
|
|
|
|
unsigned long int fib(unsigned long int n) {
|
|
|
|
|
if (n <= 1) return n;
|
|
|
|
|
return fib(n - 1) + fib(n - 2);
|
|
|
|
@ -12,26 +12,44 @@ unsigned long int fib(unsigned long int n) {
|
|
|
|
|
int main() {
|
|
|
|
|
char buffer[1024]; // Buffer to store input data
|
|
|
|
|
|
|
|
|
|
// Read data from stdin into buffer
|
|
|
|
|
ssize_t bytes_read = read(0, buffer, sizeof(buffer) - 1);
|
|
|
|
|
if (bytes_read < 0) {
|
|
|
|
|
perror("Error reading input");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
buffer[bytes_read] = '\0';
|
|
|
|
|
buffer[bytes_read] = '\0'; // Null-terminate the string
|
|
|
|
|
|
|
|
|
|
// Remove potential newline character at the end of the input
|
|
|
|
|
if (bytes_read > 0 && (buffer[bytes_read - 1] == '\n' || buffer[bytes_read - 1] == '\r')) {
|
|
|
|
|
buffer[bytes_read - 1] = '\0';
|
|
|
|
|
// 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");
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read and calculate Fibonacci numbers
|
|
|
|
|
unsigned long int num1, num2;
|
|
|
|
|
char *line = strtok(buffer, "&");
|
|
|
|
|
char *second_part = strtok(NULL, "&"); // Assume the rest of the string is the second number
|
|
|
|
|
if (sscanf(first_num_start, "%lu", &num1) != 1 || sscanf(second_num_start, "%lu", &num2) != 1) {
|
|
|
|
|
fprintf(stderr, "Failed to parse the numbers correctly.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (line && sscanf(line, "%lu", &num1) == 1 &&
|
|
|
|
|
second_part && sscanf(second_part, "%lu", &num2) == 1) {
|
|
|
|
|
// Calculate Fibonacci numbers and their sum
|
|
|
|
|
unsigned long int fib1 = fib(num1);
|
|
|
|
|
unsigned long int fib2 = fib(num2);
|
|
|
|
|
unsigned long int sum = fib1 + fib2;
|
|
|
|
@ -42,13 +60,6 @@ int main() {
|
|
|
|
|
|
|
|
|
|
// Write to stdout
|
|
|
|
|
write(1, output, len);
|
|
|
|
|
} else {
|
|
|
|
|
const char *error_msg = "Invalid input. Please enter two numbers separated by '&'. Your input was: ";
|
|
|
|
|
char output_buffer[2048]; // Buffer to hold the error message and user input
|
|
|
|
|
int len = snprintf(output_buffer, sizeof(output_buffer), "%s%s\n", error_msg, buffer);
|
|
|
|
|
write(1, output_buffer, len);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|