botgob

Allmän lösning klar

Mar 9th, 2019
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.52 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. vector<string> split(string a, char c) {
  8.     vector<string> ekvVector;
  9.     string tmp = "";
  10.     for (int i = 0; i < a.size(); i++) {
  11.         if (a[i] == c) {
  12.             ekvVector.push_back(tmp);
  13.             tmp = "";
  14.         } else {
  15.             tmp += a[i];
  16.         }
  17.     }
  18.     if (tmp != "") ekvVector.push_back(tmp);
  19.     return ekvVector;
  20. }
  21. int skitMetod(string a, int yType) {
  22.     int endResult = 0;
  23.     int bPos = 0;
  24.     int ePos = 0;
  25.     for (int i = 0; i < a.size(); i++) {
  26.         if (a[i] == 'y') {
  27.  
  28.             if (a.find("y(") != string::npos || a.find("y'(") != string::npos) {
  29.                 if (yType == 3) {
  30.                     int parPos1 = 0;
  31.                     for (int i = 0; i < a.length(); i++) {
  32.                         if (a[i] == ')') {
  33.                             parPos1 = i;
  34.                         }
  35.                     }
  36.                     bPos = i;
  37.                     if (a.find("'") != string::npos) {
  38.                         a.erase(bPos, 3);
  39.                         parPos1 -= 3;
  40.                     } else {
  41.                         a.erase(bPos, 2);
  42.                         parPos1 -= 2;
  43.                     }
  44.                     if (a.find("-") != string::npos) {
  45.                         a.erase(parPos1, 4);
  46.                     } else {
  47.                         a.erase(parPos1, 3);
  48.                     }
  49.                    
  50.                 }
  51.             } else {
  52.                 bPos = i;
  53.                 if (yType == 0) {
  54.                     a.erase(bPos, 3);
  55.  
  56.                 } else if (yType == 1) {
  57.                     a.erase(bPos, 2);
  58.  
  59.                 } else if (yType == 2) {
  60.                     a.erase(bPos, 1);
  61.  
  62.                 }
  63.             }
  64.         }
  65.         if (a != "") {
  66.             endResult = stoi(a);
  67.         } else if (a.find("-") != string::npos) {
  68.             endResult = stoi(a);
  69.             endResult *= (-1);
  70.         } else {
  71.             endResult = 1;
  72.         }
  73.         return endResult;
  74.     }
  75. }
  76.  
  77. int main() {
  78.     string ekvString = "";
  79.     vector<string> ekvList;
  80.     cout << "Skriv in din ekvation i formen av ay''+by'+cy (minus skrivs +- t.ex ay''+-by'+y)" << "\n";
  81.     cin >> ekvString;
  82.     cout << "\n";
  83.     ekvList = split(ekvString, '+');
  84.     double a, b, c;
  85.     string yBis = "yBis";
  86.     cout << ekvList.size() << "\n";
  87.     for (int i = 0; i < ekvList.size(); i++) {
  88.         if (ekvList[i].find("y''") != string::npos) {
  89.             cout << "y'' found: " << skitMetod(ekvList[i], 0) << "\n";
  90.             a = skitMetod(ekvList[i], 0);
  91.         } else if (ekvList[i].find("y'") != string::npos && !(ekvList[i].find("y''") != string::npos)) {
  92.             cout << "y' found : " << skitMetod(ekvList[i], 1) << "\n";
  93.             b = skitMetod(ekvList[i], 1);
  94.         } else if (ekvList[i].find("y") != string::npos && !(ekvList[i].find("y''") != string::npos) && !(ekvList[i].find("y'") != string::npos)) {
  95.             cout << "y found : " << skitMetod(ekvList[i], 2) << "\n";
  96.             c = skitMetod(ekvList[i], 2);
  97.         }
  98.     }
  99.     /*
  100.     for (int i = 0; i < ekvList.size(); i++) {
  101.         if (ekvList[i].find(yBis) != string::npos) {
  102.             cout << "yBis found!" << "\n";
  103.             cout << skitMetod(ekvList[i], 0) << "\n";
  104.             a = skitMetod(ekvList[i], 0);
  105.         } else if (ekvList[i].find("yPrim") != string::npos) {
  106.             cout << "yPrim found!" << "\n";
  107.             cout << skitMetod(ekvList[i], 1) << "\n";
  108.             b = skitMetod(ekvList[i], 1);
  109.         } else if (ekvList[i].find("yF") != string::npos) {
  110.             cout << "yF found!" << "\n";
  111.             cout << skitMetod(ekvList[i], 2) << "\n";
  112.             c = skitMetod(ekvList[i], 2);
  113.         }
  114.     }
  115.     */
  116.     b = b / a;
  117.     c = c / a;
  118.  
  119.     double root1 = ((b / 2)*(-1)) + (sqrt((b / 2)*(b / 2) - c));
  120.     double root2 = ((b / 2)*(-1)) - (sqrt((b / 2)*(b / 2) - c));
  121.  
  122.  
  123.     cout << "\n" << "Allm\x84na l\x94sningen: " << "C1e^(" << root1 << "x) + C2e^(" << root2 << "x)\n" << "\n";
  124.     string yLösning = "";
  125.     string yPrimLösning = "";
  126.  
  127.     cout << "Skriv in l\x94sning p\x86 ekvationen i form av y(x)=z\n";
  128.     cin >> yLösning;
  129.     cout << "Skriv in l\x94sning p\x86 ekvationen i form av y'(x)=z\n";
  130.     cin >> yPrimLösning;
  131.     cout << "\n";
  132.     vector<string> lösningarVector = split(yLösning, '=');
  133.     vector<string> lösningPrimVector = split(yPrimLösning, '=');
  134.     int xVärde = skitMetod(yLösning, 3);
  135.     int xPrimVärde = skitMetod(yPrimLösning, 3);
  136.     int yLösningInt = stoi(lösningarVector[1]);
  137.     int yPrimLösningInt = stoi(lösningPrimVector[1]);
  138.  
  139.     double c1;
  140.     double c2;
  141.     double e = 2.71828182;
  142.  
  143.     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)));
  144.     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)));
  145.  
  146.  
  147.     cout << "C1 = " << c1 << "\n";
  148.     cout << "C2 = " << c2 << "\n\n";
  149.     //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)
  150.  
  151. #ifdef _DEBUG
  152.     system("pause");
  153. #endif // _DEBUG
  154. }
Advertisement
Add Comment
Please, Sign In to add comment