How can I execute several functions at the same time?

1

I need to execute all previously listed operations at once, that is, addition, subtraction, multiplication, division and factorial of the first number with just choosing the option; that is, without leaving the switch . How can I do it?

Do I repeat the code of each one, or is there a summary way that is not so untidy and repetitive?

    
asked by bornlivedie 27.03.2017 в 22:26
source

3 answers

6

If friend that will generate problems because it is going to rewrite its content I recommend that you create other variables for the other results Example:

int x,y,result,result2,result3,result4,result5,result6;

 case 3:
    result = suma(x,y);
    printf("RESULTADO SUMA: %d\n",result);
    break;
case 4:
    result = resta(x,y);
    printf("RESULTADO RESTA: %d\n",result2);
    break;
case 5:
    result = dividir(x,y);
    printf("RESULTADO DIVISION: %d\n",result3);
    break;
case 6:
    result = multiplicar(x,y);
    printf("RESULTADO MULTIPLICACION: %d\n",result4);
    break;
case 7:
    result = factorial(x);
    printf("RESULTADO FACTORIAL: %d\n",result5);
    break;
case 8:
    printf("RESULTADO GLOBAL: %d\n",result6);

This was what I did when that happened to me.

    
answered by 27.03.2017 в 22:41
1

It's simple, once you understand what a switch( ) is: a switch( ) is a simple goto , sweetened , and with some other extra .

int num1 = 0,
    num2 = 0;

switch( opcion ) {
case 1:
  printf( "Ingrese el primer número:\n" );
  scanf( "%d", &num1 );
  break;

case 2:
  printf( "Ingrese el segundo número:\n" );
  scanf( "%d", &num2 );
  break;

case 8:

case 3:
  printf( "Suma: %d\n", suma( num1, num2 ) );
  if( opcion == 3 )
    break;

case 4:
  printf( "Resta: %d\n", resta( num1, num2 ) );
  if( opcion == 4 )
    break;

case 5:
  printf( "Multiplicación: %d\n", multiplica( num1, num2 ) );
  if( opcion == 5 )
    break;

case 6:
  if( !num2 )
    printf( "NO se puede dividir entre 0.\n" );
  else
    printf( "Division: %d\n", divide( num1, num2 ) );

  if( opcion == 6 )
    break;

case 7:
  printf( "Factorial: %d\n", factorial( x ) );
  break;
};

As you see, it is not necessary that the labels are ordered , or that all labels do something. Neither is required break . And, what's more cool : The break can be in a conditional!

    
answered by 28.03.2017 в 12:05
0

You could check this code and give the user the choice they want to make

#include stdio.h
#include math.h

int main(){

    int x;

    double num;
    char sn[1];

    sn[0]='s';

    while((sn[0]=='s')|| (sn[0]=='S'))
    {
        printf("\n (1) El triple  ");

        printf("\n (2) El cuadrado)");

        printf("\n (3) Logaritmo natural");

        printf("\n (4) Logaritmo decimal ");

        printf("\n (5) seno ");

        printf("\n (6) coseno");

        printf("\n Elige una opcion");

        scanf("%d",&x);

switch(x){
    case 1:
        printf("\n\n Escribe el numero:");
        scanf("%lf",&num);
        printf("El triple  de %lf es %lf",num,3*num);
    break;
    case 2:
        printf("\n\n Escribe el numero:");
        scanf("%lf",&num);
        printf("\n el cuadrado de %lf es %lf",num,num*num);
        break;
    case 3:
        printf("\n\n Escribe el número: ");
        scanf("%lf",&num);
        printf("\n El logaritmo neperiano de %lf es %lf ",num,log(num));
    break;
    case 4:
        printf("\n\n Escribe el número: ");
        scanf("%lf",&num);
        printf("\n El logaritmo decimal de %lf es %lf", num,log10(num));    
    break;  
    case 5:
        printf("\n\n Escribe el número: ");
        scanf("%lf",&num);

        printf("\n\n El seno de %lf es %lf ",num,sin(num));
    break;
    case 6:
            printf("\n\n Escribe el número: ");
        scanf("%lf",&num);
        printf("\n\n El coseno de %lf es %lf ",num,cos(num));
        break;
    default:
        printf("\n\n Esto no es ninguna opcion");
        break;
    }

printf("\n\n quieres volver a realizar una operacion (S/N) ");
scanf("%s",sn);
    }
}
    
answered by 31.03.2017 в 19:26