저는 C 애호가가 아니므로, 저는이 일을 마지 못해 쓰는 중입니다. 이 프로그램은 2 개의 다항식을 입력하고 추가하여 표시합니다. 입력 및 표시 모듈을 썼지 만 프로그램이 실행되지 않습니다.내 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();
}
어떻게 실행하나요?
이되어 발생하는 경우 여기를
./polynomial
후 결과를 : 다음을 입력하여 지금
gcc -W -o polynomial test.c
실행 코드를 당신이 얻는 유일한 오류는? 어떻게 든 프로젝트에서 동일한 파일을 두 번 가지고 있습니까? 또는 각각에'main' 함수가있는 여러 소스? –
모든'.c' 모듈을 포함하여 전체 C 프로그램에서'main'을 하나만 가질 수 있습니다. 'main'은 전체 프로그램의 진입 점입니다. 또한'i'는 이미'<5'가 아니기 때문에 메인의'while' 루프는 실행되지 않습니다. 어쩌면 당신은 디버그 목적을 위해 그렇게했다 ... – lurker
'display' 함수는 NULL'polynomial'을 검사하지 않으므로'p' 자체가'NULL' 인 경우'p-> next! = NULL'을 segfault 할 것입니다. . – lurker