How to add counter for each if of my simple code

1
            #include <stdio.h>
            #include <stdlib.h>
            int main()
            {
                float x,y;
                char k;
                do{
                printf("Este Programa indica o quadrante no plano cartesiano\n");
                printf("Entre com o valor de x:");
                scanf("%f",&x);
                printf("Entre com o valor de y:");
                scanf("%f",&y);
                if((x>0)&&(y>0))
                printf("O Ponto P(%.2f,%.2f) pertence ao primeiro Quadrante\n",x,y);
                else
                {
                  if((x<0)&&(y>0))
                  printf("O Ponto P(%.2f.%.2f) pertence ao segundo Quadrante\n",x,y);
                  else
                  {
                    if((x<0)&&(y<0))
                    printf("O Ponto P(%.2f,%.2f) pertence ao terceiro Quadrante\n",x,y);
                    else
                    {
                      if((x>0)&&(y<0))
                      printf("O Ponto P(%.2f,%.2f) pertence ao quarto Quadrante\n",x,y);
                      else
                      {
                        if((x==0)&&((y>0||y<0)))
                        printf("O Ponto P(%.2f,%.2f) esta no eixo y\n",x,y);
                        else
                        {
                          if((y==0)&&((x>0||x<0)))
                          printf("O Ponto P(%.2f,%.2f) esta no eixo x\n",x,y);
                          else
                          {
                            if((x==0)&&(y==0))
                            printf("O Ponto P(%.2f,%.2f) esta na origem\n\n",x,y);

                fflush(stdin);
                printf("\nDeseja digitar outro ponto. (S)/(N):\n");
                scanf("%c",&k);
                }while((k=='S')||(k=='s'));
            system("Pause");

I would like to implement a counter in this code and then write the point totals in each of the situations. For example, "Total points on the x-axis:". But I can not do it for some reason. If anyone can give a tip, thank you.

    
asked by anonymous 22.03.2016 / 02:46

1 answer

1

You did not explain exactly why you're not getting it, so I'll tell you a little about the problems I encountered and come up with a simple solution.

The first thing is indentation. When you have a lot of if / else s like this, put it like this:

if (condicao1) {
    // código
} else if (condicao2) {
    // código
} else if (condicao3) {
   // código
}

So you avoid cluttering blocks that are not logically one within the other and still makes it easier to read.

With the indentation arranged and some adjustments that I comment next, it looks like this:

#include <stdio.h>

int main() {
    char k;

    int qtdDePontosNoEixoX = 0;
    int qtdDePontosNoEixoY = 0;

    do {
        printf("Este Programa indica o quadrante no plano cartesiano\n");
        printf("Entre com o valor de x:");
        float x;
        scanf("%f",&x);
        printf("Entre com o valor de y:");
        float y;
        scanf("%f",&y);

        if((x>0)&&(y>0)) {
            printf("O Ponto P(%.2f,%.2f) pertence ao primeiro Quadrante\n",x,y);
        } else if((x<0)&&(y>0)) {                    
            printf("O Ponto P(%.2f.%.2f) pertence ao segundo Quadrante\n",x,y);
        } else if((x<0)&&(y<0)) {
            printf("O Ponto P(%.2f,%.2f) pertence ao terceiro Quadrante\n",x,y);
        } else if((x>0)&&(y<0)) {
            printf("O Ponto P(%.2f,%.2f) pertence ao quarto Quadrante\n",x,y);
        } else if((x==0)&&((y>0||y<0))) {
            printf("O Ponto P(%.2f,%.2f) esta no eixo y\n",x,y);
            qtdDePontosNoEixoY++;
        } else if((y==0)&&((x>0||x<0))) {
            printf("O Ponto P(%.2f,%.2f) esta no eixo x\n",x,y);
            qtdDePontosNoEixoX++;
        } else if((x==0)&&(y==0)) {
            qtdDePontosNoEixoX++;
            qtdDePontosNoEixoY++;
            printf("O Ponto P(%.2f,%.2f) esta na origem\n\n",x,y);
        }

        printf("\nDeseja digitar outro ponto. (S)/(N):\n");
        getchar(); // lendo e jogando fora a quebra de linha que fica na entrada por causa do Enter
        k = getchar();
    } while(k=='S' || k=='s');

    printf("%d dos pontos que você digitou estavam no eixo X e %d estavam no eixo Y\n", 
              qtdDePontosNoEixoX, qtdDePontosNoEixoY);

}

I removed the #include <stdlib.h> , which is not being used. It's interesting to know what you include and why, so you do not add anything to your executable at all.

I moved the declaration of variables x and y to near their scanf . Try to declare your variables always close to where you are using them. So you avoid polluting a namespace that is larger than you need and having to go up and down the code to find the type of variables or their initial value.

I removed the fflush(stdin) . The fflush function only applies to output streams . For inbound flows it has undefined behavior . I've put a simple getchar() to read this Enter and throw away and that's enough for that simple case (if we start to make error checks at the production level, we would have to tinker with a lot more thing).

As for the solution, after organizing the code, it is easy to see where to move: just create two variables, one for each axis, initialize with 0 and increment in 1 in appropriate cases.

    
22.03.2016 / 04:29