所以我编译程序并像./beetle int一样运行它,基本上是可执行文件和2个int,我想测试可执行文件后是否有超过2个参数,如果有一个不是int的参数(如 300b),如果有一个参数是浮点数,而不是整数,并且如果参数溢出
我坚持如何对非整数和溢出进行测试,有人能指出我正确的方向吗?
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define PI 3.14159265
void beetleSimulation(int, int);
int main ( int argc, char *argv[] )
{
char * pEnd;
if ( argc != 3 ) // argc should be 2 for correct execution
{
// If the number of arguments is not 2
fprintf(stderr, "Error: Program has %d arguments ", argc);
return 0;
}
else
{
//run the beetle simulation
beetleSimulation(strtol(argv[1], &pEnd, 10), strtol(argv[2], &pEnd, 10 ));
if (!*pEnd)
printf("Success!");
else
printf("Failed!");
return 0;
}
}
void beetleSimulation(int size, int iterations){
int i;
double xCount;
double yCount;
int timeCount;
int overallCount = 0;
int degree;
double radian;
int border;
for(i=0; i < iterations; i++){
xCount = 0;
yCount = 0;
timeCount = 0;
border -= size;
while(xCount < size && xCount > border && yCount <size && yCount >border ){
timeCount += 1;
degree = rand() % 360;
radian = degree / (180 * PI);
xCount += sin(radian);
yCount += cos(radian);
printf("Radian is %f\n", radian);
printf("X and Y are %f and %f\n", xCount, yCount);
}
//when beetle has died, add time it took to overall count, then go through for loop again
overallCount += timeCount;
printf("Time for this run is %d, overall its %d\n",timeCount, overallCount);
}
//calculate average time
double averageTime = overallCount/iterations;
printf("Average Time is %f",averageTime);
}