![]() |
|
|||||||
|
|
Thread Tools | Search this Thread | Rate Thread |
|
#11
|
|||
|
|||
Re: Can help me to solve this problem ??Quote:
bobsam ...erm.... can i ask you some more ? How to do it in while ? And .... this is because instructor dont want to give us any hints ..... ask us do it by ourselves ...... |
|||
|
#12
|
|||
|
|||
Re: Can help me to solve this problem ??Quote:
Code:
Code:
|
|
#13
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationCPP / C++ / C Code:
Can I ask you all, why I can't use +,-,x,/ and = ? I can't do it except I substitute the +-x/= with 1,2,3,4,0 ..... Please help me solve it! The actually is : printfLast edited by admin : 06-Sep-2008 at 14:17.
Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
|
|
#14
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationQuote:
Make the operator a char variable. Note that when you use scanf for a numeric variable, a '\n' char is always left in the system input buffer. You have to get rid of it before you try scanf to read a char variable. Also, since you have gone to so much trouble to get the output like you like it, I recommend that you test input for valid user entry. (I always recommend this. Strongly.) Maybe something like: CPP / C++ / C Code:
Quote:
When posting code: 1. Highlight the code in the edit window. Not your description or other narrative; just the code itself. 2. Click the # icon above the edit window. This puts "code tags" around the code 3. Click the "Preview Post" button before submitting your post. This lets you see if you got it right. Regards, Dave |
|
#15
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationThanks very much !!!
|
|
#16
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationMay i ask one more question ? Erm .. it is about my switch loops .... Can i shorter it ? It is damn long ! Any better way to do it ? I mean may be by using if ..... else , or others . Very sorry because I'm beginner . really thanks a lots ! My results have to be in alphabet .....
|
|
#17
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationQuote:
For example, looking back at your original post: There is no standard library function that can read numbers with commas separating groups of three digits in the integer part of a number. You certainly can't use scanf("%f)" or any other form of standard C library I/O functions. Since you are apparently happy with scanf for inputs, I'll look at the output requirements, as I try to guess what you really need to do. If you do need to change the input stuff, I would break that off as a separate test program and make sure I could meet requirements with just the input. Then I would integrate it into the program along with the calculation and output parts. For now, let's stick with scanf. Writing the value of a numeric quantity in words is an interesting problem, and there are many approaches; some are more "elegant" than others. I think that using arrays of pointers to char is a reasonable way, and I'll show an example. But---if you are a beginner and the course is for beginners, then I'm not sure that is appropriate for your application. I have never seen a problem where the input is a mixed number (integer part and a fractional part expressed in decimal) where the integer part was given with comma-separated groups of three digits. I mean 10,000.33? Really? That brings up the most important point from my perspective: Exactly what was the assignment? By that, I mean: What range of input and output numbers is the program expected to handle? What is the required precision (number of correct significant decimal digits or number of decimal places in the output)? If you are going to use floats to hold internal values, the total precision of any variable will be limited to something like seven significant decimal digits. Furthermore, since internal representations are based on binary floating point numbers, many perfectly fine, exact decimal numbers can't be represented exactly inside the machine. So, for example, 1.0078125 can be represented exactly as the value of a floating point variable, but 10000.1 can not. The result: If the answer is exactly 1.0078125, you may be able to print its value exactly. If the result should be 10000.1, and you just spit out stuff calculated from the machine digits, the decimal digits might come out to be something like 10000.0996094... OK, enough of that. Let's get to the easy part: the integer values and how to print the names of the numbers. Quote:
About switch statements versus if-else stuff: It's a matter of style, not substance. Your original question is more important: If you are going to print integer values from 1 to 1000, do you need 1000 cases (or, equivalently, 1000 if() conditions)? Here's what I like to do: Separate the tasks of the program. I can test the output part without having to supply inputs and let the program do the calculations each time. So: Let's just concentrate on integer outputs for now. Furthermore, lets just handle integer values from zero to nine. With a case statement: CPP / C++ / C Code:
I suggest that you compile and run something like this. Make absolutely sure that it works for all valid inputs (it bails out if you give it anything other than a decimal number from zero to nine). If you want to change stuff, then do it. If you want to start all over with our own nomenclature, then do it. But, make sure you understand it and test it. With if-else stuff CPP / C++ / C Code:
(Same comments as the previous example.) Now, what's the difference between the programs? Style, not substance. On simple things like this, I can't see where it makes a heckofa lot of difference. I think it's important to be familiar with both forms, since sometimes one might be more obvious (and easier to debug or to "verify by inspection"). Now, here's how I might do it so that I don't have all of those cases or all of those if() thingies: Use an array or pointers to char. Initialize the array to point to literal strings with the names of the numbers: With an array of pointers to literal strings consisting of the names of the numbers: CPP / C++ / C Code:
Which is "best"? Take your choice. Now, to the bigger picture, what about 11, 12, 13, ..., 19? I think that the most straightforward way is just to make all of these special cases. So, it would require 20 case statement labels, 19 if() conditions, or a simple augmentation of the name array. I'll show the latter: CPP / C++ / C Code:
OK so far? Note how careful I am to make sure that I feed legitimate values to the array index. That's really, really important. How about 20-99? Now, from this point, the nomenclature becomes regular (in English, at least). I am assuming that you will always print 87 as "eighty-seven," and not "four score and seven." Stuff like that. CPP / C++ / C Code:
Note that I have used the convention that I consider "standard." Namely, that for numbers greater than 20, if the units digit is not zero, there is a hyphen. So, for example, 37 is printed out as "thirty-seven" Now, you can go on from here with hundreds, thousands, etc. You don't need any new arrays and the logic is not very difficult. For numbers greater than 20, Just extract digits and index into an array. A little more head scratching should lead to the ability to print numbers with "hundred", "thousand," etc. You don't need any more arrays. The rest is just simple arithmetic and simple logic. (I know, I know: That's easy for me to say. See Footnote.) I would probably put the actual number-to-name stuff in one or more functions that would each write to a string rather than printing out directly, but, again, that's a matter of style, rather than substance. I will say, that as programs get larger than a few tens of lines style becomes extremely important. (Yours has more than a thousand lines, and I don't think your output stuff actually works very well, and it isn't very extensible, I'm thinking.) I hate to repeat myself, but I can't believe that this is, seriously, a real "beginner's" assignment. (We haven't even got to the floating point stuff yet.) What, exactly, is the course? Are there any prerequisites? Regards, Dave Footnote: "No one was born knowing this stuff, you know." ---davekw7x |
|
#18
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationSeriously , I'm really a beginner
![]() However , i'm really appreciate . Thank you so much ! I gain so much in this forum ! |
|
#19
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationQuote:
There's nothing wrong with being a non-beginner, and there's nothing wrong with non-beginners asking questions. If an instructor gives a class of beginners an assignment that, in my opinion, is not a "typical" beginner's assignment, then a couple of things come to mind: 1. My opinion of "typical" is completely irrelevant. 2. The instructor wants people to learn to dig things out that are not covered explicitly in materials for that class or the various prerequisite courses. 3. All of the above. Regards, Dave As I re-read your previous posts, I am struck by your statement to the effect that, "instructor doesn't want to give us any hints...ask us do it by ourselves .." I assume that implies permission to get help from strangers on the internet rather than asking the instructor directly. Or were you really expected to isolate yourself, totally insulated from outside resources, and "think" of a solution? If that's the case, please ignore all my previous posts. In other words: "I object!" ---Hamilton Burger on about a million episodes (maybe more) of Perry Mason "The jury will disregard that question and answer..." ---Judges on about a million episodes (maybe more) of Perry Mason |
|
#20
|
|||
|
|||
Re: Write a calculator program that lets users enter a simple mathematic operationThanks a lot ! you help me a lots ! Now i complete my assignment already ! Thansk !
|
Recent GIDBlog
Once again, no time for hobbies by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| An easy problem solve for most of you | slapshot6819 | C++ Forum | 2 | 20-Nov-2007 03:14 |
| Please help me to solve C++ problem | fahad | C++ Forum | 1 | 01-Nov-2007 05:43 |
| Problem Description: Graph’s Degree.....can help me to solve this out? | thefinalyy | C++ Forum | 0 | 24-Aug-2007 06:42 |
| I have a problem to solve summation in c++ | logieen | C++ Forum | 4 | 22-May-2007 08:25 |
| A problem using streams. (I've spent a month trying to solve it) | Mararia | C++ Forum | 8 | 14-Dec-2006 17:52 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The