Skip to content

Instantly share code, notes, and snippets.

@metaxy
Created January 27, 2010 13:20
Show Gist options
  • Save metaxy/287826 to your computer and use it in GitHub Desktop.
Save metaxy/287826 to your computer and use it in GitHub Desktop.
#include <QtCore/QCoreApplication>
#include <iostream>
#include <QTextStream>
#include <QStringList>
#include <math.h>
#include <QtDebug>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTextStream stream(stdin);
QString grad;
QString polynom;
/*cout << "Welchen Grad soll das Polynom habe?";
grad = stream.readLine();*/
cout << "Geben Sie das Polynom ein. Z.B 1x^4 + 3x^3 + 7x^1 \n";
polynom = stream.readLine();// 1x^2 + 3x^4 + 1^d
QStringList summen = polynom.split(" + ");
QMap <int,int> pol;
int maxGrad = 0;
for(int i = 0; i < summen.size();++i) {
QStringList a = summen.at(i).split("x^");
QString kof = a.at(0);
QString g = a.at(1);
pol[g.toInt()] = kof.toDouble();
if(g.toInt() > maxGrad)
maxGrad = g.toInt();
}
qDebug() << "rev 1";
qDebug() << " maxGrad = " << maxGrad << " pol = " << pol;
/*cout << "Geben sie ersten " << grad.toStdString() << " Werte ein. Z.B. 1,3,5,6";
QStringList werte = stream.readLine().split(",");*/
QList<int> werte;
maxGrad+=2;
for(int i = 1; i <= maxGrad; i++) {
int res = 0;
for(int j = 0; j <= maxGrad;j++) {
res += pol[j] * pow(i,j);
}
werte << res;
}
qDebug() << "Werte = " << werte;
QMap<int,QMap<int,int> > cache;
//fill in
for(int i = 0; i < maxGrad;i++) {
QMap<int,int> c;
c[0] = werte.at(i);
cache[i] = c;
}
for(int i = 0; i < maxGrad;i++) {
for(int j = 0; j < maxGrad - i - 1;j++) {
QMap<int,int> c = cache[j];
QMap<int,int> c2 = cache[j+1];
c[i+1] = c2[i]-c[i];
qDebug() << " i = " << i << " J = " << j << c2[i]-c[i];
cache[j] = c;
}
}
qDebug() << cache;
return a.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment