Я не энтузиаст С, и я пишу это, неохотно, как часть моего задания. Программа предназначена для ввода двух полиномов и их добавления и отображения. Я писал модули для ввода и отображения, однако программа не запускается.Моя программа на C не работает
В Dev-C++
говорится, что у меня есть несколько определений основного.
#include<stdio.h>
#include<conio.h>
// This is my implementation to add and multiply
// two polynomials using linked list
// So far, it just inputs and displays the polynomial
struct term {
int exp;
int coef;
struct term *next;
};
struct term* addTerm(struct term *polynomial,int exp,int coef){ // adds a term to polynomial
if(polynomial == NULL){
polynomial = (struct term *)malloc(sizeof(struct term));
polynomial->exp = exp;
polynomial->coef = coef;
}else{
struct term *newTerm = (struct term *)malloc(sizeof(struct term));
newTerm->exp = exp;
newTerm->coef = coef;
polynomial->next = newTerm;
}
return polynomial;
}
void display(struct term *polynomial){ // displays the polynomial
struct term *p = polynomial;
while(p->next != NULL){
printf("+ %dx%d",p->coef,p->exp); p = p->next;
}
}
void main(){ // run it
int i = 5;
int coef = 0;
int exp = 0;
struct term *polynomial = NULL;
while(i++ < 5){
printf("Enter CoEfficient and Exponent for Term %d",i);
scanf("%d %d",&coef,&exp);
polynomial = addTerm(polynomial,exp,coef);
}
display(polynomial);
getch();
}
Как его запустить?
ли что * только * ошибка вы получаете? Вы каким-то образом имеете один и тот же файл в своем проекте дважды? Или несколько источников с функцией 'main' в каждом? –
У вас может быть только один «главный» во всей программе на C, включая все модули '.c'. 'main' является точкой входа для всей программы. Кроме того, цикл while в main не будет выполняться, так как 'i' уже не' <5'. Возможно, вы сделали это для целей отладки ... – lurker
Ваша функция 'display' не проверяет многочлен NULL', поэтому он будет segfault в 'p-> next! = NULL', если' p' сам является 'NULL' , – lurker