944,001 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3113
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Sep 1st, 2007
-1

Really need help with the interactive Calculator program, or will be dead

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstring>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. const int arraysize=30; //an array of could hold up to 30 characters
  8. typedef char string; //defind string as char data type
  9. string num1[arraysize]; //array to store first set of numbers
  10. string sign[arraysize]; //array to store the operation sign
  11. string num2[arraysize]; //array to store second set of numbers
  12. string num3[arraysize]; //array to store the answer
  13.  
  14. cin>>num1>>sign>>num2;
  15. num3=int(num1-'2')+int(num2-'1');
  16.  
  17. cout<<num1<<endl<<sign<<num2<<endl<<--------<<num3<<endl;
  18.  
  19.  
  20.  
  21. return 0;
  22.  
  23. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tendyhk is offline Offline
2 posts
since Sep 2007
Sep 1st, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

I think you need to plan how to do this on paper to get ideas.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Sep 2nd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Sep 2nd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

> 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
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 2nd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

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...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Sep 3rd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

Dare I ask about NOLAE???
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Sep 3rd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

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

hope i explained myself clearly...
Featured Poster
Reputation Points: 424
Solved Threads: 57
Posting Virtuoso
Nichito is offline Offline
1,594 posts
since Mar 2007
Sep 3rd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

pseudocode?
Reputation Points: 9
Solved Threads: 7
Junior Poster
ft3ssgeek is offline Offline
124 posts
since Mar 2007
Sep 3rd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

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

C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <cstring>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. {
  9. const int arraysize=30; //an array of could hold up to 30 characters
  10. typedef char string; //defind string as char data type
  11. string num1[arraysize]; //array to store first set of numbers
  12. string sign[arraysize]; //array to store the operation sign
  13. string num2[arraysize]; //array to store second set of numbers
  14. string num3[arraysize]; //array to store the answer
  15.  
  16. char null='\0';
  17. int num1size=0;
  18. int num2size=0;
  19.  
  20.  
  21.  
  22. char mul='*'; //This use to compare the sign store in the sign array
  23.  
  24. cin>>num1>>sign>>num2;
  25.  
  26. if(sign[0]==mul) //compare sign arry, if sign is multiplication then program terminate
  27. {
  28. cout<<"The Program can not yet handle multiplication"<<endl;
  29. return 0;
  30. }
  31. else //addition begin to run if sign equal to +
  32. {
  33.  
  34. while(null!=num1[num1size]) //calculate the size of arry num1
  35. {
  36. num1size++;
  37. }
  38. num1size--;
  39. while(null!=num2[num2size]) //calculate the size of array num2
  40. {
  41. num2size++;
  42. }
  43. num2size--;
  44.  
  45.  
  46. int answer=int(num1[num1size]-'0')+int(num2[num2size]-'0');
  47. itoa(answer,num3,num1size);
  48.  
  49.  
  50. cout<<num1[num1size]
  51. <<endl
  52. <<strlen(num1)
  53. <<answer
  54. <<endl
  55. <<"--------"
  56. <<endl;
  57.  
  58. }
  59.  
  60. return 0;
  61.  
  62. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
tendyhk is offline Offline
2 posts
since Sep 2007
Sep 3rd, 2007
0

Re: Really need help with the interactive Calculator program, or will be dead

> I dont know how to convert int back to char
http://faq.cprogramming.com/cgi-bin/...&id=1043284385

[edit] Don't use itoa ... it is wrong [/edit]
Last edited by iamthwee; Sep 3rd, 2007 at 4:20 pm.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: compiler errors
Next Thread in C++ Forum Timeline: Why do I get the following printf() result?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC