永发信息网

C语言编程题,运行不了,大家给我看一下

答案:4  悬赏:70  手机版
解决时间 2021-02-23 13:29
  • 提问者网友:一抹荒凉废墟
  • 2021-02-22 14:02
#include
#include

typedef struct Lnode
{
int e;
double c;
struct Lnode *next;

}Lnode,*linklist;

linklist creat_linklist()
{
linklist l=NULL;
Lnode *s;
int i,n,e;
double c;

printf("\n");
printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("Enter %d c,e:",i);
scanf("%lf%d",&c,&e);
s=(Lnode*)malloc(sizeof(Lnode));
s->c=c;
s->e=e;
s->next=l;
l=s;
}

while(l)
{
printf("%.2f^%d ",l->c,l->e);
l=l->next;
}

return l;
}

linklist count (linklist l1,linklist l2)
{
Lnode *s,*l;
double x;
char ch;

l=(Lnode*)malloc(sizeof(Lnode));

while(l1&&l2)
{
printf("请选择符号:\n+\n-\n");
scanf(" %c",&ch);

switch(ch)
{
case '+':

if(l1->e==l2->e)
{
x=l1->c+l2->c;
}

else if(l1->e>l2->e)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l2->e;
s->c=l2->c;
s->next=l;
l=s;
l2=l2->next;
}

else
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l1->e;
s->c=l1->c;
s->next=l;
l=s;
l1=l1->next;
}

break;
case '-':
if(l1->e==l2->e)
{
x=l1->c-l2->c;
l1=l1->next;
l2=l2->next;
}

else if(l1->e>l2->e)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l2->e;
s->c=(-1)*l2->c;
s->next=l;
l=s;
l2=l2->next;
}

else
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l1->e;
s->c=l1->c;
s->next=l;
l=s;
l1=l1->next;
}

break;
default:
printf("Uknown operator\n");
}

if(x!=0)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l1->e;
s->c=x;
s->next=l;
l=s;

l1=l1->next;
l2=l2->next;
}

while(l1!=NULL)
{
s=(Lnode*)malloc(sizeof(Lnode));
s=l1;
l1=l1->next;
s->next=l;
l=s;
}

while(l2!=NULL)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l2->e;

if(ch='-')
{
s->c=(-1)*l2->c;
}

else
{
s->c=l2->c;
}

l2=l2->next;
s->next=l;
l=s;
}
}
return l;
}

void swap(linklist p,linklist q)
{
double m;
int n;
m=p->c;
p->c=q->c;
q->c=m;

n=p->e;
p->e=q->e;
q->e=n;
}

void sort(linklist l)
{
Lnode *p,*q;
p=l;
if(p==NULL)
return;
while(p)
{
q=p;
while(q)
{
if(q->ee)
swap(q,p);
else q=q->next;
}
p=p->next;
}
}

int main(void)
{
Lnode *l1,*l2;
linklist l,s;
l1=(Lnode*)malloc(sizeof(Lnode));
l2=(Lnode*)malloc(sizeof(Lnode));
l1=creat_linklist();
l2=creat_linklist();
l=count(l1,l2);
sort(l);
s=l;

while(s)
{
printf("%.2f^%d ",s->c,s->e);
s=s->next;
}

printf("\n");
return 0;
}

说详细点
最佳答案
  • 五星知识达人网友:往事隔山水
  • 2021-02-22 14:20
表示蛋疼!程序的作用是什么?看了半天,没看明白!改了一些,主要是尾指针指空的问题。在做“+”、“-”之前的应该都没问题了,问题是你后面的逻辑太混乱了,建议你画画流程图。
#include
#include

typedef struct Lnode
{
int e;
double c;
struct Lnode *next;

}Lnode,*linklist;

linklist creat_linklist()
{
linklist l=NULL;
Lnode *s;
int i,n,e;
double c;

printf("\n");
printf("Enter n:");
scanf("%d",&n);

for(i=1;i<=n;i++)
{
printf("Enter %d c,e:",i);
scanf("%lf%d",&c,&e);
s=(Lnode*)malloc(sizeof(Lnode));
s->c=c;
s->e=e;
s->next=l;
l=s;
}
//真笨,这样 l 又指空了
s=l;
while(s)
{
printf("%.2f^%d ",s->c,s->e);
s=s->next;
}

return l;
}

linklist count (linklist l1,linklist l2)
{
Lnode *s,*l;
double x;
char ch;

//l=(Lnode*)malloc(sizeof(Lnode));//尾结点,直接指空
l=NULL;

while(l1&&l2)
{
printf("请选择符号:\n+\n-\n");
scanf(" %c",&ch);

switch(ch)
{
case '+':

if(l1->e==l2->e)
{
x=l1->c+l2->c;
}

else if(l1->e>l2->e)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l2->e;
s->c=l2->c;
s->next=l;
l=s;
l2=l2->next;
}

else
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l1->e;
s->c=l1->c;
s->next=l;
l=s;
l1=l1->next;
}

break;
case '-':
if(l1->e==l2->e)
{
x=l1->c-l2->c;
l1=l1->next;
l2=l2->next;
}

else if(l1->e>l2->e)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l2->e;
s->c=(-1)*l2->c;
s->next=l;
l=s;
l2=l2->next;
}

else
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l1->e;
s->c=l1->c;
s->next=l;
l=s;
l1=l1->next;
}

break;
default:
printf("Uknown operator\n");
}

if(x!=0)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l1->e;
s->c=x;
s->next=l;
l=s;

l1=l1->next;
l2=l2->next;
}

while(l1!=NULL)
{
s=(Lnode*)malloc(sizeof(Lnode));
s=l1;
l1=l1->next;
s->next=l;
l=s;
}

while(l2!=NULL)
{
s=(Lnode*)malloc(sizeof(Lnode));
s->e=l2->e;

if(ch='-')
{
s->c=(-1)*l2->c;
}

else
{
s->c=l2->c;
}

l2=l2->next;
s->next=l;
l=s;
}
}
return l;
}

void swap(linklist p,linklist q)
{
double m;
int n;
m=p->c;
p->c=q->c;
q->c=m;

n=p->e;
p->e=q->e;
q->e=n;
}

void sort(linklist l)
{
Lnode *p,*q;
p=l;
if(p==NULL)
return;
while(p)
{
q=p;
while(q)
{
if(q->ee)
swap(q,p);
else q=q->next;
}
p=p->next;
}
}

int main(void)
{
Lnode *l1,*l2;
linklist l,s;
l1=(Lnode*)malloc(sizeof(Lnode));
l2=(Lnode*)malloc(sizeof(Lnode));
l1=creat_linklist();
l2=creat_linklist();
l=count(l1,l2);
sort(l);
s=l;

while(s)
{
printf("%.2f^%d ",s->c,s->e);
s=s->next;
}

printf("\n");
return 0;
}
全部回答
  • 1楼网友:刀戟声无边
  • 2021-02-22 17:44
这个功能函数是用于计算数组的平均值以及大于平均值的 你传进去任意的一个要计算的数组(src)及它的长度(length),还有存放大于平均值的数组(dest)就行 不一定限制长度就是10的,大小是任意的。 #include <stdio.h> double fun4(double src[], int length, double dest[]) {    int i, j;    double total = 0.0, avg = 0.0;    for(i = 0; i < length; i++)    {     total += src[i];    }    avg = total / length;    for(i = 0, j = 0; i < length; i++)    {     if(src[i] > avg)     {     dest[j] = src[i];     j++;      }    }       return avg; } int main(void) {    int i;    double src[] = {1,2,3,4,5,6,7,8,9,10};    double dest[10];    double avg = 0.0;      for(i = 0; i < 10; i++)    {     dest[i] = -1;    }    avg = fun4(src,10,dest);       printf("源数组数据为:\n");    for(i = 0; i < 10; i++)    {     printf("%lf ",src[i]);    }    printf("\n平均值为:%lf \n大于平均值的数为:\n", avg);    for(i = 0; i < 10; i++)    {     if(dest[i] != -1)     printf("%lf ",dest[i]);    }    getchar();    return 0; }
  • 2楼网友:妄饮晩冬酒
  • 2021-02-22 16:15
while(l) { printf("%.2f^%d ",l->c,l->e); l=l->next; } return l; 创建链表时候输出 结果返回的l是NULL值 提个中间变量做输出 还有if(x!=0) double变量不能这么判断 while(l1!=NULL) while(l2!=NULL) 是不是应该放在while循环之外更加好
  • 3楼网友:梦中风几里
  • 2021-02-22 15:58
你的链表创建函数没错,但创建的链表是逆向创建,懂吗,你先创建了最后一个结点。 错误:l1=(Lnode*)malloc(sizeof(Lnode)); l2=(Lnode*)malloc(sizeof(Lnode)); l1=creat_linklist(); l2=creat_linklist(); 前面两部是无用操作,在creat_linklist函数中已经分配了内存。 你明显搞不清楚概念,Lnode*就是linklist啊。
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯