Skip to content

Instantly share code, notes, and snippets.

@niklasbuschmann
Last active June 9, 2020 13:13
Show Gist options
  • Save niklasbuschmann/b698068f8a8cf7239e33d39dea85866d to your computer and use it in GitHub Desktop.
Save niklasbuschmann/b698068f8a8cf7239e33d39dea85866d to your computer and use it in GitHub Desktop.
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
vector<double> x, y;
int get_file(string filename) {
double i, j;
ifstream file(filename);
while (file >> i >> j) {
x.push_back(i);
y.push_back(j);
}
return x.size();
}
int find_index(double xval) {
for (int j = 0; true; j++) {
if (xval >= x[j] && xval <= x[j+1])
return j;
}
}
int main() {
ofstream output("a17-res.dat");
int n = get_file("a16-interpol.dat");
vector <double> alpha(n), beta(n), gamma(n), delta(n), lambda(n+1), mu(n+1), d(n+1), M(n+1);
mu[0] = 2.0;
for (int j = 1; j < n; j++) {
lambda[j] = (x[j+1] - x[j]) / (x[j+1] - x[j-1]);
mu[j] = (x[j] - x[j-1]) / (x[j+1] - x[j-1]);
d[j] = (6 / (x[j+1] - x[j-1])) * ((y[j+1] - y[j]) / (x[j+1] - x[j]) - (y[j] - y[j-1]) / (x[j] - x[j-1]));
}
for (int i = 1; i <= n; i++) {
double f = -mu[i] / mu[i-1];
mu[i] = 2 + f * lambda[i-1];
d[i] = d[i] + f * d[i-1];
}
M[n] = d[n] / mu[n];
for (int j = n - 1; j >= 0; j--) {
M[j] = (d[j] - (lambda[j] * M[j+1])) / mu[j];
alpha[j] = y[j];
beta[j] = (y[j+1] - y[j]) / (x[j+1] - x[j]) - ((2 * M[j] + M[j+1]) * (x[j+1] - x[j]) / 6);
gamma[j] = M[j] / 2;
delta[j] = (M[j+1] - M[j]) / (6 * (x[j+1] - x[j]));
}
for (double xval = x.front(); xval < x.back(); xval += (x.back() - x.front()) / 300) {
int j = find_index(xval);
double D = xval - x[j];
double S = alpha[j] + beta[j] * D + gamma[j] * D * D + delta[j] * D * D * D;
cout << xval << " " << S << endl;
output << xval << " " << S << endl;
}
}
#include <iostream>
#include <vector>
#include <map>
#include <numeric>
using namespace std;
map<char, int> M = {{'M', 1000}, {'D', 500}, {'C', 100}, {'L', 50}, {'X', 10}, {'V', 5}, {'I', 1}}; // requires C++11
int calc(string s) {
vector <int> n;
for (int i = 0; i < s.size(); i++) {
n.push_back(M[toupper(s[i])]);
n[i-1] *= (n[i-1] < n[i]) ? -1 : 1;
}
return accumulate(n.begin(), n.end(), 0);
}
int main() {
string s;
cout << "Eingabe: ";
cin >> s;
cout << "Ausgabe: " << calc(s) << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment