Question

How can I make a program in c++ that lets the user create a polynomial by...

How can I make a program in c++ that lets the user create a polynomial by letting him add values when he chooses the option in a menu. Another option in the menu should be to print the polynomial in order. Meaning that (for example), if the first coefficient entered was 2x^2, but the second coefficient entered was 2x, the polynomial should be printed as "2x+2x^2",

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<bits/stdc++.h>

using namespace std;

struct Term{ //term of polynomial

    int coeff,pow;

};

class comparator{  //compare terms

    public:

    bool operator()(Term t1, Term t2){

        return t1.pow>t2.pow;

    }

};

void printq(priority_queue<Term, vector<Term>,comparator>q){  //print polynomial

    while(q.size()){

        auto t = q.top();

        q.pop();

        cout<<t.coeff<<"x^"<<t.pow<<"  ";

    }

    cout<<endl;

}

int main(){

    

    int choice;

    priority_queue<Term, vector<Term>,comparator>q;  //q to store polynomial in sorted order

    label:

    cout<<"1.Enter term\n2.Print polynomial\n";

    cout<<"Enter choice: ";

    cin>>choice;

    if(choice==1){

        cout<<"Enter coefficient and power: ";

        int p,c;

        cin>>c>>p;

        q.push({c,p});

    }

    else if(choice==2){

        printq(q);

    }

    goto label;

    

    return 0;

}

Add a comment
Know the answer?
Add Answer to:
How can I make a program in c++ that lets the user create a polynomial by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Instructions We're going to create a program which will allow the user to add values to a list, p...

    Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List<double> _values new List<double>) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the...

  • For this lab, write a program that lets the user enter a state and the program...

    For this lab, write a program that lets the user enter a state and the program returns that state's capital, stored in a dictionary Requirements: Start by hard coding in all of the states and their capitals the user should be able to enter the name of a state or ask for all states to be printed (one key/value pair per line) If the state is in the dictionary, print its state capital in the form: "The capital of Vermont...

  • Need help on following Java GUI problem: Write a program that lets a user display and...

    Need help on following Java GUI problem: Write a program that lets a user display and modify pictures. Create a window. Add four buttons so that clicking a particular button will shift the image by a small amount in the north, south, east or west direction inside the window. Add a menu bar with two menus: File and Image. The File menu should contain an Open menu item that the user can select to display JPEG and PNG files from...

  • Create a GUI program (a Windows Forms Application) called GooseEggCalculator that lets a user enter the...

    Create a GUI program (a Windows Forms Application) called GooseEggCalculator that lets a user enter the number of eggs produced by each of four geese. Your program should provide textboxes with labels for the user to enter the values. When the user clicks a Calculate button, your app should sum the textboxes, then display the total number of eggs, as well as the number of eggs in dozens and remaining eggs, as shown on the example. Feel free to make...

  • I have this c++ program that i have to create but i am stuck on it....

    I have this c++ program that i have to create but i am stuck on it. Write a height conversion program that shall allow user to convert from feet and inches to meters (option 1) and vice versa (option 2). User shall be able to specify the type of conversion (a menu of two options). Display an error message if an invalid option is specified. Otherwise, the program would read in a length in feet and inches (two separate integer...

  • Create a program that allows a user to enter up to five names of friends. Use...

    Create a program that allows a user to enter up to five names of friends. Use a twodimensional array to store the friends’ names. After each name is entered, the user should have the option to enter another name or print out a report that shows each name entered thus far

  • In C++ write a simple menu program that displays a menu for the user in an...

    In C++ write a simple menu program that displays a menu for the user in an object-oriented technique. The menu should keep showing up until the user chooses to quit. Also, there is a sub-menu inside a menu. The user should get at least a line displayed after he or she chooses that particular option meaning if the user presses 1 for the read actors from the file. The output can look like "reading actors from a file" and then...

  • You are to create a program that queries the user for a range of integer values...

    You are to create a program that queries the user for a range of integer values (min and max). For this range, you are to find all of the prime numbers and print them to the screen. The user is then given the option to request that all of the non-prime numbers be printed to the screen with at least one factor pair that proves that they are not prime. (Using language C)

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • How do I program this problem using c++. Create a process to prompt the user to...

    How do I program this problem using c++. Create a process to prompt the user to enter the number of seconds and determine the whole number of days, hours, minutes, and seconds corresponding to the entered number of seconds. For example if the number of seconds entered was 183945, then 2 days, 3 hours, 5 minutes and 45 seconds should be displayed. There are 86400 seconds in a day, 3600 seconds in an hour, and 60 seconds in a minute....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT