public
Created

Dailyprogrammer 135, properly indented this time.

  • Download Gist
DailyProg135Easy.cpp
C++
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
#include <vector>
#include <iostream>
#include <string>
#include <ctime>
#include <cstdlib>
#include <initializer_list>
#include <cmath>
#include <stdexcept>
 
using namespace std;
 
int nrand(int n)
/*
** I have to admit, I cheated a bit here.This is taken from Accelerated C++,
** but I wanted a fair way to make a random number. Because Rand isn't truly
** random, there could be a difference in distribution if you simply took
** rand()%n. The rest I came up with myself. The reasoning of nrand is
** quite simple, if people want I can explain the reasoning.
*/
 
{
if (n <= 0 || n > RAND_MAX){
throw domain_error("Argument to nrand is out of range");
}
 
const int bucket_size = RAND_MAX / n;
int r;
do {
r = rand() / bucket_size;
}
while (r >= n);
 
return r;
}
 
vector<int> make_sum_numbers(const vector<int>& numbers)
{
vector<int> nums_in_sum;
int i = 0;
while(i<4){
nums_in_sum.push_back(numbers[nrand(numbers.size())]);
++i;
}
return nums_in_sum;
}
 
vector<string> make_sum_operator(const vector<string>& operators)
{
vector<string> opers_in_sum;
int i = 0;
while (i<3){
opers_in_sum.push_back(operators[nrand(operators.size())]);
++i;
}
return opers_in_sum;
}
 
int solve_sum(vector<int> sum_numbers, vector<string> sum_operators, const vector<string>& operators)
{
for(size_t i = 0; i<operators.size(); ++i){
 
for(int j = 0; j<(int)(sum_operators.size()); ++j){
 
if (operators[i] == sum_operators[j]){
switch(i){
case 0:{
sum_numbers[j]*=sum_numbers[j+1];
break;}
case 1:{
sum_numbers[j]+=sum_numbers[j+1];
break;}
case 2:{
sum_numbers[j]-=sum_numbers[j+1];
break;}
}
 
sum_numbers.erase(sum_numbers.begin()+j+1);
sum_operators.erase(sum_operators.begin()+j);
--j;
}
}
}
return (sum_numbers[0]);
}
 
int main()
{
vector<string> operators = {"*", "+", "-"};
int min, max;
cin>>min>>max;
 
if(min<0||max<0||(min==max)){
throw domain_error("Min/max: out of range");
return 1;
}
 
vector<int> numbers;
int current = min;
 
while (current <= max){
numbers.push_back(current);
++current;
}
 
while(true){ //entering the loop to keep going, break with EOF.
 
//Took me a while to figure this out: seeding the rng
//before the makeloops because it would return the same number
//every time if seeded inside.
unsigned seed = static_cast<unsigned>(time(0));
srand(seed);
vector<int> sumNumbers = make_sum_numbers(numbers);
vector<string> sumOperators = make_sum_operator(operators);
 
for(int i = 0; i<7; ++i)
{
cout<<((i%2 == 0)?(to_string(sumNumbers[i/2])):(sumOperators[i/2]))<<" ";
}
cout<<endl;
time_t t1 = time(0);
int useranswer;
int solution = solve_sum(sumNumbers, sumOperators, operators);
if(!(cin>>useranswer)){
break;
}
while(useranswer!=solution){
cout<<"Try again!"<<endl;
cin>>useranswer;
}
time_t t2 = time(0);
double time_taken = (double)(t2) - (double)(t1);
cout<<"Good job! It took you "<<time_taken<<" s"<<endl;
 
 
}
 
return 0;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.