267 lines
5.4 KiB
C++
267 lines
5.4 KiB
C++
|
#include <stdio.h>
|
|||
|
#include <stdlib.h>
|
|||
|
#include <math.h>
|
|||
|
#include <string.h>
|
|||
|
#include <ctype.h>
|
|||
|
|
|||
|
typedef struct node {
|
|||
|
int coef;
|
|||
|
int exp;
|
|||
|
int root;
|
|||
|
struct node* rlink;
|
|||
|
struct node* llink;
|
|||
|
} node;
|
|||
|
node *head, *tail, *current, *prev, *temp ,*head_root , *current_root , *tail_root;
|
|||
|
|
|||
|
void init_f() {
|
|||
|
head = (node *)malloc(sizeof(node));
|
|||
|
tail = (node *)malloc(sizeof(node));
|
|||
|
head->rlink = tail;
|
|||
|
tail->llink = head;
|
|||
|
|
|||
|
head_root = (node *)malloc(sizeof(node));
|
|||
|
tail_root = (node *)malloc(sizeof(node));
|
|||
|
|
|||
|
head_root->rlink = tail_root;
|
|||
|
tail_root->llink = head_root;
|
|||
|
}
|
|||
|
|
|||
|
void read_poly(char str[] ){
|
|||
|
int argCount ;
|
|||
|
while (1) {
|
|||
|
|
|||
|
node* newNode = (node*)malloc(sizeof(node));
|
|||
|
|
|||
|
argCount = sscanf(str, "%dx^%d%s", &newNode->coef, &newNode->exp, str);
|
|||
|
|
|||
|
if (argCount >= 2) { // <20><><EFBFBD>\Ū<><C5AA><EFBFBD>Y<EFBFBD>ƩM<C6A9><4D><EFBFBD><EFBFBD>
|
|||
|
|
|||
|
newNode->llink = tail->llink;
|
|||
|
newNode->rlink = tail;
|
|||
|
tail->llink->rlink = newNode;
|
|||
|
tail->llink = newNode;
|
|||
|
|
|||
|
if (argCount == 2) {
|
|||
|
break ;
|
|||
|
}
|
|||
|
|
|||
|
} else {
|
|||
|
if(!isdigit(newNode->coef)){
|
|||
|
newNode->exp = 0;
|
|||
|
|
|||
|
newNode->llink = tail->llink;
|
|||
|
newNode->rlink = tail;
|
|||
|
tail->llink->rlink = newNode;
|
|||
|
tail->llink = newNode;
|
|||
|
|
|||
|
break ;
|
|||
|
|
|||
|
}else{
|
|||
|
// printf("newNode->coef = %d\n",newNode->coef);
|
|||
|
// printf("isdigit(newNode->coef) = %d",isdigit(newNode->coef));
|
|||
|
|
|||
|
free(newNode);
|
|||
|
break ;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
void compact(char str[]){ /* <20><><EFBFBD><EFBFBD>str<74><72><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ťաA<D5A1><41><EFBFBD>ܱo<DCB1><6F><EFBFBD><EFBFBD> */
|
|||
|
int i, j;
|
|||
|
|
|||
|
for (i = j = 0; str[i]; i++, j++) {
|
|||
|
for (; str[i]==' '; i++);
|
|||
|
str[j] = str[i];
|
|||
|
}
|
|||
|
str[j] = '\0';
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ťի᪺<EFBFBD>r<EFBFBD><EFBFBD><EFBFBD>G%s\n",str);
|
|||
|
}
|
|||
|
|
|||
|
void clear_node(){
|
|||
|
// <20><><EFBFBD><EFBFBD><EFBFBD>O<EFBFBD><4F><EFBFBD><EFBFBD>
|
|||
|
current = head->rlink;
|
|||
|
while (current != tail) {
|
|||
|
temp = current;
|
|||
|
current = current->rlink;
|
|||
|
free(temp);
|
|||
|
}
|
|||
|
free(head);
|
|||
|
free(tail);
|
|||
|
free(head_root);
|
|||
|
free(tail_root);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void show( int n, int mode){
|
|||
|
int i;
|
|||
|
if(mode == 0){
|
|||
|
printf("\n<EFBFBD><EFBFBD><EFBFBD>{<7B><>p(x)= ");
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
while (current != tail) {
|
|||
|
if(abs(current->coef)>1){
|
|||
|
printf("%s%d", current->coef > 1? "":"\b",current->coef);
|
|||
|
}else if(current->coef == -1){
|
|||
|
printf("%s", current->exp == 0? "\b-1":"\b-");
|
|||
|
}
|
|||
|
switch(current->exp){
|
|||
|
case 0:
|
|||
|
if(current->coef == 1 ) printf("1+");
|
|||
|
else printf("+");
|
|||
|
break;
|
|||
|
case 1:
|
|||
|
printf("x+");
|
|||
|
break;
|
|||
|
default:
|
|||
|
printf("x^%d+",current->exp);
|
|||
|
}
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
printf("\b ");
|
|||
|
|
|||
|
}else { /* mode==1<>A<EFBFBD><41><EFBFBD>ܩұo<D2B1><6F> */
|
|||
|
|
|||
|
current_root = head_root->rlink;
|
|||
|
|
|||
|
while(current_root != tail_root){
|
|||
|
printf("%d ",current_root->root);
|
|||
|
current_root = current_root->rlink;
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
int findRoot() /* <20><><EFBFBD>ڡA<DAA1>ұo<D2B1>Ѧs<D1A6>Jq[]<5D>A<EFBFBD>^<5E>Ǯڪ<C7AE><DAAA>Ӽ<EFBFBD> */
|
|||
|
{
|
|||
|
|
|||
|
current_root = head_root->rlink;
|
|||
|
|
|||
|
int r, c = 0, i, j, k, sum;
|
|||
|
int sign[2] = {1,-1};
|
|||
|
|
|||
|
current = tail->llink;
|
|||
|
r = abs(current->coef); /* <20><><EFBFBD>X<EFBFBD>̧C<CCA7><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Y<EFBFBD><59> */
|
|||
|
|
|||
|
if (current->exp != 0) { /* <20>̧C<CCA7><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> >= 1<>A<EFBFBD>h0<68><30><EFBFBD>i<EFBFBD><69><EFBFBD><EFBFBD> */
|
|||
|
if ((i = current->exp) > 0) {
|
|||
|
node* newNode = (node*)malloc(sizeof(node));
|
|||
|
|
|||
|
newNode->root = 0; /* <20>̧C<CCA7><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƴ]<5D><>i */
|
|||
|
|
|||
|
newNode->llink = tail_root->llink;
|
|||
|
newNode->rlink = tail_root;
|
|||
|
tail_root->llink->rlink = newNode;
|
|||
|
tail_root->llink = newNode;
|
|||
|
c = 1;
|
|||
|
}
|
|||
|
current = head->rlink;
|
|||
|
while(current!=tail){
|
|||
|
current->exp -= i; /* <20>Ҧ<EFBFBD><D2A6><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƴ<EFBFBD><C6B4>hi */
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
// r = abs(p[2*p[0]]); /* <20><><EFBFBD>X<EFBFBD>̧C<CCA7><43><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Y<EFBFBD><59> */
|
|||
|
|
|||
|
for (i = 1; i <= r; i++) { /* <20>Ҽ{<7B><><EFBFBD>㰣r<E3B0A3><72><EFBFBD>]<5D><>i<EFBFBD>A<EFBFBD><41><EFBFBD>`<60>N<EFBFBD><4E><EFBFBD>t<EFBFBD><74> */
|
|||
|
if (r%i != 0) continue; /* <20><><EFBFBD><EFBFBD><EFBFBD>㰣r<E3B0A3><72>i<EFBFBD>N<EFBFBD><4E><EFBFBD>L<EFBFBD>A<EFBFBD>~<7E><><EFBFBD>Ҽ{<7B>U<EFBFBD>@<40><>i */
|
|||
|
|
|||
|
for (j = 0; j < 2; j++) { /* j<><6A>0<EFBFBD>A<EFBFBD>N<EFBFBD><4E><EFBFBD>Ҽ{+i<><69><EFBFBD><EFBFBD><EFBFBD>ҡFj<46><6A>1<EFBFBD>A<EFBFBD>N<EFBFBD><4E><EFBFBD>Ҽ{-i<><69><EFBFBD><EFBFBD><EFBFBD>p */
|
|||
|
for (sum = 0, current = head->rlink ; current!= tail ; current = current->rlink) /* <20>p<EFBFBD><70><EFBFBD>C<EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>G<EFBFBD><47>(<28>Y<EFBFBD><59>*x^<5E><><EFBFBD><EFBFBD>)<29>ò֥[<5B><>sum<75><6D><EFBFBD><EFBFBD> */
|
|||
|
sum += current->coef *(int)(pow(sign[j]*i,current->exp));
|
|||
|
if (sum == 0) {
|
|||
|
node* newNode = (node*)malloc(sizeof(node));
|
|||
|
newNode->root = sign[j]*i; /* bingo! <20>o<EFBFBD>{<7B>X<EFBFBD>G<EFBFBD><47><EFBFBD><EFBFBD><F3AABABE>ƮڡA<DAA1>N<EFBFBD><4E><EFBFBD>g<EFBFBD>Jq<4A>}<7D>C */
|
|||
|
|
|||
|
newNode->llink = tail_root->llink;
|
|||
|
newNode->rlink = tail_root;
|
|||
|
tail_root->llink->rlink = newNode;
|
|||
|
tail_root->llink = newNode;
|
|||
|
c = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return c;
|
|||
|
}
|
|||
|
|
|||
|
void sort(){
|
|||
|
|
|||
|
//<2F>Y<EFBFBD>S<EFBFBD><53><EFBFBD><EFBFBD><EFBFBD>ƩΥu<CEA5><75><EFBFBD>@<40><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
if (head->rlink == tail || head->rlink->rlink == tail) {
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
int flag = 1;
|
|||
|
while(flag){
|
|||
|
flag = 0;
|
|||
|
current = head->rlink;
|
|||
|
|
|||
|
while(current->rlink != tail){
|
|||
|
temp = current->rlink;
|
|||
|
|
|||
|
if(current->exp < temp->exp ){
|
|||
|
temp->llink = current->llink;
|
|||
|
current->rlink = temp->rlink;
|
|||
|
|
|||
|
temp->rlink->llink = current;
|
|||
|
temp->rlink = current;
|
|||
|
|
|||
|
current->llink->rlink = temp;
|
|||
|
current->llink = temp;
|
|||
|
|
|||
|
flag = 1;
|
|||
|
}
|
|||
|
else{
|
|||
|
current = current->rlink;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
int main() {
|
|||
|
char str[100];
|
|||
|
int poly_num ,c , high;
|
|||
|
init_f();
|
|||
|
|
|||
|
printf("\n*** <20>Ѧh<D1A6><68><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ʈ<EFBFBD> ***\n\n<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD>h<EFBFBD><EFBFBD><EFBFBD><EFBFBD> ==> ");
|
|||
|
|
|||
|
if (fgets(str, sizeof(str), stdin) == NULL) {
|
|||
|
printf("<EFBFBD><EFBFBD><EFBFBD>J<EFBFBD><EFBFBD><EFBFBD>~<7E>I\n");
|
|||
|
return 1;
|
|||
|
}
|
|||
|
|
|||
|
compact(str);
|
|||
|
read_poly(str);
|
|||
|
|
|||
|
sort();
|
|||
|
|
|||
|
current = head->rlink;
|
|||
|
high= current->exp;
|
|||
|
|
|||
|
show(c,0);
|
|||
|
|
|||
|
c = findRoot();
|
|||
|
|
|||
|
if (c > 0) {
|
|||
|
printf("\n\n<EFBFBD>ѱo<EFBFBD><EFBFBD><EFBFBD>Ʈڬ<EFBFBD><EFBFBD>G");
|
|||
|
show(c,1);
|
|||
|
}
|
|||
|
else
|
|||
|
printf("\n\n<EFBFBD><EFBFBD><EFBFBD>Ʈڤ<EFBFBD><EFBFBD>s<EFBFBD>b\n");
|
|||
|
|
|||
|
|
|||
|
|
|||
|
clear_node();
|
|||
|
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|