/*

*/

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include "polynomial.h"

#define ___ " "

#define __1 "%i"

#define __2 "%i %i"

#define __3 "%i "

#define _k$ "x^%i"

#define $jj "%c "

#define _Rs "Result of Addition: "

#define _Rm "Result of Subtraction: "

#define _Ra "Result of Multiplication: "

#define _$$ "Number of terms in polynomial: "

#define a$$ "Result of Evaluation: %i

"

#define $_$ "Enter coeficient and exponent of term %i: "

#define $__$ "Evaluate polynomial at integer value: "

#define _C$ "** Data for Polynamial %i **

"

#define _R$ "Polynomial %i: "

void death_star ( Polynomial * ) ; /* has a trash^H^H^H^H^Hpolynomial compactor */

void omg_ponies ( Polynomial ) ; /* ponies collect the garbage */

void displayMenu ( void ) {

puts ( "**** Operations on Polynomials ****" ) ;

puts ( "1. Add" ) ;

puts ( "2. Subtract" ) ;

puts ( "3. Multiply" ) ;

puts ( "4. Evaluate" ) ;

puts ( "5. Quit" ) ;

printf ( "Your selection: " ) ;

}

int main ( void ) {

int a = 0 ;

while ( a != 5 &&! feof ( stdin ) ) {

displayMenu ( ) ; scanf_s ( "%i" ,& a ) ;

if ( a > 0 && a < 4 ) {

Polynomial _ , $ , t = NULL ; printf ( _C$ , 1 ) ; $ = create ( ) ; printf ( _R$ , 1 ) ; display ( $ ) ;

printf ( _C$ , 2 ) ; _ = create ( ) ; printf ( _R$ , 2 ) ; display ( _ ) ;

if ( a == 1 ) { t = add ( $ , _ ) ; printf ( _Rs ) ; display ( t ) ; }

else if ( a == 2 ) { t = subtract ( $ , _ ) ; printf ( _Rm ) ; display ( t ) ; }

else if ( a == 3 ) { t = multiply ( $ , _ ) ; printf ( _Ra ) ; display ( t ) ; }

if ( t ) omg_ponies ( t ) ; omg_ponies ( _ ) ; omg_ponies ( $ ) ;

} else if ( a == 4 ) {

Polynomial $ ; printf ( _C$ , 1 ) ; $ = create ( ) ; printf ( _R$ , 1 ) ; display ( $ ) ;

int w ; printf ( $__$ ) ; scanf_s ( __1 ,& w ) ; printf ( a$$ , evaluate ( $ , w ) ) ; omg_ponies ( $ ) ;

}

puts ( "" ) ;

}

puts ( "BYE" ) ;

return 0 ;

}

Polynomial create ( ) {

int _ ; printf ( _$$ ) ; scanf_s ( __1 ,& _ ) ; Polynomial $f = NULL , $l = NULL ;

for ( int __ = 0 ; __ < _ ; __ ++ ) {

Polynomial $ = calloc ( 1 , sizeof ( Term ) ) ; printf ( $_$ , __ + 1 ) ;

scanf_s ( __2 ,& $ -> coef ,& ( $ -> exp ) ) ;

if ( ! $f ) $f = $ ; if ( $l ) { $l -> next = $ ; $l = $ ; } else $l = $ ;

}

death_star ( & $f ) ; return $f ;

}

void display ( Polynomial $$ ) {

int _ = 0 ;

for ( Polynomial $ = $$ ; $ ; $ = $ -> next ) {

int zx = $ -> coef , xz = $ -> exp ; char x$ = zx >= 0 ? '+' : '-' ;

if ( ! _ &&! zx ) printf ( __3 , zx ) ; else if ( zx ) {

//if(_&&(abs(zx)>>1||xz))printf($jj,x$);

if ( ! ( ! _ && zx >= 0 ) ) printf ( $jj , x$ ) ;

if ( abs ( zx ) >> 1 ) printf ( __1 , abs ( zx ) ) ;

if ( xz ) { if ( xz == 1 ) printf ( "x" ) ; else printf ( _k$ , xz ) ; }

printf ( ___ ) ;

} _ ++;

} puts ( "" ) ;

}

int evaluate ( Polynomial $_ , int v ) {

int _ = 0 ; for ( Polynomial $ = $_ ; $ ; $ = $ -> next ) _ += $ -> coef * ( int ) pow ( v , $ -> exp ) ;

return _ ;

}

Polynomial add ( Polynomial $_ , Polynomial _$ ) {

Polynomial __ = NULL , __$ = NULL ; for ( Polynomial $ = $_ ; $ ; $ = $ -> next ) {

Polynomial _ = calloc ( 1 , sizeof ( Term ) ) ; _ -> coef = $ -> coef ; _ -> exp = $ -> exp ;

if ( ! __ ) { __ = _ ; __$ = _ ; } else { __$ -> next = _ ; __$ = _ ; }

}

for ( Polynomial $ = _$ ; $ ; $ = $ -> next ) {

int l$ = 1 ;

for ( Polynomial $$ = __ ; $$ && l$ ; $$ = $$ -> next ) if ( $ -> exp == $$ -> exp )

{ l$ --; $$ -> coef += $ -> coef ; }

if ( l$ ) { Polynomial $n$ = calloc ( 1 , sizeof ( Term ) ) ;

$n$ -> coef = $ -> coef ; $n$ -> exp = $ -> exp ; __$ -> next = $n$ ; __$ = $n$ ;

}

}

death_star ( & __ ) ; return __ ;

}

Polynomial subtract ( Polynomial $_ , Polynomial _$ ) {

Polynomial __ = NULL , __$ = NULL ; for ( Polynomial $ = $_ ; $ ; $ = $ -> next ) {

Polynomial _ = calloc ( 1 , sizeof ( Term ) ) ; _ -> coef = $ -> coef ; _ -> exp = $ -> exp ;

if ( ! __ ) { __ = _ ; __$ = _ ; } else { __$ -> next = _ ; __$ = _ ; }

}

for ( Polynomial $ = _$ ; $ ; $ = $ -> next ) {

int l$ = 1 ;

for ( Polynomial $$ = __ ; $$ && l$ ; $$ = $$ -> next ) if ( $ -> exp == $$ -> exp )

{ l$ --; $$ -> coef -= $ -> coef ; }

if ( l$ ) { Polynomial $n$ = calloc ( 1 , sizeof ( Term ) ) ;

$n$ -> coef =- $ -> coef ; $n$ -> exp = $ -> exp ; __$ -> next = $n$ ; __$ = $n$ ;

}

}

death_star ( & __ ) ; return __ ;

}

Polynomial multiply ( Polynomial om , Polynomial nom ) {

Polynomial __ = NULL , __$ = NULL ; for ( Polynomial $ = om ; $ ; $ = $ -> next ) {

for ( Polynomial $$ = nom ; $$ ; $$ = $$ -> next ) {

Polynomial _ = calloc ( 1 , sizeof ( Term ) ) ; _ -> coef = $ -> coef * $$ -> coef ;

_ -> exp = $ -> exp + $$ -> exp ;

if ( ! __ ) { __ = _ ; __$ = _ ; } else { __$ -> next = _ ; __$ = _ ; }

}

}

death_star ( & __ ) ; return __ ;

}

void omg_ponies ( Polynomial b$e ) {

while ( b$e ) { Polynomial e_ = b$e -> next ; free ( b$e ) ; b$e = e_ ; }

}

void death_star ( Polynomial * s$$s$ ) {

Polynomial l$ = NULL , $p , r ;

for ( Polynomial V =* s$$s$ ; V ; V = V -> next ) {

if ( ! V -> coef ) continue ;

if ( ! l$ ) { l$ = calloc ( 1 , sizeof ( Term ) ) ; l$ -> coef = V -> coef ; l$ -> exp = V -> exp ; }

else { $p = NULL ;

for ( Polynomial h = l$ ; h ; h = h -> next ) {

if ( h -> exp == V -> exp ) { h -> coef += V -> coef ; break ; }

if ( V -> exp > h -> exp ) {

r = calloc ( 1 , sizeof ( Term ) ) ; r -> exp = V -> exp ; r -> coef = V -> coef ;

r -> next = h ; if ( $p ) { $p -> next = r ; } else { l$ = r ; } break ;

}

if ( ! h -> next ) { h -> next = calloc ( 1 , sizeof ( Term ) ) ;

h -> next -> coef = V -> coef ; h -> next -> exp = V -> exp ; break ; }

$p = h ;

}

}

}

omg_ponies ( * s$$s$ ) ;* s$$s$ = l$ ;