Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Object Oriented ProgrammingRSS Feeds

Program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expres......

Posted By: Easy Tutor     Category: C++ Programming     Views: 2542

Write a program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expression is too large to be represented as a ``normal'' signed integer (type integer if you are working Pascal, type int if you are working in C).

An unspecified number of lines. Each line will contain an integer, one of the two operators + or *, and another integer.

For each line of input, print the input followed by 0-3 lines containing as many of these three messages as are appropriate: ``first number too big'', ``second number too big'', ``result too big''.

Code for Program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expres...... in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-17.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         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<<Data<<endl;

         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"<<endl;
           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"<<endl;
           flag=1;
        }

         else
        operand_2=atoi(Ptr);

         if(flag)
        cout<<"result too big"<<endl;

         else
        {
           long result;

           if(Operator=='+')
              result=operand_1+operand_2;

           elseif(Operator=='*')
              result=operand_1*operand_2;

           if(result>32767)
              cout<<"result too big"<<endl;
        }
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program that reads an expression consisting of two non-negative integer and an operator. Determine if either integer or the result of the expres...... is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!