Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- vector<string> split(string a, char c) {
- vector<string> ekvVector;
- string tmp = "";
- for (int i = 0; i < a.size(); i++) {
- if (a[i] == c) {
- ekvVector.push_back(tmp);
- tmp = "";
- } else {
- tmp += a[i];
- }
- }
- if (tmp != "") ekvVector.push_back(tmp);
- return ekvVector;
- }
- int skitMetod(string a, int yType) {
- int endResult = 0;
- int bPos = 0;
- int ePos = 0;
- for (int i = 0; i < a.size(); i++) {
- if (a[i] == 'y') {
- if (a.find("y(") != string::npos || a.find("y'(") != string::npos) {
- if (yType == 3) {
- int parPos1 = 0;
- for (int i = 0; i < a.length(); i++) {
- if (a[i] == ')') {
- parPos1 = i;
- }
- }
- bPos = i;
- if (a.find("'") != string::npos) {
- a.erase(bPos, 3);
- parPos1 -= 3;
- } else {
- a.erase(bPos, 2);
- parPos1 -= 2;
- }
- if (a.find("-") != string::npos) {
- a.erase(parPos1, 4);
- } else {
- a.erase(parPos1, 3);
- }
- }
- } else {
- bPos = i;
- if (yType == 0) {
- a.erase(bPos, 3);
- } else if (yType == 1) {
- a.erase(bPos, 2);
- } else if (yType == 2) {
- a.erase(bPos, 1);
- }
- }
- }
- if (a != "") {
- endResult = stoi(a);
- } else if (a.find("-") != string::npos) {
- endResult = stoi(a);
- endResult *= (-1);
- } else {
- endResult = 1;
- }
- return endResult;
- }
- }
- int main() {
- string ekvString = "";
- vector<string> ekvList;
- cout << "Skriv in din ekvation i formen av ay''+by'+cy (minus skrivs +- t.ex ay''+-by'+y)" << "\n";
- cin >> ekvString;
- cout << "\n";
- ekvList = split(ekvString, '+');
- double a, b, c;
- string yBis = "yBis";
- cout << ekvList.size() << "\n";
- for (int i = 0; i < ekvList.size(); i++) {
- if (ekvList[i].find("y''") != string::npos) {
- cout << "y'' found: " << skitMetod(ekvList[i], 0) << "\n";
- a = skitMetod(ekvList[i], 0);
- } else if (ekvList[i].find("y'") != string::npos && !(ekvList[i].find("y''") != string::npos)) {
- cout << "y' found : " << skitMetod(ekvList[i], 1) << "\n";
- b = skitMetod(ekvList[i], 1);
- } else if (ekvList[i].find("y") != string::npos && !(ekvList[i].find("y''") != string::npos) && !(ekvList[i].find("y'") != string::npos)) {
- cout << "y found : " << skitMetod(ekvList[i], 2) << "\n";
- c = skitMetod(ekvList[i], 2);
- }
- }
- /*
- for (int i = 0; i < ekvList.size(); i++) {
- if (ekvList[i].find(yBis) != string::npos) {
- cout << "yBis found!" << "\n";
- cout << skitMetod(ekvList[i], 0) << "\n";
- a = skitMetod(ekvList[i], 0);
- } else if (ekvList[i].find("yPrim") != string::npos) {
- cout << "yPrim found!" << "\n";
- cout << skitMetod(ekvList[i], 1) << "\n";
- b = skitMetod(ekvList[i], 1);
- } else if (ekvList[i].find("yF") != string::npos) {
- cout << "yF found!" << "\n";
- cout << skitMetod(ekvList[i], 2) << "\n";
- c = skitMetod(ekvList[i], 2);
- }
- }
- */
- b = b / a;
- c = c / a;
- double root1 = ((b / 2)*(-1)) + (sqrt((b / 2)*(b / 2) - c));
- double root2 = ((b / 2)*(-1)) - (sqrt((b / 2)*(b / 2) - c));
- cout << "\n" << "Allm\x84na l\x94sningen: " << "C1e^(" << root1 << "x) + C2e^(" << root2 << "x)\n" << "\n";
- string yLösning = "";
- string yPrimLösning = "";
- cout << "Skriv in l\x94sning p\x86 ekvationen i form av y(x)=z\n";
- cin >> yLösning;
- cout << "Skriv in l\x94sning p\x86 ekvationen i form av y'(x)=z\n";
- cin >> yPrimLösning;
- cout << "\n";
- vector<string> lösningarVector = split(yLösning, '=');
- vector<string> lösningPrimVector = split(yPrimLösning, '=');
- int xVärde = skitMetod(yLösning, 3);
- int xPrimVärde = skitMetod(yPrimLösning, 3);
- int yLösningInt = stoi(lösningarVector[1]);
- int yPrimLösningInt = stoi(lösningPrimVector[1]);
- double c1;
- double c2;
- double e = 2.71828182;
- c1 = (((yLösningInt*(root2*pow(e, root2*xPrimVärde)))) - (pow(e, root2*xVärde))*(yPrimLösningInt)) / ((pow(e, root1*xVärde))*(root2*pow(e, root2*xPrimVärde)) - (pow(e, root2*xVärde))*(root1*pow(e, root1*xPrimVärde)));
- c2 = (((pow(e, root1*xVärde))*(yPrimLösningInt)) - ((yLösningInt)*(root1*pow(e, root1*xPrimVärde)))) / ((pow(e, root1*xVärde))*(root2*pow(e, root2*xPrimVärde)) - (pow(e, root2*xVärde))*(root1*pow(e, root1*xPrimVärde)));
- cout << "C1 = " << c1 << "\n";
- cout << "C2 = " << c2 << "\n\n";
- //c1 = ylösningsint c2=yprimlösningsint b1=e^(root2*xvärde) b2=root2*e^(root2*xprimvärde) a1 = e^(root1*xvärde) a2 = root1*e^(root1*xprimvärde)
- #ifdef _DEBUG
- system("pause");
- #endif // _DEBUG
- }
Advertisement
Add Comment
Please, Sign In to add comment