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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
//FinalProject.cpp
//Write your description program here.
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::fixed;
using std::showpoint;
#include <iomanip>
using std::setprecision;
#include <string>
using std::string;
#include <fstream>
using std::ofstream;
double add(double number1,double number2); //function prototypes
double sub(double number1,double number2);
double mul(double number1,double number2);
double div(double number1,double number2);
double add(double array[],int const SIZE);
double avg(double array[],int const SIZE);
void sort(double array[],int const SIZE);
int search(double array[],int const SIZE,double const KEY);
void save(double array[],int const SIZE);
void print(double array[],int const SIZE);
//Extra functions
double inputValue();
void inputArrayValues(double array[],int const SIZE);
int Menu();
void clearInput();
int main(){
bool theUserWantToContinue=true;
int choice;
int const SIZE=5;
double numbers[SIZE]={0};
cout<<fixed<<showpoint<<setprecision(2);
while(theUserWantToContinue){
choice=Menu();
double number1,number2;
switch(choice){
case 1:
number1=inputValue();
number2=inputValue();
cout<<"\nResult: "<<number1<<'+'<<number2<<'='<<add(number1,number2)
<<'\n'<<endl;
clearInput();
break;
case 2:
number1=inputValue();
number2=inputValue();
cout<<"\nResult: "<<number1<<'-'<<number2<<'='<<sub(number1,number2)
<<'\n'<<endl;
clearInput();
break;
case 3:
number1=inputValue();
number2=inputValue();
cout<<"\nResult: "<<number1<<'*'<<number2<<'='<<mul(number1,number2)
<<'\n'<<endl;
clearInput();
break;
case 4:
number1=inputValue();
number2=inputValue();
if(number2==0){
cout<<"\nError: DIVIDE BY ZERO\n"<<endl;
}else{
cout<<"\nResult: "<<number1<<'/'<<number2<<'='<<div(number1,number2)
<<'\n'<<endl;
}//end if..else
clearInput();
break;
case 5:
cout<<"Enter "<<SIZE<<" values:\n";
inputArrayValues(numbers,SIZE);
cout<<"\nInput was entered\n"<<endl;
save(numbers,SIZE);
clearInput();
break;
case 6:
print(numbers,SIZE);
break;
case 7:
cout<<"\nThe sum of array is: "<<add(numbers,SIZE)<<'\n'<<endl;
break;
case 8:
sort(numbers,SIZE);
cout<<"\nSorted array\n"<<endl;
save(numbers,SIZE);
break;
case 9:
cout<<"Search number\n";
number1=inputValue();
int position;
position=search(numbers,SIZE,number1);
if(position!=-1){
cout<<'\n'<<number1<<" is in subscript array: "
<<position<<'\n'<<endl;
}else{
cout<<'\n'<<number1<<" NOT FOUND\n"<<endl;
}//end if..else
clearInput();
break;
case 10:
double average;
average=avg(numbers,SIZE);
cout<<"\nAverage: "<<average<<'\n'<<endl;
break;
case 11:
theUserWantToContinue=false;
break;
}//end switch
}//end while
return 0; //indicates success
}//end main
int Menu(){
int choice=0;
bool wrongInputIsEntered=true;
string str;
cout<<"(+) Add Two Values\n"
<<"(-) Sub Two Values\n"
<<"(*) Mul Two Values\n"
<<"(/) Div Two Values\n"
<<"(I) nput Values\n"
<<"(P)rint Values\n"
<<"(S)um Array\n"
<<"S(o)rt Array\n"
<<"S(e)arch Array\n"
<<"(A)vg of Array\n"
<<"(Q)uit\n"
<<"Select: ";
getline(cin,str);
while(wrongInputIsEntered){
if(str.size()>1){
cout<<"Enter a valid option\nEnter option again: ";
getline(cin,str);
}else if(!(str=="/"||str=="+"||str=="-"||str=="*"||str=="I"
||str=="i"||str=="P"||str=="p"||str=="S"||str=="s"
||str=="O"||str=="o"||str=="E"||str=="e"
||str=="A"||str=="a"||str=="Q"||str=="q")){
cout<<"$Enter a valid option\nEnter option again: ";
getline(cin,str);
}else{
wrongInputIsEntered=false;
}//end if..else..else
}//end while
if(str=="+"){
choice=1;
}else if(str=="-"){
choice=2;
}else if(str=="*"){
choice=3;
}else if(str=="/"){
choice=4;
}else if(str=="I"||str=="i"){
choice=5;
}else if(str=="P"||str=="p"){
choice=6;
}else if(str=="S"||str=="s"){
choice=7;
}else if(str=="O"||str=="o"){
choice=8;
}else if(str=="E"||str=="e"){
choice=9;
}else if(str=="A"||str=="a"){
choice=10;
}else if(str=="Q"||str=="q"){
choice=11;
}//end if..else..else..
return choice;
}//end function Menu
void inputArrayValues(double array[],int const SIZE){
double value;
for(int i=0;i<SIZE;i++){
value=inputValue();
array[i]=value;
}//end for
}//end function inputArrayValues
double inputValue(){
double value;
cout<<"Enter number: ";
cin>>value;
while(cin.fail()){
cin.clear();
cin.ignore(1000,'\n');
cout<<"NUMBERS ONLY\nEnter number again: ";
cin>>value;
}//end while
return value;
}//end function inputValue
void save(double array[],int const SIZE){
ofstream myfile;
myfile.open("Dex.txt");
for(int i=0;i<SIZE;i++){
myfile<<array[i]<<((i+1)%5==0?'\n':' ');
}//end for
myfile.close();
}//end function save
int search(double array[],int const SIZE,double const KEY){
for(int i=0;i<SIZE;i++)
if(array[i]==KEY) return i;
return -1;
}//end function search
void print(double array[],int const SIZE){
cout<<'\n'<<endl;
for(int i=0;i<SIZE;i++)
cout<<array[i]<<((i+1)%5==0?'\n':' ');
cout<<'\n'<<endl;
}//end function print
double add(double number1,double number2){
return number1+number2;
}//end add function
double sub(double number1,double number2){
return number1-number2;
}//end function sub
double mul(double number1,double number2){
return number1*number2;
}//end function mul
double div(double number1,double number2){
double result;
if(number2!=0){
result=number1/number2;
return result;
}//end if
return 0;
}//end function div
double add(double array[],int const SIZE){
double sum=0;
for(int i=0;i<SIZE;i++)
sum+=array[i];
return sum;
}//end function add
double avg(double array[],int const SIZE){
double average;
double sum;
sum=add(array,SIZE);
average=sum/SIZE;
return average;
}//end function avg
void sort(double array[],int const SIZE){
bool swap=true;
int number_swap=0;
while(swap){
for(int i=0;i<SIZE-1;i++){
if(array[i+1]<array[i]){
int tmp;
tmp=array[i+1];
array[i+1]=array[i];
array[i]=tmp;
number_swap++;
}//end if
}//end for
if(number_swap==0)
swap=false;
else{
number_swap=0;
}//end if..else
}//end while
}//end function sort
void clearInput(){
cin.clear();
cin.ignore(1000,'\n');
}//end function clearInput
|