2013-08-31 4 views
-2

저는 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(); 
} 

어떻게 실행하나요?

+0

이되어 발생하는 경우 여기를 ./polynomial

후 결과를 : 다음을 입력하여 지금 gcc -W -o polynomial test.c

실행 코드를 당신이 얻는 유일한 오류는? 어떻게 든 프로젝트에서 동일한 파일을 두 번 가지고 있습니까? 또는 각각에'main' 함수가있는 여러 소스? –

+0

모든'.c' 모듈을 포함하여 전체 C 프로그램에서'main'을 하나만 가질 수 있습니다. 'main'은 전체 프로그램의 진입 점입니다. 또한'i'는 이미'<5'가 아니기 때문에 메인의'while' 루프는 실행되지 않습니다. 어쩌면 당신은 디버그 목적을 위해 그렇게했다 ... – lurker

+0

'display' 함수는 NULL'polynomial'을 검사하지 않으므로'p' 자체가'NULL' 인 경우'p-> next! = NULL'을 segfault 할 것입니다. . – lurker

답변

3

IDE 프로젝트에는 여러 개의 .c 파일이 있으며 둘 중 하나는 main() 기능을 포함하고 있습니다. 또는 IDE에서 허용하는 경우 동일한 .c 파일을 두 번 이상 프로젝트에 추가했을 수 있습니다.

+0

예, 메인에 다른 테스트 .c 파일이 있습니다. 그건 허용되지 않습니까? 죄송합니다, 저는 자바 프로그래머입니다. –

+0

@LittleChild : 귀하의 IDE가 프로젝트를 관리하는 방법을 모르겠지만 일반적으로 프로젝트 당 하나의'main()'이 있으며 프로젝트의 모든 내용이 함께 컴파일되고 연결됩니다. – NPE

+0

내 과제에'Dev-C++'을 사용하고 싶습니다. 'main()'으로 여러 파일들을 어떻게 관리 할 수 ​​있습니까? 내 말은, 모든 프로그램에 대해 여러 프로젝트를 만들고 싶지 않다는 것입니다. –

1

변경 .c

하기 전에 각각의 끝에 고유 한 숫자 문자를 추가하여이 같은 파일과 유사한 이름을 가진 모든 파일의 이름은 터미널을 열고 프로젝트의 디렉토리로 CWD 변경 src 폴더. 유형의 코드를 컴파일하려면 다음 명령을 : 당신은 여전히 ​​문제가

1
#include <stdio.h> 
#include <stdlib.h> 
#pragma DONT 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 { 
    struct term *next; 
    int exp; 
    int coef; 
}; 

struct term *addTerm(struct term **polynomial, int exp, int coef){ // adds a term to polynomial 

    struct term *newTerm; 
    newTerm = malloc(sizeof *newTerm); 
    newTerm->exp = exp; 
    newTerm->coef = coef; 

#if APPEND_AT_TAIL 
    for (; *polynomial;polynomial = &(*polynomial)->next) {;} 
    newTerm->next = NULL; 
#else 
    newTerm->next = *polynomial; 
#endif 
    *polynomial = newTerm ; 

    return newTerm; 
} 

void display(struct term *polynomial){ // displays the polynomial 
    struct term *p; 
    for(p = polynomial; p; p = p->next){ 
     printf("+ {%d * %d}", p->coef, p->exp); 
    } 
} 

int main(void){ // run it 
    int i ; 
    int coef = 0; 
    int exp = 0; 
    struct term *polynomial = NULL; 

    for(i=0; i < 5; i++){ 
     printf("Enter CoEfficient and Exponent for Term %d> ", i); 
     scanf("%d %d",&coef,&exp); 
     addTerm(&polynomial, exp, coef); 
    } 
    display(polynomial); 
    // getch(); 
    return 0; 
} 
관련 문제