PDA

View Full Version : C++ Computer Scientists brahs... Need help with algorithm design.



Neg_Train
04-20-2014, 05:25 PM
Here's the assignment

http://puu.sh/8gSzJ/540765ddf6.png


My question is.

How would i go about inputting the data?

Since the amount of input is arbitrary. Should i use a while not EOF, in conjunction with an array to store the info?

Or should i overload the operator by using the string class (not sure if that makes sense here )

I took C before but this assignment is like a weird little mindfuk. I dont even know what "tools" to use.

niktak11
04-20-2014, 05:26 PM
Make a fraction struct or class with two member variables, numerator and denominator. Then make a vector of of the class/structs and input the values from the file into it

Neg_Train
04-20-2014, 05:28 PM
I would use a vector

I just started this class like 2 weeks ago. Vectors have not been covered yet.

So far...

it's just basic programming concepts (loops,while, if-else)
operator overloading
Classes..

Thats why im at a mindfuk here. I literally got no tools at my disposal.

Neg_Train
04-20-2014, 05:37 PM
Make a fraction struct or class with two member variables, numerator and denominator. Then make a vector of of the class/structs and input the values from the file into it


#ifndef FRACTION_H //interface of Value class
#define FRACTION_H
#include<iostream>

class Fraction
{
public:
Fraction(int a, int b);







};
#endif



I think I understand you somewhat. But you got me lost at the Vectors part. How would that help for the next operation?
for example:

1/2 + 3/4

Ok two member variables a, b are mapped to 1 and 2.

What next?

niktak11
04-20-2014, 05:48 PM
I'd use a vector since you don't know how many elements there are at compile time. You can also use a dynamic array although a vector would be simpler

As far as the actual algorithm goes, start with creating a function that finds the least common denominator

MiscMathematician
04-20-2014, 06:17 PM
I think I understand you somewhat. But you got me lost at the Vectors part. How would that help for the next operation?
for example:

1/2 + 3/4

Ok two member variables a, b are mapped to 1 and 2.

What next?
Been years since I did this, but here's the gist (google for details):

You need to friend osstream, create the operators >> and << for your class fraction.

cin >> fraction will do this:

cin >> int >> char >> int

Then after defining fraction frac1, frac2 and char sign, you input

cin >> frac1 >> sign >> frac2

Create operations in your class that allow you to add/subtract/whatever fractions, and also an equals operator so that

frac3 = frac2 - frac1

makes sense

Neg_Train
04-20-2014, 06:22 PM
Been years since I did this, but here's the gist (google for details):

You need to friend osstream, create the operators >> and << for your class fraction.

cin >> fraction will do this:

cin >> int >> char >> int

Then after defining fraction frac1, frac2 and char sign, you input

cin >> frac1 >> sign >> frac2

Create operations in your class that allow you to add/subtract/whatever fractions, and also an equals operator so that

frac3 = frac2 - frac1

makes sense

Yes. Thats what the assignment requires.

Interesting.

Man C++ is so fcking different from C.

whiterice77
04-20-2014, 06:34 PM
Here's how I would do it (pseudocode, thinking in terms of C but maybe usefujl):

1. Remove all white space from input (replace ' ' with ''), idk the actual function name.
2. Now you know where everything is, IE char 1 = numerator, char 2 = /, char 3 = denominator, char 4 = sign, etc.

int left_numerator = atoi(input[0]) (replace atoi wtih whatever the function is called that converts string to int)
int left_denominator = atoi(input[2])
int current_answer = left_numerator/left_denominator

input = substring(input(3, length of input)) // cut off the first 3 somehow, might be off by 1 here not sure


while(true){
if(length(input) = 0)
break



int cur_operator = atoi(input[0])
int cur_numerator = atoi(input[1]) (replace atoi wtih whatever the function is called that converts string to int)
int cur_denominator = atoi(input[3])


etc..
then you run something like
if(operator == '+')
current_answer = current_answer + (cur_numerator/cur_denominator)
else if.. etc


after the operation"

input = substr(input,3,end)// whatever gets substring

}

i havent used C++ in a long time so idk if thats too useful

edit: eplace ints with floats lul

Neg_Train
04-20-2014, 07:42 PM
Here's how I would do it (pseudocode, thinking in terms of C but maybe usefujl):

1. Remove all white space from input (replace ' ' with ''), idk the actual function name.
2. Now you know where everything is, IE char 1 = numerator, char 2 = /, char 3 = denominator, char 4 = sign, etc.

int left_numerator = atoi(input[0]) (replace atoi wtih whatever the function is called that converts string to int)
int left_denominator = atoi(input[2])
int current_answer = left_numerator/left_denominator

input = substring(input(3, length of input)) // cut off the first 3 somehow, might be off by 1 here not sure


while(true){
if(length(input) = 0)
break



int cur_operator = atoi(input[0])
int cur_numerator = atoi(input[1]) (replace atoi wtih whatever the function is called that converts string to int)
int cur_denominator = atoi(input[3])


etc..
then you run something like
if(operator == '+')
current_answer = current_answer + (cur_numerator/cur_denominator)
else if.. etc


after the operation"

input = substr(input,3,end)// whatever gets substring

}

i havent used C++ in a long time so idk if thats too useful

edit: eplace ints with floats lul

Thanks that could work, but i have to use classes in this program.

It def works for C though

niktak11
04-20-2014, 07:56 PM
For each operation:
1. find the least common denominator
2. convert each of the operands to have said denominator
3. perform the operation
4. Delete the two operands and the operator and put the result of the operation above in their place
5. Repeat until there's only one element left in the vector (which will be the answer assuming the expression was valid)

MiscMathematician
04-20-2014, 09:31 PM
I booted up my old compiler (really old), and here's something to get you started



#include<iostream.h>

class frac
{
private:
int top; int bot;

public:
frac(int t=0, int b=1)
{
top=t;
bot=b;
}

void set(int a, int b){top=a; bot=b;}

friend ostream& operator<< (ostream &out, frac &myfrac);
friend istream& operator>> (ostream &out, frac &myfrac);

frac operator - (frac &f1);

double getNum() { return top; }
double getDen() { return bot; }

};


ostream& operator<< (ostream &out, frac &myfrac)
{
out << myfrac.top << "/" << myfrac.bot;
return out;
}

istream& operator>> (istream &out, frac &myfrac)
{
int a,b;
char c;
out >> a >> c >> b;
myfrac.set(a,b);
return out;
}

frac frac:: operator - (frac &f1)
{

return frac(top*f1.bot - bot*f1.top, bot*f1.bot);

}

void main()
{
frac a,b;
char opp;
cout << "enter an operation: " << endl;
cin >> a >> opp >>b;

switch(opp)
{
case '-':
cout << endl << a-b;

}

}


This will take any input like "1/3-2/3" or "1 / 3 - 2/ 3" and will output "-3/9".

Notes:

Declaring frac a defaults to a=0/1

I only made the subtraction operator, you should be able to follow the example and do the rest.

You should also make a "set to lowest terms" function after each operation.

You may want to add an equality operator as well.

You should check that an inputted denominator is not zero.

Neg_Train
04-20-2014, 10:15 PM
I booted up my old compiler (really old), and here's something to get you started



#include<iostream.h>

class frac
{
private:
int top; int bot;

public:
frac(int t=0, int b=1)
{
top=t;
bot=b;
}

void set(int a, int b){top=a; bot=b;}

friend ostream& operator<< (ostream &out, frac &myfrac);
friend istream& operator>> (ostream &out, frac &myfrac);

frac operator - (frac &f1);

double getNum() { return top; }
double getDen() { return bot; }

};


ostream& operator<< (ostream &out, frac &myfrac)
{
out << myfrac.top << "/" << myfrac.bot;
return out;
}

istream& operator>> (istream &out, frac &myfrac)
{
int a,b;
char c;
out >> a >> c >> b;
myfrac.set(a,b);
return out;
}

frac frac:: operator - (frac &f1)
{

return frac(top*f1.bot - bot*f1.top, bot*f1.bot);

}

void main()
{
frac a,b;
char opp;
cout << "enter an operation: " << endl;
cin >> a >> opp >>b;

switch(opp)
{
case '-':
cout << endl << a-b;

}

}


This will take any input like "1/3-2/3" or "1 / 3 - 2/ 3" and will output "-3/9".

Notes:

Declaring frac a defaults to a=0/1

I only made the subtraction operator, you should be able to follow the example and do the rest.

You should also make a "set to lowest terms" function after each operation.

You may want to add an equality operator as well.

You should check that an inputted denominator is not zero.

holy fuk strong thanks brah.

Strong genuis