网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
11月28日漏签0天
c语言吧 关注:801,328贴子:4,372,156
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 7回复贴,共1页
<<返回c语言吧
>0< 加载中...

c语言萌新求助!做数据结构的问题遇到坎了!请尽情吐槽变量命名

  • 只看楼主
  • 收藏

  • 回复
  • 定好时间飞
  • 毛蛋
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
c语言萌新求助!做数据结构的问题遇到坎了!
请尽情吐槽变量命名,我会努力改的
题目:02-线性结构2 一元多项式的乘法与加法运算 (20分)
我的加法运算已经测试过了,没问题。但是乘法运算直接从中间输出,后面跟输出样例都对的上的(如图),前面不知道为什么漏了。
lz已经测试很久很久很久了,还是不会
代码如下
#include<stdio.h>
#include<stdlib.h>
typedef struct F *list;
struct F{
int coef;
int expon;
list next;
};
list read(int N);
list add(list l1,list l2);
list multiply(list l1,list l2);
list insert(list l,list i);
void print(list l);
int main(){
int N1,N2;
list l1,l2;
list result1,result2;
scanf("%d",&N1);
l1 = read(N1);
scanf("%d",&N2);
l2 = read(N2);
//¶ÁÈë²Ù×÷
//result1 = add(l1,l2);
result2 = multiply(l1,l2);
print(result2);
//print(result1);
}
list read(int N){
list l0,l;
l = (list)malloc(sizeof(F));
l0 = l;
for(int i = N;i >0; i--){
list rear = (list)malloc(sizeof(F));
scanf("%d %d",&rear ->coef,&rear ->expon);
l ->next = rear;
l = l ->next;
}
l ->next =NULL;
return l0;
}
list add(list l1,list l2){
list result = (list)malloc(sizeof(F));
list rear = result;
l1 = l1 ->next;
l2 = l2 ->next;
while(l1&&l2){
list new1 =(list)malloc(sizeof(F));
if(l1 ->expon > l2 ->expon){
new1 ->coef =l1 ->coef;
new1 ->expon =l1 ->expon;
rear ->next = new1;
rear = rear ->next;
l1 = l1 ->next;
}
else if(l1 ->expon < l2 ->expon){
new1 ->coef = l2 ->coef;
new1 ->expon =l2 ->expon;
rear ->next = new1;
rear = rear ->next;
l2 = l2 ->next;
}
else if(l1 ->expon = l2 ->expon){
new1 ->coef =l1 ->coef +l2 ->coef;
new1 ->expon=l1 ->expon;
rear ->next =new1;
rear = rear ->next;
l1 = l1 ->next;
l2 = l2 ->next;
}
}
while (l1){
list new1 =(list)malloc(sizeof(F));
new1 ->coef =l1 ->coef;
new1 ->expon =l1 ->expon;
rear ->next = new1;
rear = rear->next;
l1 = l1 ->next;
}
while (l2){
list new1 =(list)malloc(sizeof(F));
new1 ->coef =l2 ->coef;
new1 ->expon =l2 ->expon;
rear ->next = new1;
rear = rear ->next;
l2 = l2 ->next;
}
rear ->next =NULL;
return result;
}
list insert(list l,list i){
list step =l;
while(step ->next && i){
if (step ->next->expon > i ->expon){
step = step ->next;
}
else if(step ->next ->expon = i->expon){
step ->next ->coef += i->coef;
free (i);
break;
}
else{
i ->next =step ->next->next;
step ->next->next =i;
break;
}
}
while(!step ->next){
step ->next =i;
i ->next =NULL;
}
return l;
}
list multiply(list l1,list l2){
list result = (list)malloc(sizeof(F));
list rear =result;
l1 =l1 ->next;
l2 =l2 ->next;
list a =l1;
while (a){
list new1 = (list)malloc(sizeof(F));
new1 ->coef =a->coef *l2 ->coef;
new1 ->expon=a ->expon +l2 ->expon;
rear ->next =new1;
a =a->next;
}
rear ->next =NULL;
l2 = l2->next;
while(l2){
a =l1;
while(a){
list new1 = (list)malloc(sizeof(F));
new1 ->coef = a ->coef*l2 ->coef;
new1 ->expon =a ->expon + l2 ->expon;
result =insert(result,new1);
a = a->next;
}
l2 =l2 ->next;
}
return result;
}
void print(list l){
l = l->next;
while(l){
printf("%d %d",l->coef,l->expon);
if(l ->next){
printf(" ");
}
else{
printf("\n");
}
l = l->next;
}
}


  • xfhdfgvdg
  • 帕秋莉糕
    12
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
题目是啥


2025-11-28 03:12:38
广告
不感兴趣
开通SVIP免广告
  • Sedo
  • 毛蛋
    1
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
你也在学浙大数据结构?


  • 听一半╮的曲
  • 大能力者
    8
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
这个不难


  • a踽行
  • 低能力者
    5
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
有偿帮助私聊 满意为止


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 7回复贴,共1页
<<返回c语言吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示