题目描述
测试程序
#include <stdlib.h>
#include <stdio.h>
#define MAXLINE 1024
#define SWAP(a,b,t) (t)=(a),(a)=(b),(b)=(t)
typedef struct MonomialStruct{
int coef;
int expo;
struct MonomialStruct * next;
} Monomial;
typedef Monomial *Polynomial;
void PrintMonomial(int coef, int expo);
Monomial* ParseMonomial(char **s);
Polynomial InsertAfterTail(Polynomial head, Monomial* pNewNode);
Polynomial TrimZeroTerms(Polynomial head);
Polynomial SortPolynomial(Polynomial head);
Polynomial Minus(Polynomial p1, Polynomial p2);
Polynomial Multiply(Polynomial p1, Polynomial p2);
Monomial * CreateMonomial(int coef, int expo)
{
Monomial * p = (Monomial*)malloc(sizeof(Monomial));
if( !p )
return NULL;
p->coef = coef;
p->expo = expo;
p->next = NULL;
return p;
}
Polynomial DeletePolynomial(Polynomial head)
{
while(head) {
Monomial * d = head;
head = head->next;
free(d);
}
return NULL;
}
Polynomial AddMonomial(Polynomial head, int coef, int expo)
{
Monomial * p;
if( coef==0 )
return head;
for( p = head; p && p->expo!=expo; p = p->next )
;
if( p )
p->coef += coef;
else {
p = CreateMonomial(coef, expo);
head = InsertAfterTail(head, p);
}
return head;
}
Polynomial Add(Polynomial p1, Polynomial p2)
{
Polynomial h = NULL;
for( ; p1; p1=p1->next )
h = AddMonomial(h, p1->coef, p1->expo);
for( ; p2; p2=p2->next )
h = AddMonomial(h, p2->coef, p2->expo);
return h;
}
int IsEXPO(char *s)
{
return ( (s[0]=='x' || s[0]=='X') && s[1]=='^');
}
int IsEndingChar(char ch)
{
return (ch==0 || ch=='\n');
}
char * SkipSpaceChars(char *s)
{
while( *s==' ' || *s=='\t' )
s++;
return s;
}
int GetSignChar(char **s)
{
char *p = SkipSpaceChars(*s);
if( *p=='+' || *p=='-' ) {
*s = p + 1;
return (*p);
}
return 0;
}
Polynomial ParsePolynomial()
{
char linebuffer[1024], *s = linebuffer;
Polynomial hResult = NULL;
if( !fgets(linebuffer, sizeof(linebuffer), stdin) )
return NULL;
while( 1 ) {
Monomial *pNewNode;
int signChar = GetSignChar(&s);
pNewNode = ParseMonomial(&s);
if( !pNewNode )
break;
if( signChar == '-' )
pNewNode->coef = -pNewNode->coef;
hResult = InsertAfterTail(hResult, pNewNode);
}
return hResult;
}
Polynomial PrintPolynomial(Polynomial pHead)
{
Polynomial p = pHead;
int firstTerm = 1, nothingOutputYet = 1;
for( ; p; p = p->next )
{
int c = p->coef;
int k = p->expo;
if( c==0 )
continue;
if( firstTerm ) {
PrintMonomial(c,k);
firstTerm = 0;
} else {
printf(" %c ", c>0 ? '+' : '-');
PrintMonomial(c>0?c:-c, k);
}
nothingOutputYet = 0;
}
if( nothingOutputYet )
putchar('0');
putchar('\n');
return pHead;
}
int main()
{
Polynomial p1, p2, pResult;
char cmdString[MAXLINE], cmd;
p1 = ParsePolynomial();
if( !fgets(cmdString, sizeof(cmdString), stdin) )
return 0;
cmd = cmdString[0];
if( cmd!='+' && cmd!='-' && cmd!='*' ) {
printf("input=");
PrintPolynomial( p1 );
return 0;
}
p2 = ParsePolynomial();
PrintPolynomial( p1 );
printf("%c\n", cmd);
PrintPolynomial( p2 );
printf("=\n");
if( cmd=='+' )
pResult = PrintPolynomial( SortPolynomial( TrimZeroTerms( Add(p1,p2) ) ) );
else if( cmd=='-' )
pResult = PrintPolynomial( SortPolynomial( TrimZeroTerms( Minus(p1, p2) ) ) );
else
pResult = PrintPolynomial( SortPolynomial( TrimZeroTerms( Multiply(p1, p2) ) ) );
DeletePolynomial(p1);
DeletePolynomial(p2);
DeletePolynomial(pResult);
return 0;
}
题目思路
思路其实并不难,但是实现起来非常难,其中有很多很多细节要去分析和注意。
题目的坑
1.char **s; 则*s[1]与(*s)[1] 是不一样的 2.输入为0时用依次取位的方法要加以特判(测试点里面说”输入多项式5“ 无语0.0~) 3.输入为空行时要认为是0 4.当处理指数时,对于无x的项应当直接跳过,否则会有被吞掉的位。 5.特别留心指针为空的情况,比如p=NULL 当调用p->next时程序崩溃
AC代码
void print(int coef){
if(coef == 1) return ;
else if(coef == -1) printf("-");
else printf("%d", coef);
}
void PrintMonomial(int coef, int expo){
if(expo == 0) printf("%d", coef);
else if(expo == 1){
print(coef);
printf("x");
}
else{
print(coef);
printf("x^%d", expo);
}
return ;
}
#include<ctype.h>
Monomial* ParseMonomial(char **s){
*s = SkipSpaceChars(*s);
if(IsEndingChar(**s)) return NULL;
Polynomial p = (Polynomial)malloc(sizeof(Monomial));
p->coef = 1, p->expo = 0;
int t = 0;
if((*s)[0] == 'x' || (*s)[0] == 'X'){
t = 1;
if((*s)[1] == '^') *s += 2;
else{
p->expo = 1;
*s += 1;
return p;
}
}
else{
if(!isdigit(**s)){
if(**s == '-') p->coef *= -1;
*s += 1;
}
int sum = 0;
if(**s == '0') p->coef = 0;
while(isdigit(**s)){
sum = **s - '0' + sum * 10;
*s += 1;
}
if(sum != 0) p->coef *= sum;
if((*s)[0] == 'x' || (*s)[0] == 'X'){
t = 1;
if((*s)[1] == '^') *s += 2;
else{
p->expo = 1;
*s += 1;
return p;
}
}
}
if(t == 0) return p;
int sign = 1;
if(!isdigit(**s)){
if(**s == '-') sign *= -1, *s += 1;
}
int Sum = 0;
while(isdigit(**s)){
Sum = **s - '0' + Sum * 10;
*s += 1;
}
if(Sum != 0) p->expo = sign * Sum;
return p;
}
Polynomial InsertAfterTail(Polynomial head, Monomial* pNewNode){
if(head == NULL){
head = pNewNode;
head->next = NULL;
return head;
}
else{
Polynomial tmp = head;
while(tmp->next){
tmp = tmp->next;
}
tmp->next = pNewNode;
tmp->next->next = NULL;
return head;
}
}
Polynomial TrimZeroTerms(Polynomial head){
Polynomial H = NULL;
while(head){
if(head->coef != 0)
H = InsertAfterTail(H, CreateMonomial(head->coef, head->expo));
head = head->next;
}
return H;
}
Polynomial SortPolynomial(Polynomial head){
if(head == NULL || head->next == NULL) return head;
Polynomial p, q, temp;
for(p = head; p ; p = p->next){
temp = p;
for(q = p->next; q ; q = q->next)
if(q->expo < temp->expo){
temp = q;
}
int tmp;
SWAP(p->coef, temp->coef, tmp);
SWAP(p->expo, temp->expo, tmp);
}
return head;
}
Polynomial MinusMonomial(Polynomial head, int coef, int expo){
Monomial * p;
if( coef==0 )
return head;
for( p = head; p && p->expo!=expo; p = p->next )
;
if( p )
p->coef -= coef;
else {
p = CreateMonomial(-coef, expo);
head = InsertAfterTail(head, p);
}
return head;
}
Polynomial Minus(Polynomial p1, Polynomial p2){减法外层函数
Polynomial h = NULL;
for( ; p1; p1 = p1->next)
h = AddMonomial(h, p1->coef, p1->expo);
for( ; p2; p2 = p2->next)
h = MinusMonomial(h, p2->coef, p2->expo);
return h;
}
Polynomial Multiply(Polynomial p1, Polynomial p2){
Polynomial h = NULL;
Polynomial k;
for( ; p1; p1 = p1->next)
for(k = p2 ; k; k = k->next)
h = AddMonomial(h, p1->coef * k->coef, p1->expo + k->expo);
return h;
}
|