Написать функцию для умножения двух многочленов - C (СИ)

Узнай цену своей работы

Формулировка задачи:

1) Задание: Ввести строку. Вывести пословно вместе со счетчиком повторений этого слова.
Листинг программы
  1. #include <string>
  2. #include <sstream>
  3. #include <vector>
  4. #include <iostream>
  5. using namespace std;
  6. //Ввести строку. Вывести пословно вместе со счетчиком повторений этого слова.
  7. //-----------------------------------------------------------------------------
  8. int main()
  9. {
  10. string str;
  11. cout << "Введите предложение:";
  12. getline(cin, str);
  13. cout << str;
  14. istringstream is(str);
  15. vector<string> array;
  16. string word;
  17. while (is >> word)
  18. {
  19. array.push_back(word); // добавляем в конец вектора по слову пока не кончится предложение
  20. }
  21. int *count = new int[array.size()]; // массив для подсчета слов
  22. for(int i = 0; i < array.size(); i++)
  23. {
  24. count[i] = 0;
  25. }
  26.  
  27. // считаем слова
  28. for(int i = 0; i < array.size(); i++)
  29. {
  30. for(int j = 0; j < array.size(); j++)
  31. {
  32. if(array.at(i) == array.at(j))
  33. count[i]++;
  34. }
  35. }
  36. // выводим
  37. cout << endl << "Вывод: " << endl;
  38. for(int i = 0; i < array.size(); i++)
  39. {
  40. cout << endl << array.at(i) << " " << count[i] << endl;
  41. }
  42. }
2) Ввести n и числа a1, a2,...,an Вычислить и вывести коэффициенты многочлена p(x) = (x+a1)*(x+a1*a2)*...*(x+a1*a2*...*an) ( написать функцию для умножения двух многочленов)
Листинг программы
  1. long int p(int x, int* V, int N, int mul=1, int current=0)
  2. {
  3. if (current < N-1)
  4. return (x+mul*V[current])*p(x, V, N, mul*V[current], current+1);
  5. else
  6. return (x+mul*V[current]);
  7. }
  8. void main()
  9. {
  10. srand(static_cast<unsigned int>(time(0)));
  11. int N = rand()%3+5;
  12. int *V = new int [N];
  13. for (int i=0; i<N; i++)
  14. V[i] = rand()%5+1;
  15. cout << p(10, V, N);
  16. delete [] V;
  17. system("pause");
  18. }
P.S. Если не сложно напишите плиз комменты к прогам, а то я си только начал учить, а до с++ ещё далеко)

Решение задачи: «Написать функцию для умножения двух многочленов»

textual
Листинг программы
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct NODE {
  6.     const char * n_word;
  7.     size_t n_count;
  8.     struct NODE * n_left;
  9.     struct NODE * n_right;
  10. } node_t;
  11.  
  12. int add_node(node_t ** p_tree, const char * wrd) {
  13.     if ( ! *p_tree ) {
  14.         if ( ! ( *p_tree = malloc(sizeof(node_t)) ) )
  15.             return -1;
  16.         (*p_tree)->n_word = wrd;
  17.         (*p_tree)->n_count = 1;
  18.         (*p_tree)->n_left = NULL;
  19.         (*p_tree)->n_right = NULL;
  20.        
  21.         return 0;
  22.     }
  23.     else {
  24.         int diff = strcmp((*p_tree)->n_word, wrd);
  25.         if ( diff == 0 ) {
  26.             (*p_tree)->n_count += 1;
  27.             return 0;
  28.         }
  29.         else
  30.             return ( diff > 0 ) ? add_node(&((*p_tree)->n_left), wrd) : add_node(&((*p_tree)->n_right), wrd);
  31.     }
  32. }
  33.  
  34. void dump_tree(const node_t * tree) {
  35.     if ( tree ) {
  36.         dump_tree(tree->n_left);
  37.         printf("%-30s %3lu\n", tree->n_word, tree->n_count);
  38.         dump_tree(tree->n_right);
  39.     }
  40. }
  41.  
  42. void del_tree(node_t ** p_tree) {
  43.     if ( *p_tree ) {
  44.         del_tree(&((*p_tree)->n_left));
  45.         del_tree(&((*p_tree)->n_right));
  46.         free(*p_tree);
  47.         *p_tree = NULL;
  48.     }
  49. }
  50.  
  51. #define DELIM " \t\n"
  52.  
  53. int main(void) {
  54.     char buf[BUFSIZ];
  55.    
  56.     while ( printf("String: ") && fgets(buf, BUFSIZ, stdin) && *buf != '\n' ) {
  57.         node_t * tree = NULL;
  58.         char * wrd = strtok(buf, DELIM);
  59.         while ( wrd ) {
  60.             if ( add_node(&tree, wrd) ) {
  61.                 fprintf(stderr, "Memory error!\n");
  62.                 exit(1);
  63.             }
  64.             wrd = strtok(NULL, DELIM);
  65.         }
  66.        
  67.         if ( ! tree ) {
  68.             fprintf(stderr, "Empty string!\n");
  69.         }
  70.         else {
  71.             printf("----------------------------------------\nWord                         Count\n----------------------------------------\n");
  72.             dump_tree(tree);
  73.             del_tree(&tree);
  74.             printf("----------------------------------------\n\n");
  75.         }
  76.     }
  77.    
  78.     return 0;
  79. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

8   голосов , оценка 4.25 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут
Похожие ответы