Reads an expression consisting of two
non-negative integer and an operator. Determine if either integer or the result
# include
#include
# include
# include <string.h>
# include
int main( )
{
clrscr( );
fstream File("CP-17.txt",ios::in|ios::nocreate);
if(!File)
{
cout<<"\n Unable to open the input file."<
cout<<"\n Press any key to exit.";
getch( );
exit(exit_failure);
}
char Data[100]={NULL};
do
{
strset(Data,NULL);
File.getline(Data,80);
if(strcmp(Data,NULL)==0)
break;
cout<
int flag=0;
int operand_1=0;
int operand_2=0;
char* Ptr=NULL;
char operator=NULL;
ptr=strtok(Data," ");
if(strlen(Ptr)>5 || atol(Ptr)>32767)
{
cout<<"first number too big"<
flag=1;
}
else
operand_1=atoi(Ptr);
ptr=strtok(NULL," ");
operator=ptr[0];
ptr=strtok(null,"\n");
if(strlen(ptr)>5 || atol(Ptr)>32767)
{
cout<<"second number too big"<
flag=1;
}
else
operand_2=atoi(ptr);
if(flag)
cout<<"result too big"<
else
{
long result;
if(operator=='+')
result=operand_1+operand_2;
elseif(operator=='*')
result=operand_1*operand_2;
if(result>32767)
cout<<"result too big"<
}
}
while(1);
file.close( );
getch( );
return 0;
}
Illustrate static member functions in
C++ Programming
#include
#include
/*************************************************************************///------------------------------ check --------------------------------///*************************************************************************/
class check
{
private:
int data1;
staticint data2;
public:
void setvalue(int);
void showdata();
staticvoid showcount();
};
/*************************************************************************///--------------------- static data initialization --------------------///*************************************************************************/
int check::data2=0;
/*************************************************************************///--------------------------- setvalue(int) ---------------------------///*************************************************************************/
void check::setvalue(intvalue)
{
data1=value;
data2=value;
}
/*************************************************************************///--------------------------- showdata( ) ----------------------------///*************************************************************************/
void check::showdata()
{
cout<<"\n\t ordinary data = data1 = "<
cout<<"\t static data = data2 = "<
}
/*************************************************************************///--------------------------- showcount( ) -------------------------///*************************************************************************/
void check::showcount()
{
cout<<"\n\t static with static is "<
}
int main( )
{
clrscr();
check obj1;
check obj2;
obj1.setvalue(5);
obj1.showdata();
obj2.showdata();
check::showcount();
getch();
return 0;
}
No comments:
Post a Comment