Pagini recente » Cod sursa (job #1544833) | Cod sursa (job #2631748) | Cod sursa (job #2543129) | Cod sursa (job #2201120) | Cod sursa (job #1416934)
#include <iostream>
#include <string>
#include <fstream>
#include <stdlib.h>
using namespace std;
const char *in_file_name = "factorial.in";
const char *out_file_name = "factorial.out";
void die(bool assertion, const char *message)
{
if (assertion) {
fprintf(stderr, "(%s, %d): ",__FILE__, __LINE__);
perror(message);
exit(EXIT_FAILURE);
}
}
bool is_valid_int(const char *str)
{
while (*str)
{
if(!isdigit(*str))
return false;
else
str++;
}
return true;
}
int get_number(int P, int N)
{
int count, aux;
while (P > 0) {
count = 0;
N += 5;
aux = N;
while (true) {
if (aux % 5 == 0) {
count++;
aux /= 5;
}
else
break;
}
P -= count;
}
return N;
}
int main()
{
string line;
int P, N;
ifstream in_file;
ofstream out_file;
in_file.open(in_file_name, ios::in);
die(!in_file, "Error opening file for reading");
out_file.open(out_file_name, ios::out);
die(!out_file, "Error opening file for writing");
getline(in_file, line);
die(!is_valid_int(line.c_str()), "Invaild input");
P = atoi(line.c_str());
die(P < 0, "Number must be positive");
if (P == 0)
out_file << 1;
else
out_file << get_number(P, 0);
in_file.close();
out_file.close();
return EXIT_SUCCESS;
}