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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
|
#include <iostream>
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);
int main(void)
{
double dnum1, dnum2,dnum3;
int inum1, inum2,inum3;
float fnum1, fnum2,fnum3;
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)
{
cout<<"please enter your first numeber ";
cin>>inum1;
dnum1 = inum1;
fnum1 = inum1;
cout<<"please enter your second numeber ";
cin>>inum2;
dnum2 = inum2;
fnum2 = inum2;
cout<<"please enter your third numeber ";
cin>>inum3;
dnum3 = inum3;
fnum3 = inum3;
cout<<endl;
}
else if (choice == 2)
{
cout<<"please enter your first numeber ";
cin>>fnum1;
dnum1 = fnum1;
inum1 = fnum1;
cout<<"please enter your second numeber ";
cin>>fnum2;
dnum2 = fnum2;
inum2 = fnum2;
cout<<"please enter your third numeber ";
cin>>fnum3;
dnum3 = fnum3;
inum3 = fnum3;
cout<<endl;
}
else if (choice == 3)
{
cout<<"please enter your first numeber ";
cin>>dnum1;
fnum1 = dnum1;
inum1 = dnum1;
cout<<"please enter your second numeber ";
cin>>dnum2;
fnum2 = dnum2;
inum2 = dnum2;
cout<<"please enter your third numeber ";
cin>>dnum3;
fnum3 = dnum3;
inum3 = dnum3;
cout<<endl;
}
enum IFDCAL {INTEGER = 1, FLOAT, DOUBLE};
IFDCAL myOps = INTEGER;
myOps = IFDCAL(choice);
switch(myOps)
{
case INTEGER:
cout<<"the result of interger type value is " <<funnyfunc(inum1, inum2, inum3)<<endl;
break;
case FLOAT:
cout<<"the result of float type value is " <<funnyfunc(fnum1, fnum2, fnum3)<<endl;
break;
case DOUBLE:
cout<<"the result of double type value is " <<funnyfunc(dnum1, dnum2, dnum3)<<endl;
break;
default:
cout<<"thank you for using !"<<endl;
}
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;
myOps = IFDCAL(choice);
switch(myOps)
{
case HARMONIC:
cout<<"the result of Harmonic mean is " <<Harmonic(dnum1, dnum2, dnum3)<<endl;
break;
case ARITHMETIC:
cout<<"the result of arithmetic mean is " <<Arithmetic(fnum1, fnum2, fnum3)<<endl;
break;
case GEOMETRIC:
cout<<"the result of geometric mean is " <<Geometric(dnum1, dnum2, dnum3)<<endl;
break;
default:
cout<<"thank you for using !"<<endl;
}
}
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)
{
return ((val1*val2*vale3*)^(.3333));
}
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;
}
|