Guest User

Untitled

a guest
Apr 17th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.19 KB | None | 0 0
  1. // parser.cpp: определяет точку входа для консольного приложения.
  2. //
  3. #include "StdAfx.h"
  4. #include <iostream>
  5. #include <string>
  6. #include <stdlib.h>
  7. #include <fstream>
  8. using namespace std;
  9. #include <sstream>
  10. #include <math.h>
  11. #define M_PI     3.14159265358979323846
  12. int AmountTmp;
  13. namespace parser
  14. {
  15.     //Сумма и Разность
  16.     double mysum (const string line1, const string line2)
  17.     {
  18.         return atof(line1.c_str())+atof(line2.c_str());
  19.     }
  20.     //Деление
  21.     double mydivision (const string line1, const string line2)
  22.     {
  23.         return atof(line1.c_str())/atof(line2.c_str());
  24.     }
  25.     //Умножение
  26.     double mymultiplication (const string line1, const string line2)
  27.     {
  28.         return atof(line1.c_str())*atof(line2.c_str());
  29.     }
  30.     //Cos
  31.     double mycos (const string line)
  32.     {
  33.         return cos(atof(line.c_str())/180*M_PI);
  34.     }
  35.     //Sin
  36.     double mysin (const string line)
  37.     {
  38.         return sin(atof(line.c_str())/180*M_PI);
  39.     }
  40.     //Sqr
  41.     double mysqr (const string line)
  42.     {
  43.         return sqrt(atof(line.c_str()));
  44.     }
  45.     //Ln
  46.     double myln (const string line)
  47.     {
  48.         return log(atof(line.c_str()));
  49.        
  50.     }
  51.     //Log
  52.     double mylog (const string line1, const string line2)
  53.     {
  54.         return log(atof(line1.c_str()))/log(atof(line2.c_str()));
  55.        
  56.     }
  57.     //Exent
  58.     double myexent (const string line1, const string line2)
  59.     {
  60.         return pow(atof(line1.c_str()),atof(line2.c_str()));
  61.     }
  62.     string DoubleToString(double Value)
  63. {
  64.     ostringstream out;
  65.     out << Value;
  66.     return out.str();
  67. }
  68.    
  69.     struct var
  70.     {
  71.     string Letter;
  72.     int Length;
  73.     string Line[100];
  74.     string outcome;
  75.     ;
  76.     };
  77.    
  78.     struct minimum
  79.     {
  80.         int start;
  81.         string type;
  82.         int end;
  83.     };
  84.    
  85.     minimum min(int sin,int cos,int number, int sign,int staple,int comma, int x, int exent, int letter,int log, int ln,int sqr, int equally, int dot, int semicolon)
  86.     {
  87.     int i,min,step;
  88.     minimum first[]={sin,"Sin",sin+3,cos,"Cos",cos+3,number,"Number",number+1,sign,"Sign",sign+1,staple,"Staple",staple+1,comma,"Comma",comma+1,x,"X",x+1,exent,"Exent",exent+1,letter,"Letter",letter+1,log,"Log",log+3,ln,"Ln",ln+2,sqr,"Sqr",sqr+3,equally,"Equally",equally+1,dot,"Dot",dot+1,semicolon,"Semicolon",semicolon+1};
  89.    
  90.     if (first[0].start == first[8].start || first[1].start == first[8].start || first[9].start == first[8].start || first[10].start == first[8].start || first[11].start == first[8].start)
  91.     first[8].start=-1;
  92.     for (i=0;i<15;i++)
  93.     {
  94.         if (first[i].start==-1)
  95.         {
  96.         }
  97.         else
  98.         {
  99.         min=first[i].start;
  100.         step=i;
  101.         break;
  102.         }
  103.     }
  104.    
  105.    
  106.     for (;i<15;i++)
  107.         if (((first[i].start<min)&&(first[i].start!=-1)))
  108.         {   min=first[i].start;
  109.             step=i;
  110.         }
  111.     return first[step];
  112. };
  113.     string* sort(string line)
  114.     {
  115.     string *process=new string[line.length()];
  116.     int i,j=0;
  117.    
  118.     if (line.find_last_of("=")!=line.find_first_of("="))
  119.         line.erase(line.find_first_of("="),line.find_last_of("=")-1);
  120.  
  121.     while (line.empty()==false)
  122.     {
  123.    
  124.         minimum first=min(line.find("sin"),line.find("cos"),line.find_first_of("0123456789"),line.find_first_of("*-+/"),line.find_first_of("()"),
  125.         line.find_first_of(","),line.find_first_of('x'),line.find_first_of('^'),line.find_first_of("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"),
  126.         line.find("log"),line.find("ln"),line.find("sqr"),line.find_first_of("="),line.find_first_of("."),line.find_first_of(";"));
  127.  
  128.     if (first.type=="Sin" || first.type=="Cos" || first.type==("Log") || first.type==("Sqr"))
  129.         {
  130.             for (i=first.start;i<3;i++)
  131.                 process[j]+=line[i];
  132.                 j++;
  133.         }
  134.    
  135.     if (first.type==("Ln"))
  136.         {
  137.             for (i=first.start;i<2;i++)
  138.                 process[j]+=line[i];
  139.                 j++;
  140.         }
  141.    
  142.     if (first.type=="Sign" || first.type=="Staple" || first.type=="X" || first.type=="Exent" || first.type=="Exent"|| first.type=="Equally" || first.type=="Semicolon")
  143.         {
  144.             i=first.start;
  145.             process[j]+=line[i];
  146.             j++;
  147.         }
  148.                
  149.     if (first.type=="Number" || first.type=="Comma" || first.type=="Dot")
  150.         {  
  151.             if (line.find_first_not_of("0123456789,.")!=-1)
  152.             first.end=line.find_first_not_of("0123456789,.");
  153.             else
  154.             first.end=line.length();
  155.             if (line.find_first_of(',')!=-1)
  156.                 line[line.find_first_of(',')]='.';
  157.             for (i=first.start;i<first.end;i++)
  158.             {
  159.                 process[j]+=line[i];
  160.             }
  161.        
  162.     j++;
  163.         }
  164.     if (first.type=="Letter")
  165.     {
  166.         i=first.start;
  167.         if (line[i]>96 && line[i]<123)
  168.             line[i]-=32;
  169.        
  170.         process[j]+=line[i];
  171.         j++;
  172.     }
  173.     line.erase(first.start,first.end);
  174.     }
  175. AmountTmp=j;
  176.  
  177. return process;
  178.     };
  179.    
  180.     void work(void)
  181.     {
  182.     ifstream InputFile("Input.txt");
  183.     char buf[50];
  184.     string raw[100];
  185.     int StringCount=0;
  186.    
  187.     while (!InputFile.eof())
  188.     {
  189.         InputFile.getline(buf,50);
  190.         raw[StringCount]=(buf);
  191.         StringCount++;
  192.     }
  193.    
  194.     int *Amount=new int[StringCount];  
  195.     string** processed=new string*[StringCount];
  196.    
  197.     var* hang=new var[StringCount];
  198.    
  199.     for (int i=0; i<StringCount;i++)
  200.     {
  201.     processed[i]=parser::sort(raw[i]);
  202.     Amount[i]=AmountTmp;
  203.     }
  204.        
  205.     struct comp
  206.     {
  207.     char Symbol;
  208.     int Number;
  209.     };
  210.    
  211.     int NewStringCount=StringCount;
  212.    
  213.     comp compare;
  214.    
  215.     for (int i=0; i<StringCount;i++)
  216.     {
  217.     if (!processed[i][0].empty())
  218.     {
  219.         compare.Symbol=processed[i][0].at(0);
  220.         compare.Number=i;
  221.    
  222.     for (int j=0; j<StringCount;j++)
  223.     {
  224.         if (compare.Number!=j && !processed[j][0].empty())
  225.         if (processed[j][0].at(0)==compare.Symbol)
  226.         {
  227.             processed[compare.Number][0].erase();
  228.             compare.Number=j;
  229.             NewStringCount--;
  230.         }
  231.            
  232.     }
  233.     }
  234.     }
  235.    
  236.     int noempty=0;
  237.    
  238.     for (int i=0,k,j;i<StringCount;i++)
  239.     {
  240.     hang[noempty].Length=0;
  241.     hang[noempty].Letter=processed[i][0].at(0);
  242.     if (!processed[i][0].empty())
  243.     {  
  244.     for (j=2,k=0; j<Amount[i];j++,k++)
  245.     {
  246.         hang[noempty].Line[k]=processed[i][j];
  247.         hang[noempty].Length++;
  248.     }
  249.     if (j!=0)
  250.         noempty++;
  251.     }
  252.     }
  253.     StringCount=noempty;
  254.  
  255.    
  256.  
  257. for (int g=0,k;g<StringCount*StringCount;g++)
  258. for (int k=0;k<StringCount;k++){
  259. bool find=true;
  260. while(find){
  261.  
  262. int OpenPos=0,ClosePos=hang[k].Length,j;
  263.  
  264. for (int j=OpenPos;j<ClosePos;j++)
  265.     for (int i=0; i<StringCount;i++)
  266.         if (hang[k].Line[j]==hang[i].Letter && hang[i].Letter!=hang[k].Letter){
  267.     if (!hang[i].outcome.empty()){
  268.     }
  269.     else
  270.         goto break1;
  271.     }
  272.  
  273. for (j=0;j<hang[k].Length;j++)
  274.     if (hang[k].Line[j]==")"){
  275.         ClosePos=j;
  276.         hang[k].Line[j].erase();
  277.         break;
  278.     }
  279. for (int l=j-1;l>=0;l--)
  280.     if (hang[k].Line[l]=="("){
  281.         OpenPos=l;
  282.         hang[k].Line[l].erase();
  283.         break;
  284.         }
  285.    
  286.  
  287.     if (OpenPos==0 && ClosePos==hang[k].Length)
  288.         find=false;
  289.    
  290. for (int i=OpenPos;i<ClosePos;i++)
  291. {
  292.     int LPos=0,RPos=0,Pos=0;
  293.     struct nice{
  294.     bool symbol;
  295.     int  number;
  296.     };
  297.     nice cheak={false,0};
  298.    
  299. for (int j=OpenPos;j<ClosePos;j++)
  300.     if (hang[k].Line[j]=="+" || hang[k].Line[j]=="-"){
  301.             Pos=j;
  302.             break;
  303.     }
  304.  
  305. for (int j=OpenPos;j<ClosePos;j++)
  306.     if (hang[k].Line[j]=="*" || hang[k].Line[j]=="/"){
  307.             Pos=j;
  308.             break;
  309.     }
  310.    
  311. for (int j=OpenPos;j<ClosePos;j++)
  312.     if (hang[k].Line[j]=="^" || hang[k].Line[j]=="sin" || hang[k].Line[j]=="cos" || hang[k].Line[j]=="ln" || hang[k].Line[j]=="log" || hang[k].Line[j]=="sqr"){
  313.             Pos=j;
  314.             break;
  315.     }
  316.  
  317. for (int j=OpenPos;j<ClosePos;j++)
  318.     for (int i=0; i<StringCount;i++)
  319.         if (hang[k].Line[j]==hang[i].Letter && hang[i].Letter!=hang[k].Letter){
  320.            
  321.             cheak.symbol=true;
  322.             cheak.number=i;
  323.             Pos=j;
  324.             break;
  325.     }
  326.         //else
  327.             //if (hang[k].Line[j].find_first_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")!=-1)
  328.                 //goto break1;
  329. if (cheak.symbol)
  330.     if (!hang[cheak.number].outcome.empty()){
  331.     }
  332.     else
  333.         goto break1;
  334.  
  335.  
  336. for (int j=Pos-1;j>=0;j--)
  337.     if (!hang[k].Line[j].empty()){
  338.             LPos=j;
  339.             break;
  340.     }
  341. for (int j=Pos+1;j<=hang[k].Length;j++)
  342.     if (!hang[k].Line[j].empty()){
  343.             RPos=j;
  344.             break;
  345.     }
  346.  
  347. if (hang[k].Line[Pos]=="^") {
  348.     hang[k].Line[LPos]=DoubleToString(myexent(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
  349.     hang[k].Line[Pos].erase();
  350.     hang[k].Line[RPos].erase();
  351. }  
  352.    
  353. if (hang[k].Line[Pos]=="+") {
  354.     hang[k].Line[LPos]=DoubleToString(mysum(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
  355.     hang[k].Line[Pos].erase();
  356.     hang[k].Line[RPos].erase();
  357. }
  358.  
  359. if (hang[k].Line[Pos]=="-") {
  360.     hang[k].Line[RPos]="-"+hang[k].Line[RPos];
  361.     hang[k].Line[LPos]=DoubleToString(mysum(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
  362.     hang[k].Line[Pos].erase();
  363.     hang[k].Line[RPos].erase();
  364. }
  365.  
  366. if (hang[k].Line[Pos]=="*") {
  367.     hang[k].Line[LPos]=DoubleToString(mymultiplication(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
  368.     hang[k].Line[Pos].erase();
  369.     hang[k].Line[RPos].erase();
  370. }
  371.  
  372. if (hang[k].Line[Pos]=="/") {
  373.     hang[k].Line[LPos]=DoubleToString(mydivision(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
  374.     hang[k].Line[Pos].erase();
  375.     hang[k].Line[RPos].erase();
  376. }
  377.  
  378. if (hang[k].Line[Pos]=="sin") {
  379.     hang[k].Line[Pos]=DoubleToString((((int)mysin(hang[k].Line[RPos].c_str()))*100)/100);
  380.     hang[k].Line[RPos].erase();
  381. }
  382.  
  383. if (hang[k].Line[Pos]=="cos") {
  384.     hang[k].Line[Pos]=DoubleToString((((int)mycos(hang[k].Line[RPos].c_str()))*100)/100);
  385.     hang[k].Line[RPos].erase();
  386. }
  387.  
  388. if (hang[k].Line[Pos]=="sqr") {
  389.     hang[k].Line[Pos]=DoubleToString(mysqr(hang[k].Line[RPos].c_str()));
  390.     hang[k].Line[RPos].erase();
  391. }
  392.  
  393. if (hang[k].Line[Pos]=="ln") {
  394.     hang[k].Line[Pos]=DoubleToString(myln(hang[k].Line[RPos].c_str()));
  395.     hang[k].Line[RPos].erase();
  396. }
  397.  
  398. if (hang[k].Line[Pos]=="log") {
  399.     hang[k].Line[Pos]=DoubleToString(mylog(hang[k].Line[RPos].c_str(),hang[k].Line[RPos+2].c_str()));
  400.     hang[k].Line[RPos].erase();
  401.     hang[k].Line[RPos+1].erase();
  402.     hang[k].Line[RPos+2].erase();
  403. }
  404. if (cheak.symbol)
  405.     hang[k].Line[Pos]=hang[cheak.number].outcome;
  406.  
  407. }
  408.  
  409. }
  410. for (int i=0; i<hang[k].Length;i++)
  411.     if (hang[k].Line[i].find_first_of("0123456789")!=-1){
  412.     hang[k].outcome=hang[k].Line[i];
  413.     break;
  414.     }
  415. break1:;
  416. }
  417.  
  418. string tmpStr;
  419. char tmpChar;
  420. for (int i=0; i<StringCount;i++)
  421.     for (int j=0; j<StringCount-1; j++){
  422.         if(hang[j].Letter.at(0)>hang[j+1].Letter.at(0)){
  423.         tmpChar=hang[j].Letter.at(0);tmpStr=hang[j].outcome;
  424.         hang[j].Letter.at(0)=hang[j+1].Letter.at(0);hang[j].outcome=hang[j+1].outcome;
  425.         hang[j+1].Letter.at(0)=tmpChar;hang[j+1].outcome=tmpStr;
  426.         }
  427.     }
  428.    
  429. fstream output("Output.txt",ios::out);     
  430. for (int i=0; i<StringCount;i++){  
  431.     if (!hang[i].outcome.empty())
  432.     output << hang[i].Letter << "=" << hang[i].outcome << endl;
  433. }
  434. }
  435. }  
  436. int main()
  437. {
  438.     parser::work();
  439.     system("pause");
  440.     return 0;
  441. }
Add Comment
Please, Sign In to add comment