-2

我不是 C 爱好者,我不情愿地写这篇文章,作为我任务的一部分。该程序是输入两个多项式并将它们相加并显示它们。我编写了模块来输入和显示,但是程序没有运行。

其中Dev-C++说我对 main 有多种定义。

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

我如何让它运行?

4

3 回答 3

3

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

于 2013-08-31T18:54:44.207 回答
1

通过在每个文件的末尾附加一个唯一的数字字符,将具有相似名称的所有文件的名称更改为同一文件.c

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

现在通过键入以下内容运行代码: ./polynomial

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

于 2013-08-31T19:03:21.467 回答
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;
}
于 2013-08-31T19:11:14.777 回答