int main(void){ //initialize main with void return type.

long int userx; //user inputed x
long int yval; //yvalue

char buf[BUFSIZ]; //create a buffer with the defined buffer size
char *c; //character used for checking to make sure user entered an integer



while(1<2){
printf("Please input an integer value for x: "); //ask for user input

  if (fgets(buf, sizeof(buf), stdin) != NULL) //get user input and store in buffer
  {
    userx = strtol(buf, &c, 10); //convert to long

    if (buf[0] != '\n' && (*c == '\n' || *c == '\0')){ //check to make sure user actually entered a value and that value is an integer
      yval = fofx(userx); //get y value
      printf ("The value of the polynomial for x = %ld is: %ld\n", userx,yval); //display output
      break; //exit loop
    } 
    else  {
      printf ("Please enter only an integer value\n"); //user entered and invalid integer, loop repeats.
    }
  }  
}


return(0); //return zero value, aka indicate everything worked as expected
}
Пример #2
0
int main(){
	int inputNum;
	int num1;
	int num2;
	int array1[ARRAY_SIZE];
	int array2[ARRAY_SIZE];
	int results[2 * ARRAY_SIZE];
 
	fillArrayWithZeroes(array1, ARRAY_SIZE);
	fillArrayWithZeroes(array2, ARRAY_SIZE);
	fillArrayWithZeroes(results, (2 * ARRAY_SIZE));
 
	char ch;
 	scanf("%d%c%d", &num1, &ch, &num2);
 	
	printf("num1 = %d\n", num1);
	printf("num2 = %d\n", num2);
	
	//Check for bad stdin inputs
	if ((num1 >= 100) || (num1 <= -100))
    {
        printf("num1 not in the range [-100, 100]!\n");
        return -1;
    }
    else if ((num2 >= 100) || (num2 <= -100))
    {
        printf("num2 not in the range [-100, 100]!\n");
        return -1;
    }
    else if ((num1 < 1) || (num1 < 1)){
    	printf("Input for n cannot be smaller than one!");
        return -1;
    }
    else if ((num2 < 1) || (num2 < 1)){
    	printf("Input for n cannot be smaller than one!");
        return -1;
    }
    else if (ch != ' '){
		printf("Found a char that isn't space in stdin!");
		return -1;
    }
    
    fillArrayWithCoefficients(array1, num1);
    fillArrayWithCoefficients(array2, num2);
	printArray(array1, num1);
	printArray(array2, num2);
 
 	printf("f(x) = ");
	printPoly(array1, num1);
	printf("g(x) = ");
	printPoly(array2, num2);
	
	for(int i = 0; i < num2; i++) {
		for(int j = 0; j < num1; j++) {
			results[i + j] = results[i + j] + (array2[i] * array1[j]);
		}
	}
	printf("f(x)g(x) = ");
	printPoly(results, (num1 + num2 - 1));
	printf("f(1)g(1) = %d\n", fofx(results, (num1 + num2 - 1), 1));
}