#include <iostream>
#include <sstream>
#include <list>
using namespace std;
string showExponentSP(int temp, int item); //用來處理輸入裡面最先出現的項次
string showExponent(int temp, int item); //用來處理剩下的項次
//item 表示"項次"
//temp 表示"輸入的值"
int main()
{
string str = "";
list<int> l;
int input;
int counter = 0;
int level = 8;
cin>>input;
while( cin )
{
l.push_back(input);
counter++;
if(counter == 9)
{
list<int>::iterator iter = l.begin();
while( iter != l.end() ) {
if(str.empty())
{
str.append(showExponentSP(*iter,level));
}
else
{
str.append(showExponent(*iter,level));
}
++iter;
level--;
}
cout<<str<<endl;
str.clear();
l.clear();
counter = 0;
level = 8;
}
cin>>input;
}
return 0;
}
string showExponentSP(int temp, int item)
{
string s="";
stringstream ss(s);
if(item == 0)
{
ss<<temp;
}
else if(item == 1)
{
if (temp == 0)
{
ss<<"";
}
else
{
if(temp == -1)
ss<<"-x";
else if(temp == 1)
ss<<"x";
else
ss<<temp<<"x";
}
}
else
{
if (temp == 0)
{
ss<<"";
}
else
{
if(temp == -1)
ss<<"-x^"<<item;
else if(temp == 1)
ss<<"x^"<<item;
else
ss<<temp<<"x^"<<item;
}
}
return ss.str();
}
string showExponent(int temp, int item)
{
string s="";
stringstream ss(s);
if(item>1)
{
if(temp == -1)
ss<<" - "<<"x^"<<item;
else if(temp == 1)
ss<<" + "<<"x^"<<item;
else if(temp<0)
ss<<" - "<<(-temp)<<"x^"<<item;
else if(temp>0)
ss<<" + "<<temp<<"x^"<<item;
else
ss<<"";
}
else if(item == 0)
{
if(temp<0)
ss<<" - "<<(-temp);
else if(temp>0)
ss<<" + "<<temp;
else
ss<<"";
}else
{
if(temp == -1)
ss<<" - "<<"x";
else if(temp == 1)
ss<<" + "<<"x";
else if(temp<-1)
ss<<" - "<<(-temp)<<"x";
else if(temp>1)
ss<<" + "<<temp<<"x";
else
ss<<"";
}
//cout<<item<<" ";
return ss.str();
}
Friday, November 19, 2010
ACM-392 Polynomial Showdown with C++
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment