1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
//includes and definitions
#define _USE_MATH_DEFINES
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<ctype.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<string>
using namespace std;
// The function below deals with the math necessary so it didn't have to be re-written a zillion times
double voltage (double t, double n)
{
// declaration of variables
double V, Vsum, i, tempSum;
Vsum = 0;
for (i = 1; i<=n; i++) //handles summing
{
tempSum = (1/pow(2*i-1,2))*cos(((2*i-1)*M_PI*t)/3);
Vsum = Vsum + tempSum;
}
V = (3/2)-(12/pow(M_PI,2))*Vsum;
return(V);
}
void main ()
{
//declaration of variables
int selection;
double t, n, E, tsubone, tsubtwo, Vsubone, Vsubtwo, deltaV, deltaT, V, tempSum, i;
char rerun = 'y', tryagain = 'y';
string filename;
// write to file housekeeping
ofstream voltageData;
cout << "Please insert a file name" << endl;
getline (cin, filename);
voltageData.open(filename, ios::app);
do //lets the user start again from the begining
{
cout << "Please make a selection.\n Method 1, 2, or 3?" << endl;
voltageData << "Please make a selection.\n Method 1, 2, or 3?" << endl;
cin >> selection;
while (selection != 1 && selection != 2 && selection != 3) // forces the user into a valid choice
{
cout << "Please make a valid selection.\n Would you like method 1, 2, or 3?" << endl;
voltageData << "Please make a valid selection.\n Would you like method 1, 2, or 3?" << endl;
cin >> selection;
}
cout << "You chose method " << selection << endl;
voltageData << "You chose method " << selection << endl;
switch (selection) // goes to the method the user picked
{
case 1: // takes input of time and number of terms
while (rerun == 'y') // allows the user to rerun
{
cout << "Please pick a time between -3 and 3" << endl;
voltageData << "Please pick a time between -3 and 3" << endl;
cin >> t;
while (t > 3 || t <-3) // forces the user into a valid selection
{
cout << "Please pick a time between -3 and 3" << endl;
voltageData << "Please pick a time between -3 and 3" << endl;
cin >> t;
}
cout << "You chose time " << t << endl;
voltageData << "You chose time " << t << endl;
cout << "How many terms would you like in the series?" << endl;
voltageData << "How many terms would you like in the series?" << endl;
cin >> n;
cout << "You chose " << n << " terms." << endl;
voltageData << "You chose " << n << " terms." << endl;
V = voltage (t, n);
cout << "At time " << t << " with " << n << " terms the voltage is " << V << endl;
voltageData << "At time " << t << " with " << n << " terms the voltage is " << V << endl;
cout << "Would you like to try new numbers? (y/n)" << endl;
voltageData << "Would you like to try new numbers? (y/n)" << endl;
cin >> rerun;
}
break;
case 2: // takes input of time and smallest term
while (rerun == 'y') //allows the user to reun
{
cout << "Please pick a time between -3 and 3" << endl;
voltageData << "Please pick a time between -3 and 3" << endl;
cin >> t;
while (t > 3 || t <-3) // forces the user into a valid selection
{
cout << "Please pick a time between -3 and 3" << endl;
voltageData << "Please pick a time between -3 and 3" << endl;
cin >> t;
}
cout << "You chose time " << t << endl;
voltageData << "You chose time " << t << endl;
cout << "Please give the minimum value of a summed term." << endl;
voltageData << "Please give the minimum value of a summed term." << endl;
cin >> E;
cout << "Your minimum value is " << E << endl;
voltageData << "Your minimum value is " << E << endl;
i=1;
tempSum = E + 1;
while (fabs(tempSum) > E)
{
tempSum = (1/pow(2*i-1,2))*cos(((2*i-1)*M_PI*t)/3);
i++;
}
n = i;
V = voltage (t, n);
cout << "At time " << t << " with a minimum term value of " << E << " it took " << n << " terms to find a voltage of " << V << endl;
cout << "Would you like to try new numbers? (y/n)" << endl;
voltageData << "At time " << t << " with a minimum term value of " << E << " it took " << n << " terms to find a voltage of " << V << endl;
voltageData << "Would you like to try new numbers? (y/n)" << endl;
cin >> rerun;
}
break;
case 3: //takes two times and finds the difference in voltage
while (rerun == 'y') //allows the user to rerun
{
cout << "Please pick your first time between -3 and 3:" << endl;
voltageData << "Please pick your first time between -3 and 3:" << endl;
cin >> tsubone;
while (tsubone > 3 || tsubone <-3) // forces the user into a valid selection
{
cout << "Please pick a time between -3 and 3:" << endl;
voltageData << "Please pick a time between -3 and 3:" << endl;
cin >> tsubone;
}
cout << "You chose time " << tsubone << endl;
voltageData << "You chose time " << tsubone << endl;
cout << "Please pick your seccond time between -3 and 3 and larger than your first time:" << endl;
voltageData << "Please pick your seccond time between -3 and 3 and larger than your first time:" << endl;
cin >> tsubtwo;
while (tsubtwo > 3 || tsubtwo <-3 || tsubtwo <= tsubone) // forces the user into a valid selection
{
cout << "Please pick a time between -3 and 3 and larger than your first time:" << endl;
voltageData << "Please pick a time between -3 and 3 and larger than your first time:" << endl;
cin >> tsubtwo;
}
cout << "You chose time " << tsubtwo << endl;
voltageData << "You chose time " << tsubtwo << endl;
cout << "How many terms would you like in your series?" << endl;
voltageData << "How many terms would you like in your series?" << endl;
cin >> n;
Vsubone = voltage (tsubone, n);
Vsubtwo = voltage (tsubtwo, n);
deltaV = Vsubtwo - Vsubone;
deltaT = tsubtwo - tsubone;
cout << "The voltage at time " << tsubone << " was " << Vsubone << endl;
cout << "The voltage at time " << tsubtwo << " was " << Vsubtwo << endl;
cout << "Over a time of " << deltaT << " the change in voltage was " << deltaV << endl;
voltageData << "The voltage at time " << tsubone << " was " << Vsubone << endl;
voltageData << "The voltage at time " << tsubtwo << " was " << Vsubtwo << endl;
voltageData << "Over a time of " << deltaT << " the change in voltage was " << deltaV << endl;
cout << "Would you like to try new numbers? (y/n)" << endl;
voltageData << "Would you like to try new numbers? (y/n)" << endl;
cin >> rerun;
}
break;
default:
cout << "Something strange is happening" << endl;
break;
}
cout << "Would you like to start again from the begining? (y/n)" << endl;
voltageData << "Would you like to start again from the begining? (y/n)" << endl;
cin >> tryagain;
} while (tryagain == 'y');
voltageData.close(); //closes the file
system("pause");
}
|