Rakibul_Ahasan

Untitled

Mar 22nd, 2021 (edited)
370
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.72 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define LL long long
  4. //Lab 4
  5. bool isOperator(char ch)
  6. {
  7.     if (ch == '+' || ch == '-' || ch == '*' ||  
  8.         ch == '/' ||  ch == '=' ||  ch == '%')
  9.         return true;
  10.     return false;
  11. }
  12.  
  13. bool isKeyword(string str)
  14. {
  15.     if (str == "if" || str == "else" ||
  16.        str == "while" || str == "do" ||  
  17.         str == "break" || str == "continue"||
  18.        str == "int" || str == "double" ||
  19.         str == "float" || str == "return" ||
  20.         str == "char" || str == "case" ||
  21.         str == "char" || str == "sizeof" ||
  22.         str == "long" || str == "switch" ||
  23.         str == "string" || str == "void" ||
  24.         str == "static")
  25.        
  26.         return true;
  27.     return false;
  28. }
  29.  
  30. bool isConstant(char str)
  31. {
  32.     if (str == '0' || str == '1' || str == '2' ||
  33.         str == '3' || str == '4' || str == '5' ||  
  34.         str == '6' || str == '7' || str == '8' ||  
  35.         str == '9')
  36.         return true;
  37.     return false;
  38. }
  39.  
  40. bool variable(string str)
  41. {
  42.     if(isKeyword(str)) return false;
  43.    
  44.     if(!((str[0] >= 'A' and str[0] <= 'Z') or (str[0] >= 'a' and str[0] <= 'z') or (str[0] == '_'))) return false;
  45.    
  46.     for (int i = 1; i < str.size(); i++) {
  47.         if (!((str[i] >= 'a' and str[i] <= 'z') or (str[i] >= 'A' and str[i] <= 'Z')
  48.           or (str[i] >= '0' and str[i] <= '9') or str[i] == '_')) return false;
  49.     }
  50.     return true;
  51. }
  52.  
  53. void solve(){
  54.    
  55.    ofstream fout;
  56.    string line,strOpeartor,strKeyword,strConstant,strVariable;
  57.    string temp;
  58.    fout.open("program.txt");
  59.    
  60.    while (fout) {
  61.         getline(cin, line);
  62.        
  63.         for(int i=0;i<line.size();i++){
  64.             int op = 1;
  65.             if(isOperator(line[i])){
  66.                 for(int j=0;j<strOpeartor.size();j++){
  67.                     if(strOpeartor[j]==line[i]){
  68.                         op = 0;
  69.                     }
  70.                 }
  71.                 if(op){
  72.                   strOpeartor.push_back(line[i]);  
  73.                   strOpeartor.push_back(',');  
  74.                 }
  75.             }
  76.             if(isConstant(line[i])){
  77.                 int co = 1;
  78.                 for(int j=0;j<strConstant.size();j++){
  79.                     if(strConstant[j]==line[i]){
  80.                         co = 0;
  81.                     }
  82.                 }
  83.                 if(co){
  84.                   strConstant.push_back(line[i]);
  85.                   strConstant.push_back(',');  
  86.                 }
  87.             }
  88.         }
  89.  
  90.         for(int i=0;i<line.size();i++){
  91.            
  92.             if(line[i]!=' ') temp+=line[i];
  93.            
  94.             if(line[i]==' ' or i+1==line.size()){
  95.                 if(isKeyword(temp)){
  96.                     strKeyword+=temp+" ";
  97.                 }
  98.                 if(variable(temp)){
  99.                     strVariable+=temp+" ";
  100.                 }
  101.                 temp="";
  102.             }
  103.         }
  104.        
  105.         if (line == ".") break;
  106.  
  107.     }
  108.     cout<<"Operator: "<<strOpeartor<<"\n";
  109.     cout<<"Keyword: "<<strKeyword<<"\n";
  110.     cout<<"Constant: "<<strConstant<<"\n";
  111.     cout<<"Identifier: "<<strVariable<<"\n";
  112.    
  113.     fout.close();
  114. }
  115.  
  116. int main()
  117. {
  118.     solve();
  119.     return 0;
  120. }
  121.  
Add Comment
Please, Sign In to add comment