Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- struct Polinom {
- int pow;
- float cof;
- Polinom* next;
- };
- void Summ(Polinom** R, Polinom* P, Polinom* Q);
- int read(const char* filePath, Polinom* P);
- void write(const char* filePath, Polinom* R);
- int main() {
- Polinom* P, * Q, * R;
- R = nullptr;
- P = new Polinom;
- Q = new Polinom;
- if (!read("1.txt", P)) // 1.txt - файл с полиномом P в формате a b, a - степень, b - коэффициент
- return 1;
- if (!read("2.txt", Q)) // 2.txt - файл с полиномом Q
- return 1;
- Summ(&R, P, Q); // Вычисление R
- write("result.txt", R);
- return 0;
- }
- void Summ(Polinom** R, Polinom* P, Polinom* Q) {
- if ((P || Q) && !(*R)) {
- *R = new Polinom;
- (*R)->next = nullptr;
- }
- while (P || Q) {
- if (P && Q) {
- if (P->pow > Q->pow) {
- (*R)->pow = P->pow;
- (*R)->cof = P->cof;
- P = P->next;
- (*R) = (*R)->next;
- }
- else if (Q->pow > P->pow) {
- (*R)->pow = Q->pow;
- (*R)->cof = Q->cof;
- Q = Q->next;
- (*R) = (*R)->next;
- }
- else if (Q->cof != -(P->cof)) {
- (*R)->pow = Q->pow;
- (*R)->cof = P->cof + Q->cof;
- P = P->next;
- Q = Q->next;
- (*R) = (*R)->next;
- }
- else {
- P = P->next;
- Q = Q->next;
- }
- }
- else if (P) {
- (*R)->pow = P->pow;
- (*R)->cof = P->cof;
- P = P->next;
- (*R) = (*R)->next;
- }
- else if (Q) { // Только Q не просмотрен
- (*R)->pow = Q->pow;
- (*R)->cof = Q->cof;
- P = P->next;
- (*R) = (*R)->next;
- }
- }
- /* if (P && Q) { // P и Q не просмотрены
- if (P->pow > Q->pow) {
- (*R)->pow = P->pow;
- (*R)->cof = P->cof;
- Summ(&(*R)->next, P->next, Q);
- }
- else if (Q->pow > P->pow) {
- (*R)->pow = Q->pow;
- (*R)->cof = Q->cof;
- Summ(&(*R)->next, P, Q->next);
- }
- else if (Q->cof != -(P->cof)) {
- (*R)->pow = Q->pow;
- (*R)->cof = P->cof + Q->cof;
- Summ(&(*R)->next, P->next, Q->next);
- }
- else {
- Summ(R, P->next, Q->next);
- }
- }
- else if (P) { // Только P не просмотрен
- (*R)->pow = P->pow;
- (*R)->cof = P->cof;
- Summ(&(*R)->next, P->next, Q);
- }
- else if (Q) { // Только Q не просмотрен
- (*R)->pow = Q->pow;
- (*R)->cof = Q->cof;
- Summ(&(*R)->next, P, Q->next);
- }*/
- }
- int read(const char* filePath, Polinom* P) {
- FILE* file;
- errno_t error = fopen_s(&file, filePath, "r");
- if (error) {
- printf("Failed to open the file \"%s\".", filePath);
- return 0;
- }
- int i;
- fscanf_s(file, "%d", &i);
- for (int j = 0; j < i - 1; j++) {
- fscanf_s(file, "%d", &P->pow);
- fscanf_s(file, "%f", &P->cof);
- P->next = new Polinom;
- P = P->next;
- }
- fscanf_s(file, "%d", &P->pow);
- fscanf_s(file, "%f", &P->cof);
- P->next = nullptr;
- fclose(file);
- return 1;
- }
- void write(const char* filePath, Polinom* R) {
- FILE* file;
- errno_t error = fopen_s(&file, filePath, "w+");
- if (error) {
- printf("Failed to open the file \"%s\".", filePath);
- return;
- }
- while (R) {
- fprintf(file, "Power: %d, coefficient: %.3f\n", R->pow, R->cof);
- R = R->next;
- }
- fclose(file);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement