Advertisement
Guest User

trabalho

a guest
Sep 25th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.83 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <fstream>
  5. #include <string.h>
  6.  
  7. using namespace std;
  8.  
  9. void gravaComando(char a, char b, fstream &out, string line)
  10. {
  11.     if(line=="An;"){out<<a<<b<<"0"<<endl;cout<<a<<b<<"0"<<endl;}
  12.     if(line=="nAoB;"){out<<a<<b<<"1"<<endl;cout<<a<<b<<"1"<<endl;}
  13.     if(line=="AnB;"){out<<a<<b<<"2"<<endl;cout<<a<<b<<"2"<<endl;}
  14.     if(line=="zeroL;"){out<<a<<b<<"3"<<endl;cout<<a<<b<<"3"<<endl;}
  15.     if(line=="nAeB;"){out<<a<<b<<"4"<<endl;cout<<a<<b<<"4"<<endl;}
  16.     if(line=="Bn;"){out<<a<<b<<"5"<<endl;cout<<a<<b<<"5"<<endl;}
  17.     if(line=="AxB;"){out<<a<<b<<"6"<<endl;cout<<a<<b<<"6"<<endl;}
  18.     if(line=="ABn;"){out<<a<<b<<"7"<<endl;cout<<a<<b<<"7"<<endl;}
  19.     if(line=="AnoB;"){out<<a<<b<<"8"<<endl;cout<<a<<b<<"8"<<endl;}
  20.     if(line=="nAxB;"){out<<a<<b<<"9"<<endl;cout<<a<<b<<"9"<<endl;}
  21.     if(line=="B;"){out<<a<<b<<"a"<<endl;cout<<a<<b<<"a"<<endl;}
  22.     if(line=="AB;"){out<<a<<b<<"b"<<endl;cout<<a<<b<<"b"<<endl;}
  23.     if(line=="umL;"){out<<a<<b<<"c"<<endl;cout<<a<<b<<"c"<<endl;}
  24.     if(line=="AoBn;"){out<<a<<b<<"d"<<endl;cout<<a<<b<<"d"<<endl;}
  25.     if(line=="AoB;"){out<<a<<b<<"e"<<endl;cout<<a<<b<<"e"<<endl;}
  26.     if(line=="A;"){out<<a<<b<<"f"<<endl;cout<<a<<b<<"f"<<endl;}
  27. }
  28.  
  29. bool foundInObjStr(string tofind, char pattern[],size_t &posfind)
  30. {
  31.     size_t match;
  32.     match=tofind.find(pattern);
  33.     if(match==tofind.npos){return false;}
  34.     else{posfind=match;return true;}
  35. }
  36.  
  37. int main()
  38. {
  39.     size_t foundPos;
  40.     string line;
  41.     fstream arqIn, arqOut;
  42.     arqIn.open("74181.alu",ios::in|ios::binary);
  43.     arqOut.open("74181.hex",ios::out|ios::binary);
  44.     if(arqIn.is_open() && arqOut.is_open())
  45.     {
  46.         cout<<"geracao do arquivo compilado:"<<endl<<"Inicio: "<<endl;
  47.         do
  48.             {
  49.                 //verify if the line is an instruction, if it is, burn in arqOut
  50.                 arqIn>>line;
  51.                 char a,b;
  52.                 if(!foundInObjStr(line,"=",foundPos))
  53.                     {
  54.                         gravaComando(a,b,arqOut,line);
  55.                     }
  56.                 //if it is not, it is an attribution,so it does it
  57.                 else
  58.                     {
  59.                         if(line[0]=='A'){a=line[line.length()-2];}
  60.                         if(line[0]=='B'){b=line[line.length()-2];}
  61.                     }
  62.             }while(!arqIn.eof());
  63.         arqIn.close();
  64.         arqOut.close();
  65.         cout<<"Sucesso ao ler codigo fonte e criar arquivo compilado"<<endl;
  66.     }
  67.     else
  68.     {
  69.         if(!arqIn.is_open()){cout<<"Falha ao ler codigo fonte - verifique as permissoes de leitura em seu S.O."<<endl; return 1;}
  70.         if(!arqOut.is_open()){cout<<"Falha ao gerar arquivo compilado - verifique as permissoes de escrita em seu S.O."<<endl; return 2;}
  71.     }
  72.     //execute the .hex in arduino
  73.     cout<<"Transmissao das instrucoes ao arduino"<<endl;
  74.     arqOut.open("74181.hex",ios::in);
  75.     if(!arqOut.is_open()){cout<<"Falha ao ler codigo fonte - verifique as permissoes de leitura em seu S.O."<<endl; return 1;}
  76.     else
  77.         {
  78.             string comport;
  79.             cout<<"Informe a porta COM:";
  80.             cin.ignore();
  81.             getline(cin,comport);
  82.             while(!arqOut.eof())
  83.             {
  84.                 switch('\n')
  85.                 {
  86.                     line.clear();
  87.                     arqOut>>line;
  88.                     //char args[4];
  89.                     //strcpy(args,line.c_str());
  90.                     string comando;
  91.                     comando="envia "+comport+" "+line[0]+" "+line[1]+" "+line[2];
  92.                     //comando+=args[0];
  93.                     //comando+=" ";
  94.                     //comando+=args[1];
  95.                     //comando+=" ";
  96.                     //comando+=args[2];
  97.                     system(comando.c_str());
  98.                 }
  99.             }
  100.         }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement