Without doing very crazy things and damaging the readability, there is very little that can be done. You can declare the two variables on the same line, reduce the blank lines and unnecessary comments, delete the #include
without use and delete the variable that is not required, as well as removing 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))));
}
Otherwise I don’t know if I want to change the structure of the code. I don’t know what the requirements are. It is possible to make a loop to reduce it further. It is possible to create a function to handle the loading of the two points.
You can reduce code size and increase complexity. Pays off? You’re prepared to deal with this complexity?
If you want, as shown in the comments below, then you can do it this way:
#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)));
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
Did it get simpler? No. Did it get shorter? No. If it was an exercise about creating a function, okay. But then it should be in the question. I should post how I had tried to do the job, what problems I was having with it. The question would be another.
Obviously if you pass wrong arguments to the function, it will all go wrong. What is the advantage of doing this if not an example of creating a function? None. Even if it is an example of a function, it has better examples, it only teaches to increase complexity and make the code less robust. All this because there was no repetition of any code. It had an appearance of repetition, but not actual repetition. What was done has nothing to do with DRY, that it would be something effect, quite the contrary.
I had to create 3 extra variables and 2 flow deviations, one of them remote to the main code, which makes it difficult to follow the flow, and I had to parameterize something I didn’t need. This is called complexity.
It could do in different ways, it could have two or four functions to write on the screen and ask for data. Would it help in something? It would decrease complexity, but increase "repetition". The function that computes the square could eliminate some of the "repetition" and could pass the points to and solve there within the function which coordinate to use. This getting more complex, needless, arguable gain (actually for experienced programmers there is no discussion of which is worse).
Rethink your concept of repetition, legibility must come first: KISS.
I could do it, but I’ll just give you a few ways. When a code repeats too much something is wrong, or the code could be improved, this is your case. Try to separate the reading of data, create a method where you send the struct by reference, Ole as you will display the text to the user is quite easy and you can!
– Guilherme Guini