include stdio struct member int coef int exp struct member prev struct

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include<stdio.h>
struct member
{
int coef;
int exp;
struct member *prev;
struct member *next;
};
newMember(struct member *currMember)
{
struct member *member;
member=malloc(sizeof(struct member));
currMember->next=member;
currMember->prev=currMember;
currMember=member;
}
scanm(struct member *currMember)
{
int coef,exp,elements,counter;;
struct member *member;
printf("Введите кол-во элементов: "); scanf("%d",&elements);
for (counter=0;counter<elements;counter++)
{
printf("# Элемент №%d\n",counter+1);
printf("\tВведите коэфициент:"); scanf("%d",&coef);
printf("\tВведите степень:"); scanf("%d",&exp);
currMember->coef=coef;
currMember->exp=exp;
member=malloc(sizeof(struct member));
currMember->next=member;
member->prev=currMember;
currMember=member;
currMember->next=0;
}
}
printm(struct member *current)
{
int counter,coef,exp,first=0;
while((current->next!=0))
{
exp=current->exp;
coef=current->coef;
if (coef!=0)
{
if(coef>0)
{
if (first==0) {printf("%dx^%d",coef,exp);first=1;} else printf(" + %dx^%d",coef,exp);
}
else
{
if (first==0) {printf("%dx^%d",coef,exp);first=1;} else printf(" - %dx^%d",coef,exp);
}
}
current=current->next;
}
}
delMember(struct member *currMember)
{
currMember->prev->next=currMember->next;
currMember->next->prev=currMember->prev;
currMember=currMember->prev;
}
simple(struct member *firstMember)
{
int coef,exp;
struct member *current=firstMember;
struct member *tmpcurrent=firstMember;
while((current->next!=0))
{
tmpcurrent=current->next;
while((tmpcurrent->next!=0))
{
if (tmpcurrent->exp==current->exp)
{
current->coef+=tmpcurrent->coef;
delMember(tmpcurrent);
}
tmpcurrent=tmpcurrent->next;
}
current=current->next;
}
}
summ(struct member *chain1, struct member *chain2)
{
struct member *current=chain1;
while((current->next!=0)) current=current->next;
current->next=chain2;
chain2->prev=current;
}
main()
{
struct member firstMember1,firstMember2;
struct member *currMember1=&firstMember1,*currMember2=&firstMember2;
scanm(currMember1);
scanm(currMember2);
simple(&firstMember1);
simple(&firstMember2);
printf("Вы ввели:\nпервый многочлен: ");
printm(&firstMember1);
printf("\nвторой многочлен: ");
printm(&firstMember2);
summ(&firstMember1,&firstMember2);
simple(&firstMember1);
printf("\nСумма введенных многочленов: ");
printm(&firstMember1);
}