Question
programing in c language
Write a program, using the following functions, that reads two polynomials and prints the polynomials before and after the addition.
a) polyRead
- Read in a polynomial and convert it to its linked list representation. - Return a pointer to the header node of this polynomial.
b) polyWrite
- Output a polynomial using a form that clearly displays it.
c) polyAdd
- Compute c = a + b. Do not change either a or b.
where, a, b, c, are polynomial pointers.
• Test the program with the polynomials given below.
The polynomials are given through the keyboard as inputs.
5x5+7x4-3x3-x2+9[space] x5+7x3+2x2-x[space]
Where, [space] indicates the end of a polynomial.

3/4 4. Write a program, using the following functions, that reads two polynomials and prints the polynomials before and after the addition. a) polyRead - Read in a polynomial and convert t to its linked list representation - Return a pointer to the header node of this polynomial b) poly Write -Output a polynomial using a form that clearly displays it. c) polyAdd Compute c a+ b. Do not change either a or b. where, a, b, c, are polynomial pointers. Test the program with the polynomials given below The polynomials are given through the keyboard as inputs. 5x5+ 7x4-3x3-x2+ 9Ispace] x5+ 7x3+ 2x2-xIspace] Where, [space] indicates the end of a polynomial.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

//

// main.c

// AbcArithExpresssion

//

// Created by Abhirama Gopala Dasa on 08/05/18.

// Copyright © 2018 Abhirama Gopala Dasa. All rights reserved.

//

#include <iostream>

#include "string"

#include "cstring"

#include "cstdlib"

#include "cstdio"

using namespace std;

struct polynomial{

  

char *charPointer;

struct polynomial *next;

};

struct polynomial *head;

int arrayCount=0;

char* removeSpaces(char *arr,int size){

char *newArr = (char*) malloc(sizeof(char)*size);

int i=0;

int count=0;

while(i<100){

if(*(arr+i)!=' '){

newArr[i]=*(arr+i);

++count;

}

i++;

}

newArr[count]='\0';

arrayCount=count;

return newArr;

}

static struct polynomial* makeLinkedList(char *pointer) {

for (int i=0; *(pointer+i)!='\0'; i++) {

struct polynomial *temp = (struct polynomial*)malloc(sizeof(struct polynomial));

temp->next=nullptr;

temp->charPointer = (char*) malloc(sizeof(char));

*(temp->charPointer)=*(pointer+i);

if(head==nullptr){

head= temp;

}else{

struct polynomial *temp1 = head;

while(temp1->next!=nullptr){

temp1 = temp1->next;

}

temp1->next = temp;

}

}

return head;

}

void polyWrite(struct polynomial *pointer,int size){

struct polynomial *temp = pointer;

while(temp!=nullptr){

printf("%c",*(temp->charPointer));

temp=temp->next;

}

}

int getSize(struct polynomial *temp){

struct polynomial *temp1=temp;

int count=0;

if(temp1==nullptr){

return 0;

}

while(temp1!=nullptr){

++count;

temp1=temp1->next;

}

return count;

}

static struct polynomial * polyRead() {

char str[100];

std::string s;

cin>>s;

  

for (int i=0; i<strlen(s.c_str()); i++) {

str[i]=s.c_str()[i];

}

char* pointer =removeSpaces(str, 100);

struct polynomial *temp= makeLinkedList(pointer);

return temp;

}

//question not clear about how to parse polynomials

static struct polynomial * polyAdd(struct polynomial *head){

struct polynomial *temp = head;

int size=getSize(temp);

char arr[size];

int i=0;

while(temp!=nullptr){

arr[i++] = *(temp->charPointer);

temp=temp->next;

}

int j=0;

char newArr[size];

for (int i=0; i<size; i++) {

if(arr[i]=='*'){

char lhs;

char rhs;

if(i-1>0){

lhs = arr[i-1];

}

if(i+1<size){

rhs = arr[i+1];

}

}

}

return nullptr;

}

int main(){

printf("Enter the expression");

struct polynomial * temp = polyRead();

polyWrite(temp, getSize(temp));

  

  

}

Sample output.

Add a comment
Know the answer?
Add Answer to:
programing in c language Write a program, using the following functions, that reads two polynomials and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using C++ language, write a code to implement the "evaluate function" described below using the given...

    Using C++ language, write a code to implement the "evaluate function" described below using the given poly.cpp file and poly.h header file. (ignore the other 3 functions if asked) poly.cpp file: poly.h header file: You are asked to implement four functions for a data structure used to store polynomials. The data structure used for polynomials is a linked list that consists of terms where each term consists of a coefficient, an exponent and a pointer to the next term. A...

  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • please use the c language Assignment 12 The program to write in this assignment is a...

    please use the c language Assignment 12 The program to write in this assignment is a program that calculates score statistics and manages the grades of the students. First, the student list and are given in a file named "student.dat" as in the following: 이성우 77 홍길동 88 scores 201 1710086 2012700091 This program reads the input file "student.dat" and keeps this data in a linear linked list. Each node must be a struct which contains the following: student id...

  • In this lab, using C++, you will create an abstract data type, using a doubly-linked circular...

    In this lab, using C++, you will create an abstract data type, using a doubly-linked circular structure to store the values and links. You must create it by your own and not use any existing containers. You will need a QueueNode. You can use a struct for this, as each node will not have any associated functions. It will have members for data, and the next and previous pointers. Data will be positive integers. There will be no NULL pointers....

  • Please write a c++ header file, class implementation file and main file that does all of...

    Please write a c++ header file, class implementation file and main file that does all of the following and meets the requirements listed below. Also include a Output of your code as to show that your program works and functions properly. EXERCISING A DOUBLY-LINKED LIST CLASS This project consists of two parts, the second of which appears below. For the first part, write a class that implements an unordered list abstract data type using a doubly-linked list with pointers to...

  • using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100....

    using C++ Write a program that: a) Inputs an integer n from the keyboard where n<=100. If n is out of range then print out an error message and ask for another input. This process repeats until a valid value for n is obtained. b) Inputs two 1D arrays of doubles A and B (of size n) from the keyboard. c) Inputs an integer k (from 1 to 3) from the keyboard. d) If k = 1 then it calculates...

  • C++ assignment Write a program that reads a sequence of integers and prints the following: 1....

    C++ assignment Write a program that reads a sequence of integers and prints the following: 1. The number of odd and even numbers of inputs Use a sentinel value to signal the end of inputs. If the sentinel value is the first you enter, give a message “NO input is entered!”. Input Validation: Do not accept a negative number.

  • 1) Write a C program that reads a hexadecimal value from the keyboard and then stores...

    1) Write a C program that reads a hexadecimal value from the keyboard and then stores the value into an unsigned char variable. Read two int values p and n from the keyboard, where the values are less than 8. Implement the following commands: S – sets the n bits starting at position p to 11..1 R – resets the n bits starting at position p to 00…0 F – flips the n bits starting at position p to their...

  • Write a C program named space_to_line.c that features a while loop that continuously reads input from...

    Write a C program named space_to_line.c that features a while loop that continuously reads input from the user one character at a time and then prints that character out. The exception is that if the user inputs a space character (‘ ‘), then a newline character (‘\n’) should be printed instead. This will format the output such that every word the user inputs is on its own line. Other than changing the spaces to newlines, the output should exactly match...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT