Without doing anything crazy, impair readability, there is very little that can be done. You can declare the two variables on the same line, reduce blank lines and unnecessary comments, eliminate unused include
and delete unnecessary variable, as well as unnecessary parentheses.
#include <stdio.h>
#include <math.h>
typedef struct {
int x;
int y;
} ponto;
int main() {
ponto p1, p2;
printf("Digite o ponto p1(X): ");
scanf("%d", &p1.x);
printf("Digite o ponto p1(Y): ");
scanf("%d", &p1.y);
printf("Digite o ponto p2(X): ");
scanf("%d", &p2.x);
printf("Digite o ponto p2(Y): ");
scanf("%d", &p2.y);
printf("Resultado: %.2f", sqrt(pow(p2.x - p1.x, 2) + (pow(p2.y - p1.y, 2))));
return 0;
}
Otherwise I do not know if you want to change the code structure. I do not know what the requirements are. It is possible to tie a tie to further reduce it. You can create a function to handle the loading of the colon.
You can reduce the size of the code and increase its complexity. Does it pay? Are you prepared to deal with this complexity?
If you want, as shown in the comments below, then you can do this:
#include <stdio.h>
#include <math.h>
typedef struct {
int x;
int y;
} ponto;
int quadradoPontos(int p1, int p2) {
return pow(p2 - p1, 2);
}
void lePonto(char nome, char coordenada, ponto *p) {
printf("\nDigite o ponto p%c(%c): ", nome, coordenada);
if (coordenada == 'X') {
scanf("%d", &p->x);
} else {
scanf("%d", &p->y);
}
}
int main() {
ponto p1, p2;
lePonto('1', 'X', &p1);
lePonto('1', 'Y', &p1);
lePonto('2', 'X', &p2);
lePonto('2', 'Y', &p2);
printf("\nResultado: %.2f", sqrt(quadradoPontos(p1.x, p2.x) + quadradoPontos(p1.y, p2.y)));
return 0;
}
See working on ideone and in C ++ Shell .
Is it simpler? No. Is it shorter? No. If it was an exercise in creating a function. Okay. But there you should be in the question. She should post how she had tried to do the job, what problems she was having with her. The question would be different.
Obviously if you pass wrong arguments to the function, everything will go wrong. What is the advantage of doing this if it is not an example of function creation? None. Even though it is an example function, it has better examples, this only teaches to increase complexity and make the code less robust. I rude because I did not have any code. It had a semblance of repetition, but no repetition of fact. What was done has nothing to do with DRY , which would be something effect, quite the opposite.
I had to create 3 extra variables and 2 flow deviations, one of them remote from the main code, which makes it difficult to follow the flow, and I had to parameterize something I did not need. This is called complexity.
You could do it in different ways, you could have two or four functions to write on the screen and ask for data. Would it help anything? It would reduce complexity, but increase "repetition." The function that calculates the square could eliminate a little of the "repetition" and could pass the points to and solve there within the function which coordinate to use. This becomes more complex, unnecessarily, debatable gain (in fact for experienced programmers there is no discussion of which is worse).
Repeat your concept of repetition. Readability should come first. KISS .