本文整理汇总了C++中Polynomial::clear方法的典型用法代码示例。如果您正苦于以下问题:C++ Polynomial::clear方法的具体用法?C++ Polynomial::clear怎么用?C++ Polynomial::clear使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Polynomial
的用法示例。
在下文中一共展示了Polynomial::clear方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: getPolynomial
// Gets user input and turns it into a polynomail
Polynomial getPolynomial(Polynomial& p)
{
p.clear();
// Temp term to put into a list
Term temp;
// Line of input poly from user
string line;
// Lists of terms and strings to build a poly
list<Term> terms;
list<string> strings;
// User input
cin.ignore();
getline(cin, line);
// Put + in front of any - as a delimiter for terms in a poly
int stringSearchPosition(0);
while (line.find('-', stringSearchPosition) != string::npos)
{
line.insert(line.find('-', stringSearchPosition), 1, '+');
stringSearchPosition = line.find('-', stringSearchPosition) + 1;
}
// Position markers
size_t prev(0), pos;
// Break up the user input into substrings with + as the delimeter of terms
while ((pos = line.find_first_of("+", prev)) != string::npos)
{
if (pos > prev)strings.push_back(line.substr(prev, pos - prev));
prev = pos + 1;
}
// Take care of the end of the line
if (prev < line.length())strings.push_back(line.substr(prev, std::string::npos));
// Put the list of substrings into terms then into a poly
for (std::list<std::string>::const_iterator i = strings.begin(); i != strings.end(); ++i)
{
Term t(*i);
p.addTerm(t);
}
// Return the poly
return p;
}
示例2: displayMenu
// A glorious menu and user interface
void displayMenu(Polynomial& p1, Polynomial& p2, Polynomial& result)
{
// My choices
int userSelection(-1);
cout << "Please select one: \n\n";
cout << "1:\tEnter Polynomial One\n";
cout << "2:\tEnter Polynomial Two\n";
cout << "3:\tAdd Polynomials\n";
cout << "4:\tDisplay Polynomial One\n";
cout << "5:\tDisplay Polynomial Two\n";
cout << "6:\tDisplay Result\n";
cout << "7:\tClear entries\n";
cout << "8:\tExit\n\n";
// Filter input so program accepts single digits only
while (!(cin >> userSelection))
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
// Warn the user about an improper input
cout << endl << "Single digits only" << endl;
cout << "Make another selection: ";
}
// What happens when making a choice
switch (userSelection)
{
// Give me poly1
case 1:
cout << endl << endl;
cout << "Enter the first polynomial: ";
// See if the polynomial is poperly entered
try{
getPolynomial(p1);
cout << "Polynomial one successfully entered\n";
}
// Bad input, try again
catch (exception e)
{
cout << endl << endl;
cout << "Incorrect polynomial formatting entered, use formatting like 3X^3+1" << endl;
}
cout << endl << endl;
break;
// Give me poly2
case 2:
cout << endl << endl;
cout << "Enter the second polynomial: ";
// See if the polynomial is poperly entered
try{
getPolynomial(p2);
cout << "Polynomial two successfully entered\n";
}
// Try again
catch (exception e)
{
cout << endl << endl;
cout << "Incorrect polynomial formatting entered, use formatting like 3X^3+1" << endl;
}
cout << endl << endl;
break;
// Add the polys
case 3:
cout << endl << endl;
if (p1.is_empty()) cout << "Polynomial One is empty, please enter a value before you add\n";
if (p2.is_empty()) cout << "Polynomial Two is empty, please enter a value before you add\n";
else
{
result = p1 + p2;
cout << "The result is: " << result << endl;
}
break;
// Show what user entered for poly1
case 4:
cout << endl << endl;
if (p1.is_empty()) cout << "Polynomial One is empty, please enter a value before you add\n";
else cout << "Polynomial one is " << p1 << endl;
break;
// Show what user entered for poly2
case 5:
cout << endl << endl;
if (p2.is_empty()) cout << "Polynomial Two is empty, please enter a value before you add\n";
else cout << "Polynomial two is " << p2 << endl;
break;
// Show what user entered for poly2
case 6:
cout << endl << endl;
if (result.is_empty()) cout << "No result found, please add some polynomials together\n";
//.........这里部分代码省略.........