PA2

main.c

////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015; Section 01
//Programming Assignment: pa2
//Due: Sept. 16, 2015
//File Type: main function file.
//Purpose: lead user inputs values and recall functions.
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
#include "equations.h"

int main(void){
	//declare variables.
	int menu;
	int encoded_character, shift;
	double mass, acceleration;
	double radius, height;
	char plaintext_character;
	double x1, x2, y1, y2;
	double theta;
	int r1, r2, r3;
	int a;
	double b, x, z;

	//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: 2.15\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: ");

		fflush(stdin);//clear scanf buffer.
		if (scanf("%d", &menu) != 1 || menu > 7){//determine user enter is valid or not.
			printf("\n***************************************\n*                                     *\n");
			printf("* Error: please enter a valid choice! *\n*                                     *\n");
			printf("***************************************\n");
		}
		else//if user input is valid then enter else clause.
		{
			//Newton's Second Law of Motion
			if (menu == 1)
			{
				system("cls");
				printf("\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", &mass, &acceleration) != 2){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else//if user input is valid then enter else clause.
				{
					printf("\nF = m x a = %.3lf x %.3lf = %.3lf\n\n", mass, acceleration, calculate_newtons_2nd_law(mass, acceleration));
				}
			}
			//Volume of A Cylinder
			if (menu == 2)
			{
				system("cls");
				printf("\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", &radius, &height) != 2){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else//if user input is valid then enter else clause.
				{
					printf("\nvolume cylinder = PI * r^2 * h = %lf x %.3lf^2 x %.3lf = %.3lf\n\n", PI, radius, height, calculate_volume_cylinder(radius, height));
				}
			}
			//Character Encoding
			if (menu == 3)
			{
				system("cls");
				printf("\n3. Character Encoding\nPlease enter a plain text character and a shift: \n");
				if (scanf("\n%c%d", &plaintext_character, &shift) != 2){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else if (shift > (90 - plaintext_character) || shift > (122 - plaintext_character)){//determine user enter invalid or not.
					return printf("\nError: shift value must be less than 26! \n\n");
				}
				else//if user input is valid then enter else clause.
				{
					printf("\nencoded character = (plain text character - 'A') + 'a' + shift = %d - %d + %d + %d= %c\n\n", plaintext_character, 65, 97, shift, perform_character_encoding(plaintext_character, shift));
				}
			}
			//Distance Between Two Points
			if (menu == 4)
			{
				system("cls");
				printf("\n4. Distance Between Two Points\nPlease enter a value for x1, y1, x2 and y2: \n");
				if (scanf("%lf%lf%lf%lf", &x1, &x2, &y1, &y2) != 4){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else//if user input is 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, calculate_distance_between_2pts(x1, x2, y1, y2));
				}
			}
			//Tangent
			if (menu == 5)
			{
				system("cls");
				printf("\n5. Tangent\nPlease enter a value for theta: \n");
				if (scanf("%lf", &theta) != 1){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else//if user input is valid then enter else clause.
				{
					printf("\ntan_theta = sin(theta) / cos(theta) = sin(%.3lf) / cos(%.3lf) = %.3lf\n\n", theta, theta, calculate_tangent_theta(theta));
				}
			}
			//Equivalent Parallel Resistance
			if (menu == 6)
			{
				system("cls");
				printf("\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 TWO VALID FLOATS!\n\n");
				}
				else//if user input is 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, calculate_parallel_resistance(r1, r2, r3));
				}
			}
			//General Equation
			if (menu == 7)
			{
				system("cls");
				printf("\n7. General Equation\nPlease enter a value for a, b, x and z: \n");
				if (scanf("%d%lf%lf%lf", &a, &b, &x, &z) != 4){//determine user enter invalid or not.
					printf("\nError: please enter TWO VALID FLOATS!\n\n");
				}
				else if (a % 2 == 0){//determine user enter invalid or not.
					return printf("\nError: please enter a ODD INTEGER for the last term!\n\n");
				}
				else//if user input is 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), calculate_general_equation(a, b, x, z));
				}
			}
		}
	} while (menu != 0);//end do while loop when menu == 0.

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

func.h

////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015; Section 01
//Programming Assignment: pa2
//Due: Sept. 16, 2015
//File Type: header file.
//Purpose: include lib and pre-define functions.
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <ctype.h>
#include <math.h>
#ifndef _HEADERNAME_H 
#define _HEADERNAME_H
#define _CRT_SECURE_NO_WARNINGS
#define PI 3.1415926

double calculate_newtons_2nd_law(double mass, double acceleration);
double calculate_volume_cylinder(double radius, double height);
char   perform_character_encoding(char plaintext_character, int shift);
double calculate_distance_between_2pts(double x1, double x2, double y1, double y2);
double calculate_tangent_theta(double theta);
double calculate_parallel_resistance(int r1, int r2, int r3);
double calculate_general_equation(int a, double b, double x, double z);

#endif

func.c

////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
//Programmer: Nathan Scott
//Class: CptS 121, Fall  2015; Section 01
//Programming Assignment: pa2
//Due: Sept. 16, 2015
//File Type: source file.
//Purpose: introduce functions and logics.
////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////
#include "equations.h"//include the header file.


////////////////////////////////////////////////////////////////////////////////////////////
//1. Newton's Second Law of Motion
////////////////////////////////////////////////////////////////////////////////////////////
double calculate_newtons_2nd_law(double mass, double acceleration)
{
	system("cls");
	double result;
	return result = mass * acceleration;
}


////////////////////////////////////////////////////////////////////////////////////////////
//2. Volume of A Cylinder
////////////////////////////////////////////////////////////////////////////////////////////
double calculate_volume_cylinder(double radius, double height)
{
	system("cls");
	double result;
	return result = PI * pow(radius, 2) * height;
}


////////////////////////////////////////////////////////////////////////////////////////////
//3. Character Encoding
////////////////////////////////////////////////////////////////////////////////////////////
char   perform_character_encoding(char plaintext_character, int shift)
{
	system("cls");
	//encoded_character = (plaintext_character - 65) + 97 + shift;
	char result = (plaintext_character - 65) + 97 + shift;
	return result;
}


////////////////////////////////////////////////////////////////////////////////////////////
//4. Distance Between Two Points
////////////////////////////////////////////////////////////////////////////////////////////
double calculate_distance_between_2pts(double x1, double x2, double y1, double y2)
{

	system("cls");
	double result = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
	return result;

}


////////////////////////////////////////////////////////////////////////////////////////////
//5. Tangent
////////////////////////////////////////////////////////////////////////////////////////////
double calculate_tangent_theta(double theta)
{
	system("cls");
	double result = sin(theta * PI / 180) / cos(theta * PI / 180);
	return result;
}


////////////////////////////////////////////////////////////////////////////////////////////
//6. Equivalent Parallel Resistance
////////////////////////////////////////////////////////////////////////////////////////////
double calculate_parallel_resistance(int r1, int r2, int r3)
{
	system("cls");
	double result = (double)1 / (((double)1 / r1) + ((double)1 / r2) + ((double)1 / r3));
	return result;
}


////////////////////////////////////////////////////////////////////////////////////////////
//7. General Equation
////////////////////////////////////////////////////////////////////////////////////////////
double calculate_general_equation(int a, double b, double x, double z)
{
	system("cls");
	double result = ((double)3 / (double)5) + (b * z) - (x / (a % 2)) + PI;
	return result;
}


////////////////////////////////////////////////////////////////////////////////////////////
//THE END
////////////////////////////////////////////////////////////////////////////////////////////