Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <stdbool.h>
- struct node {
- int coeficiente;
- char letra;
- int expoente;
- struct node *next;
- struct node *previous;
- };
- struct node *head = NULL;
- struct node *current = NULL;
- struct node *prev = NULL;
- char d;
- //display the list
- void print_list(){
- struct node *current = head;
- //struct node *current = (struct node*) malloc(sizeof(struct node));
- while(current != NULL){
- if (current->coeficiente==1 && current->expoente==1 )
- printf("%c\n",current->letra);
- else if(current->coeficiente==1 && current->expoente==1)
- printf("(%c)\n",current->letra);
- else if(current->expoente==1)
- printf("(%d%c)\n",current->coeficiente,current->letra);
- else if(current->coeficiente==1)
- printf("(%c^%d)\n",current->letra,current->expoente);
- else
- printf("(%d%c^%d)\n",current->coeficiente ,current->letra,current->expoente);
- current = current -> next;
- }
- }
- //insert link at the first location
- void insertFirst(int coeficiente,char letra, int expoente) {
- //create a link
- struct node *link = (struct node*) malloc(sizeof(struct node));
- link->coeficiente = coeficiente;
- link->letra = letra;
- link->expoente = expoente;
- //point it to old first node
- link->next = head;
- //point first to new first node
- head = link;
- }
- /*
- //delete first item
- struct node* deleteFirst() {
- //save reference to first link
- struct node *tempLink = head;
- //mark next to first link as first
- head = head->next;
- //return the deleted link
- return tempLink;
- }
- //is list empty
- bool isEmpty() {
- return head == NULL;
- }
- */
- int length() {
- int length = 0;
- struct node *current;
- for(current = head; current != NULL; current = current->next) {
- length++;
- }
- return length;
- }
- void sort() {
- int i, j, k, tempcoeficiente, tempexpoente;
- char tempchar;
- struct node *current;
- struct node *next;
- int size = length();
- k = size ;
- for ( i = 0 ; i < size - 1 ; i++, k-- ) {
- current = head;
- next = head->next;
- for ( j = 1 ; j < k ; j++ ) {
- if ( current->expoente < next->expoente) {
- tempexpoente = current->expoente;
- current->expoente = next->expoente;
- next->expoente = tempexpoente;
- tempcoeficiente = current->coeficiente;
- current->coeficiente = next->coeficiente;
- next->coeficiente = tempcoeficiente;
- tempchar = current->letra;
- current->letra = next->letra;
- next->letra = tempchar;
- }
- current = current->next;
- next = next->next;
- }
- }
- }
- void reverse(struct node** head_ref) {
- struct node* prev = NULL;
- struct node* current = *head_ref;
- struct node* next;
- while (current != NULL) {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- *head_ref = prev;
- }
- void normalize(){
- struct node *current = head;
- int factor_n=current->expoente;
- while(current != NULL){
- if(current->coeficiente%factor_n ==0){
- current->coeficiente=current->coeficiente/factor_n;
- if(current->coeficiente==1)
- printf("%c^%d ",current->letra,current->expoente);
- else
- printf("%d%c^%d ",current->coeficiente,current->letra,current->expoente);
- }
- else
- printf("(%d/%d)%c^%d ",current->coeficiente,factor_n,current->letra,current->expoente);
- current = current -> next;
- }
- }
- //delete a link with given key
- struct node* delete(char letra, int coeficiente) {
- //start from the first link
- struct node* current = head;
- struct node* previous = NULL;
- //if list is empty
- if(head == NULL) {
- return NULL;
- }
- //navigate through list
- while(current->letra != letra && current->coeficiente !=coeficiente) {
- //if it is last node
- if(current->next == NULL) {
- return NULL;
- } else {
- //store reference to current link
- previous = current;
- //move to next link
- current = current->next;
- }
- }
- //found a match, update the link
- if(current == head) {
- //change first to point to next link
- head = head->next;
- } else {
- //bypass the current link
- previous->next = current->next;
- }
- return current;
- // }
- //find a link with given key
- }
- void soma(){
- struct node *current =head;
- while(current->next != NULL ){
- printf("ze\n");
- if(current->letra == current->next->letra && current->expoente == current->next->expoente){
- current->coeficiente = current->coeficiente + current->next->coeficiente;
- current->letra = current->next->letra;
- printf("oi");
- current->expoente = current->next->expoente;
- if(current->next!=NULL)
- current->next = current->next->next;
- //insertFirst(pointer_node->coeficiente,pointer_node->letra,pointer_node->expoente);
- //printf("\n\n%d %c %d\n",pointer_node->coeficiente,pointer_node->letra,pointer_node->expoente );
- }
- else{
- current = current ->next;
- }
- }
- //printf("%c letra %d expoente atual\n", current->letra,current->expoente);
- //struct node *pointer_node =(struct node*) malloc(sizeof(struct node));
- //printf("%c letra %d expoente proximo\n", current->next->letra,current->next->expoente);
- //delete(current->letra,current->coeficiente);
- //printf("%p\n",pointer_node);
- }
- int main() {
- struct node *current =(struct node*) malloc(sizeof(struct node));
- int n,i=0,a,c;
- char b;
- scanf("%d",&n);
- //print list
- while(i<n){
- scanf("%d %c %d",&a ,&b, &c);
- insertFirst(a,b,c);
- reverse(&head);
- i++;
- }
- sort();
- //normalize();
- print_list();
- printf("\n");
- soma();
- print_list();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement