void teste_somme( int bits, double c1, int e1, double c2, int e2 )
 {
  int exact, approx, erreur ;
  exact = arrondi(c1*e1+c2*e2) ;
  approx = multiplie(bits,c1,e1) + multiplie(bits,c2,e2) ;
  erreur = arrondi(1000*double(exact-approx)/exact) ;
  if (erreur<0) { erreur = -erreur ; }
  std::cout
    <<std::right<<std::setw(2)<<bits<<" bits : "
    <<std::left<<exact<<" ~ "<<approx
    <<" ("<<erreur<<"/1000)"<<std::endl ;
 }
Example #2
0
char* apply_function(int fonc,arg* argu,int nbArg){
	switch(fonc){
		case 1:
			return plus(argu,nbArg);
			break;
		case 2:
			return moins(argu,nbArg);
			break;
		case 3:
			return multiplie(argu,nbArg);
			break;
		case 4:
			return divise(argu,nbArg);
			break;
		case 5:
			return concat(argu,nbArg);
			break;
		case 6:
			return boucle();
			break;
		default:
			return NULL;	
		//TODO renvoyer erreur
			break;
	}
}
int main()
 {
  int bits ;

  std::cout<<std::endl ;
  for ( bits = 2 ; bits <= 8 ; bits = bits + 2 )
   { teste_approxime(bits,0.65) ; }

  std::cout<<std::endl ;
  for ( bits = 2 ; bits <= 8 ; bits = bits + 2 )
   { teste_approxime(bits,0.35) ; }

  std::cout<<std::endl ;
  for ( bits = 1 ; bits <= 8 ; bits = bits + 1 )
   {
    int exact = arrondi(0.65*3515+0.35*4832) ;
    int approx = multiplie(bits,0.65,3515) + multiplie(bits,0.35,4832) ;
    std::cout << bits << " bits : 0.65*3515+0.35*4832 = " << exact << " ~ " << approx << std::endl ;
   }

  std::cout<<std::endl ;
  return 0 ;
 }
Example #4
0
/* Read the RPN string and carry out the operations
	param: 	numInputTokens is the number of tokens entered via command line
	param: 	inputString is the string of tokens
	pre:	inputString is a valid string of Reverse polish Notation - supported operators +, -, x, /
	pre:	numInputTokens is > 0
	post:	none
	ret:	the size of the dynamic array
*/
double calculate(int numInputTokens, char **inputString) {
	double result = 0.0;
	char *s;
	struct DynArr *stack;

	/*set up the stack */
	stack = createDynArr(20);

	int i;

	/* start at 1 to skip the name of the calculator calc */
	for(i=1; i < numInputTokens; i++) {
		s = inputString[i];

		/* General algorithm:
		1) Check if the string 's' is in the list of operators
		1 a) If 's' is an operator, then call the corresponding function
		1 b) If 's' is not an operator, check if it is a number
		2 a) If 's' is not a number, produce an error
		2 b) If 's' is a number, push it onto the stack */

		if(strcmp(s, "+") == 0){
			add(stack);
		} else if(strcmp(s,"-") == 0){
            subtract(stack);
		} else if(strcmp(s, "/") == 0){
			/* FIXME: replace printf with your own function */
			divide(stack);
		} else if(strcmp(s, "x") == 0){
			/* FIXME: replace printf with your own function */
			multiplie(stack);
		} else if(strcmp(s, "^") == 0){
			/* FIXME: replace printf with your own function */
			exponent(stack);
		}
		else {
			/* FIXME: You need to develop the code here (when s is not an operator )*/
			printf("you did not give a math character\n");

		}
	}	//end for

	/* FIXME: You will write this part of the function
	 If the stack looks OK, then store the final value in result
	 Print the result */

	return result;
}