PA1

main.c

/*******************************************************************************************
* Programmer: Nathan Scott
* Class: CptS 121, Fall  2015; Section 01
* Programming Assignment: pa1
* Date: August 31, 2015
* Description: Design, implement, compile, and test C solutions to the following problems.
*******************************************************************************************/
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#define PI 3.1415926

int main(void){
	char plaintext_character = NULL, encoded_character = NULL;
	int a = 0, R1 = 0, R2 = 0, R3 = 0, shift = 0, userInput = 0;
	double f = 0, acc = 0, r = 0, h = 0, x1 = 0, x2 = 0, y1 = 0, y2 = 0, b = 0, c = 0, d = 0, y = 0, z = 0, x = 0,
		theta1 = 0, theta2 = 0;

	//system("mode con cols=170 lines=40");//define window size.
	system("color 5f");//change window color.

	do{//do while loop start.
		printf("\nAuthor: Nathan Scott\nVerson: 1.1.9\n");
		printf("\n-----------------MENU-----------------\n");//print out the user menu.
		printf("| 1. Newton’s Second Law of Motion  |\n");
		printf("| 2. Volume of A Cylinder            |\n");
		printf("| 3. Character Encoding              |\n");
		printf("| 4. Distance Between Two Points     |\n");
		printf("| 5. Tangent                         |\n");
		printf("| 6. Equivalent Parallel Resistance  |\n");
		printf("| 7. General Equation                |\n");
		printf("| 0. Exit                            |\n");
		printf("--------------------------------------\n\nEnter: ");


		if (scanf("%d", &userInput) != 1 || userInput > 7){//determine user enter invalid or not.
			printf("\n------------------------------------\n\nError: please enter a valid choice!\n");
			fflush(stdin);
		}
		else//if user enter valid then enter else clause.
		{
			fflush(stdin);
			if (userInput == 1)//Newton's Second Law of Motion
			{
				printf("\n------------------------------------\n\n1. Newton’s Second Law of Motion\nForce(F) = Mass(m) x Acceleration(a)\nPlease enter a value for m and a: \n");
				if (scanf("%lf%lf", &f, &acc) != 2){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else//if user enter valid then enter else clause.
				{
					printf("\nF = m x a = %.3lf x %.3lf = %.3lf\n\n", f, acc, f * acc);
				}
				fflush(stdin);
			}

			if (userInput == 2)//Volume of A Cylinder
			{
				printf("\n------------------------------------\n\n2. Volume of A Cylinder\nVolume Cylinder = PI * radius(r)^2 * height(h)\nPlease enter a value for r and h: \n");
				if (scanf("%lf%lf", &r, &h) != 2){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else//if user enter valid then enter else clause.
				{
					printf("\nvolume cylinder = PI * r^2 * h = %lf x %.3lf^2 x %.3lf = %.3lf\n\n", PI, r, h, PI * pow(r, 2) * h);
				}
				fflush(stdin);
			}

			if (userInput == 3)//Character Encoding
			{
				printf("\n------------------------------------\n\n3. Character Encoding\nPlease enter a plain text character and a shift: \n");
				if (scanf("%c%d", &plaintext_character, &shift) != 2){//determine user enter invalid or not.
					printf("\nError: please enter ONE VALID CHARACTERS and ONE INTEGER!\n\n");
				}
				else if (shift > (90 - plaintext_character) || shift > (122 - plaintext_character)){//determine user enter invalid or not.
					printf("\nError: shift value must be less than 26! \n\n");
				}
				else//if user enter valid then enter else clause.
				{
					encoded_character = (plaintext_character - 65) + 97 + shift;
					printf("\nencoded character = (plain text character - 'A') + 'a' + shift = %d - %d + %d + %d= %c\n\n", plaintext_character, 65, 97, shift, encoded_character);
				}
				fflush(stdin);
			}

			if (userInput == 4)//Distance Between Two Points
			{
				printf("\n------------------------------------\n\n4. Distance Between Two Points\nPlease enter a value for x1, x2, y1 and y2: \n");
				if (scanf("%lf%lf%lf%lf", &x1, &x2, &y1, &y2) != 4){//determine user enter invalid or not.
					printf("\nError: please enter a FOUR VALID FLOATS!\n\n");
				}
				else//if user enter valid then enter else clause.
				{
					printf("\ndistance = square root of ((x1 - x2)^2 + (y1 - y2)^2) = sqrt((%.3lf - %.3lf)^2 + (%.3lf - %.3lf)^2) = %.3f\n\n", x1, x2, y1, y2, sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2)));
				}
				fflush(stdin);
			}
			if (userInput == 5)//Tangent
			{
				printf("\n------------------------------------\n\n5. Tangent\nPlease enter a value for theta1 and theta2: \n");
				if (scanf("%lf%lf", &theta1, &theta2) != 2){//determine user enter invalid or not.
					printf("\nError: please enter a TWO VALID FLOATS!\n\n");
				}
				else//if user enter valid then enter else clause.
				{
					printf("\ntan_theta = sin(theta) / cos(theta) = sin(%.3lf) / cos(%.3lf) = %.3lf\n\n", theta1, theta2, sin(theta1 * PI / 180) / cos(theta2 * PI / 180));
				}
				fflush(stdin);
			}

			if (userInput == 6)//Equivalent Parallel Resistance
			{
				printf("\n------------------------------------\n\n6. Equivalent Parallel Resistance\nPlease enter a value for R1, R2 and R3: \n");
				if (scanf("%d%d%d", &R1, &R2, &R3) != 3){//determine user enter invalid or not.
					printf("\nError: please enter THREE VALID INTEGERS!\n\n");
				}
				else//if user enter valid then enter else clause.
				{
					printf("\nparallel_resistance = 1 / (1 / R1 + 1 / R2 + 1 / R3) = 1 / (1 / %d + 1 / %d + 1 / %d) = %.3lf\n\n", R1, R2, R3, (double)1 / (((double)1 / R1) + ((double)1 / R2) + ((double)1 / R3)));
				}
				fflush(stdin);
			}

			if (userInput == 7)//General Equation
			{
				printf("\n------------------------------------\n\n7. General Equation\nPlease enter a value for b, z, x and a: \n");
				if (scanf("%lf%lf%lf%d", &b, &z, &x, &a) != 4){//determine user enter invalid or not.
					printf("\nError: please enter THREE VALID FLOATS and ONE ODD VALID INTEGER!\n\n");
				}
				else if (a % 2 == 0){//determine user enter invalid or not.
					printf("\nError: please enter a ODD INTEGER for the last term!\n\n");
				}
				else//if user enter valid then enter else clause.

				{
					printf("\ny = (3 / 5) + b * z - x / (a % 2) + PI = %.3lf + %.3lf * %.3lf - %.3lf / (%.3lf) + 3.1415926) = %.3lf\n\n", (double)3 / (double)5, b, z, x, (double)(a % 2), ((double)3 / (double)5) + (b * z) - (x / (double)(a % 2)) + PI);
				}
				fflush(stdin);
			}
		}
	} while (userInput != 0);//end do while loop.

	printf("Good Bye!\n");//after user enter 0 exit.
	return 0;
}