/* STRENGTH.C */
/* subroutines for stress, buckling, shear */


#include <stdio.h>
#include <math.h>
#include <graphics.h>
#pragma hdrstop
#include "externs.h"

void section()
{
	float b, d, b1, d1, b2, d2, n, wall;
	float pi = 3.1416;
	char section, junk;
	int buf, angnum;

  if(!firstsection) {
    printf("New structural type or material? ");
    if(getch() == 'n') return;
  }

  firstsection = 0;

  printf("\nNumber of structural material? ");
    scanf("%d", &material);
	 printf("\nChoose type of section:\n");
	 printf("\ts)olid rectangular\n");
	 printf("\th)ollow rectangulatr\n");
	 printf("\tc)ircular (solid)\n");
	 printf("\tt)ubular\n");
	 printf("\ta)ngular\n");
	 section = getch();
	 switch (section) {
	  case 'a':
/*	    printf("Enter angle info from tables:\n");
	    printf("\tMoment of inertia? ");
	    scanf("%f%c", &momin, &junk);
	    printf("\tSection modulus? ");
	    scanf("%f%c", &secmod, &junk);
	    printf("\tLeast radius of gyration? ");
	    scanf("%f%c", &radgyr, &junk);
	    printf("\tCross-sectional area? ");
	    scanf("%f%c", &area, &junk);
	    printf("Thank you...\n");
*/
	    printf("Enter angle number: ");
	    scanf("%d", &angnum);
	    angnum = angnum - 40;
	    momin = momi[angnum];
	    secmod = secm[angnum];
	    radgyr = aradg[angnum];
	    area = aarea[angnum];
	    break;
	  case 's':
		printf("width (b)? ");
		scanf("%f%c", &b, &junk);
		printf("height (d)? ");
		scanf("%f%c", &d, &junk);
		momin = b * d * d * d / 12;
		printf("Moment of Inertia of solid ");
		printf("rectangular section = %g\n", momin);
		secmod = b * d * d / 6;
		printf("Section Modulus = %g\n", secmod);
		radgyr =  0.289 * d;
		printf("Radius of Gyration = %g\n", radgyr);
		area = b * d;
		printf("Cross-sectional area = %g\n", area);
		break;
	  case 'h':
		printf("Outside width (b1)? ");
		scanf("%f%c", &b1, &junk);
		printf("Outside height (d1)? ");
		scanf("%f%c", &d1, &junk);
		printf("Inside width (b2)? ");
		scanf("%f%c", &b2, &junk);
		printf("Inside height (d2)? ");
		scanf("%f%c", &d2, &junk);
		momin = ((b1 * d1 * d1 * d1 ) - (b2 * d2 * d2 * d2 ) )/12;
		printf("Moment of Inertia of hollow ");
		printf("rectanular section = %g\n", momin);
		secmod = ((b1 * d1 * d1 * d1 )-(b2 * d2 * d2 * d2 ))/6/d1;
		printf("Section Modulus = %g\n", secmod);
		n = (b1 * d1 * d1 * d1 ) - (b2 * d2 * d2 * d2 );
		d = 12 * ( b1 * d1 - b2 * d2 );
		radgyr = sqrt( n / d );
		printf("Radius of Gyration = %g\n", radgyr);
		area = ( b1 * d1 ) - ( b2 * d2 );
		printf("Cross-sectional area = %g\n", area);
		break;
	  case 'c':
		printf("Diameter? ");
		scanf("%f%c", &d, &junk);
		momin = pi * d * d * d * d / 64;
		printf("Moment of Inertia of circular");
		printf(" section = %g\n", momin);
		secmod = pi * d * d * d / 32;
		printf("Section Modulus = %g\n", secmod);
		radgyr = d / 4;
		printf("Radius of Gyration = %g\n", radgyr);
		area = pi * d * d / 4;
		printf("Cross-sectional area = %g\n", area);
		break;
	  case 't':
		printf("Outside diameter? ");
		scanf("%f%c", &d1, &junk);
		printf("1) I.D or 2) Wall thickness? ");
		scanf("%d%c", &buf, &junk);
		if ( buf == 1 ) {
			printf("Inside diameter? ");
			scanf("%f%c", &d2, &junk);
		}
		else {
			printf("Wall thickness? ");
			scanf("%f%c", &wall, &junk);
			d2 = d1 - 2 * wall;
		}
		momin = pi * (d1*d1*d1*d1 - d2*d2*d2*d2) / 64;
		printf("Moment of Inertia of tube = %g\n", momin);
		secmod = pi * (d1*d1*d1*d1 - d2*d2*d2*d2) / 32 / d1;
		printf("Section Modulus = %g\n", secmod);
		radgyr = sqrt(( d1 * d1 + d2 * d2 ) / 4 );
		printf("Radius of Gyration = %g\n", radgyr);
		area = pi / 4 * ( d1 * d1 - d2 * d2 );
		printf("Cross-sectional area = %g\n", area);
		break;
	}
}
/***********************************************************/
void tensilestress()
{
  float force, junk;

  printf("\nEnter force (+tensile, -compressive)  ");
  scanf("%f%c", &force, &junk);
  if(force > 0) {
    printf("\nTensile stress = %.0f psi\n", force / area);
    printf("Safety factor = %.2f\n", yield[material] / (force/area));
  }
  else {
    printf("\nCompressive stress = %.0f psi\n", -force / area);
    printf("Safety factor = %.2f\n", -compress[material] / (force/area));
  }
}
/***********************************************************/
void shearstress()
{
  float force, junk;

  printf("\nEnter force  ");
  scanf("%f%c", &force, &junk);
  printf("\nTensile stress = %.0f psi\n", force / area);
  printf("Safety factor = %.2f\n", shear[material] / (force/area));
}
/***********************************************************/
/* Program to calc beam stress in various ways */


void beam()
{
  void simunif(), cenrecbeam(), anypoint(), twoequal();
  void cantunif(), cantone();
  char ch;

	printf(" Choose:\n");
	printf("\t1) Simple beam - uniform load\n");
	printf("\t2) Simple beam - Concentrated load at any point\n");
	printf("\t3) Simple beam - 2 equal loads (= distance from ends)\n");
	printf("\t4) Cantilever beam - uniform load\n");
	printf("\t5) Cantilever beam - Concentrated load\n");
	ch = getch();
	switch (ch) {
		case '1':
			simunif();
			break;
		case '2':
			anypoint();
			break;
		case '3':
			twoequal();
			break;
		case '4':
			cantunif();
			break;
		case '5':
			cantone();
			break;
	}
}

void simunif()
{
	float w, l, s, y, i, e;

	printf("\nSimple beam - uniform load:\n\n");
	printf("Load (lbs./in.)? ");
	scanf("%f", &w);
	printf("Length of beam (in.)? ");
	scanf("%f", &l);
/*	printf("Section modulus ? ");
	scanf("%f", &s);
	printf("Moment of inertia? ");
	scanf("%f", &i);
	printf("Modulus of elasticity (E)? ");
	scanf("%f", &e);
*/
	s = secmod;
	i = momin;
	e = emod[material];
	y = 5 * w * l * l * l * l / 384 / e / i;
	printf("\nMaximum moment (at center) = %g in.lb.\n", w*l*l/8);
	printf("Maximum stress (at center) = %g psi\n\n", w*l*l/8/s);
	printf("Maximum deflection = %g in.\n", y);
	printf("\tat center\n");
	printf("Safety factor = %.2f\n", yield[material] / (w*l*l/8/s));
}

void anypoint()
{
	float p, kl, l, s, k, x, y, i, e;

	printf("\nSimple beam - Concentrated load at any point:\n\n");
	printf("Load (lbs.)? ");
	scanf("%f", &p);
	printf("Length of beam (in.)? ");
	scanf("%f", &l);
	printf("Distance of load from end (kl) (in.)? ");
	scanf("%f", &kl);
/*	printf("Section modulus ? ");
	scanf("%f", &s);
	printf("Moment of inertia? ");
	scanf("%f", &i);
	printf("Modulus of elasticity (E)? ");
	scanf("%f", &e);
*/
	s = secmod;
	i = momin;
	e = emod[material];
	k = kl / l;
	x = 2 * k / 3 - k * k / 3;
	y = p * l * l * l / 3 / e / i * ( 1 - k ) * sqrt(x*x*x);
	printf("\nMaximum moment = %g in.lb.\n", p*kl*(1-kl/l));
	printf("Maximum stress ( at load ) = %g psi\n\n", p*kl*(1-kl/l)/s);
	printf("Maximum deflection = %g in.\n", y);
	printf("\tat x = %g in.\n", l * sqrt(x));
	printf("Safety factor = %.2f\n", yield[material] / (p*kl*(1-kl/l)/s));
}

void twoequal()
{
	/* two equal concentrated loads equal distance from supports */

	float p, d, l, s, i, e, x;

	printf("\nSimple beam - 2 equal loads distance d from ends:\n\n");
	printf("Each load (lbs.)? ");
	scanf("%f", &p);
	printf("Length of beam (in.)? ");
	scanf("%f", &l);
	printf("Distance of loads from ends (d) (in.)? ");
	scanf("%f", &d);
/*	printf("Section modulus ? ");
	scanf("%f", &s);
	printf("Moment of inertia? ");
	scanf("%f", &i);
	printf("Modulus of elasticity (E)? ");
	scanf("%f", &e);
*/
	s = secmod;
	i = momin;
	e = emod[material];
	printf("\nMaximum moment = %g in.lbs.\n", p * d);
	printf("Maximum stress ( between loads ) = %g psi\n", p * d / s);
	x = ( 3 * l * l ) - ( 4 * d * d );
	printf("Maximum deflection ( at center ) = %g in.\n", p*d/24/e/i*x);
	printf("Safety factor = %.2f\n", yield[material] / (p * d / s));
}

void cantunif()
{
	float w, l, s, y, i, e;

	printf("\nCantilever beam - uniform load:\n\n");
	printf("Load per unit length (w) - (lbs./in.)? ");
	scanf("%f", &w);
	printf("Length of beam (in.)? ");
	scanf("%f", &l);
/*	printf("Section modulus ? ");
	scanf("%f", &s);
	printf("Moment of inertia? ");
	scanf("%f", &i);
	printf("Modulus of elasticity (E)? ");
	scanf("%f", &e);
*/
	s = secmod;
	i = momin;
	e = emod[material];
	y = w*l*l*l*l/8.0/e/i;

	printf("\nMaximum moment = %g in.lb.\n", -w*l*l/2);
	printf("Maximum stress ( at load ) = %g psi\n\n", (w*l*l/2)/s);
	printf("Maximum deflection = %g in.\n", y);
	printf("Safety factor = %.2f\n", yield[material] / ((w*l*l/2)/s));
}
void cantone()
{
	float p, l, s, y, i, e;

	printf("\nCantilever beam - concentrated load:\n\n");
	printf("Load (p) - (lbs)? ");
	scanf("%f", &p);
	printf("Length of beam (in.)? ");
	scanf("%f", &l);
/*	printf("Section modulus ? ");
	scanf("%f", &s);
	printf("Moment of inertia? ");
	scanf("%f", &i);
	printf("Modulus of elasticity (E)? ");
	scanf("%f", &e);
*/
	s = secmod;
	i = momin;
	e = emod[material];
	y = p*l*l*l/3.0/e/i;

	printf("\nMaximum moment = %g in.lb.\n", -p*l);
	printf("Maximum stress (at fixed point) = %g psi\n\n", (p*l)/s);
	printf("Maximum deflection = %g in.\n", y);
	printf("Safety factor = %.2f\n", yield[material] / ((p*l)/s));
}

/* Program to calculate various column formulas */


void column()
{
	float e, l, r, a, s, c, k, phi, x;
	float pi = 3.1416;
	char junk;
	int ends;

/*	printf("Modulus of Elasticity E ? ");
	scanf("%f%c",&e, &junk);
	printf("Least radius of gyration (in.)? ");
	scanf("%f%c", &r, &junk);
	printf("Cross-sectional area (in.2)? ");
	scanf("%f%c", &a, &junk);
	printf("Maximum tensile stress (psi)? ");
	scanf("%f%c", &s, &junk);
*/
	e = emod[material];
	r = radgyr;
	a = area;
	s = yield[material];


  printf("\n Column buckling calculations: \n\n");
	printf("Pick one:\n");
	printf("\t1) Rounded ends\n\t2) Fixed ends\n");
	scanf("%d%c",&ends, &junk);
	printf("Unsupported length (in.)? ");
	scanf("%f%c", &l, &junk);
	if (ends == 1 ) {
		k = 1.0;
		c = 150;
		phi = 0.00016;
	}
	if (ends == 2 ) {
		k = 0.5;
		c = 75;
		phi = 0.00004;
	}
	if ((ends < 1 ) || (ends > 2 ) )  exit(0);
	printf("\n\t\t\t(Eshbach 5-42f.)");
	printf("\nSlenderness ratio = %g\n", l / r );
	printf("\nEuler formula (l / r > 150):\n");
	printf("\tMax. buckling load = %g\n", pi*pi*e*a/(k*l/r)/(k*l/r));
	printf("\nRankine formula (20 < l / r < 200):\n");
	x = 1 + phi * l * l / r / r;
	printf("\tBuckling load = %g\n", s*a/x);
	printf("\nStraight line formula (max l/r: rd, 160; fix, 320):\n");
	printf("\tBuckling load = %g\n", a*(s-c*l/r));
/*	printf("\nStandard building - strutural steel:\n");
	printf("\tMaximum load = %g\n",
		a * ( 17000 - 0.485 * l * l / r / r ));
*/
}
