Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // parser.cpp: определяет точку входа для консольного приложения.
- //
- #include "StdAfx.h"
- #include <iostream>
- #include <string>
- #include <stdlib.h>
- #include <fstream>
- using namespace std;
- #include <sstream>
- #include <math.h>
- #define M_PI 3.14159265358979323846
- int AmountTmp;
- namespace parser
- {
- //Сумма и Разность
- double mysum (const string line1, const string line2)
- {
- return atof(line1.c_str())+atof(line2.c_str());
- }
- //Деление
- double mydivision (const string line1, const string line2)
- {
- return atof(line1.c_str())/atof(line2.c_str());
- }
- //Умножение
- double mymultiplication (const string line1, const string line2)
- {
- return atof(line1.c_str())*atof(line2.c_str());
- }
- //Cos
- double mycos (const string line)
- {
- return cos(atof(line.c_str())/180*M_PI);
- }
- //Sin
- double mysin (const string line)
- {
- return sin(atof(line.c_str())/180*M_PI);
- }
- //Sqr
- double mysqr (const string line)
- {
- return sqrt(atof(line.c_str()));
- }
- //Ln
- double myln (const string line)
- {
- return log(atof(line.c_str()));
- }
- //Log
- double mylog (const string line1, const string line2)
- {
- return log(atof(line1.c_str()))/log(atof(line2.c_str()));
- }
- //Exent
- double myexent (const string line1, const string line2)
- {
- return pow(atof(line1.c_str()),atof(line2.c_str()));
- }
- string DoubleToString(double Value)
- {
- ostringstream out;
- out << Value;
- return out.str();
- }
- struct var
- {
- string Letter;
- int Length;
- string Line[100];
- string outcome;
- ;
- };
- struct minimum
- {
- int start;
- string type;
- int end;
- };
- 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)
- {
- int i,min,step;
- 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};
- 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)
- first[8].start=-1;
- for (i=0;i<15;i++)
- {
- if (first[i].start==-1)
- {
- }
- else
- {
- min=first[i].start;
- step=i;
- break;
- }
- }
- for (;i<15;i++)
- if (((first[i].start<min)&&(first[i].start!=-1)))
- { min=first[i].start;
- step=i;
- }
- return first[step];
- };
- string* sort(string line)
- {
- string *process=new string[line.length()];
- int i,j=0;
- if (line.find_last_of("=")!=line.find_first_of("="))
- line.erase(line.find_first_of("="),line.find_last_of("=")-1);
- while (line.empty()==false)
- {
- minimum first=min(line.find("sin"),line.find("cos"),line.find_first_of("0123456789"),line.find_first_of("*-+/"),line.find_first_of("()"),
- line.find_first_of(","),line.find_first_of('x'),line.find_first_of('^'),line.find_first_of("QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm"),
- line.find("log"),line.find("ln"),line.find("sqr"),line.find_first_of("="),line.find_first_of("."),line.find_first_of(";"));
- if (first.type=="Sin" || first.type=="Cos" || first.type==("Log") || first.type==("Sqr"))
- {
- for (i=first.start;i<3;i++)
- process[j]+=line[i];
- j++;
- }
- if (first.type==("Ln"))
- {
- for (i=first.start;i<2;i++)
- process[j]+=line[i];
- j++;
- }
- if (first.type=="Sign" || first.type=="Staple" || first.type=="X" || first.type=="Exent" || first.type=="Exent"|| first.type=="Equally" || first.type=="Semicolon")
- {
- i=first.start;
- process[j]+=line[i];
- j++;
- }
- if (first.type=="Number" || first.type=="Comma" || first.type=="Dot")
- {
- if (line.find_first_not_of("0123456789,.")!=-1)
- first.end=line.find_first_not_of("0123456789,.");
- else
- first.end=line.length();
- if (line.find_first_of(',')!=-1)
- line[line.find_first_of(',')]='.';
- for (i=first.start;i<first.end;i++)
- {
- process[j]+=line[i];
- }
- j++;
- }
- if (first.type=="Letter")
- {
- i=first.start;
- if (line[i]>96 && line[i]<123)
- line[i]-=32;
- process[j]+=line[i];
- j++;
- }
- line.erase(first.start,first.end);
- }
- AmountTmp=j;
- return process;
- };
- void work(void)
- {
- ifstream InputFile("Input.txt");
- char buf[50];
- string raw[100];
- int StringCount=0;
- while (!InputFile.eof())
- {
- InputFile.getline(buf,50);
- raw[StringCount]=(buf);
- StringCount++;
- }
- int *Amount=new int[StringCount];
- string** processed=new string*[StringCount];
- var* hang=new var[StringCount];
- for (int i=0; i<StringCount;i++)
- {
- processed[i]=parser::sort(raw[i]);
- Amount[i]=AmountTmp;
- }
- struct comp
- {
- char Symbol;
- int Number;
- };
- int NewStringCount=StringCount;
- comp compare;
- for (int i=0; i<StringCount;i++)
- {
- if (!processed[i][0].empty())
- {
- compare.Symbol=processed[i][0].at(0);
- compare.Number=i;
- for (int j=0; j<StringCount;j++)
- {
- if (compare.Number!=j && !processed[j][0].empty())
- if (processed[j][0].at(0)==compare.Symbol)
- {
- processed[compare.Number][0].erase();
- compare.Number=j;
- NewStringCount--;
- }
- }
- }
- }
- int noempty=0;
- for (int i=0,k,j;i<StringCount;i++)
- {
- hang[noempty].Length=0;
- hang[noempty].Letter=processed[i][0].at(0);
- if (!processed[i][0].empty())
- {
- for (j=2,k=0; j<Amount[i];j++,k++)
- {
- hang[noempty].Line[k]=processed[i][j];
- hang[noempty].Length++;
- }
- if (j!=0)
- noempty++;
- }
- }
- StringCount=noempty;
- for (int g=0,k;g<StringCount*StringCount;g++)
- for (int k=0;k<StringCount;k++){
- bool find=true;
- while(find){
- int OpenPos=0,ClosePos=hang[k].Length,j;
- for (int j=OpenPos;j<ClosePos;j++)
- for (int i=0; i<StringCount;i++)
- if (hang[k].Line[j]==hang[i].Letter && hang[i].Letter!=hang[k].Letter){
- if (!hang[i].outcome.empty()){
- }
- else
- goto break1;
- }
- for (j=0;j<hang[k].Length;j++)
- if (hang[k].Line[j]==")"){
- ClosePos=j;
- hang[k].Line[j].erase();
- break;
- }
- for (int l=j-1;l>=0;l--)
- if (hang[k].Line[l]=="("){
- OpenPos=l;
- hang[k].Line[l].erase();
- break;
- }
- if (OpenPos==0 && ClosePos==hang[k].Length)
- find=false;
- for (int i=OpenPos;i<ClosePos;i++)
- {
- int LPos=0,RPos=0,Pos=0;
- struct nice{
- bool symbol;
- int number;
- };
- nice cheak={false,0};
- for (int j=OpenPos;j<ClosePos;j++)
- if (hang[k].Line[j]=="+" || hang[k].Line[j]=="-"){
- Pos=j;
- break;
- }
- for (int j=OpenPos;j<ClosePos;j++)
- if (hang[k].Line[j]=="*" || hang[k].Line[j]=="/"){
- Pos=j;
- break;
- }
- for (int j=OpenPos;j<ClosePos;j++)
- 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"){
- Pos=j;
- break;
- }
- for (int j=OpenPos;j<ClosePos;j++)
- for (int i=0; i<StringCount;i++)
- if (hang[k].Line[j]==hang[i].Letter && hang[i].Letter!=hang[k].Letter){
- cheak.symbol=true;
- cheak.number=i;
- Pos=j;
- break;
- }
- //else
- //if (hang[k].Line[j].find_first_of("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM")!=-1)
- //goto break1;
- if (cheak.symbol)
- if (!hang[cheak.number].outcome.empty()){
- }
- else
- goto break1;
- for (int j=Pos-1;j>=0;j--)
- if (!hang[k].Line[j].empty()){
- LPos=j;
- break;
- }
- for (int j=Pos+1;j<=hang[k].Length;j++)
- if (!hang[k].Line[j].empty()){
- RPos=j;
- break;
- }
- if (hang[k].Line[Pos]=="^") {
- hang[k].Line[LPos]=DoubleToString(myexent(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
- hang[k].Line[Pos].erase();
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="+") {
- hang[k].Line[LPos]=DoubleToString(mysum(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
- hang[k].Line[Pos].erase();
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="-") {
- hang[k].Line[RPos]="-"+hang[k].Line[RPos];
- hang[k].Line[LPos]=DoubleToString(mysum(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
- hang[k].Line[Pos].erase();
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="*") {
- hang[k].Line[LPos]=DoubleToString(mymultiplication(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
- hang[k].Line[Pos].erase();
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="/") {
- hang[k].Line[LPos]=DoubleToString(mydivision(hang[k].Line[LPos].c_str(),hang[k].Line[RPos].c_str()));
- hang[k].Line[Pos].erase();
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="sin") {
- hang[k].Line[Pos]=DoubleToString((((int)mysin(hang[k].Line[RPos].c_str()))*100)/100);
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="cos") {
- hang[k].Line[Pos]=DoubleToString((((int)mycos(hang[k].Line[RPos].c_str()))*100)/100);
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="sqr") {
- hang[k].Line[Pos]=DoubleToString(mysqr(hang[k].Line[RPos].c_str()));
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="ln") {
- hang[k].Line[Pos]=DoubleToString(myln(hang[k].Line[RPos].c_str()));
- hang[k].Line[RPos].erase();
- }
- if (hang[k].Line[Pos]=="log") {
- hang[k].Line[Pos]=DoubleToString(mylog(hang[k].Line[RPos].c_str(),hang[k].Line[RPos+2].c_str()));
- hang[k].Line[RPos].erase();
- hang[k].Line[RPos+1].erase();
- hang[k].Line[RPos+2].erase();
- }
- if (cheak.symbol)
- hang[k].Line[Pos]=hang[cheak.number].outcome;
- }
- }
- for (int i=0; i<hang[k].Length;i++)
- if (hang[k].Line[i].find_first_of("0123456789")!=-1){
- hang[k].outcome=hang[k].Line[i];
- break;
- }
- break1:;
- }
- string tmpStr;
- char tmpChar;
- for (int i=0; i<StringCount;i++)
- for (int j=0; j<StringCount-1; j++){
- if(hang[j].Letter.at(0)>hang[j+1].Letter.at(0)){
- tmpChar=hang[j].Letter.at(0);tmpStr=hang[j].outcome;
- hang[j].Letter.at(0)=hang[j+1].Letter.at(0);hang[j].outcome=hang[j+1].outcome;
- hang[j+1].Letter.at(0)=tmpChar;hang[j+1].outcome=tmpStr;
- }
- }
- fstream output("Output.txt",ios::out);
- for (int i=0; i<StringCount;i++){
- if (!hang[i].outcome.empty())
- output << hang[i].Letter << "=" << hang[i].outcome << endl;
- }
- }
- }
- int main()
- {
- parser::work();
- system("pause");
- return 0;
- }
Add Comment
Please, Sign In to add comment