got my first HW on the first day of class, have no idea of what to do, please help me!!!!
The HW question is this

write a program that will act as an interactive calculator capable of handling very large (larger than the largest long integer) nonnegative integers. this calculator need perform only the operations of addidtion and multiplication

in this program, each input line is of the form

num1 op num2

and should produce output such as

num1
op num2
-------------
num3

where num1 and num2 are(possibly very large) nonnegative integers, op is the single character + or *, and num3 is the integer that result fromt he desired calculation.

Program 1

Write the interactive calculator described in Programming Problem 5 on page 64 of the text.

· Do not use classes. Do use an appropriate typedef for your long numbers.

· Do use fixed-length arrays to store the digits of the long numbers the program manipulates.

· Ignore the optional part of the problem.

· Assume there is at least one space between each number and the operator symbol. Of course there will not be spaces between the digits in a number (i.e. 123, not 1 2 3).

· Do not implement multiplication. If the operator symbol is *, have the output indicate that multiplication is not yet implemented.

· Be sure to follow the guidelines given in the programming standards document on this course website.


Just to be clear, input to the program should look like this:

000655123897667676422 + 21346615433431

or like this:

564557655123897667676422 * 77621346615433431

Of course, in the second case, the program just says that it can't yet handle multiplication.


what I had though is to use 4 array, 3 that hold the numbers, and one that hold the sign, but I dont know how to write an array plus another array and save it to another array. plese help, here's the code that I had try

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	const int arraysize=30;	//an array of could hold up to 30 characters
	typedef char string;	//defind string as char data type
	string num1[arraysize]; //array to store first set of numbers
	string sign[arraysize];	//array to store the operation sign
	string num2[arraysize];	//array to store second set of numbers
	string num3[arraysize];	//array to store the answer

	cin>>num1>>sign>>num2;
	num3=int(num1-'2')+int(num2-'1');

	cout<<num1<<endl<<sign<<num2<<endl<<--------<<num3<<endl;



	return 0;

}

Recommended Answers

All 13 Replies

Member Avatar for iamthwee

I think you need to plan how to do this on paper to get ideas.

don't expect any problem-solving from us if you dont try it first... and actualli iamthwee is right... you should plan how you're gonna work it before you code anything... if not it's just blindfolded bat swinging...

> got my first HW on the first day of class
> Write the interactive calculator described in Programming Problem 5 on page 64 of the text.
I find these statements to be inconsistent.

How does this work then?
It's the first day of the advanced class, but you haven't bothered with the beginners class. As such, you're already way out of your depth.

It's the 5th day of the class (hence problem 5), but it's the first one you could be bothered to turn up for.

If "or will be dead" means you'll be kicked off the course, then that is perhaps a good thing. It will allow you to find your true path, and everyone else on the course will benefit from the extra attention the tutor can give, which would otherwise be wasted on you.

The problem is simple enough, but I wouldn't say that it's in the "first day of the beginners class" range of problems.

> 000655123897667676422 + 21346615433431
Before you write the code, do you know how to do this on paper?

> Be sure to follow the guidelines given in the programming standards document on this course website.
How are we supposed to make it look like your own work, when we don' t even know what these are :icon_rolleyes:

there's this very interesting language called NOLAE... if you can write a program in that code, you certainly can do it in any programming language...

also you should be able to draw flow charts...

Dare I ask about NOLAE??? :D

i don't know if that's what its called in english... (should've made sure first... :D), but its the language base for every other languages, where commands are written in plain english... i.e:

[B]integer [/B]num;
[B]read [/B]num;
num=>num*num;
[B]write [/B]num;

hope i explained myself clearly...

pseudocode?

here is my updated code, I dont know how to convert int back to char and store in the string, can any one help me? and how to do the carry in the array? please help me

#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;


int main()
{
	const int arraysize=30;	//an array of could hold up to 30 characters
	typedef char string;	//defind string as char data type
	string num1[arraysize]; //array to store first set of numbers
	string sign[arraysize];	//array to store the operation sign
	string num2[arraysize];	//array to store second set of numbers
	string num3[arraysize];	//array to store the answer

	char null='\0';
	int num1size=0;
	int num2size=0;



	char mul='*';			//This use to compare the sign store in the sign array

	cin>>num1>>sign>>num2;
	
	if(sign[0]==mul)		//compare sign arry, if sign is multiplication then program terminate
	{
		cout<<"The Program can not yet handle multiplication"<<endl;
		return 0;
	}
	else					//addition begin to run if sign equal to +
	{

		while(null!=num1[num1size])	//calculate the size of arry num1
		{
			num1size++;
		}
		num1size--;
		while(null!=num2[num2size])	//calculate the size of array num2
		{
			num2size++;
		}
		num2size--;


		int answer=int(num1[num1size]-'0')+int(num2[num2size]-'0');
		itoa(answer,num3,num1size);


	cout<<num1[num1size]
		<<endl
		<<strlen(num1)
		<<answer
		<<endl
		<<"--------"
		<<endl;

	}

	return 0;

}
#include<windows.h>
#include<sstream>
#include<string>
using namespace std;

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
char szClassName[ ] = "calculator";

int WINAPI WinMain (HINSTANCE hThisInstance,
                    HINSTANCE hPrevInstance,
                    LPSTR lpszArgument,
                    int nFunsterStil)

{
    HWND hwnd;               /* This is the handle for our window */
    MSG messages;            /* Here messages to the application are saved */
    WNDCLASSEX wincl;        /* Data structure for the windowclass */

    /* The Window structure */
    wincl.hInstance = hThisInstance;
    wincl.lpszClassName = szClassName;
    wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
    wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
    wincl.cbSize = sizeof (WNDCLASSEX);

    /* Use default icon and mouse-pointer */
    wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
    wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
    wincl.lpszMenuName = NULL;                 /* No menu */
    wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
    wincl.cbWndExtra = 0;                      /* structure or the window instance */
    /* Use Windows's default color as the background of the window */
    wincl.hbrBackground = (HBRUSH)(COLOR_BACKGROUND);

    /* Register the window class, and if it fails quit the program */
    if (!RegisterClassEx (&wincl))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           "calculator",       /* Title Text */
           WS_OVERLAPPEDWINDOW, /* default window */
           CW_USEDEFAULT,       /* Windows decides the position */
           CW_USEDEFAULT,       /* where the window ends up on the screen */
           257,                 /* The programs width */
           282,                 /* and height in pixels */
           HWND_DESKTOP,        /* The window is a child-window to desktop */
           NULL,                /* No menu */
           hThisInstance,       /* Program Instance handler */
           NULL                 /* No Window Creation data */
           );

    /* Make the window visible on the screen */
    ShowWindow (hwnd, nFunsterStil);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* Translate virtual-key messages into character messages */
        TranslateMessage(&messages);
        /* Send message to WindowProcedure */
        DispatchMessage(&messages);
    }

    /* The program return-value is 0 - The value that PostQuitMessage() gave */
    return messages.wParam;
}


class Calculator
{
 public:    
               
        //Constructor
        Calculator();
        //WinProc Functions
        void command(WPARAM, LPARAM);        
        void create(HWND);
        int  destroy();
        //Other
        void perform_previous_op(); 
        void clear();
        void reset();
 
 private:         
        
        long double ldSubtotal, 
                    ldTotal;
             string strSubtotal;                
             bool   bDecimal, 
                    bClear; 
             char   prev_op;        
             HWND   hNumbers[10], 
                    hOperators[4], 
                    hDecimal, 
                    hClear, 
                    hStaticWindow, 
                    hEquals;     
        
};

enum keypad
{
     ID_Button_0,
     ID_Button_1, ID_Button_2, ID_Button_3,
     ID_Button_4, ID_Button_5, ID_Button_6,
     ID_Button_7, ID_Button_8, ID_Button_9, 
     ID_Decimal,  ID_Button_EQU,     
     ID_Button_ADD,   ID_Button_SUB, 
     ID_Button_MULT,  ID_Button_DIV,
     ID_Button_CLEAR, ID_Static 
};

/*  This function is called by the Windows function DispatchMessage()  */

LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{   
   
   static Calculator MyCalc;
               
    switch (message)                  /* handle the messages */
    {        
        case WM_CREATE:   MyCalc.create(hwnd);               break;   
        case WM_COMMAND:  MyCalc.command(wParam, lParam);    break;
        case WM_DESTROY:  MyCalc.destroy();                  break;        
            
        default:                      /* for messages that we don't deal with */
            return DefWindowProc (hwnd, message, wParam, lParam);
    }

    return 0;
}




/////////////////////////////////////////
///////// Function Definitions //////////
/////////////////////////////////////////

Calculator::Calculator()
{
    reset();
}

void Calculator::create(HWND hwnd)
{

   int index = 1;
   TCHAR *numbers[]   = {"1","2","3","4","5","6","7","8","9"};
   
   for(int y=150; y>49; y-=50)
   for(int x=0;  x<101; x+=50)    
   {                 
      hNumbers[index] = CreateWindow("BUTTON", numbers[index-1], 
                            WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                            x, y, 50, 50, hwnd, (HMENU)index,
                            (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);  
                            index++;                    
   }
   
   index = 0;
   TCHAR *operators[] = {"+","-","*","/"};
   
   int x  = 150, y = 0;
   int ID = ID_Button_ADD;      
   
   for(int i=0; i<4; i++)
   {
      hOperators[i] = CreateWindow("BUTTON", operators[i], 
                          WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                          x, y+=50, 50, 50, hwnd, (HMENU)ID++,
                          (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
   }
                          
           
   
   hNumbers[0]   = CreateWindow("BUTTON", "0", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                           0, 200, 100, 50, hwnd, (HMENU)ID_Button_0,
                           (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL); 
                          
   hDecimal      = CreateWindow("BUTTON", ".", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                           100, 200, 50, 50, hwnd, (HMENU)ID_Decimal,
                           (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
                          
   hClear        = CreateWindow("BUTTON", "Clear", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                           200, 50, 50, 100, hwnd, (HMENU)ID_Button_CLEAR,
                           (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);  
                          
   hStaticWindow = CreateWindow("STATIC", NULL , WS_CHILD|WS_VISIBLE|WS_BORDER|ES_RIGHT,
                           5, 10, 188, 20, hwnd, (HMENU)ID_Static, 
                           (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);
   
   hEquals       = CreateWindow("BUTTON", "=", WS_CHILD|WS_VISIBLE|BS_PUSHBUTTON,
                           200, 150, 50, 100, hwnd, (HMENU)ID_Button_EQU,
                           (HINSTANCE)GetWindowLong(hwnd, GWL_HINSTANCE), NULL);  
}


void Calculator::command(WPARAM wParam, LPARAM lParam)
{   
    
    WORD msg = LOWORD(wParam);
    TCHAR *numbers[]   = {"0","1","2","3","4","5","6","7","8","9","."};
     
     if(HIWORD(wParam) == BN_CLICKED)
     {     
        //Numbers 1 thru 9
        if(msg >= ID_Button_1 && msg <= ID_Button_9)
        {    
             if(bClear)
             
                clear();
             
             else if(prev_op == '=')
             
                reset();             
                           
             strSubtotal += numbers[msg];
             SetWindowText(hStaticWindow, strSubtotal.c_str());      
        }
        
        //Number 0 (special case to eliminate leading zeros)
        else if(msg == ID_Button_0 && strSubtotal.size() && !bClear)
        {                           
             strSubtotal += "0";
             SetWindowText(hStaticWindow, strSubtotal.c_str());
        }
        
        //Decimal (special case to eliminate multiple decimals)
        else if(msg == ID_Decimal && !bDecimal)
        {
             if(bClear)
             
                clear();             
             
             bDecimal = true;
             strSubtotal += ".";
             SetWindowText(hStaticWindow, strSubtotal.c_str());
        } 
        
        //Operators
        else if(msg >= ID_Button_EQU && msg <= ID_Button_DIV)
        {
             bClear   = true;
             bDecimal = false;             

             if(prev_op)
                                                  
                perform_previous_op();                               
               
             istringstream atol(strSubtotal);
             atol >> ldTotal;                                  
                 
             switch(msg)
             {
                case ID_Button_EQU:  prev_op = '=';   break;
                case ID_Button_ADD:  prev_op = '+';   break;
                case ID_Button_SUB:  prev_op = '-';   break;
                case ID_Button_MULT: prev_op = '*';   break;
                case ID_Button_DIV:  prev_op = '/';   break;
             }
                           
             SetWindowText(hStaticWindow, strSubtotal.c_str());
        }
        
        //Equals
        else if(msg == ID_Button_EQU)
        {
             if(prev_op)
             
                perform_previous_op();
                
             prev_op = 0;
             bClear  = true;                          
             SetWindowText(hStaticWindow, strSubtotal.c_str());
        }             
        
        //CLEAR
        else if(msg == ID_Button_CLEAR)
        {
             reset();
             SetWindowText(hStaticWindow, strSubtotal.c_str());
        }     
        
    }
}    
          

void Calculator::perform_previous_op()
{    
   istringstream atol(strSubtotal);
   atol >> ldSubtotal;
   
   switch(prev_op)
   {
      case '+':  ldTotal += ldSubtotal;   break;
      case '-':  ldTotal -= ldSubtotal;   break;
      case '*':  ldTotal *= ldSubtotal;   break;
      case '/':  ldTotal /= ldSubtotal;   break;   
   }
   
   ostringstream ltoa;
   ltoa << ldTotal;
   strSubtotal = ltoa.str();     
}


void Calculator::clear()
{
     bClear = false;
     strSubtotal.clear();
     SetWindowText(hStaticWindow, strSubtotal.c_str());
}


void Calculator::reset()
{
     prev_op    = 0;
     ldSubtotal = 0.0;
     ldTotal    = 0.0;
     bClear     = false;
     bDecimal   = false;            
     strSubtotal.clear();
}                           

  
int Calculator::destroy() 
{
     PostQuitMessage(0);
     return 0;
}

pseudocode?

yup... something like that... it's basically describing a flow chart in plain english (meaning with no drawings)...

One thing I'd like to mention about the psuedocode comment - its unusual to specify data types in pseudocode - normally, details like that are left anonymous, concentrating on the high-level flow of the program

(The reason being that data types are an irrelevent detail from an algorithm point of view)

for rookies, pseudocode may be a great way to organize ideas... in this case it is great, because they organize their ideas in plain english, so they just have to substitute their pseudocode for the real commands...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.