How do I simulate a program with a \frac{}{} command

This is a discussion on How do I simulate a program with a \frac{}{} command within the C++ Programming forums, part of the General Programming Boards category; Code: I'm doing an excerise to make factions, but I need to simulate a \frac{}{} command. To display numbers in ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    45

    How do I simulate a program with a \frac{}{} command

    Code:
    I'm doing an excerise  to make factions, but I need to simulate a \frac{}{}
    command. To display numbers in faction and decimal form. How do I simulate a 
    \frac{}{} command? Thanks.
    Last edited by xbusterx; 09-28-2008 at 04:02 PM.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    What is a \frac{} command?

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    It's latex-speak.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You don't, at a console. If you're trying for "nice" output like on paper, just write a LaTeX file. If you need to fake it on a screen, print out "2 3/8" or something.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by robwhit View Post
    Code:
    What is a \frac{} command?
    I need to simulate a \frac{}{} command.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    You don't, at a console. If you're trying for "nice" output like on paper, just write a LaTeX file. If you need to fake it on a screen, print out "2 3/8" or something.
    I don't think you get what I mean like:

    Code:
                   
    \frac{1}{4} is -     1/4
    
                                    or 
     
                                 0.25

  7. #7
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    then do 1/4 in your program.
    Code:
    #include <iostream>
    
    int main(){
      float frac = 1/4;
      std::cout<< frac;
      return 0;
    }

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xbusterx View Post
    I don't think you get what I mean like:

    Code:
                   
    \frac{1}{4} is -     1/4
    
                                    or 
     
                                 0.25
    So just print it out. I don't know what kind of spacing you're going for over there on the right, so you'll have to fiddle, but:
    Code:
    cout << "\\frac{1}{4} is - \t 1/4" << endl << "\t\t\t or" << endl << "\t\t\t 0.25" << endl;

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by tabstop View Post
    So just print it out. I don't know what kind of spacing you're going for over there on the right, so you'll have to fiddle, but:
    Code:
    cout << "\\frac{1}{4} is - \t 1/4" << endl << "\t\t\t or" << endl << "\t\t\t 0.25" << endl;


    lol That wouldn't work though I'm trying to prompt the user not print it obivously I know how to print it all your doing is making stings that's not what I'm trying to do.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by xbusterx View Post
    lol That wouldn't work though I'm trying to prompt the user not print it obivously I know how to print it all your doing is making stings that's not what I'm trying to do.
    And where did "prompting the user" come from? If that's what you wanted to do, why didn't you say so the first (or second, or third) time?

    What are you stuck on about trying to get input?

  11. #11
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    then explain what you are trying to do?

    do you mean you want to parse it?

    Code:
    int denominator, numerator;
    scanf("\\frac{&#37;d}{%d}", &numerator, &denominator);
    cout << numerator << " / " << denominator;

  12. #12
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Quote Originally Posted by cyberfish View Post
    then explain what you are trying to do?

    do you mean you want to parse it?

    Code:
    int denominator, numerator;
    scanf("\\frac{%d}{%d}", &numerator, &denominator);
    cout << numerator << " / " << denominator;
    ok did that but how would I make into decimal form?

  13. #13
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,145
    the output?

    the scanf line reads the values into denominator and numerator.

  14. #14
    Registered User
    Join Date
    Apr 2006
    Posts
    2,011
    Quote Originally Posted by xbusterx View Post
    ok did that but how would I make into decimal form?
    Code:
    cout << double(numerator)/double(denominator);
    You have to convert it to double to get it to be treated as a decimal.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  15. #15
    Registered User
    Join Date
    Sep 2008
    Posts
    45
    Here is what the excerise says

    Code:
     One such command displays fractions. For example 5/4 is entered
    "\frac{5}{4}". Write a computer program that simulates a \frac{}{}
    command. The program reads two integers n1 and n2  
    
    
      EXAMPLE 1:
    Enter fraction in the form n1/n2: 5/4
     .....                  5
    \frac{5}{4} is         --
    ......                  4
    
    ....               1
                 or 1  --
    ....               4
    
                 or 1.25

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Plan
    By Programmer_P in forum C++ Programming
    Replies: 0
    Last Post: 05-11-2009, 01:42 AM
  2. Chat Program Help?
    By Paul22000 in forum Networking/Device Communication
    Replies: 9
    Last Post: 02-09-2009, 11:35 PM
  3. help
    By hellBOY in forum C Programming
    Replies: 4
    Last Post: 01-13-2009, 10:59 AM
  4. unix `ls' command in C program
    By Moony in forum C Programming
    Replies: 3
    Last Post: 06-28-2006, 10:00 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21