Написать функцию для умножения двух многочленов - C (СИ)
Формулировка задачи:
1)
Задание: Ввести строку. Вывести пословно вместе со счетчиком повторений
этого слова.
2)
Ввести n и числа a1, a2,...,an
Вычислить и вывести коэффициенты многочлена
p(x) = (x+a1)*(x+a1*a2)*...*(x+a1*a2*...*an)
( написать функцию для умножения двух многочленов)
P.S. Если не сложно напишите плиз комменты к прогам, а то я си только начал учить, а до с++ ещё далеко)
Листинг программы
- #include <string>
- #include <sstream>
- #include <vector>
- #include <iostream>
- using namespace std;
- //Ввести строку. Вывести пословно вместе со счетчиком повторений этого слова.
- //-----------------------------------------------------------------------------
- int main()
- {
- string str;
- cout << "Введите предложение:";
- getline(cin, str);
- cout << str;
- istringstream is(str);
- vector<string> array;
- string word;
- while (is >> word)
- {
- array.push_back(word); // добавляем в конец вектора по слову пока не кончится предложение
- }
- int *count = new int[array.size()]; // массив для подсчета слов
- for(int i = 0; i < array.size(); i++)
- {
- count[i] = 0;
- }
- // считаем слова
- for(int i = 0; i < array.size(); i++)
- {
- for(int j = 0; j < array.size(); j++)
- {
- if(array.at(i) == array.at(j))
- count[i]++;
- }
- }
- // выводим
- cout << endl << "Вывод: " << endl;
- for(int i = 0; i < array.size(); i++)
- {
- cout << endl << array.at(i) << " " << count[i] << endl;
- }
- }
Листинг программы
- long int p(int x, int* V, int N, int mul=1, int current=0)
- {
- if (current < N-1)
- return (x+mul*V[current])*p(x, V, N, mul*V[current], current+1);
- else
- return (x+mul*V[current]);
- }
- void main()
- {
- srand(static_cast<unsigned int>(time(0)));
- int N = rand()%3+5;
- int *V = new int [N];
- for (int i=0; i<N; i++)
- V[i] = rand()%5+1;
- cout << p(10, V, N);
- delete [] V;
- system("pause");
- }
Решение задачи: «Написать функцию для умножения двух многочленов»
textual
Листинг программы
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct NODE {
- const char * n_word;
- size_t n_count;
- struct NODE * n_left;
- struct NODE * n_right;
- } node_t;
- int add_node(node_t ** p_tree, const char * wrd) {
- if ( ! *p_tree ) {
- if ( ! ( *p_tree = malloc(sizeof(node_t)) ) )
- return -1;
- (*p_tree)->n_word = wrd;
- (*p_tree)->n_count = 1;
- (*p_tree)->n_left = NULL;
- (*p_tree)->n_right = NULL;
- return 0;
- }
- else {
- int diff = strcmp((*p_tree)->n_word, wrd);
- if ( diff == 0 ) {
- (*p_tree)->n_count += 1;
- return 0;
- }
- else
- return ( diff > 0 ) ? add_node(&((*p_tree)->n_left), wrd) : add_node(&((*p_tree)->n_right), wrd);
- }
- }
- void dump_tree(const node_t * tree) {
- if ( tree ) {
- dump_tree(tree->n_left);
- printf("%-30s %3lu\n", tree->n_word, tree->n_count);
- dump_tree(tree->n_right);
- }
- }
- void del_tree(node_t ** p_tree) {
- if ( *p_tree ) {
- del_tree(&((*p_tree)->n_left));
- del_tree(&((*p_tree)->n_right));
- free(*p_tree);
- *p_tree = NULL;
- }
- }
- #define DELIM " \t\n"
- int main(void) {
- char buf[BUFSIZ];
- while ( printf("String: ") && fgets(buf, BUFSIZ, stdin) && *buf != '\n' ) {
- node_t * tree = NULL;
- char * wrd = strtok(buf, DELIM);
- while ( wrd ) {
- if ( add_node(&tree, wrd) ) {
- fprintf(stderr, "Memory error!\n");
- exit(1);
- }
- wrd = strtok(NULL, DELIM);
- }
- if ( ! tree ) {
- fprintf(stderr, "Empty string!\n");
- }
- else {
- printf("----------------------------------------\nWord Count\n----------------------------------------\n");
- dump_tree(tree);
- del_tree(&tree);
- printf("----------------------------------------\n\n");
- }
- }
- return 0;
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д