永发信息网

请问一下C语言程序 菜单建立问题 如 “ 一元稀疏多项式的运算 ”这个问题中的第七小问,跪求高手。周五要

答案:2  悬赏:50  手机版
解决时间 2021-02-24 09:49
  • 提问者网友:萌卜娃娃
  • 2021-02-23 19:45
{
char a;
while(1)
{printf("\t****************帐号记忆器****************\n\t\t\t1,新健\n\t\t\t2,查看\n\t\t\t3,插入\n\t\t\t4,删除\n\t\t\t5,保存\n\t\t\t6,保存类\n\t\t\t7,退出\n想干嘛:");
scanf("%s",&a);
switch(a)
{
case'1':po(o);
break;
case'2':pi(o);
break;
case'3':pp(o);
break;
case'4':pu(o);
break;
case'5':pm(o);
break;
case'6':pv();
break;
case'7':return;
default :printf("不要瞎按\n");
}}
}

一元稀疏多项式的运算 (7人)

问题描述:设有两个带头指针的单链表表示两个一元稀疏多项式A、B,实现两个一元稀疏多项式的处理。
实现要求:
⑴ 输入并建立多项式;
⑵ 输出多项式,输出形式为整数序列:n,c1,e1,c2,e2……cn,en,其中n是多项式的项数,ci,ei分别为第i项的系数和指数。序列按指数降序排列;
⑶ 多项式A和B相加,建立多项式A+B,输出相加的多项式;
⑷ 多项式A和B相减,建立多项式A-B,输出相减的多项式;
⑸ 多项式A和B相乘,建立多项式A×B,输出相乘的多项式;
⑹ 多项式A和B相除,建立多项式A/B,输出相乘的多项式;
⑺ 设计一个菜单,至少具有上述操作要求的基本功能。

第七个小题那个菜单,如何嵌入。如有全部具体程序更好
最佳答案
  • 五星知识达人网友:等灯
  • 2021-02-23 21:08
#include
#include
#define A1(a,b,c,d,e) gotoxy(a,b);printf("c%d=",d);scanf("%f",e);
#define A2(a,b,c,d,e) gotoxy(a,b);printf("e%d=",d);scanf("%f",e);
#define B1 do { printf("\n想继续吗(求函数值/求导数)(y/N)?")
#define B2 ch2=bioskey(0); printf("[%c]",ch2)
#define B3 if((ch2=='Y')||(ch2=='y')) tc(head1->next,head2->next,r); }
#define B4 while((ch2=='y')||(ch2=='Y'))
struct ma
{ float c;
float e;
struct ma *next; }
main()
{ struct ma *head1,*head2,*p,*q,*r,*rm;
float s1[100][3],s2[100][3],mid1,mid2,mid3;
int i,j,t,num1,num2;
char ch,ch1,ch2,ch11,ch21;
struct ma *add();
struct ma *mul();
void tc();
void output();
loop1: t=0; ch='y';
clrscr();
printf("请输入第一个多项式的项数:");
scanf("%d",&num1);
q=head1=(struct ma*)malloc(sizeof(struct ma));
(*q).next=NULL;
if(num1>0)
{ printf("请输入第一个多项式的系数(c) 和指数(e):");
for(i=1;i<=num1;i++)
{ if(i%3==1) t++;
if(i%3==1) { A1(1,t+2,c,i,&mid1); A2(13,t+2,e,i,&mid2);}
else if(i%3==2) { A1(28,t+2,c,i,&mid1); A2(39,t+2,e,i,&mid2);}
else { A1(53,t+2,c,i,&mid1); A2(65,t+2,e,i,&mid2);}
s1[i][1]=mid1;s1[i][2]=mid2;}
for(i=1;i<=num1;i++)
for(j=i;j<=num1;j++)
if(s1[j][2] { mid3=s1[j][2];s1[j][2]=s1[i][2];s1[i][2]=mid3;
mid3=s1[j][1];s1[j][1]=s1[i][1];s1[i][1]=mid3;}
for(i=1;i<=num1;i++)
{ p=(struct ma*)malloc(sizeof(struct ma));
(*p).c=s1[i][1];(*p).e=s1[i][2];
(*q).next=p; q=p; }
(*p).next=NULL; }
printf("请输入第二个多项式的项数:");
scanf("%d",&num2);
q=head2=(struct ma*)malloc(sizeof(struct ma));
(*q).next=NULL;
if(num2>0)
{ printf("请输入第一个多项式的系数(c) 和指数(e):");
for(i=1;i<=num2;i++)
{ if(i%3==1) t++;
if(i%3==1) { A1(1,t+4,c,i,&mid1); A2(13,t+4,e,i,&mid2);}
else if(i%3==2) { A1(28,t+4,c,i,&mid1); A2(39,t+4,e,i,&mid2);}
else { A1(53,t+4,c,i,&mid1); A2(65,t+4,e,i,&mid2);}
s2[i][1]=mid1;s2[i][2]=mid2;}
for(i=1;i<=num2;i++)
for(j=i;j<=num2;j++)
if(s2[j][2] { mid3=s2[j][2];s2[j][2]=s2[i][2];s2[i][2]=mid3;
mid3=s2[j][1];s2[j][1]=s2[i][1];s2[i][1]=mid3; }
for(i=1;i<=num2;i++)
{ p=(struct ma*)malloc(sizeof(struct ma));
(*p).c=s2[i][1];(*p).e=s2[i][2];
(*q).next=p; q=p; }
(*p).next=NULL; }
while(ch=='Y'||ch=='y')
{ printf("\n请输入功能代码:\n");
printf("1 --加法 ;2--减法;3--乘法;9--重新输入多项式;0--退出;");
ch=bioskey(0); printf("[%c]",ch);
printf("\n输入的第一个多项式为:"); output((*head1).next);
printf("\n输入的第二个多项式为:"); output((*head2).next);
switch(ch)
{ case '1': { printf("\n两个多项式的和为:");
if((num1==0)&&(num2==0)) r=NULL;
else if(num1==0) r=(*head2).next;
else if(num2==0) r=(*head1).next;
else { ch1='+';r=add(head1,head2,ch1);}
output(r);
B1;B2;B3 B4;
break;}
case '2': { printf("\n两个多项式的差为:");
if((num1==0)&&(num2==0)) r=NULL;
else if(num1==0) { r=(*head2).next;
while(r!=NULL) { r->c*=-1;r=r->next;}
r=(*head2).next;}
else if(num2==0) r=(*head1).next;
else {ch1='-';r=add(head1,head2,ch1);}
output(r);B1;B2;B3 B4;
while((ch2=='y')||(ch2=='Y'));
break;}
case '3': { printf("\n两个多项式的积为:");
if((num1==0)||(num2==0)) r=NULL;
else r=mul(head1,head2);
output(r);B1;B2;B3 B4;
break;}
case '0': exit(0);
case '9': { while(head1!=NULL) {free(head1);head1=(*head1).next;}
while(head2!=NULL) {free(head2);head2=(*head2).next;}}goto loop1;
default: break;}
printf("\n想继续吗 ?(y/n)");
ch=bioskey(0); printf("[%c]",ch);
} }

struct ma *add( struct ma *head1,struct ma *head2,char ch1)
{ struct ma *head3,*p,*q,*r,*s;
head3=r=s=(struct ma*)malloc(sizeof(struct ma));
p=head1->next; q=head2->next;
do { r=(struct ma*)malloc(sizeof(struct ma));
(*s).next=r;
s=r;
if(p->e>q->e)
{ if(ch1=='+') (*r).c=q->c;
else (*r).c=-(q->c); (*r).e=q->e; q=(*q).next;}
else if(p->ee) { (*r).c=p->c ; (*r).e=p->e; p=(*p).next; }
else { if(ch1=='+') (*r).c=(p->c)+(q->c);
else (*r).c=(p->c)-(q->c);
(*r).e=(p->e); p=(*p).next;q=(*q).next;}
}while((p!=NULL)&&(q!=NULL));
while(q!=NULL)
{ r=(struct ma*)malloc(sizeof(struct ma));
(*s).next=r; s=r;
if(ch1=='+') (*r).c=q->c ;
else (*r).c=-(q->c);(*r).e=q->e; q=(*q).next;}
while(p!=NULL)
{ r=(struct ma*)malloc(sizeof(struct ma));
(*s).next=r; s=r;
(*r).c=p->c ; (*r).e=p->e;p=(*p).next; }
(*r).next=NULL;
s=r=(*head3).next;
while(r->next!=NULL)
if(r->e==(r->next)->e)
{ r->c+=(r->next)->c; s=r->next;
r->next=(r->next)->next; free(s); s=r; }
else { r=r->next; s=s->next; }
s=r=(*head3).next;
return(r);
}

struct ma *mul(struct ma *head1,struct ma *head2)
{ struct ma *head3,*head4,*p,*q,*r,*s,*r1,*s1;
struct ma *w[100];
int t=0;

head3=r=s=(struct ma*)malloc(sizeof(struct ma));
head4=r1=s1=(struct ma*)malloc(sizeof(struct ma));
p=head1->next; q=head2->next;

while(p!=NULL)
{ s=(struct ma*)malloc(sizeof(struct ma));
s->c=(p->c)*(q->c);
s->e=(p->e)+(q->e);
r->next=s;
r=s;
p=p->next;}
s->next=NULL; s=r=head3->next; w[0]=head3;
p=head1->next; q=q->next;

while(q!=NULL)
{ while(p!=NULL)
{ s1=(struct ma*)malloc(sizeof(struct ma));
s1->c=(p->c)*(q->c);s1->e=(p->e)+(q->e);
r1->next=s1;
r1=s1;
p=p->next;
}
s1->next=NULL;r1=s1=head4->next;
t++;
w[t]=(struct ma*)malloc(sizeof(struct ma));
w[t]->next=add(w[t-1],head4,'+');
while(head3->next!=NULL) {r=r->next;free(s);head3->next=s=r;}
s=r=head3;
while(head4->next!=NULL) {r1=r1->next;free(s1);head4->next=s1=r1;}
s1=r1=head4;
p=head1->next; q=q->next;
}
return(w[t]->next);}

void tc(struct ma *p,struct ma *q, struct ma *r)
{ char ch1,ch2;
float x;
float qzhi(struct ma *head,float x);
void qdao();
printf("\n请选择多项式的序号:");
printf("\n 1--第一个;2--第二个;3--第三个");
ch1=bioskey(0);printf("[%c]",ch1);
printf("\n请输入功能代码:");
printf("\n z--求函数值,d--求导函数");
ch2=bioskey(0);printf("[%c]",ch2);
switch(ch1)
{ case '1': { if(ch2=='z')
{ printf("\n请输入自变量 x:");
scanf("%f",&x);
printf("多项式的值为:%10.4f",qzhi(p,x));}
if(ch2=='d') qdao(p); break;}
case '2': { if(ch2=='z')
{ printf("\n请输入自变量 x:");
scanf("%f",&x);
printf("多项式的值为:%10.4f",qzhi(q,x));}
if(ch2=='d') qdao(q); break;}

case '3': { if(ch2=='z')
{ printf("\n请输入自变量 x:");
scanf("%f",&x);
printf("多项式的值为:%10.4f",qzhi(r,x));}
if(ch2=='d') qdao(r); break;} } }

float qzhi(struct ma *head,float x)
{ float value=0;
while(head!=NULL)
{ value+=(head->c)*pow(x,head->e);head=head->next;}
return(value);
}

void qdao(struct ma *head)
{ struct ma *p,*q,*r,*t;
p=head;
t=r=q=(struct ma *)malloc(sizeof(struct ma));
while(p!=NULL)
{ q=(struct ma *)malloc(sizeof(struct ma));
r->next=q;r=q;
q->c=(p->c)*((p->e));q->e=(p->e)-1;
p=p->next;}
q->next=NULL;
printf("\n多项式的导数为:");
output(t->next);
p=t;
while(t!=NULL) { p=t->next;free(t);t=p;}

}

void output(struct ma *r)
{ int l=0;
struct ma *t,*s,*head;
t=r;
while(r->next!=NULL)
if(r->e==(r->next)->e)
{ r->c+=(r->next)->c; s=r->next;
r->next=(r->next)->next; free(s); s=r; }
else { r=r->next; s=s->next; }
s=r=t;

head=(struct ma*)malloc(sizeof(struct ma));
head->next=r;
s=head;
while(r!=NULL)
{ if((r->c)==0) {s->next=r->next; free(r);r=s->next;}
else { s=s->next;r=r->next;}}
s=r=head->next;
while(r!=NULL)
{ if(r->c==1) { l++;
if(r->e==1) printf("x");
else if(r->e<0) printf("x^(%.0f)",r->e);
else if(r->e==0) printf("1");
else printf("x^%.0f",r->e);}
else if(r->c==-1) { l++;
if(r->e==1) printf("-x");
else if(r->e<0) printf("-x^(%.0f)",r->e);
else if(r->e==0) printf("-1");
else printf("-x^%.0f",r->e);}
else { l++;
if(r->e==1) printf("%4.1fx",r->c);
else if(r->e<0) printf("%4.1fx^(%.0f)",r->c,r->e);
else if(r->e==0) printf("%4.1f",r->c);
else printf("%4.1fx^%.0f",r->c,r->e); }
if(((*r).next!=NULL)&&((r->next)->c>0)) printf(" +");
r=r->next; }
if(l==0) printf("0");
}
全部回答
  • 1楼网友:未来江山和你
  • 2021-02-23 21:58
pos=last->next;下一结点 }; void freelist(linklist &l); void sortlist(linklist &l); } while(last; if(head->pos-> void deletenextnode(node *d); void sweepnextnode(node *s); void outputlist(linklist &l)!=null && last->.h; } head->.h" using namespace std; bool createlist(linklist &l) { // free(pos); head->c=0; head->是否需要排序标志 node *head=l->nextdatastruct;/多项式的项数 int e; / 相关函数的实现;< node *pos; endl,html代码 { sweepnextnode(last); flag=true; } if(last-> } void freelist(linklist &l) { / node *last; node *temp.cpp #include < << endl; return false;next=null; node *next; pos=l; while(pos!=null) { next=pos->next;next==null) { return; } while(flag) { flag=true, int c); deletenextnode(last); flag=true; } last=last->next; } } } void deletenextnode(node *d) { node *temp; temp=d->next; d->next=temp->next; free(temp); } void sweepnextnode(node *s) //一点偷懒的办法,只交换值,不修改指针 { int c,e; c=s->c;e=s->e; s->c=s->next->c;s->e=s->next->e; s->next->c=c;s->next->e=e; } void outputlist(linklist &l) { node *pos; pos=l->next; cout << "输出表达式:"; while(pos!=null) { if(pos->c>0) { cout << "+"; } if(pos->c!=1) { cout << pos->c; } if(pos->e!=0) { cout << "x^"; cout << pos->e; } pos=pos->next; } cout << endl; } 主单元文件main.cpp: #include #include #include #include "listoper.h" using namespace std; linklist anaystring(char astring[], int alength); int main(int argc, char *argv[]) //------------------------------- { linklist l; char instr[1024]; int len; cout << "一元稀疏多项式计算器" << endl; cout << "copyright@1999-2004, gashero liu." << endl; cout << "作者:刘晓明" << endl << endl; cout << "请输入一个1024个字符以内的稀疏多项式:"; cin >> instr; len=strlen(instr); l=anaystring(instr,len); sortlist(l); outputlist(l); freelist(l); system("pause"); return 0; } linklist anaystring(char astring[], int alength) //--------------- //todo: 字符串分析函数 { linklist l=null; node *pos=null; node *last; node *head; createlist(l); head=l; last=head; int c=0; int e=0; char temp[1]; char tp; bool plus=true; char status='n'; //状态指示符,我省略了系数为负的情况 for(int i=0;inext=pos; last=pos; c=0;e=0; status='c'; i--; continue; } } } pos=createnode(e,c); last->next=pos; return l; 不知道是不是你需要的;/多项式的指数 struct list *next;e=e; pos->c=c; pos->next=null; return pos; if(last==null||last->: // // typedef struct list *linklist;/file;datastruct.h" exit(1);哈哈哈哈哈; l=head; return true; } node *createnode(int e, int c) { /c: listoper;file: listoper.h #ifndef datastruct #define datastruct #include "datastruct;stdlib.h>e==pos->e) { last->c+=pos-> #endif #include "listoper; #include #ifndef datastruct #define datastruct #include ".h" #endif / last=head;next!=null) { flag=false; pos=last->e) //,对应文件listoper.cpp: /next; if(last->e
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯