Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS //подключаем необходимые библиотеки
- #include <stdio.h> //ввоод/вывод
- #include <locale.h> //русский язык
- #include <string.h> //функции для работы со строками
- #include <stdlib.h> //выделение памяти
- #include <math.h>
- typedef struct Node {
- int degree;
- int val;
- struct Node* next;
- } Node;
- void PrintPolinomyal(Node* head) {
- Node* p = head;
- if (!p) {
- printf("Пустой многочлен\n");
- return;
- }
- printf("%dx^%d ", p->val, p->degree);
- p = p->next;
- for (; p != NULL; p = p->next) {
- if (p->val > 0) {
- printf("+%dx^%d ", p->val, p->degree);
- }
- else if (p->val < 0) {
- printf("%dx^%d ", p->val, p->degree);
- }
- }
- printf("\n");
- }
- Node* addElement(Node* head, int value, int degree, unsigned int position)
- {
- Node* new_node, * tmp;
- int i = 0;
- new_node = (Node*)malloc(sizeof(Node));
- new_node->degree = degree;
- new_node->val = value;
- new_node->next = NULL;
- if (!head)
- return new_node;
- if (!position)
- {
- new_node->next = head;
- return new_node;
- }
- tmp = head;
- while (tmp->next && i < position - 1)
- {
- tmp = tmp->next;
- i++;
- }
- if (!tmp->next)
- {
- tmp->next = new_node;
- return head;
- }
- new_node->next = tmp->next;
- tmp->next = new_node;
- return head;
- }
- Node* ClearList(Node* L) {
- Node* next, * tmp = L;
- while (tmp)
- {
- next = tmp->next;
- free(tmp);
- tmp = next;
- }
- L = NULL;
- printf("Лист очищен\n");
- }
- Node* deleteElement(Node* head, int position) {
- Node* elm = head, * tmp;
- int i = 0;
- if (!elm){
- printf("List is empty\n\n");
- return head;
- }
- //удаляем голову
- if (!position){
- elm = head->next;
- head->next = NULL;
- free(head);
- return elm;
- }
- while (elm != NULL && i < position - 1){
- elm = elm->next;
- i++;
- }
- //если индекс больше длины списка, ничего не делаем
- if (!elm) {
- return head;
- }
- if (!elm->next) {
- return head;
- }
- tmp = elm->next;
- if ((elm->next)->next){
- elm->next = (elm->next)->next;
- tmp->next = NULL;
- }
- else {
- elm->next = NULL;
- }
- free(tmp);
- return head;
- }
- Node* ChangePolinomyal(Node* L) {
- int ind = 0;
- Node* p = L;
- for (; p != NULL;) {
- if (p->degree % 2 == 0) {
- //printf("%d\n", p->degree);
- L =deleteElement(L, ind);
- //PrintPolinomyal(L);
- p = L;
- ind = 0;
- }
- else{
- ind++;
- p = p->next;
- }
- }
- return L;
- }
- Node* CreatePolinomyal() {
- printf("Введите количество членов в многочлене L:\n");
- int n;
- scanf("%d", &n);
- printf("Почленно введите данные многочлена:\n");
- Node* L = NULL;
- for (int i = 0; i < n; ++i) {
- int tmp_degree, tmp_val;
- scanf("%d%d", &tmp_degree, &tmp_val);
- L = addElement(L, tmp_degree, tmp_val, i);
- }
- return L;
- }
- int main(){
- setlocale(LC_ALL, "rus");
- Node* L;
- L = CreatePolinomyal();
- PrintPolinomyal(L);
- L = ChangePolinomyal(L);
- printf("Измененный многочлен:\n");
- PrintPolinomyal(L);
- ClearList(L);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement