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
|
#include <iostream>
#include <string>
#include <cmath>
#include <cstdlib>
#include <windows.h>
#include <limits>
#include <limits.h>
#include <climits>
using namespace std;
bool die(const string & msg);
void restore();
//Function prototypes
double triple(double x); //1
double times9(double x); //2
void triple(double & result, double x); //3
void times3(double & x); //4
void countdown(unsigned seconds ); //5
double getDouble( const string & prompt); //6
double getDoubleRetry( const string & prompt ); //7
string getString( const string & prompt ); //8
void output(unsigned howMany, const string & msg ); //9
string remove( char c, const string & s ); //10
string remove(const string & junk, const string & s); //11
bool prime( unsigned n ); //12
unsigned primes(unsigned max ); //13
double average(); //14
double averagePositive(); //15
int main(){
cout<<"Function1: " <<triple(1.1) <<endl; //1
cout<<"Function2: " <<times9(3) <<endl; //2
double result;
triple(result ,3); cout<<"Function3: " <<result <<endl; //3
double x=3;
times3(x); cout<<"Function4: " <<(x) <<endl; //4
cout<<"Function5: Countdown with Timer = " <<endl; countdown(5);
cout<<endl; //5
cout<<"Function6: " << getDouble("Input") <<endl; //6
cout <<"Function7: " << getDoubleRetry("Input again")
<< endl << endl; //7
cout<<"Function8: " <<getString("Input String") <<endl;
string getString( const string & prompt ); //8
cout<<"Function9: " <<endl; output(4, "Hello Professor");
cout<<endl; //9
cout<<"Function10: " <<remove('p', "Pinapple") <<endl; //10
cout<<"Function11: " <<remove( "aeiou", "Pineapple" ) <<endl;
//11
cout<<"Function12: 1 = prime, 0 = not prime." << endl;
cout << "33: " << prime(33) << endl;
cout << "3: " << prime(3) << endl << endl;
//12
cout<<"Function13: " <<primes(100) <<endl; //13
cout<<"Function14: " <<average() <<endl; //14
cout<<"Function15: " <<averagePositive()
<<endl;//15
} //End Main
//Begin
//die function............................................
bool die(const string & msg){
cout<< "Fatal Error: " <<msg <<endl;
exit(EXIT_FAILURE);
}
//restore function
void restore(){
cin.clear();
cin.ignore(10000, '\n');
}
//FUNCTIONS
double triple(double x){
return (3 * x);
}//1.................................................
double times9(double x){
return (triple(triple(x)));
}//2.................................................
void triple(double & result, double x){
result = (triple(x));
}//3.................................................
void times3(double & x){
x = (triple(x));
} //4................................................
void countdown( unsigned seconds ){
for( ; seconds>0; seconds--){
Sleep(1000);
cout<<seconds <<endl <<endl;
}if (seconds == 0){
cout<<"BLAST OFF! " <<endl;}
} //5...............................................
double getDouble( const string & prompt ){
cout<<"Please input a double ";
double inputPrompt;
cin>> inputPrompt
|| die("Invalid, please enter a double number");
return (inputPrompt);
} //6.................................................
double getDoubleRetry(const string & prompt){
double inputPrompt1;
cout<<"Please input a double again ";
for (;;){ if (!(cin>> inputPrompt1)){
restore();
cout<<"Please input a valid number: ";
}
else{
return inputPrompt1;
}//else
} // for loop
} //7....................................................
string getString( const string & prompt ){
string inputString;
cout<< prompt <<endl;
cin>> inputString ||
die("Please enter string again: Input failure");
return inputString;
} //8...................................................
void output( unsigned howMany, const string & msg ){
for (;howMany > 0;howMany--){
cout <<msg <<endl;
}
} //9...................................................
string remove(char c, const string & s){
string removedLtr;
for (unsigned character = 0; character < s.size(); character++){
if (!(s[character] == c)){
removedLtr += s[character];
}
}return removedLtr;
} //10......................................................
string remove( const string & junk, const string & s ){
string removeLtrs1 = s;
for (unsigned character = 0; character < junk.size(); character++){
removeLtrs1 = remove(junk[character], removeLtrs1);
}
return (removeLtrs1);
}//11........................................................
bool prime(unsigned n){
if (n < 4 && n > 1){
return true;
}if (!(n % 2 == 0)){
for (unsigned f = 3;; f++){
if (f > (n / f)){
return true;
}if (n % f == 0){
return false;
}//end inside if
}//end for
}//outside if end
else{
return false;
}//else end
} //12......................................................
unsigned primes(unsigned max){
unsigned pCounter = 0;
for (unsigned begin = 2; begin <= max; begin++){
if (prime(begin)){
pCounter++;
}
}return (pCounter);
} //13......................................................
double average(){
cout<<"Function14: input numbers for average " <<endl
<<"non-number to quit " <<endl;
double i;
double total = 0;
double count = 0;
double avg = 0;
for (;;){
if (!(cin >> i)){
restore();
if (count == 0){die("No Number Inputs");
}//end inside if
else {break;
}//end else
}//end outside if
count++;
total += i;
} // End for
avg = total / count;
return avg;
}//14......................................................
double averagePositive(){
cout<<"Function15: input numbers for positive # average "
<<endl <<"non-number to quit " <<endl;
double i;
double total = 0;
double count = 0;
double avg = 0;
for (;;){
if (!(cin >> i)){
restore();
if (count == 0){die("No Number Inputs");
}//inside if
else {break;
}//end else
}//end outside if
if (i > 0){
count++;
total += i;
}//end if
} // End for
avg = total / count;
return avg;
}//15...........................................................
|