标题:指针的指针,传递数组的地址,应该用什么变量接受?
只看楼主
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
结帖率:84.62%
已结贴  问题点数:20 回复次数:2 
指针的指针,传递数组的地址,应该用什么变量接受?
程序代码:
#include<stdio.h>

typedef struct MultiNomial
{
  char data;
  int coef;
  int power;
}MultiNo;

int NA,NB;

void Sort(MultiNo A[],int N)
{
   int i,j;
   int sign = 1;
   for(i = 1; i<N && sign; i++)
    {
      sign = 0;
      for(j = N-1; j>=i; j--)
        {
          if(A[j].power < A[j+1].power)
            {
              A[0] = A[j];
              A[j] = A[j+1];
              A[j+1] = A[0];
              sign = 1;
            }
        }
    }
}

void Print(MultiNo *A,int N)
{
  int i;
  i = 1;
  while(i<=N)
   { 

     if(A[i].coef > 0)
        printf("+%d%c%d",A[i].coef,A[i].data,A[i].power);
     else
        printf("%d%c%d",A[i].coef,A[i].data,A[i].power);
     i++;
   }

 putchar('\n');
}

void Creat(MultiNo *A,int *N)
{
  int i;
   printf("please input N mutilnomials and N<50:\n");
   scanf("%d",&(*N));
   for(i=1; i<=(*N); i++)
    {
      printf("please input the %d datas as 3,x,2: ",i);
      fflush(stdin);
      scanf("%d,%c,%d",&(A[i].coef),&(A[i].data),&(A[i].power));
    }
}

void Add(MultiNo **A,MultiNo B[])
{
   int i,j,k;
   MultiNo C[50];
   i = j = k = 1;
   while(i<=NA && j<=NB)
    {
       if((*A[i]).power > B[j].power)
         C[k++] = *A[i++];
       else if((*A[i]).power < B[j].power)
         C[k++] = B[j++];
       else
        {
          (*A[i++]).coef += B[j++].coef;
          C[k++] = *A[i];
        }
    }
   while(i<=NA)
    C[k++] = *A[i++];
   while(j<=NB)
    C[k++] = B[j++];
   k--;
   for(i=1,j = 1; i<=k; i++)
    {
      if(C[i].coef != 0)
       *A[j++] = C[i];
    }
   NA = j-1;
}


void Sub(MultiNo *A,MultiNo B[])
{
   int i,j,k;
   MultiNo C[50];
   i = j = k = 1;
   for(;i<=NA && j<=NB;)
    {
       if(A[i].power > B[j].power)
         C[k++] = A[i++];
       else if(A[i].power < B[j].power)
         {
           B[j].coef = -B[j].coef;
           C[k++] = B[j++];
         }
       else
        {
          A[i].coef -= B[j++].coef;
          C[k++] = A[i++];
        }
    }
   while(i<=NA)
    C[k++] = A[i++];
   while(j<=NB)
    {
      B[j].coef = -B[j].coef;
      C[k++] = B[j++];
    }
   k--;
   for(i=1,j = 1; i<=k; i++)
    {
      if(C[i].coef != 0)
       { A[j++] = C[i];
         printf("%d%c%d ",C[i].coef,C[i].data,C[i].power);
       }
    }
   NA = j-1;
}

void main()
{
   MultiNo A[50],B[50];
   int i,j;
   char operation;
   Creat(A,&NA);
   Creat(B,&NB);
   Sort(A,NA);
   Sort(B,NB);
   fflush(stdin);
   printf("enter + to add the two multinomial:\nenter - to subtract the two multinomial:\n");
   scanf("%c",&operation);
   switch(operation)
    {
       case '+':Add(A,B);break;
       case '-':Sub(A,B);break;
       default :puts("wrong choice");break;
    }
   Print(A,NA);
   getch();
}

搜索更多相关主题的帖子: color power 
2013-01-05 12:06
chuanglan
Rank: 2
等 级:论坛游民
威 望:2
帖 子:91
专家分:29
注 册:2012-8-14
得分:0 
这是一个结构体数组,然后我向经过子函数的调用后值应该改变,但是现在不能改变,请问我应该用什么类型的指针来接受结构体数组的地址?
2013-01-05 12:07
a151141
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:1
帖 子:197
专家分:680
注 册:2012-10-19
得分:20 
跟一维数组相似;
用个结构体指针接收就行;

世界上幸福的事就是抓到一只羊,更幸福的事就是抓到两只羊……
2013-01-06 20:43



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-395175-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.436552 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved