c언어로 다항식 덧셈 만들기 도와주세요

jmk101711의 이미지

#define _CRT_SECURE_NO_WARNINGS
#include
#include
#define SWAP(x,y,t) ((t)=(x),(x)=(y),(y)=(t))
#define COMPARE(x,y) (((x)<(y)) ? -1:((x)==(y))? 0:1)
//coef 계수
// expon 차수
typedef struct {
float coef;
int exp;
} polynomial;
void sort(polynomial list[], int n) {
int i, j, min, temp;
for (i = 0; i < n - 1; i++) {
min = i;
for (j = i + 1; j < n; j++)
if (list[j].exp < list[min].exp)
min = j;
SWAP(list[i].exp, list[min].exp, temp);
}
}
int size(polynomial* A) {
int Aindex = 0;
int Asize = 0;
while ((A[Aindex].exp != -1)) {
Aindex++;
}
return Aindex;
}
polynomial padd(polynomial* A, polynomial* B) {
polynomial* D = (polynomial*)malloc(sizeof(polynomial) * 10);
int Aindex = 0;
int Bindex = 0;
int Dindex = 0;
while ((Aindex == size(A)) && (Bindex == size(B))) {
switch (COMPARE(A[Aindex].exp, B[Bindex].exp))
{
//B가 A보다 큰경우
case -1:
D[Dindex].coef = B[Bindex].coef;
D[Dindex].exp = B[Bindex].exp;
Bindex++;
Dindex++;
break;
//B와 A가 같은경우
case 0:D[Dindex].coef = A[Aindex].coef + B[Bindex].coef;
D[Dindex].exp = A[Aindex].exp + B[Bindex].exp;
Aindex++;
Bindex++;
Dindex++;
break;
//A가 B보다 큰경우
case 1:
D[Dindex].coef = A[Aindex].coef;
D[Dindex].exp = A[Aindex].exp;
Aindex++;
Dindex++;
break;
}
//남은 A항들 넣기
for (; Aindex <=size(A);) {
D[Dindex].coef = A[Aindex].coef;
D[Dindex].exp = A[Aindex].exp;
Aindex++;
Dindex++;
}
//남은 B항들 넣기
for (; Bindex <= size(B);) {
D[Dindex].coef = B[Bindex].coef;
D[Dindex].exp = B[Bindex].exp;
Bindex++;
Dindex++;
}
}
return *D;
}

int main() {
printf("3.1 다항식 생성\n");
polynomial* A;
A = (polynomial*)malloc(sizeof(polynomial) * 10);
for (int i = 0; i < 10; i++) {
printf("A의 항을 입력하세요. (coef(실수) expon(정수)): ");
scanf_s("%f", &A[i].coef); scanf_s("%d", &A[i].exp);
if (((A[i].coef) == -1) && ((A[i].exp) == -1)) break;
}
int i = 0;
printf("coef expon\n");
while (1) {
printf("%.3f %d\n", A[i].coef, A[i].exp);
i++;
if (((A[i].coef) == -1) && ((A[i].exp) == -1)) break;
}
polynomial* B;
B = (polynomial*)malloc(sizeof(polynomial) * 10);
for (int j = 0; j < 10; j++) {
printf("B의 항을 입력하세요. (coef(실수) expon(정수)): ");
scanf_s("%f", &B[j].coef); scanf_s("%d", &B[j].exp);
if (((B[j].coef) == -1) && ((B[j].exp) == -1)) break;
}
int j = 0;
printf("coef expon\n");
while (1) {
printf("%.3f %d\n", B[j].coef, B[j].exp);
j++;
if (((B[j].coef) == -1) && ((B[j].exp) == -1)) break;
}
printf("3.2 다항식 덧셈\n");
polynomial* D;
int d = 0;
D = (polynomial*)malloc(sizeof(polynomial) * 10);

*D = padd(A, B);
printf("coef expon\n");
while (d==size(D))
{
printf("%.3f %d\n", D[d].coef, D[d].exp);
d++;
}
}

이렇게 프로그램을 짜봣는데 덧셈값이 안나오네요 ㅜㅜ

세벌의 이미지

#include <stdio.h>
 
struct Polynomial {
  int coeff;
  int exp;
};
 
struct Polynomial first[15], second[15], result[15];
 
void display (struct Polynomial poly[], int terms)
{
  int i;
  printf ("\n");
  for (i = 0; i < terms - 1; i++) {
    printf ("%dX^%d+ ", poly[i].coeff, poly[i].exp);
  }
  printf ("%dX^%d ", poly[i].coeff, poly[i].exp);
}
 
int readExpression (struct Polynomial poly[])
{
  int terms, i;
  printf ("\nNumber of terms: ");
  scanf ("%d", &terms);
  printf ("\nEnter the coeffecients and exponents in DESCENDING order");
  for (i = 0; i < terms; i++) {
    printf ("\nCoeffecient :");
    scanf ("%d", &poly[i].coeff);
    printf ("Exponent :");
    scanf ("%d", &poly[i].exp);
  }
  return terms;
}
 
int addExpressions (int firstCount, int secondCount)
{
  int i, j, k;
  i = 0;
  j = 0;
  k = 0;
  while (i < firstCount && j < secondCount) {
    if (first[i].exp == second[j].exp) {
      result[k].coeff = first[i].coeff + second[j].coeff;
      result[k].exp = first[i].exp;
      i++;
      j++;
      k++;
    }
    else if (first[i].exp > second[j].exp) {
      result[k].coeff = first[i].coeff;
      result[k].exp = first[i].exp;
      i++;
      k++;
    }
    else {
      result[k].coeff = second[i].coeff;
      result[k].exp = second[j].exp;
      j++;
      k++;
    }
  }
 
  while (i < firstCount) {
    result[k].coeff = first[i].coeff;
    result[k].exp = first[i].exp;
    k++;
    i++;
  }
 
  while (j < secondCount) {
    result[k].coeff = second[j].coeff;
    result[k].exp = second[j].exp;
    k++;
    j++;
  }
  return k;
}
 
int main ()
{
  int firstCount, secondCount, resultCount;
  printf ("\nFirst Expression:\n");
  firstCount = readExpression (first);
  printf ("\nSecond Expression:\n");
  secondCount = readExpression (second);
  printf ("\nFirst Expression");
  display (first, firstCount);
  printf ("\nSecond Expression");
  display (second, secondCount);
  resultCount = addExpressions (firstCount, secondCount);
  printf ("\nResultant Expression:\n");
  display (result, resultCount);
  return 0;
}

참고
https://gist.github.com/jamesgeorge007/e05e538d76a78e1dea623237284e82b4

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.