Array output empty
close being done this, output section coming blank.
the output (16 times) looks if input 1 1/3 4 fractions:
first fraction: second fraction: adding result: 1 multiplying result: 1
i'm not getting fractions input translate output, i'm not sure gap/mistake is
using system; using system.collections.generic; using system.linq; using system.text; public class fraction { public int wholenumber { get; set; } public int numerator { get; set; } public int denominator { get; set; } private void reduce()//reduces fraction lowest { if (numerator < denominator) { wholenumber = 0; return; } if (numerator == denominator) { wholenumber = 1; numerator = 0; return; } if (numerator > denominator) { wholenumber = (int)math.floor((decimal)numerator / denominator); numerator = numerator - wholenumber * denominator; } } //three parameters public fraction(int wholenumber, int numerator, int denominator) { wholenumber = wholenumber; numerator = wholenumber * denominator + numerator; reduce(); } //two parameters public fraction(int numerator, int denominator) { wholenumber = 0; numerator = numerator; denominator = denominator; reduce(); } //set values public fraction() { wholenumber = 0; numerator = 0; denominator = 1; } //addition operation public static fraction operator +(fraction firstfraction, fraction secondfraction) { if (firstfraction.denominator == secondfraction.denominator) { int firstproduct = (firstfraction.wholenumber * firstfraction.denominator) + firstfraction.numerator; int secondproduct = (secondfraction.wholenumber * secondfraction.denominator) + secondfraction.numerator; return (new fraction(firstproduct + secondproduct, firstfraction.denominator)); } else { int commondenominator = firstfraction.denominator * secondfraction.denominator; int firstproduct = ((firstfraction.wholenumber * firstfraction.denominator) + firstfraction.numerator) * secondfraction.denominator; int secondproduct = ((secondfraction.wholenumber * secondfraction.denominator) + secondfraction.numerator) * firstfraction.denominator; return (new fraction(firstproduct + secondproduct, commondenominator)); } } //multiplication operation public static fraction operator *(fraction firstfraction, fraction secondfraction) { //remove whole number part firstfraction.numerator = (firstfraction.wholenumber * firstfraction.denominator) + firstfraction.numerator; firstfraction.wholenumber = 0; secondfraction.numerator = (secondfraction.wholenumber * secondfraction.denominator) + secondfraction.numerator; secondfraction.wholenumber = 0; int newnumerator = (firstfraction.numerator * secondfraction.numerator); int newdenominator = (firstfraction.denominator * secondfraction.denominator); return (new fraction(newnumerator, newdenominator)); } } //second section of code class program { static void main(string[] args) { fraction[] fractionarray = new fraction[4]; int wholenumber, numerator, denominator; (int = 0; < 4; i++) //asks user input fractions, 4 total { console.write("enter whole number: "); wholenumber = int32.parse (console.readline()); console.write("enter numerator: "); numerator = int32.parse(console.readline()); console.write("enter denominator: "); denominator = int32.parse(console.readline()); console.writeline("---"); fractionarray[i] = new fraction(wholenumber, numerator, denominator ); } //add, multiply 0-3, total output 16, connected array output section jointopsarray(fractionarray[0], fractionarray[0]); jointopsarray(fractionarray[0], fractionarray[1]); jointopsarray(fractionarray[0], fractionarray[2]); jointopsarray(fractionarray[0], fractionarray[3]); jointopsarray(fractionarray[1], fractionarray[0]); jointopsarray(fractionarray[1], fractionarray[1]); jointopsarray(fractionarray[1], fractionarray[2]); jointopsarray(fractionarray[1], fractionarray[3]); jointopsarray(fractionarray[2], fractionarray[0]); jointopsarray(fractionarray[2], fractionarray[1]); jointopsarray(fractionarray[2], fractionarray[2]); jointopsarray(fractionarray[2], fractionarray[3]); jointopsarray(fractionarray[3], fractionarray[0]); jointopsarray(fractionarray[3], fractionarray[1]); jointopsarray(fractionarray[3], fractionarray[2]); jointopsarray(fractionarray[3], fractionarray[3]); console.readline(); } //array output section static void jointopsarray(fraction fractionone, fraction fractiontwo) { console.writeline("----------------------"); console.writeline("first fraction: ", fractionone); console.writeline("second fraction: ", fractiontwo); //adding conditions , loop fraction add = new fraction(); add = fractionone + fractiontwo; bool condition1 = (add.numerator > 0) && (add.wholenumber > 0); bool condition2 = (add.numerator == 0); bool condition3 = (add.wholenumber == 0); if (condition1) { console.writeline("adding result: {0} {1}/{2}", add.wholenumber, add.numerator, add.denominator); } else if (condition2) { console.writeline("adding result: {0} ", add.wholenumber, add.numerator, add.denominator); } else if (condition3) { console.writeline("adding result: {1}/{2}", add.wholenumber, add.numerator, add.denominator); } //multiply conditions , loop fraction multiply = new fraction(); multiply = fractionone * fractiontwo; bool condition4 = (multiply.numerator > 0) && (multiply.wholenumber > 0); bool condition5 = (multiply.numerator == 0); bool condition6 = (multiply.wholenumber == 0); if (condition4) { console.writeline("multiplying result: {0} {1}/{2}", multiply.wholenumber, multiply.numerator, multiply.denominator); } else if (condition5) { console.writeline("multiplying result: {0} ", multiply.wholenumber, multiply.numerator, multiply.denominator); } else if (condition6) { console.writeline("multiplying result: {1}/{2}", multiply.wholenumber, multiply.numerator, multiply.denominator); } } }
still missing first fraction , second fraction. adding , multiplication results correct, need show fractions being used.
basically want like:
console.writeline("first fraction: {0} {1}/{2} ", fractionone.wholenumber, fractionone.numerator, fractionone.denominator);
however, depending on how have stored values in class members may
have parsing such as:
console.writeline("first fraction: {0} {1}/{2} ", fractionone.numerator / fractionone.denominator, fractionone.numerator % fractionone.denominator, fractionone.denominator);
- wayne
Visual Studio Languages , .NET Framework > Visual C#
Comments
Post a Comment