Professional Documents
Culture Documents
Assignment-1 CC 03-134192-025 Muhammad Hamza
Assignment-1 CC 03-134192-025 Muhammad Hamza
Assignment-1 CC 03-134192-025 Muhammad Hamza
2. B= (( c + a) * d ) / f
3. While ( a < 5 )
a= a+1
4. Char MyCourse[5];
5. if ( a< b)
a=a*a;
else
b=b*b;
Q2. Write a program in C++ or Java that reads a source file and performs the followings operations:
3. Recognizes constants
4. Recognizes Keywords
5. Recognizes identifiers
#include<iostream>
#include<fstream>
#include<stdlib.h> // random number generate
#include<string.h>
#include<ctype.h> // handle of character.
using namespace std;
int isKeyword(char buffer[]){
char keywords[32][10] = {"auto","break","case","char","const","continue","default",
"do","double","else","enum","extern","float
","for","goto",
"if","int","long","register","return","short","
signed",
"sizeof","static","struct","switch","typedef",
"union",
"unsigned","void","volatile","while"};
int i, flag = 0;
for(i = 0; i < 32; ++i){
if(strcmp(keywords[i], buffer) == 0){
flag = 1;
break;
}
}
return flag;
}
int main()
{
char ch, buffer[15], operators[] = "+-*/%=";
ifstream fin("program.txt");
int i,j=0;
if(!fin.is_open()){
cout<<"error while opening the file\n";
exit(0);
}
while(!fin.eof()){
ch = fin.get();
if(isKeyword(buffer) == 1)
cout<<buffer<<" is keyword\n";
else
}
fin.close();
return 0;
}
6. Recognize operators
#include<iostream>
#include<stdbool.h>
using namespace std;
bool isOperator(char ch)
{
if (ch == '+' || ch == '-' || ch == '*' ||
ch == '/' || ch == '>' || ch == '<' ||
ch == '=')
return (true);
return (false);
}
7. Recognize numbers
#include<iostream>
#include<stdbool.h>
using namespace std;
bool isRealNumber(char* str)
{
int i,strlen, len = strlen(str);
bool hasDecimal = false;
if (len == 0)
return (false);
for (i = 0; i < len; i++) {
if (str[i] != '0' && str[i] != '1' && str[i] != '2'
&& str[i] != '3' && str[i] != '4' && str[i] != '5'
&& str[i] != '6' && str[i] != '7' && str[i] != '8'
&& str[i] != '9' && str[i] != '.' ||
(str[i] == '-' && i > 0))
return (false);
if (str[i] == '.')
hasDecimal = true;
}
return (hasDecimal);
}
Deadline:
Tuesday, 24-Oct-2021 before 09:00 PM (Via LMS)