mirror of
https://github.moeyy.xyz/https://github.com/TheAlgorithms/C.git
synced 2023-10-11 15:56:24 +08:00
added stdint.h for fixed width ints
This commit is contained in:
parent
6a09ade47d
commit
860b1fd501
@ -1,8 +1,9 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h> /* for memmove */
|
#include <stdint.h>
|
||||||
|
#include <string.h> /* for memmove */
|
||||||
|
|
||||||
int main(int argc, char* argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
int position = 0, num_bad_chars = 0;
|
int position = 0, num_bad_chars = 0;
|
||||||
int num_digits = 4;
|
int num_digits = 4;
|
||||||
@ -12,15 +13,15 @@ int main(int argc, char* argv[])
|
|||||||
int64_t prod = 1, max_prod = 0;
|
int64_t prod = 1, max_prod = 0;
|
||||||
|
|
||||||
/* if second command-line argument is given,
|
/* if second command-line argument is given,
|
||||||
* use it as the number of digits to compute
|
* use it as the number of digits to compute
|
||||||
* successive product for
|
* successive product for
|
||||||
*/
|
*/
|
||||||
if (argc == 2)
|
if (argc == 2)
|
||||||
num_digits = atoi(argv[1]);
|
num_digits = atoi(argv[1]);
|
||||||
|
|
||||||
/* allocate memory to store past values */
|
/* allocate memory to store past values */
|
||||||
buffer = calloc(num_digits, sizeof(uint8_t));
|
buffer = calloc(num_digits, sizeof(uint8_t));
|
||||||
if(!buffer)
|
if (!buffer)
|
||||||
{
|
{
|
||||||
perror("Unable to allocate memory for buffer");
|
perror("Unable to allocate memory for buffer");
|
||||||
return -1;
|
return -1;
|
||||||
@ -31,57 +32,58 @@ int main(int argc, char* argv[])
|
|||||||
if (!fp)
|
if (!fp)
|
||||||
{
|
{
|
||||||
perror("Unable to open file");
|
perror("Unable to open file");
|
||||||
free(buffer); /* free allocated memory */
|
free(buffer); /* free allocated memory */
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* loop through all digits in the file */
|
/* loop through all digits in the file */
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
/* get character from file */
|
/* get character from file */
|
||||||
ch = getc(fp);
|
ch = getc(fp);
|
||||||
|
|
||||||
/* the ASCII codes of digits is between 0x30 and 0x39.
|
/* the ASCII codes of digits is between 0x30 and 0x39.
|
||||||
* any character not in this range implies an invalid character
|
* any character not in this range implies an invalid character
|
||||||
*/
|
*/
|
||||||
if (ch < 0x30 || ch > 0x39)
|
if (ch < 0x30 || ch > 0x39)
|
||||||
{
|
{
|
||||||
num_bad_chars ++; /* this is used to get the bad characters in the sequence of 13 characters */
|
num_bad_chars++; /* this is used to get the bad characters in the sequence of 13 characters */
|
||||||
continue;
|
continue;
|
||||||
} else if (num_bad_chars > 0)
|
}
|
||||||
num_bad_chars --;
|
else if (num_bad_chars > 0)
|
||||||
|
num_bad_chars--;
|
||||||
num = ch - 0x30; /* convert character digit to number */
|
|
||||||
num_prev = buffer[0]; /* previous n^th digit */
|
num = ch - 0x30; /* convert character digit to number */
|
||||||
|
num_prev = buffer[0]; /* previous n^th digit */
|
||||||
|
|
||||||
/* left shift the buffer -
|
/* left shift the buffer -
|
||||||
* using a for loop or a faster memory move
|
* using a for loop or a faster memory move
|
||||||
*/
|
*/
|
||||||
memmove(buffer, buffer+1, num_digits-1);
|
memmove(buffer, buffer + 1, num_digits - 1);
|
||||||
/*
|
/*
|
||||||
for (int i = 1; i < num_digits; i++)
|
for (int i = 1; i < num_digits; i++)
|
||||||
buffer[i-1] = buffer[i];
|
buffer[i-1] = buffer[i];
|
||||||
*/
|
*/
|
||||||
|
|
||||||
buffer[num_digits-1] = num; /* save the latest number in buffer */
|
buffer[num_digits - 1] = num; /* save the latest number in buffer */
|
||||||
|
|
||||||
if (num_prev != 0)
|
if (num_prev != 0)
|
||||||
{
|
{
|
||||||
/* since product is accumulated, the new product can be obtained by simply
|
/* since product is accumulated, the new product can be obtained by simply
|
||||||
* multiplying the new digit and dividing with the oldest digit
|
* multiplying the new digit and dividing with the oldest digit
|
||||||
*/
|
*/
|
||||||
prod /= num_prev; /* divide first to avoid over-flows */
|
prod /= num_prev; /* divide first to avoid over-flows */
|
||||||
prod *= num;
|
prod *= num;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
prod = 1;
|
prod = 1;
|
||||||
for(int i = 0; i < num_digits; i++)
|
for (int i = 0; i < num_digits; i++)
|
||||||
{
|
{
|
||||||
if(buffer[i] == 0)
|
if (buffer[i] == 0)
|
||||||
{
|
{
|
||||||
prod = 0;
|
prod = 0;
|
||||||
break; /* break innermost for-loop */
|
break; /* break innermost for-loop */
|
||||||
}
|
}
|
||||||
prod *= buffer[i];
|
prod *= buffer[i];
|
||||||
}
|
}
|
||||||
@ -93,14 +95,14 @@ int main(int argc, char* argv[])
|
|||||||
max_prod = prod;
|
max_prod = prod;
|
||||||
position = ftell(fp) - num_bad_chars - num_digits - 1;
|
position = ftell(fp) - num_bad_chars - num_digits - 1;
|
||||||
}
|
}
|
||||||
} while(!feof(fp)); /* loop till end of file is reached */
|
} while (!feof(fp)); /* loop till end of file is reached */
|
||||||
|
|
||||||
printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod, position);
|
printf("Maximum product: %lld\t Location: %d^th position\n\t", max_prod, position);
|
||||||
fseek(fp, position, SEEK_SET); /* move cursor to identified position in file */
|
fseek(fp, position, SEEK_SET); /* move cursor to identified position in file */
|
||||||
/* loop through all digits */
|
/* loop through all digits */
|
||||||
for (; num_digits > 0; num_digits--)
|
for (; num_digits > 0; num_digits--)
|
||||||
{
|
{
|
||||||
char ch = getc(fp); /* get character */
|
char ch = getc(fp); /* get character */
|
||||||
/* skip invalid character */
|
/* skip invalid character */
|
||||||
if (ch < 0x30 || ch > 0x39)
|
if (ch < 0x30 || ch > 0x39)
|
||||||
continue;
|
continue;
|
||||||
@ -110,8 +112,8 @@ int main(int argc, char* argv[])
|
|||||||
printf("%c = %lld\n", ch, max_prod);
|
printf("%c = %lld\n", ch, max_prod);
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(fp); /* close file */
|
fclose(fp); /* close file */
|
||||||
free(buffer); /* free allocated memory */
|
free(buffer); /* free allocated memory */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user