Created
November 22, 2010 23:33
-
-
Save gurkanoluc/710933 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
struct NODE { | |
int factor; | |
int power; | |
struct NODE *next; | |
}; | |
int main(void) { | |
struct NODE *llist; | |
struct NODE *llist_1; | |
struct NODE *llist_2; | |
int choice; | |
while(1) | |
{ | |
printf("Yapilacak islemi seciniz : \n"); | |
printf("----------------------------------\n"); | |
printf("(1) Polinom Oluştur\n"); | |
printf("(2) Polinom Topla\n"); | |
printf("(3) Polinom Çarp\n"); | |
printf("----------------------------------\n"); | |
printf("Seçim yapmak için seçeneklerin karşılarındaki rakamları yazınız\n"); | |
printf("Çıkmak için -1'e basınız\n"); | |
printf("----------------------------------\n"); | |
scanf("%d", &choice); | |
if(choice == 1) | |
{ | |
llist = (struct NODE *) malloc(sizeof (struct NODE)); | |
read_poly(llist); | |
free(llist); | |
} | |
else if(choice == 2) | |
{ | |
llist_1 = (struct NODE *) malloc(sizeof (struct NODE)); | |
llist_2 = (struct NODE *) malloc(sizeof (struct NODE)); | |
sum_polies(llist_1, llist_2); | |
free(llist_1); | |
free(llist_2); | |
} | |
else if(choice == 3) | |
{ | |
llist_1 = (struct NODE *) malloc(sizeof (struct NODE)); | |
llist_2 = (struct NODE *) malloc(sizeof (struct NODE)); | |
multiply_polies(llist_1, llist_2); | |
free(llist_1); | |
free(llist_2); | |
} | |
else | |
{ | |
printf("Çıkış yapılıyor..\n"); | |
printf("Bye!\n"); | |
break; | |
} | |
} | |
return (0); | |
} | |
// Polinomları toplar | |
void sum_polies(struct NODE *llist_1, struct NODE *llist_2) | |
{ | |
int factor, power; | |
// 1. polinomu oluştur | |
printf("---------------------------\n"); | |
printf("1. Polinom için değerleri giriniz\n"); | |
printf("---------------------------\n"); | |
while(1) | |
{ | |
factor = power = 0; | |
scanf("%d %d", &factor, &power); | |
if(factor == -1 && power == -1) | |
{ | |
break; | |
} | |
else | |
{ | |
append_node(llist_1, factor, power); | |
} | |
} | |
// 2. polinomu oluştur | |
printf("---------------------------\n"); | |
printf("2. Polinom için değerleri giriniz\n"); | |
printf("---------------------------\n"); | |
while(1) | |
{ | |
factor = power = 0; | |
scanf("%d %d", &factor, &power); | |
if(factor == -1 && power == -1) | |
{ | |
break; | |
} | |
else | |
{ | |
append_node(llist_2, factor, power); | |
} | |
} | |
printf("Toplama işlemi yapılıyor..\n"); | |
// 2. polinomu 1. polinoma ekle | |
while(llist_2->next != NULL) | |
{ | |
append_node(llist_1, llist_2->factor, llist_2->power); | |
llist_2 = llist_2->next; | |
} | |
// 2. polinom'un son elemanını 1. polinoma ekle | |
append_node(llist_1, llist_2->factor, llist_2->power); | |
// Üssü aynı olan ifadeleri birleştir, katsayısı 0 olanları çıkart | |
organize_poly(llist_1); | |
// Polinomu ekrana bas | |
print_poly(llist_1); | |
} | |
// Polinomları çarpar | |
void multiply_polies(struct NODE *llist_1, struct NODE *llist_2) | |
{ | |
int factor, power; | |
// 1. polinomu oluştur | |
printf("---------------------------\n"); | |
printf("1. Polinom için değerleri giriniz\n"); | |
printf("---------------------------\n"); | |
while(1) | |
{ | |
factor = power = 0; | |
scanf("%d %d", &factor, &power); | |
if(factor == -1 && power == -1) | |
{ | |
break; | |
} | |
else | |
{ | |
append_node(llist_1, factor, power); | |
} | |
} | |
// 2. polinomu oluştur | |
printf("---------------------------\n"); | |
printf("2. Polinom için değerleri giriniz\n"); | |
printf("---------------------------\n"); | |
while(1) | |
{ | |
factor = power = 0; | |
scanf("%d %d", &factor, &power); | |
if(factor == -1 && power == -1) | |
{ | |
break; | |
} | |
else | |
{ | |
append_node(llist_2, factor, power); | |
} | |
} | |
printf("Çarpma işlemi yapılıyor..\n"); | |
// Sonuç değerlerini tutacak struct | |
struct NODE *llist_3 = (struct NODE *) malloc(sizeof (struct NODE)); | |
// 2. polinom üzerinde gezinmeyi sağlayaca struct | |
struct NODE *temp = (struct NODE *) malloc(sizeof (struct NODE)); | |
// 1. polinomdaki her elemanı 2. polinomdaki her eleman ile çarp | |
while(llist_1->next != NULL) | |
{ | |
// Döngünün her dönüşünde 2. polinom'u geçici bir değişkene ata | |
*temp = *llist_2; | |
while(temp->next != NULL) | |
{ | |
// Katsayı çarpımı ve üstlerin toplamından oluşan ifadeyi 3. list'e ekle | |
append_node(llist_3, temp->factor * llist_1->factor, temp->power + llist_1->power); | |
*temp = *temp->next; | |
} | |
// 2. polinom'un son elemanı için çarpma işlemi yap | |
append_node(llist_3, temp->factor * llist_1->factor, temp->power + llist_1->power); | |
llist_1 = llist_1->next; | |
} | |
// 1. polinom'un son elemanı için çarpma işlemi | |
*temp = *llist_2; | |
while(temp->next != NULL) | |
{ | |
append_node(llist_3, temp->factor * llist_1->factor, temp->power + llist_1->power); | |
*temp = *temp->next; | |
} | |
append_node(llist_3, temp->factor * llist_1->factor, temp->power + llist_1->power); | |
// Üssü aynı olan ifadeleri birleştir, katsayısı 0 olanları çıkart | |
organize_poly(llist_3); | |
// Sonuç polinomunu ekrana bas | |
print_poly(llist_3); | |
} | |
// Polinomdaki aynı üslü olan elemanları topla | |
// Kat sayısı 0 olan elemanları çıkart | |
void organize_poly(struct NODE *llist) | |
{ | |
struct NODE *temp = (struct NODE *) malloc(sizeof (struct NODE)); | |
while(llist->next != NULL) | |
{ | |
if(llist->factor != 0 && llist->power != 0) | |
{ | |
temp = llist->next; | |
while(temp->next != NULL) | |
{ | |
if(llist->power == temp->power && temp->power != 0) | |
{ | |
llist->factor += temp->factor; | |
// Dirty hack for delete | |
temp->power = 0; | |
temp->factor = 0; | |
} | |
temp = temp->next; | |
} | |
if(llist->power == temp->power && temp->power != 0) | |
{ | |
llist->factor += temp->factor; | |
// Dirty hack for delete | |
temp->power = 0; | |
temp->factor = 0; | |
} | |
} | |
llist = llist->next; | |
} | |
temp = (struct NODE *) malloc(sizeof (struct NODE)); | |
while(llist->next != NULL) | |
{ | |
if(llist->factor != 0) | |
{ | |
append_node(temp, llist->factor, llist->power); | |
} | |
} | |
llist = temp; | |
} | |
// Polinomu okur ve ekrana basar | |
void read_poly(struct NODE *llist) | |
{ | |
int factor, power; | |
while(1) | |
{ | |
scanf("%d %d", &factor, &power); | |
if(factor == -1 && power == -1) | |
{ | |
break; | |
} | |
else | |
{ | |
append_node(llist, factor, power); | |
} | |
} | |
organize_poly(llist); | |
print_poly(llist); | |
} | |
// Polinomu ekrana basar | |
void print_poly(struct NODE *llist) | |
{ | |
while (llist->next != NULL) { | |
if(llist->factor < 0) | |
{ | |
if(llist->power == 0) | |
printf("- %d ", -1 * llist->factor); | |
else | |
printf("- %dx^%d ", -1 * llist->factor, llist->power); | |
} | |
else if(llist->factor > 0) | |
{ | |
if(llist->power == 0) | |
printf("+ %d ", llist->factor, llist->power); | |
else | |
printf("+ %dx^%d ", llist->factor, llist->power); | |
} | |
llist = llist->next; | |
} | |
if(llist->factor < 0) | |
{ | |
if(llist->power == 0) | |
printf("- %d ", -1 * llist->factor); | |
else | |
printf("- %dx^%d ", -1 * llist->factor, llist->power); | |
} | |
else if(llist->factor > 0) | |
{ | |
if(llist->power == 0) | |
printf("+ %d ", llist->factor); | |
else | |
printf("+ %dx^%d ", llist->factor, llist->power); | |
} | |
printf("\n"); | |
} | |
// Listeye eleman ekle | |
void append_node(struct NODE *llist, int factor, int power) { | |
// Listedeki son nodu bul | |
while (llist->next != NULL) | |
llist = llist->next; | |
// Bu nod'un next'i için bellekte yer ayır ve bu ayrılan yere struct'ı koy | |
llist->next = (struct NODE *) malloc(sizeof (struct NODE)); | |
llist->next->factor = factor; | |
llist->next->power = power; | |
llist->next->next = NULL; | |
} | |
// Listeden eleman silme | |
void delete_node(struct NODE *llist) { | |
struct NODE *temp; | |
temp = (struct NODE *) malloc(sizeof (struct NODE)); | |
// Silinen elemanın yerine onun next'i olan elemanı koy | |
*temp = *llist->next; | |
free(llist); | |
*llist = *temp; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment