Skip to content

Commit

Permalink
Adding input validation (C, C++, C#, Java)
Browse files Browse the repository at this point in the history
  • Loading branch information
dan-c-underwood committed Aug 6, 2015
1 parent 059ad4a commit 485e044
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
13 changes: 11 additions & 2 deletions sieve-of-eratosthenes/c/sieve_c.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <ctype.h>

int main(int argc, char const** argv)
{
Expand All @@ -11,6 +12,14 @@ int main(int argc, char const** argv)
return 1;
}

for (int pos = 0; pos < strlen(argv[1]); pos++)
{
if(!isdigit(argv[1][pos]))
{
return 1;
}
}

unsigned int max = atoi(argv[1]);
unsigned int arraySize = max * sizeof(unsigned char);
clock_t begin = clock();
Expand Down Expand Up @@ -38,7 +47,7 @@ int main(int argc, char const** argv)
}

clock_t end = clock();
float duration = (((float)end - (float)begin) / 1000000.0F ) * 1000;
float duration = (((float)end - (float)begin) / 1000000.0F ) * 1000;
printf("Number of primes: %i\n", numPrimes);
printf("Execution time: %fms\n", duration);

Expand Down
11 changes: 10 additions & 1 deletion sieve-of-eratosthenes/cpp/sieve_cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@
#include <cstdlib>
#include <math.h>
#include <stdio.h>
#include <ctype.h>
#include <chrono>
using namespace std;

int main(int argc, char const** argv)
{
if (argc != 2)
if(argc != 2)
{
return 1;
}

for (int pos = 0; pos < strlen(argv[1]); pos++)
{
if(!isdigit(argv[1][pos]))
{
return 1;
}
}

int max = atoi(argv[1]);

auto begin = chrono::high_resolution_clock::now();
Expand Down
22 changes: 19 additions & 3 deletions sieve-of-eratosthenes/csharp/sieve_csharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@

public class PrimeSieve
{
public static void Main(string[] args)
static void primes(int max)
{
var max = int.Parse(args[0]);

var primes = new bool[max];

primes[0] = true;
Expand Down Expand Up @@ -34,5 +32,23 @@ public static void Main(string[] args)
sw.Stop();
Console.WriteLine("Number of primes: {0}", foundPrimes);
Console.WriteLine("Execution time: {0}ms", sw.ElapsedMilliseconds);
}

public static int Main(string[] args)
{
try {
var max = int.Parse(args[0]);
primes(max);
return 0;
}
catch (FormatException) {
return 1;
}
catch (OverflowException) {
return 1;
}
catch (IndexOutOfRangeException) {
return 1;
}
}
}
17 changes: 13 additions & 4 deletions sieve-of-eratosthenes/java/sieve_java.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@

public class sieve_java
{

public static void main(String[] args)
static void primes(int max)
{

int max = Integer.parseInt(args[0]);
DecimalFormat df = new DecimalFormat("#.###"); df.setRoundingMode(RoundingMode.CEILING);
long startTime = System.nanoTime();

Expand Down Expand Up @@ -42,4 +39,16 @@ public static void main(String[] args)
System.out.println("Execution time: " +
df.format((endTime - startTime)/1e6) + "ms");
}

public static void main(String[] args)
{

try {
int max = Integer.parseInt(args[0]);
primes(max);
}
catch(NumberFormatException | ArrayIndexOutOfBoundsException e) {
System.exit(1);
}
}
}

1 comment on commit 485e044

@dan-c-underwood
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First part of fix for #11

Please sign in to comment.