/* Program to find various properties of triangles Original layout by Paul Davey, 2007 Debugging and restructure by Jonathan Love, 2007 Notes: - C99 Compatible: - all 'for' loops replaced with 'while' (Jonathan) - Standard variable types - no strings or files (Paul) - Used Defines for boolean replacement. (Jonathan) - All vars and functions renamed appropriately according to their return - e.g. iCalcPerimeter returns and integer, fCalcArea returns float (Jonathan) - System independent - no system calls (Paul) - Adjusted call to fCalcArea to fix iCalcPerimeter call (Jonathan) - Algorithms implemented, except isRightAngled (Paul) - Menu System (Paul) - Functional Program (Jonathan) THIS PROGRAM WILL COMPILE WITH GCC! */ #include #include #include //Constants and Defines #define TRUE 1; #define FALSE 0; void fnClear() { //Clear the screen - no library dependence, but puts cursor at screen bottom int iCounter = 0; while (iCounter < 25) //Assume 25 is screen height { printf("\n"); iCounter++; } } int iCalcPerimeter(int iSides[]) { //Calculate Perimeter int iPerimeter = 0; int iCounter = 0; while (iCounter < 3) { iPerimeter += iSides[iCounter]; iCounter++; } return iPerimeter; } int isRightAngled(int iSides[]) { //Enter isRightAngled algorithm here return FALSE; } float fCalcArea(int iSides[]) { //Calculate Area float fS = iCalcPerimeter(iSides)/2; return sqrt(fS*(fS - iSides[0])*(fS - iSides[1])*(fS - iSides[2])); } int main(int argc, char *argv[]) { //Integers int iSides[3]; int iResponse; //Booleans - which are actually integers int isExit = FALSE; int isInputDone = FALSE; //Chars and Char Pointers (Strings) char cYesNo; while (!isExit) { //Main Menu isInputDone = FALSE; //Restore false status iResponse = 0; //Get three Sides printf("Please enter the lengths of the three sides of a triangle:"); printf("\nSide 1: "); scanf("%d",&iSides[0]); printf("Side 2: "); scanf("%d",&iSides[1]); printf("Side 3: "); scanf("%d",&iSides[2]); //Do until input done = true (IE new triangle) while (!isInputDone) { fnClear(); //Ask for a menu option. Usually clear screen here printf("1: Find the perimeter of the triangle.\n"); printf("2: Find if the triangle is right angled or not.\n"); printf("3: Find the area of the triangle\n"); printf("4: Enter a new triangle\n"); printf("5: Quit Program\n\n"); printf("Please enter the number of the option you want: "); //Get input response scanf("%d", &iResponse); printf("\n"); switch (iResponse) { case 1: //Find the perimeter printf("The perimeter is:%d\n", iCalcPerimeter(iSides)); break; case 2: //Find whether triangle is right-angled isRightAngled(iSides); printf("Not Implemented Yet\n"); break; case 3: //Determine triangle area printf("The area is:%f\n",fCalcArea(iSides)); break; case 4: //Get new triangle isInputDone = TRUE; break; case 5: //Input is done and we want to exit isInputDone = TRUE; isExit = TRUE; break; default: printf("Please enter a number from the menu:"); break; } printf("\n\n"); //Print some newlines } } return 0; }