Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define LL long long
- //Lab 4
- bool isOperator(char ch)
- {
- if (ch == '+' || ch == '-' || ch == '*' ||
- ch == '/' || ch == '=' || ch == '%')
- return true;
- return false;
- }
- bool isKeyword(string str)
- {
- if (str == "if" || str == "else" ||
- str == "while" || str == "do" ||
- str == "break" || str == "continue"||
- str == "int" || str == "double" ||
- str == "float" || str == "return" ||
- str == "char" || str == "case" ||
- str == "char" || str == "sizeof" ||
- str == "long" || str == "switch" ||
- str == "string" || str == "void" ||
- str == "static")
- return true;
- return false;
- }
- bool isConstant(char str)
- {
- if (str == '0' || str == '1' || str == '2' ||
- str == '3' || str == '4' || str == '5' ||
- str == '6' || str == '7' || str == '8' ||
- str == '9')
- return true;
- return false;
- }
- bool variable(string str)
- {
- if(isKeyword(str)) return false;
- if(!((str[0] >= 'A' and str[0] <= 'Z') or (str[0] >= 'a' and str[0] <= 'z') or (str[0] == '_'))) return false;
- for (int i = 1; i < str.size(); i++) {
- if (!((str[i] >= 'a' and str[i] <= 'z') or (str[i] >= 'A' and str[i] <= 'Z')
- or (str[i] >= '0' and str[i] <= '9') or str[i] == '_')) return false;
- }
- return true;
- }
- void solve(){
- ofstream fout;
- string line,strOpeartor,strKeyword,strConstant,strVariable;
- string temp;
- fout.open("program.txt");
- while (fout) {
- getline(cin, line);
- for(int i=0;i<line.size();i++){
- int op = 1;
- if(isOperator(line[i])){
- for(int j=0;j<strOpeartor.size();j++){
- if(strOpeartor[j]==line[i]){
- op = 0;
- }
- }
- if(op){
- strOpeartor.push_back(line[i]);
- strOpeartor.push_back(',');
- }
- }
- if(isConstant(line[i])){
- int co = 1;
- for(int j=0;j<strConstant.size();j++){
- if(strConstant[j]==line[i]){
- co = 0;
- }
- }
- if(co){
- strConstant.push_back(line[i]);
- strConstant.push_back(',');
- }
- }
- }
- for(int i=0;i<line.size();i++){
- if(line[i]!=' ') temp+=line[i];
- if(line[i]==' ' or i+1==line.size()){
- if(isKeyword(temp)){
- strKeyword+=temp+" ";
- }
- if(variable(temp)){
- strVariable+=temp+" ";
- }
- temp="";
- }
- }
- if (line == ".") break;
- }
- cout<<"Operator: "<<strOpeartor<<"\n";
- cout<<"Keyword: "<<strKeyword<<"\n";
- cout<<"Constant: "<<strConstant<<"\n";
- cout<<"Identifier: "<<strVariable<<"\n";
- fout.close();
- }
- int main()
- {
- solve();
- return 0;
- }
Add Comment
Please, Sign In to add comment