Hey guys, I have a question regarding this code I made some days ago.. (works fine)

#include <stdio.h> /*para printf(),scanf()*/
#include <conio.h> /*para getch(),clrscr()*/
#include <stdlib.h>/*para exit()*/
#include <math.h>
//#include <dos.h>
//#define NUMEL 20
double f(double x);
void _error(int n);
enum{INTERVALOS};
enum{SIMPSON1_3=1,SALIR};
int n;
float a,b;
//float X[NUMEL],Fx[NUMEL];

/*Metodo de Simpson 1/3 dada una funcion*/
void Simpson1_3(double a,double b,int n,double *Area)
{
 register int i;
 double x;
 double S0,S1;
 double h;
 h=(b-a)/(2*n);
 S0=S1=0;
 for(i=1;i<=(2*n-1);++i){
   x=a+((double)i)*h;
   if(!(i%2))
	S0+=f(x);
   else
	S1+=f(x);
 }
 *Area=(h*(f(a)+4*S1+2*S0+f(b))/3.0);

  printf("\n El area es -> %5.6f \n\n\n",*Area);
  getch();
}
/*Muestra mensaje de error*/
void _error(int n)
{
  static char *msg[]={
			 "Error en los subintervalos",
			 ""
			};
  printf("\n %s",msg[n]);
  getch();
}
  /*Funcion a integrar*/
  double f(double x)
 {
   float y;
   	y = -x * x + 200;
	return y;
 }

 void LeeDatos(int opc)
{
 if(opc==SIMPSON1_3){
// clrscr();

 putchar('\n');
 printf("\n Numero de intervalos (PAR) -> ");
 }

 scanf("%d",&n);
 if((n<1)||((n%2)!=0)){          //numero de iteraciones menor que 0 o impares
  _error(INTERVALOS);
  exit(1);
 }
 else
  printf("\n Valor de a =>");
  scanf("%f",&a);
  printf("\n Valor de b =>");
  scanf("%f",&b);
}
 /*Muestra el menu en la pantalla*/
 void Menu(char *titulo,char *opciones[])
 {
	int i;
//	clrscr();
	i=0;
//	gotoxy(34,4);
	printf("%s\n\n",titulo);
	for(;*opciones[i];i++)
	printf("\t\t\t\t %d.- %s\n",i+1,opciones[i]);
	printf("\t\t\t\t %d.- Salir\n",i+1);
	putchar('\n');
	printf("\t\t\t\t Opcion: ");
 }
int main(void)
{
  double Area;

  int op;
  char *opciones[]={"Simpson 1/3",

					""
				   };
  do{
 //  clrscr();
   Menu("Metodos de Integracion",opciones);
   scanf("%d",&op);
   switch(op){
	 case SIMPSON1_3:    LeeDatos(SIMPSON1_3);
						 Simpson1_3(a,b,n,&Area);
						 break;

	 case SALIR:
	 //     gotoxy(34,20);
						 printf(" Fin de %s",__FILE__);
//
						 exit(0);
						 break;
	 default:
	 // gotoxy(34,13);
						 printf("Opcion no permitida. \n\n\n" );
						 getch();
						 break;

	}
  }while(op!=SALIR);

}

basically, my professor wants the program to ask for the ecuation instead of having it in the code.. (in this case, the ecuation is -x * x + 200 ). How could i proceed?

Thanks a lot!

Recommended Answers

All 3 Replies

@OP

You could try using scanf() in this way:

/* enter eqation */
scanf("%dx %c %dx %c %d", &v1, &v2, &v3) ;

eg:
1x + 2x - 3

But in this case, you cannot give -x in the beginning as in your example, you will need to enter 1x.

Another solution is to take input as a string and scan it.

@OP

You could try using scanf() in this way:

/* enter eqation */
scanf("%dx %c %dx %c %d", &v1, &v2, &v3) ;

eg:
1x + 2x - 3

But in this case, you cannot give -x in the beginning as in your example, you will need to enter 1x.

Another solution is to take input as a string and scan it.

ok, i think i get the idea.. but could you show me how to implement the code with the string?

thx!

With the string, it becomes a little complicated.

Just an idea:

1) loop till you get an 'x'.Store it in an array. Then, check if the first character is a '-'. If yes, convert the rest of the array into a number. else, the entire newly copied array is a number.

Repeat this procedure till you reach the end of the string. Hope you get the idea.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.