| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
程序员是怎么死的……In Reply To:不行了……写到吐血 Posted by:Renegade at 2006-04-05 22:31:32 //气死的!6.5k的代码,调了无数次……重写了一遍终于喜极而泣,55655
#include<iostream>
#include<string>
#include<cctype>
#include<list>
#include<conio.h>
using namespace std;
class elem{
public:
int coef;
int power;
elem(int c,int p):coef(c),power(p){};
};
int n,x,Len,nextsign;
char str[10000],*pstr,tmpc,xx,*tp;
int c,p;
list<elem>poly;
int i,j,sp,ep;
int POW(int x,int y)
{
int r=1;
while(y--)r*=x;
return r;
}
void poly_display(void)
{
if(poly.size()==0)cout<<"0";
for(list<elem>::iterator it=poly.begin();it!=poly.end();it++){
if(it!=poly.begin()){
if((*it).power!=0 && ((*it).coef==1 || (*it).coef==-1)){
if((*it).coef==1)cout<<"+";
else cout<<"-";
}
else {
if((*it).coef<0)cout<<(*it).coef;
else cout<<"+"<<(*it).coef;
}
}
else {
if((*it).coef==-1)cout<<"-";
else {
if((*it).coef!=1 || (*it).power==0)cout<<(*it).coef;
}
}
if((*it).power==1)cout<<"x";
if((*it).power>1)cout<<"x^"<<(*it).power;
}
cout<<endl;
}
void poly_display_x(void)
{
if(poly.size()==0)cout<<"0";
for(list<elem>::iterator it=poly.begin();it!=poly.end();it++){
if(it!=poly.begin()){
if((*it).power!=0 && ((*it).coef==1 || (*it).coef==-1)){
if((*it).coef==1)cout<<"+";
else cout<<"-";
}
else {
if((*it).coef<0)cout<<(*it).coef;
else cout<<"+"<<(*it).coef;
}
}
else {
if((*it).coef==-1)cout<<"-";
else {
if((*it).coef!=1 || (*it).power==0)cout<<(*it).coef;
}
}
if((*it).power==1)cout<<"("<<x<<")";
if((*it).power>1)cout<<"("<<x<<")^"<<(*it).power;
}
cout<<endl;
}
void derivative(void)
{
for(list<elem>::iterator it=poly.begin();it!=poly.end();it++){
if((*it).power==0)(*it).coef=0;
else {
(*it).coef*=(*it).power;
(*it).power-=1;
}
}
for(list<elem>::iterator it=poly.begin();it!=poly.end();it++){
if((*it).coef==0){poly.erase(it);it=poly.begin();}
}
}
void evaluation(void)
{
for(list<elem>::iterator it=poly.begin();it!=poly.end();it++){
(*it).coef=(*it).coef*POW(x,(*it).power);
(*it).power=0;
}
}
void result_display(void)
{
int r=0;
for(list<elem>::iterator it=poly.begin();it!=poly.end();it++){
r+=(*it).coef;
}
cout<<r<<endl;
}
int main()
{
cin>>n;
for(int pp=1;pp<=n;pp++){
cin>>x>>str;
pstr=str;
Len=strlen(str);
i=0;
nextsign=1;
sp=ep=0;
poly.clear();
while(ep<=Len-1){
sp=ep;
for(++ep;pstr[ep]!='\0';ep++){
tmpc=pstr[ep];
if(tmpc=='+' || tmpc=='-'){
pstr[ep]='\0';
break;
}
}
tp=&pstr[sp];
if(sp==0){
if(strstr(tp,"x")!=NULL){
if(strstr(tp,"x^")!=NULL){
if(tp[0]=='+' && tp[1]=='x'){
c=1;
sscanf(tp,"%c%c%d",&xx,&xx,&p);
poly.push_back(elem(c,p));
}
if(tp[0]=='-' && tp[1]=='x'){
c=-1;
sscanf(tp,"%c%c%c%d",&xx,&xx,&xx,&p);
poly.push_back(elem(c,p));
}
if(tp[0]=='x'){
c=1;
sscanf(tp,"%c%c%d",&xx,&xx,&p);
poly.push_back(elem(c,p));
}
if(isdigit(tp[0]) || isdigit(tp[1])){
sscanf(tp,"%d%c%c%d",&c,&xx,&xx,&p);
poly.push_back(elem(c,p));
}
}
else {
p=1;
if(tp[0]=='+' && tp[1]=='x'){
c=1;
poly.push_back(elem(c,p));
}
if(tp[0]=='-' && tp[1]=='x'){
c=-1;
poly.push_back(elem(c,p));
}
if(tp[0]=='x'){
c=1;
poly.push_back(elem(c,p));
}
if(isdigit(tp[0]) || isdigit(tp[1])){
sscanf(tp,"%d",&c);
poly.push_back(elem(c,p));
}
}
}
else {
sscanf(tp,"%d",&c);
p=0;
poly.push_back(elem(c,p));
}
}
else {
//puts(tp); //for debug
if(strstr(tp,"x")!=NULL){
if(strstr(tp,"x^")!=NULL){
if(!isdigit(tp[1])){
if(tp[0]=='+')c=1;
else c=-1;
sscanf(tp,"%c%c%c%d",&xx,&xx,&p);
poly.push_back(elem(c,p));
}
else {
sscanf(tp,"%d%c%c%d",&c,&xx,&xx,&p);
poly.push_back(elem(c,p));
}
}
else {
p=1;
if(!isdigit(tp[1])){
if(tp[0]=='+')c=1;
else c=-1;
poly.push_back(elem(c,p));
}
else {
sscanf(tp,"%d",&c);
poly.push_back(elem(c,p));
}
}
}
else {
sscanf(tp,"%d",&c);
p=0;
poly.push_back(elem(c,p));
}
}
pstr[ep]=tmpc;
}
cout<<"POLYNOMIAL "<<pp<<endl;
poly_display();
derivative();
poly_display();
poly_display_x();
evaluation();
poly_display();
result_display();
}
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator