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
|
#include <iostream>
#include<cmath>
using namespace std;
template <class T>
T funnyfunc(T val1, T val2, T val3);
template <class T>
T Harmonic(T val1, T val2, T val3);
template <class T>
T Arithmetic(T val1, T val2, T val3);
template <class T>
T Geometric(T val1, T val2, T val3);
void testvalue(int value, short unsigned int code);
void drawmenu(void);
void drawmenu1(void);
template <typename T>
void runprogram();
int main(void)
{
short unsigned int choice = 0;
while(choice!=4)
{
cout<<"please select from the following menu ";
drawmenu();
cin>>choice;
try
{
testvalue(choice, 3);
break;
}
catch (char* eString)
{
cout<< eString <<endl;
cout<<" please re enter your menu choice ";
}
}
if (choice == 4)
{
cout <<"the program is over";
}
else if (choice == 1)
{
runprogram<int>();
}
else if (choice == 2)
{
runprogram<float>();
}
else if (choice == 3)
{
runprogram<double>();
}
}
template <class T>
T funnyfunc(T val1, T val2, T val3)
{
return ((val1+val2)*val3);
}
template <class T>
T Harmonic(T val1, T val2, T val3)
{
return (3/((1/val1)+(1/val2)+(1/val3)));
}
template <class T>
T Arithmetic(T val1, T val2, T val3)
{
return ((val1 + val2 + val3)/3);
}
template <class T>
T Geometric(T val1, T val2, T val3)
{
T v = (1/3);
T temp;
T n = 3;
temp = (val1 * val2 * val3);
return (myPow(temp, (1/n)));
}
void testvalue(int value, short unsigned int code)
{
switch(code)
{
case 1:
if(value < 0 )
throw "error - value cannot be negative \n";
break;
case 2:
if(value==0)
throw "error you can't divided by zero \n";
break;
case 3:
if (value < 1|| value > 4)
throw "error - invalid menu selection";
break;
default:
cout<<" unhandled exception";
cout<<"program will stop";
exit(1);
}
}
void drawmenu(void)
{
cout<<endl<<"\t1 - interger"<<endl;
cout<<"\t2 - float" <<endl;
cout<<"\t3 - double"<<endl;
cout<<"\t4 - end this program"<<endl;
}
void drawmenu1(void)
{
cout<<endl<<"\t1 - Harmonic"<<endl;
cout<<"\t2 - Arithmetic" <<endl;
cout<<"\t3 - Geometric"<<endl;
cout<<"\t4 - end this program"<<endl;
}
template <typename T>
void runprogram()
{
T a, b, c;
short unsigned int choice = 0;
cout << "please enter first number ";
cin >> a;
cout << "please enter second number ";
cin >> b;
cout << "please enter third number ";
cin >> c;
while(choice!=4)
{
cout<<"please select what type of means you want ";
drawmenu1();
cin>>choice;
try
{
testvalue(choice, 3);
break;
}
catch (char* eString)
{
cout<< eString <<endl;
cout<<" please re enter your menu choice ";
}
}
enum MEAN {HARMONIC = 1, ARITHMETIC,GEOMETRIC};
MEAN myOps1 = HARMONIC;
myOps1 = MEAN(choice);
switch(myOps1)
{
case HARMONIC:
cout<<"the result of Harmonic mean is " <<Harmonic(a, b, c)<<endl;
break;
case ARITHMETIC:
cout<<"the result of arithmetic mean is " <<Arithmetic(a, b, c)<<endl;
break;
case GEOMETRIC:
cout<<"the result of geometric mean is " <<Geometric(a, b, c)<<endl;
break;
default:
cout<<"thank you for using !"<<endl;
}
}
|