|
|
|
@ -1,8 +1,9 @@
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
|
|
// 假设的 Fibonacci 函数实现
|
|
|
|
|
unsigned long int fib(unsigned long int n) {
|
|
|
|
|
if (n <= 1) return n;
|
|
|
|
|
return fib(n - 1) + fib(n - 2);
|
|
|
|
@ -17,28 +18,29 @@ int main() {
|
|
|
|
|
perror("Error reading input");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
buffer[bytes_read] = '\0'; // Null-terminate the string
|
|
|
|
|
buffer[bytes_read] = '\0';
|
|
|
|
|
|
|
|
|
|
// Variables to store the two numbers
|
|
|
|
|
unsigned long int num1, num2;
|
|
|
|
|
|
|
|
|
|
// Split the input into two parts based on newline and parse them
|
|
|
|
|
char *line = strtok(buffer, "+");
|
|
|
|
|
if (line != NULL && sscanf(line, "%lu", &num1) == 1) {
|
|
|
|
|
line = strtok(NULL, "\n");
|
|
|
|
|
if (line != NULL && sscanf(line, "%lu", &num2) == 1) {
|
|
|
|
|
char *line = strtok(buffer, "&");
|
|
|
|
|
char *second_part = strtok(NULL, "\0"); // Assume the rest of the string is the second number
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
printf("Fibonacci(%lu) + Fibonacci(%lu) = %lu + %lu = %lu\n", num1, num2, fib1, fib2, sum);
|
|
|
|
|
} else {
|
|
|
|
|
printf("Invalid input. Please enter two numbers on separate lines.\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
// 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);
|
|
|
|
|
} else {
|
|
|
|
|
printf("Invalid input. Please enter two numbers on separate lines.\n");
|
|
|
|
|
const char *error_msg = "Invalid input. Please enter two numbers separated by .\n";
|
|
|
|
|
write(1, error_msg, strlen(error_msg));
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|