00
עדכונים

מנוי במייל

קבלת עדכונים על רשומות חדשות ישירות לתיבת האמייל
יש להזין אימייל תקין על מנת להרשם לעדכונים
ברגעים אלו נשלח אליך אימייל לאישור/ביטול ההרשמה
*שים/י לב, מרגע עשית מנוי, כותב/ת הבלוג יוכל לראות את כתובת האמייל שלך ברשימת העוקבים.
חפש ב בלוג זה
X

{ [ ( בניית עזר ) ] } - מתמטיקה, תכנות, סיכומים לבחינות הבגרות ואקסל

<<<<

 מתמטיקה     ונושאים     נוספים אהרן מוריאלי תורת המספרים: סקירות מתמטיות-היסטוריותמספרי מרסן ערבית מאגר מת"ל-   חומרי למידה
שכיחות אותיות בשפה העבריתפרדוקסים סמנטיים ולוגיים

שפת ++C תרגילים ופתרונות - רשימות מקושרות - מכפלת פולינומים

התכנית בשפת ++C מקבלת כפרמטרים מצביעים לשני פולינומים ומחזירה
מצביע לפולינום המכפלה. כמו כן מדפיסה התכנית את פולינום המכפלה.

נעשה שימוש ברשימות מקושרות על מנת לייצג כל פולינום.

//description:
//- - - - - - -
//this program find the multiply result of two polynoms and prints it.
//first we are given two polynoms by the user, print them and then find the result
//by syoring each polynom in a linked list. each member in the linked list stores
//an item of the polynom.

//- -include and define statements- -
#include <conio.h>
#include <stdio.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <iomanip.h>
#include <math.h>

#define TRUE  1
#define FALSE 0

struct item                           //struct which stores each item of the polynom.
{
float  coefficient;      //coefficient of the item
int    power;            //power of the item
item  *next;             //a pointer to the next item from type item in the linked list.
};

//- -function declartions- - - - - - - - - - -

item   *get_polynom();
item   *make_new_node       (float  coefficient,int power);
item   *mul_items           (struct item poly1,struct item poly2);
void    add_two_items       (struct item *poly1,struct item poly2);
void    print_polynom       (item   *poly);
int     is_smaller          (struct item poly1,struct item poly2);
int     is_equal            (struct item poly1,struct item poly2);

//- -function bodies- - - - - - - - - - -

//function which reads the user's input and creats the linked list by calling "make"new"node' function.
//         each item is inserted to the beginning of the linked list.
//         the users input comes to its end after putting a power which is smaller than 0,
//         namely we finish the input by -1 -1.

item *get_polynom()
{

item *first,*last;
float coefficient;
int   power;

cout<<"enter coefficient and power"<<endl;
cin>>coefficient>>power;

first=make_new_node(coefficient,power);
last=first;

cout<<"enter coefficient and power"<<endl;
cin>>coefficient>>power;

while (power>=0)        //we haven't reach to the finishing item
{                 //building the polynom consisting of the items.
last->next=make_new_node(coefficient,power);
last=last->next;
cout<<"enter coefficient and power"<<endl;
cin>>coefficient>>power;
}

last->next=NULL;      //the last member of the polynom.
return first;         //returning a pointer to the polynom's head.
}//end of function

//function which links the new item, that was sent as a pointer parameter, to the linked list
//         in order to creat a polynom.

{
*head=new_item;            //the new item is linked to the beginning of the list as the first member.
}//end of function

//function which creats a new item which is the multiply result of two items which are given as parameters.
//         the function returns a pointer to the item which stores the result.

item* mul_items     (struct item poly1,struct item poly2)
{
struct item *temp;

temp=new item;                                                //the new result item.
temp->power=(poly1.power)+(poly2.power);                      //multiplying the powers.

return temp;
}//end of function

//function which asks the user for the parameters of the new item and then allocates a new place for him.
//         afterwards we send this item to the "add_item" function in order to add it to the linked list.

{
int  coefficient,power;
item *new_item;

cout<<"\nenter coefficient and power: "<<endl;   //user's input.
cin>>coefficient>>power;

new_item=new item;                  //dynamic allocation.

return FALSE;
new_item->power=power;
new_item->coefficient=coefficient;
return TRUE;                        //good allocation.
}//end of function

//function which sorts the polynom by its powers. the function is built from a nested loop which
//       scans all the polynom's items by bubble sort and checks in each itteration if the
//       first power is smaller from the second by using the "is_smaller" function.
//       if it so, the function replace the values but not the pointers to these items.

{
struct item temp;              //an auxiliary item struct in order to replace values in the bubble sort.
struct item *temp1,*temp2;     //pointers to two items.

for(temp2=temp1->next; temp2!=NULL; temp2=temp2->next)
if (is_smaller(*temp1,*temp2))
{                                     //replacing items' values
temp.power=temp1->power;
temp.coefficient=temp1->coefficient;
temp1->power=temp2->power;
temp1->coefficient=temp2->coefficient;
temp2->power=temp.power;
temp2->coefficient=temp.coefficient;
}
}//end of function

//function which checks if there are items with the same powers in order to add them to each
//         other before the printing.
//         the function is built from a nested loop which scans all the polynom's items by bubble
//       sort and checks in each itteration if there are two items with the same powers.
//       if it so, the function adds the specific items by adding their coefficients,
//         by using the "add_two_items" function. finally, the function frees the second item that was
//         added because we don't need it anymore.

{
struct item *pre,*fore,*temp;

for(pre=temp,fore=temp->next; fore!=NULL; pre=fore, fore=fore->next)
if (is_equal(*temp,*fore))
{
pre->next=fore->next;
free(fore);
fore=pre->next;
if (fore==NULL)
break;
}
}//end of function

//function which gets pointers to two items which will be multiplyed by mulyply each item of the
//         first polynom with all items of the second polynom using the "mul_items" function.
//       the function returns a pointer to the new polynom which stores the multiplying result.

{
struct item *result=NULL;        //pointer to the multiplying result.
struct item *temp1,*temp2;       //the two polynoms to be multyplying.

{
struct item *temp=mul_items((*temp1),(*temp2));
}
return result;
}//end of function

//function which frees from the memory the polynom which represented by a linked list.

{
item *pre, *fore;

{
free(pre);
free(pre);           //frre the last item of the linked list.
}
}//end of function

//function which adds two items with the same powers by adding their coefficients.
void  add_two_items (struct item *poly1,struct item poly2)
{
poly1->coefficient=poly1->coefficient+poly2.coefficient;
}//end of function

//function which checks if the power of the first item is smaller from the power of the second polynom
//       in order to print the polynom after sorting it.

int   is_smaller     (struct item poly1,struct item poly2)
{
if (poly1.power<poly2.power)
return TRUE;
return FALSE;

}//end of function

//function which checks if the power of the first item is equal to the power of the second item
//       in each polynom.

int   is_equal      (struct item poly1,struct item poly2)
{
if (poly1.power==poly2.power)
return TRUE;
return FALSE;
}//end of function

//function which creats a new item element and returns a pointer to this item.

item *make_new_node (float coefficient,int power)
{
item *help;

help=new item;                          //dynamic allocation.
help->coefficient=coefficient;          //entering the values.
help->power=power;
return help;
}//end of function

//function which prints the polynom by using the format "(coefficient*x)^power".

void     print_polynom (item *poly)
{

cout<<endl;                           //if there is a polynom indeed and it is not empty.
if (poly!=NULL)
{
cout<<poly->coefficient<<"x^"<<poly->power;
poly=poly->next;
}

while(poly!=NULL)                      //while we haven't reached to the end of the polynom.
{
if ((poly->coefficient)<0)       //coefficient is negative.
cout<<"-";
else
cout<<"+";                   //coefficient is positive.
cout<<abs(poly->coefficient);
if ((poly->power)!=0)
cout<<"x^"<<poly->power;
poly=poly->next;                 //going to print the next item in the polynom.
}
cout<<endl;
}//end of function

//- -main program- - - - - - - - - - -
void main()
{
//- -variables- - - - - - - -

struct item *p,*q,*r;        //pointers to the head of a polynom from type struct item.

//- body and algorithm- - - - - - - - - -

clrscr();

p=get_polynom();                 //creating the first polynom.
cout<<"\nthe first polynom is : ";
print_polynom(p);                //printing
cout<<endl;
q=get_polynom();                 //creating the second polynom.
cout<<"\nthe second polynom is :";
print_polynom(q);                //printing
cout<<endl;

r=mul_polynoms(p,q);             //creating the multiplying result polynom.
//  print_polynom(r);                //printing
sort(r);                         //sorting the results accordin to its powers.
//  print_polynom(r);
duplicate(r);                    //finding items with the same powers and adding them to each other.
duplicate(r);
duplicate(r);
//  print_polynom(r);                //printing the result.

duplicate(r);

cout<<"the polynom's multiplying result is :\n ";
print_polynom(r);

free_polynom(p);                //free the linked list from the meory.
free_polynom(q);
free_polynom(r);

getch();
} //end of main

הוספת תגובה

שם:
נשארו 150 תוים
נשארו 1500 תוים

תגובה אחת

צור קשר
© כל הזכויות לתוכן המופיע בדף זה שייכות ל ExcelMath91 אלא אם צויין אחרת