C++/c++

Advertisement


Question
I have written c++ code for metric analysis. If I given a c++ program as an input, the system should tell how many private variables, public variables, and public, private methods involved in each class.

the program is

#include <iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>



class metric

{

struct variable

{

char name[15];
char scope[12];

}v[40];


struct method

{

char name[30];
char scope[12];

}m[30];

};


void main()

{

char accspec[15][10]={"public", "private", "protected"};

char keyword[15][10]={"int", "char", "float", "class"};

char delimeter[10]={':','}','{',';'};


FILE *fp;
int a,type=0;
char ch_class,str[50];
char degr_visi[15], key[15],curr[15]; // to store the current keyword, access specifier


fp=fopen("j:\\labprms\\add1.cpp","r");
clrscr();
ch_class=fgetc(fp);

while(ch_class != EOF)

{
a=0;
if ((ch_class!=' ')||(ch_class!='\t')||(ch_class!='\n')||(ch_class!=';')||(ch_class!='}')||(ch_class!='{'));
{

if (isalnum(ch_class))

{
if (isalpha(ch_class))

{
while(isalnum(ch_class))

{
str[a++]=ch_class;
ch_class=fgetc(fp);
}

str[a]='\0';



for (int j=0;j<=10;j++)
if (strcmp(keyword[j],str)==0)

{
strcpy(key,keyword[j]);
printf("Keyword<%s>",str);
printf("\n");
printf("Current Keyword is:\t%s",key);
type=1;
}


for (int m=0;m<10;m++)
if (strcmp(accspec[m],str)==0)

{
strcpy(degr_visi,accspec[m]);
printf("access specifier: <%s>",str);
printf("\n");
printf("Current access specifier is: \t%s",degr_visi);
type=2;
}


}
}
    }

}
}



This program finds the first word of the program, but it is not executing further..


the contents of add1.cpp is

class a
{
   public:
         int a;
   private:
         char b,c;
}


it gives the output as:

keyword(class)
current keyword is class...

(not executing further, I have written the code for the variable metric only )

I request your kind help on this regard, and I hope that you will give me well suggestion and guidance.

Waiting for your favourable reply.. Send it as soon as possible

Thanking you

S.Karthikeyan  

Answer
Hi there.

Sorry for the delay.I am in the middle of my exams, so wont be able to help much. The line ch_class=fgetc(fp); is to be inserted just before the for loop ends so that the file may be read again.



Here is your modified code:


#include <iostream.h>
#include<conio.h>
#include<ctype.h>
#include<string.h>
#include<stdio.h>



class metric

{

struct variable

{

char name[15];
char scope[12];

}v[40];


struct method

{

char name[30];
char scope[12];

}m[30];

};


void main()

{

char accspec[15][10]={"public", "private", "protected"};

char keyword[15][10]={"int", "char", "float", "class"};

char delimeter[10]={':','}','{',';'};


FILE *fp;
int a,type=0;
char ch_class,str[50];
char degr_visi[15], key[15],curr[15]; // to store the current keyword, access specifier


fp=fopen("j:\\labprms\\add1.cpp","r");
clrscr();
ch_class=fgetc(fp);

while(ch_class != EOF)

{
a=0;
if ((ch_class!=' ')||(ch_class!='\t')||(ch_class!='\n')||(ch_class!=';')||(ch_class!='}')||(ch_class!='{'));
{

if (isalnum(ch_class))

{
if (isalpha(ch_class))

{
while(isalnum(ch_class))

{
str[a++]=ch_class;
ch_class=fgetc(fp);
}

str[a]='\0';



for (int j=0;j<=10;j++)
if (strcmp(keyword[j],str)==0)

{
strcpy(key,keyword[j]);
printf("Keyword<%s>",str);
printf("\n");
printf("Current Keyword is:\t%s",key);
type=1;
}


for (int m=0;m<10;m++)
if (strcmp(accspec[m],str)==0)

{
strcpy(degr_visi,accspec[m]);
printf("access specifier: <%s>",str);
printf("\n");
printf("Current access specifier is: \t%s",degr_visi);
type=2;
}


}
}
   }

ch_class=fgetc(fp); //inserted here
}
}


The line above has to be inserted.You get the output as :

Keyword<class>
Current Keyword is:     classaccess specifier: <public>
Current access specifier is:    publicKeyword<int>
Current Keyword is:     intaccess specifier: <private>
Current access specifier is:    privateKeyword<char>
Current Keyword is:     char


Bi
Samarth  

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Samarth Bartaria

Expertise

I can answer questions about pointers behaviour, their implementations and anamolous behaviour.Also, I speciallize in object oriented design and modelling,polymorphism in C++ and algorithm efficiency. Even questions related to database design,or simple basics about programming are welcome.

Experience

I have been using C++ for five years now for software development and scientific analyses.

Organizations
Currently, I am a student doing my computer engineering.

©2016 About.com. All rights reserved.