main.cpp (4976B)
1 #include <iostream> 2 #include <iterator> 3 #include <map> 4 #include <math.h> 5 #include <sstream> 6 #include <vector> 7 8 #define SEQUENCE_SIZE 7 9 #define POWER 7 // also the maximum space a number in the sequence takes 10 11 using namespace std; 12 13 /* the functions doing actual calculations */ 14 void populate(const string& deffinition); 15 /* --------------------------------------- */ 16 17 void pseq(const string& str); 18 void psubseq(const string& str); 19 void interactive(); 20 21 map<string, vector<int>> sequences; 22 23 int main(int argc, char** argv) 24 { 25 if (argv[1] && argv[1] == (string) "post") 26 populate("post"); 27 else 28 populate("prior"); 29 30 interactive(); 31 32 return 0; 33 } 34 35 void pseq(const string& str) 36 { 37 vector<int> sequence = sequences[str]; 38 for (int i = 0; i < sequence.size(); ++i) { 39 cout << sequence[i] << " "; 40 } 41 cout << endl; 42 } 43 44 void psubseq(const string& str) 45 { 46 // splitting the input str by spaces 47 istringstream iss(str); 48 vector<string> results((istream_iterator<string>(iss)), 49 istream_iterator<string>()); 50 51 /* if (results.size() == 1) { */ 52 /* vector<int> sequence = sequences[str]; */ 53 /* for (int i = 0; i < sequence.size(); ++i) { */ 54 /* cout << sequence[i] << " "; */ 55 /* } */ 56 /* cout << endl; */ 57 /* } else { */ 58 vector<int> sequence = sequences[results[0]]; 59 for (int i = 0; i < sequence.size(); ++i) { 60 cout << sequence[i] + stoi(results[1]) << " "; 61 } 62 cout << endl; 63 /* } */ 64 } 65 66 void interactive() 67 { 68 string str; 69 while (true) { 70 getline(cin, str, '\n'); 71 if (str.find(" ") != string::npos) 72 psubseq(str); 73 else 74 pseq(str); 75 } 76 } 77 78 void populate(const string& deffinition) 79 { 80 for (int power = 1; power <= POWER; ++power) { 81 // getting sequence name e.g. d1x1 82 string seq_name = "x"; 83 seq_name.append(to_string(power)); 84 string diff_name = "d"; 85 diff_name.append(to_string(1)); 86 string res_name = diff_name.append(seq_name); 87 ; 88 cout << res_name << " = "; 89 90 // calculates the sequence 91 vector<int> n; 92 int num; 93 for (int place = -SEQUENCE_SIZE; place <= SEQUENCE_SIZE; ++place) { 94 // raises the num in the sequence to the correct power 95 num = pow(place, power); 96 if (place == 0) 97 num = 0; 98 /* num += 1; */ 99 n.push_back(num); 100 for (int l = 0; l <= POWER - to_string(num).length(); ++l) 101 cout << " "; 102 cout << num; 103 } 104 cout << endl; 105 // stores the sequence in the map 106 sequences.insert(pair<string, vector<int>>(res_name, n)); 107 108 // calculates the differentials of the sequences 109 for (int diff = 2; true; ++diff) { 110 // gets the sequence name e.g. d2n3 (aka ddn^3) 111 string diff_name = "d"; 112 diff_name.append(to_string(diff)); 113 string res_name = diff_name.append(seq_name); 114 115 // finds the sequence it's supposed to differentiate 116 vector<int> s; 117 if (diff == 2) { 118 s = n; 119 } else { 120 string prev_diff_name = "d"; 121 prev_diff_name.append(to_string(diff - 1)); 122 prev_diff_name.append(seq_name); 123 s = sequences[prev_diff_name]; 124 } 125 126 // differentiates 127 cout << diff_name << " = "; 128 129 if (deffinition == "prior") 130 for (int l = 0; l < (diff)-1; ++l) { 131 for (int i = 0; i < POWER; ++i) 132 cout << " "; 133 cout << " "; 134 } 135 136 vector<int> m; 137 int n; 138 for (int i = 0; i < s.size() - 1; ++i) { 139 n = s[i + 1] - s[i]; 140 m.push_back(n); 141 for (int l = 0; l <= POWER - to_string(n).length(); ++l) 142 cout << " "; 143 cout << n; 144 } 145 146 // stores the differential in the map 147 sequences.insert(pair<string, vector<int>>(res_name, m)); 148 149 cout << endl; 150 151 int l = m.front(); 152 for (int i = 0; i < m.size(); ++i) { 153 if (m[i] != m.front()) 154 goto cont; 155 } 156 break; 157 cont: 158 continue; 159 } 160 if (power != POWER) 161 cout << endl; 162 } 163 } 164 165 void integrate() 166 { 167 // calculates the sequence 168 vector<int> n; 169 int num; 170 for (int place = -SEQUENCE_SIZE; place <= SEQUENCE_SIZE; ++place) { 171 // raises the num in the sequence to the correct power 172 /* num = pow(place, power); */ 173 num = place; 174 if (place == 0) 175 num = 0; 176 /* num += 1; */ 177 n.push_back(num); 178 for (int l = 0; l <= POWER - to_string(num).length(); ++l) 179 cout << " "; 180 cout << num; 181 } 182 cout << endl; 183 }