我的C程序没有运行

时间:2013-08-31 18:52:37

标签: c

我不是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();
}  

如何让它运行?

3 个答案:

答案 0 :(得分:3)

猜测,您的IDE项目中有多个.c个文件,其中多个文件包含main()个函数。或者 - 如果您的IDE允许 - 您可能不止一次将相同的.c文件添加到项目中。

答案 1 :(得分:1)

通过在.c

之前的每个末尾添加一个唯一的数字字符,将具有相似名称的所有文件的名称更改为同一文件

打开终端并将cwd更改为项目src文件夹的目录。 键入以下命令以编译代码: gcc -W -o polynomial test.c

现在输入以下命令运行代码: ./polynomial

如果您仍然遇到问题,请在此处发布结果

答案 2 :(得分: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;
}