#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_MSG_LENGTH 1000
// Function to trim a substring from 'str' starting at index 'beg' and ending at 'end',
// and store the result in 'output'.
void trim(int beg, int end, char* str, char* output) {
memset(output, '\0', MAX_MSG_LENGTH); // Clear the 'output' buffer.
for (size_t i = beg; i <= end; i++) {
output[i - beg] = str[i]; // Copy characters from 'str' to 'out'.
}
}
void handleNegativeExponent(char* num, int size, int count, int* precision_count, int& i) {
// Print the initial "0." and the required number of zeros.
printf("0.");
for (int j = 0; j < count; j++) {
printf("0"); // Print each zero for the large exponent.
(*precision_count)--; // Decrement the precision counter for each zero printed.
}
// Adjust the index in the num array to start printing from the first non-zero digit.
while (num[i] == '0' && i < size - 1) {
i++;
}
// Print the significant digits until the desired precision is reached.
while (*precision_count > 0 && i < size) {
char c = num[i++];
if (c != '.') {
printf("%c", c);
}
(*precision_count)--;
}
// Print any remaining zeros to reach the desired precision.
while (*precision_count > 0) {
printf("0");
(*precision_count)--;
}
}
void handlePositiveExponent(char* num, int size, int count, int* precision_count, int& i) {
bool is_on_comma = false; // Flag to indicate if we are on the decimal point.
bool is_on_new_comma = false; // Flag to indicate if we are on the new decimal point after the exponent shift.
bool print_dot = false; // Flag to indicate if we should print the decimal point.
// Print the number part with the correct number of decimal places.
while (*precision_count > 0) {
if (count == 0) {
is_on_new_comma = true;
print_dot = true; // Print the decimal point when the exponent count reaches 0.
}
if (print_dot) {
printf(".");
print_dot = false;
}
if (is_on_comma) {
count--; // Decrement the exponent count if we are on the comma.
}
if (is_on_new_comma) {
(*precision_count)--; // Decrement the precision counter for the new comma.
}
if (i < size) {
char c = num[i++];
if (c == '.') {
is_on_comma = true; // Set the flag if we encounter a decimal point.
} else {
printf("%c", c); // Print the character if it's not a decimal point.
}
} else {
printf("0"); // Print zero if we've reached the end of the number part.
}
}
}
int main() {
char input[MAX_MSG_LENGTH];
fgets(input, MAX_MSG_LENGTH, stdin);
char num[MAX_MSG_LENGTH];
char exponent[MAX_MSG_LENGTH];
bool to_num = true;
int counter = 0;
memset(num, '\0', MAX_MSG_LENGTH);
memset(exponent, '\0', MAX_MSG_LENGTH);
bool has_comma = false;
// Parse the input string and separate the number part and the exponent part.
for (size_t i = 0; input[i] != '\0'; i++) {
char c = input[i];
if (c == ' ') {
continue; // Skip spaces.
}
if (c == '.') {
has_comma = true; // Set the flag if a decimal point is found.
}
if (c == 'E' || c == 'e') {
to_num = false; // Switch to parsing the exponent part.
counter = 0; // Reset the counter for the exponent part.
continue;
}
if (to_num) {
num[counter++] = c; // Append character to the number part.
} else {
exponent[counter++] = c; // Append character to the exponent part.
}
}
// If there's no decimal point in the number part, add ".0" to it.
if (!has_comma) {
sprintf(num, "%s.0", num);
}
int size = strlen(num); // Get the length of the number part.
int precision_count = 8; // Set the precision to 8 decimal places.
int i = 0; // Reset the index for printing.
// Handle the case where the exponent is 0.
if (strcmp(exponent, "0") == 0) {
bool is_on_comma = false; // Flag to indicate if we are on the decimal point.
while (precision_count > 0) {
if (size > i) {
char c = num[i++];
if (is_on_comma) {
precision_count--;
}
if (c == '.') {
is_on_comma = true;
}
printf("%c", c);
} else {
printf("0");
precision_count--;
}
}
}
// Handle the case where the exponent is negative.
if (exponent[0] == '-') {
// Extract the absolute value of the exponent without the negative sign.
char newexponent[MAX_MSG_LENGTH];
int exponent_length = strlen(exponent);
trim(1, exponent_length - 1, exponent, newexponent);
// Convert the exponent to an integer.
int count = atoi(newexponent);
// Adjust the precision counter to account for the initial "0." that will be printed.
precision_count = 9; // We add 1 because we've already printed a zero.
// Ensure the precision counter does not exceed the maximum allowed precision.
if (count > 8) count = 8;
// Call the function to handle the large exponent case.
handleNegativeExponent(num, size, count, &precision_count, i);
} else {
int count = atoi(exponent); // Convert the exponent to an integer.
handlePositiveExponent(num, size, count, &precision_count, i);
}
printf("\n");
return 0;
}