2017-10-19 2 views
-3

Ich bin nicht sicher, was ich falsch mache. Einer der Fehler ist, dass das Rechteck zuerst in Int main() erwähnt wird. Ich brauche das Programm, um den Benutzer nach den Dimensionen von 2 Rechtecken zu fragen und einige Berechnungen zu machen und diese Werte zurückzugeben. Ich möchte auch, dass es irgendwie die Strukturnamen in die Header-Datei einbaut. dankeProgramm stürzt immer wieder ab, andere Probleme mit Programm

rectangle2.h

struct rectangle 
{ 
    double length;  // variable to store length 
    double width;  // variable to store width 
}; 

// function to calculate the area 
double area( struct rectangle jane ); 
// function to calculate the perimeter 
double perimeter(struct rectangle luis); 
// function to calculate the diagonal length from one corner to another 
double diagonal(struct rectangle adrian); 
// function to determine if the rectangle is a square 
// returns true when it is a square, false when it is not 
bool isSquare(struct length fernie); 
// function to determine whether the rectangle is golden 
// https://en.wikipedia.org/wiki/Golden_rectangle 
// (a + b)/a is equal to a/b 
// returns true when it is a golden rectangle, false when it is not 
bool isGolden(struct length claudia); 
// function to determine if two rectangles are similar 
// two rectangles are similar if the ratio of the length and width of 
// one rectangle is equal to the ratio of the length and width of 
// the other rectangle 
bool areSimilar(struct rectangle pedro, struct rectangle omar); 

rectangle.c

#include "rectangle2.h" 
#include <stdio.h> 
#include <stdbool.h> 

double area(struct rectangle jane) //to calculate area of rectangle 
{ 
return jane.width * jane.length; 
} 

double perimeter(struct rectangle luis) //to calculate perimeter of rectangle 
{ 
return 2 * (luis.length + luis.width); 
} 

double diagonal(struct rectangle adrian) //to calculate diagonal of rectangle 
{ 
return (adrian.length * adrian.length) + (adrian.width * adrian.width); 
} 

bool isSquare(struct length fernie ) //checks if rectangles are square 
{ 
    if((fernie.width * fernie.length) == (fernie.length * fernie.length)) 
     return true; 

    else 
     return false; 
} 

bool isGolden(struct length claudia) //checks if rectangles are golden 
{ 
if(((claudia.width + claudia.length)/claudia.width) == (claudia.width/claudia.length)) 
return true; 

else 
return false; 
} 

bool areSimilar(struct rectangle pedro, struct rectangle omar) //checks if rectangles are similar 
{ 
if((pedro.length/pedro.width) == (omar.length/omar.width)) 
return true; 

else 
return false; 
} 

main.c

int main() 

{ 
struct rectangle sides; 
sides.length; 
sides.width; 


//asks the user for the length and width for 2 rectangles 
printf("\nEnter dimensions of Rectangle 1: "); 

printf("\nEnter Length: "); 

scanf("%lf" , sides.length); 

printf("\nEnter Width: "); 

scanf("%lf" , sides.width); 
printf("\nEnter dimensions of Rectangle 2: "); 

printf("\nEnter Length: "); 

scanf("%lf",sides.length); 

printf("\nEnter Width: "); 

scanf("%lf" , sides.width); 


//printing statements after all calculations have been made 

printf("\nArea of Rectangle 1 is: %lf" , area(&jane)); 
printf("\nArea of Rectangle 2 is: %lf",area(rectangle.jane)); 

printf("\nPerimeter of Rectangle 1: %lf" , perimeter(rectangle.rec1.luis)); 
printf("\nPerimeter of Rectangle 2: %f",perimeter(rectangle.rec2.luis)); 

printf("\nDiagonal of Rectangle 1: %lf" , diagonal(rectangle.rec1.adrian)); 
printf("\nDiagonal of Rectangle 2: %lf" , diagonal(rectangle.rec2.adrian)); 

return 0; 
} 
+3

"Keeps Absturz" und "andere Probleme" sind nicht wirklich beschreibend. Darf ich Sie auf z.B. https://ericlippert.com/2014/03/05/how-to-debug-small-programs/? – Evert

+0

Auf den ersten Blick kann Ihr Programm nicht einmal kompilieren: Es gibt einen Verweis auf eine 'jane'-Variable in' main() ', und ein' jane'-Mitglied einer 'rectange'-Struktur, die beide nicht existieren. – Evert

+0

Kompiliert nicht einmal. Was soll 'rectangle.jane' in' main() 'sein? Es gibt keine Variable mit dem Namen 'rectangle' in' main() '. – AlexP

Antwort

0

Ihr Programm nicht aus mehreren Gründen zusammenstellen können.

rectangle2.h ist meistens in Ordnung.

Sie müssen den Parameter der Funktionen isSquare und isGolden von struct length zu struct rectangle ändern, weil struct length nicht definiert ist.

hier ist eine Version der gesamten Datei, wie Sie es vielleicht in Betracht gezogen haben funktioniert.

rectangle2.h

typedef enum {false, true = !false} bool; 

struct rectangle 
{ 
    double length; 
    double width; 
}; 

double area(struct rectangle jane); 
double perimeter(struct rectangle luis); 
double diagonal(struct rectangle adrian); 
bool isSquare(struct rectangle fernie); 
bool isGolden(struct rectangle claudia); 
bool areSimilar(struct rectangle pedro, struct rectangle omar); 

in rectangle.c Sie müssen stdio.h nicht enthalten, wie Sie in dieser Datei drucken und scannen nicht. Sie müssen math.h für Funktion Diagonale mathematisch korrekt enthalten. Wenn Sie Probleme damit haben, funktioniert es so, wie es war, ohne sqrt().
Sie auch struct length mit struct rectangle in Funktionen isSquare und isGolden passen die Prototypen in rectangle2.h ersetzen müssen.

rectangle.c

#include "rectangle2.h" 
#include <math.h> 

double area(struct rectangle jane) 
{ 
    return jane.width * jane.length; 
} 

double perimeter(struct rectangle luis) 
{ 
    return 2 * (luis.length + luis.width); 
} 

double diagonal(struct rectangle adrian) 
{ 
    return sqrt(adrian.length*adrian.length + adrian.width*adrian.width); 
} 

bool isSquare(struct rectangle fernie) 
{ 
    return (fernie.width == fernie.length); 
} 

bool isGolden(struct rectangle claudia) 
{ 
    double p = (claudia.length + claudia.width)/claudia.length; 
    double q = claudia.length/claudia.width; 
    return p == q; 
} 

bool areSimilar(struct rectangle pedro, struct rectangle omar) 
{ 
    return (pedro.length/pedro.width) == (omar.length/omar.width); 
} 

und schließlich ist Ihr main.c das Schlimmste. Ich bin mir sicher, dass Sie herausfinden werden, was daran falsch war, indem Sie auf mein Beispiel schauen, aber ansonsten fragen Sie frei.

main.c

#include "rectangle2.h" 
#include <stdio.h> 

struct rectangle rect1, rect2; 
char *s; 

int main() 

{ 
    printf("\nEnter dimensions of Rectangle 1:"); 
    printf("\nEnter Length: "); 
    scanf("%lf", &rect1.length); 
    printf("\nEnter Width: "); 
    scanf("%lf", &rect1.width); 

    printf("\nEnter dimensions of Rectangle 2:"); 
    printf("\nEnter Length: "); 
    scanf("%lf", &rect2.length); 
    printf("\nEnter Width: "); 
    scanf("%lf", &rect2.width); 

    printf("\nArea of Rectangle 1 is: %lf", area(rect1)); 
    printf("\nArea of Rectangle 2 is: %lf", area(rect2)); 

    printf("\nPerimeter of Rectangle 1: %lf", perimeter(rect1)); 
    printf("\nPerimeter of Rectangle 2: %lf", perimeter(rect2)); 

    s = areSimilar(rect1, rect2) ? "true" : "false"; 
    printf("\nRectangle 1 & 2 are similar: %s", s); 

    s = isSquare(rect1) ? "true" : "false"; 
    printf("\nRectangle 1 is square: %s", s); 

    printf("\nDiagonal of Rectangle 1: %lf" , diagonal(rect1)); 
    printf("\nDiagonal of Rectangle 2: %lf\n" , diagonal(rect2)); 

    return 0; 
} 

eine ausführbare Datei, damit Sie main.c und rectangle.c zu kompilieren benötigen, ist hier ein Beispiel auf einem Linux-Terminal: cc -o rec main.c rectangle.c -lm. der Schalter -lm ist da, um die Math-Bibliothek aufzunehmen. Ich denke, das gleiche Beispiel in einem Windows-Terminal borland Befehlszeile-Compiler so sein würde: bcc32 main.c rectangle.c.

Ich hoffe, das hilft. Entschuldigung, keine Zeit mehr im Moment zu schreiben.