i have project in which i have to get user input for example (3/4 - 5/7)
and than extract the numbers from that line and store it into four integers a,b,c,d how would i do that?
If it's a predictable format then you could read each character and use the ASCII table to convert each relevant char into an integer. That is, each char can be interpreted as an int, and then you would have to offset the value by the starting point of integers in the ASCII table. It's a common problem, so you should be able to find it all over the place on the web.
Hope that helps. Post your code attempt if you get stuck again.
well what i have to do is perform the fraction addition and substraction. but without using / or * and it cant not be float or double. and could u plz give me example of converting thing u r talking about?
Here's a simple program that prints the ASCII integer corresponding to character '1', for example:
1 2 3 4 5 6 7 8 9
#include <iostream>
usingnamespace std;
int main ()
{
char oneChar = '1';
int asciiIntOfOneChar = (int)oneChar;
cout << oneChar << " as ASCII int is " << asciiIntOfOneChar;
}
I assume you know how to read characters using 'cin'. This should be enough to get you started...
With respect to doing the subtraction without using * and /, you'll have to do a bit of maths:
(a/b) - (c/d) can be simplified to (ad-bc)/(bd) after finding a common denominator. To do the individual multiplications, you could create a function called 'Multiply' which takes 2 ints and returns an int. This function would have a for loop which would perform multiplication by using addition iteratively.
yes i have already come up with (ad-bc)/(bd) thing. when when user inputs the (3/4 - 5/6) how can i assing 3 to the int a, 4 to the int b, 5 to the int c and,6 to the int d.
int a = (int)expression[1] - 48;
int b = (int)expression[3] - 48;
// ...
[\code]
This is a bit messy, but it works. The '48' is the subtraction of the ASCII offset. I'm assuming that the input format will always be the same here. Perhaps someone can offer an improvement on this?
another quick question. now u put the ascii code for substraction that will only works if user puts subtraction. how can i put something that can take care of substraction, addition, multiplication and division?
I don't think you're understanding the idea of subtracting 48. Let's take it step by step. For the line: int a = (int)expression[1] - 48;
1. First we get expression[1], which is a char '3'.
2. We convert '3' to an integer, which gives 51 (look it up in the ASCII table).
3. We subtract the standard offset of 48, which gives us the 3 that we want.
We can follow steps 1-3 to get the numbers for the other numerator and denominators.
If the user enters (3/4 + 5/6), then a, b, c and d will still be read exactly. What you have to change in this case is that you're adding the fractions, i.e. (ad + bc) / bd
k i haven't been very clear. let me show u what i got so far.
first i have all the functions to perform addition, sub, div and multiplication.
second. in main () code i prompt user to put it any expression.
what im planning on doing is stick the numbers into int that i declare and store the sign into saperate variable and than write if else statement that will call the right fucntion depending on if user puts + - / or * in their input.
so the place im stuck at is sticking user input into variables.
char expression[15];
cout << "Enter expression: ";
cin >> expression;
int a = (int)expression[1] - 48;
int b = (int)expression[3] - 48;
int c = (int)expression[5] - 48;
int d = (int)expression[7] - 48;
That should work for input of the format "(a/b-c/d)", where a, b, c and d are POSITIVE single-digit integers. You can use 'expression[4]' to get the sign, and use a switch statement to handle the various cases of +, -, * and /.
Ok if they're entering a space then the above won't work. Use a string instead, and std::getline
1 2 3 4 5 6 7 8
string expression;
cout << "Enter expression: ";
std::getline(cin, expression);
int a = (int)expression[1] - 48;
int b = (int)expression[3] - 48;
int c = (int)expression[7] - 48;
int d = (int)expression[9] - 48;