主题:[转帖]c语言保存问题
我是一名青鸟的学员,以下这些知识都是我在青鸟所学的,希望各位能采纳一下。
详细请登录:[url=http://www.cpbdqn.com]网络工程师培训[/url]
#include<stdio.h>
#include<malloc.h>
struct Node
{
float c;
int e;
struct Node* next;
};
struct Node* CreatPolyn(struct Node* &head)
{
int flag=0;
struct Node *p1,*p2;
p1=p2=head=(struct Node*)malloc(sizeof(Node));
printf("请输入多项式的系数和指数\n");
printf("系数:");
scanf("%f",&(p1->c));
printf("指数:");
scanf("%d",&(p1->e));
printf("\n");
printf("是否继续输入:1继续 0终止\n");
scanf("%d",&flag);
while(flag)
{
p1->next=(struct Node*)malloc(sizeof(Node));
p1=p1->next;
printf("请输入多项式的系数和指数\n");
printf("系数:");
scanf("%f",&(p1->c));
printf("指数:");
scanf("%d",&(p1->e));
printf("\n");
printf("是否继续输入:1继续 0终止\n");
scanf("%d",&flag);
}
p1->next=NULL;
p1=p2=head;
while(p1->next) ///将输入的多项式按指数升序排列
{
p2=p1;
while(p2->next)
{
if(p2->e>(p2->next)->e)
{
float temp1=p2->c;
int temp2=p2->e;
p2->c=p2->next->c;
p2->next->c=temp1;
p2->e=p2->next->e;
p2->next->e=temp2;
}
p2=p2->next;
}
p1=p1->next;
}
return head;
}
void AddPolyn(struct Node *&p1,struct Node *&p2)
{
struct Node *pa,*pb,*pc,*head;
pa=p1;pb=p2;
head=(struct Node*)malloc(sizeof(Node));
pc=head;
while(pa&&pb)
{
if(pb->e<pa->e)
{
pc->c=pb->c;
pc->e=pb->e;
pb=pb->next;
}
else
if(pb->e==pa->e)
{
if(!(pa->c+pb->c))
{
free(pc);
pa=pa->next;
pb=pb->next;
}
else
{
pc->c=pa->c+pb->c;
pc->e=pa->e;
pa=pa->next;
pb=pb->next;
}
}
else
if(pb->e>pa->e)
{
pc->c=pa->c;
pc->e=pa->e;
pa=pa->next;
}
pc->next=(struct Node*)malloc(sizeof(Node));
pc=pc->next;
}
if(pa==NULL&&pb==NULL)
{
pc->next=NULL;
}
else
{
pa=pa?pa:pb;
pc->c=pa->c;
pc->e=pa->e;
pa=pa->next;
while(pa)
{
pc->next=(struct Node*)malloc(sizeof(Node));
pc=pc->next;
pc->c=pa->c;
pc->e=pa->e;
pa=pa->next;
}
pc->next=NULL;
}
pc=head;
while(pc)
{
printf("%4.2f %d ",pc->c,pc->e);
pc=pc->next;
}
printf("\n");
}
void OutputPolyn(struct Node* head)
{
struct Node *p1,*p2;
if(!head)
{
printf("多项式不存在,请先建立\n");
}
p1=p2=head;
int cnt=0;
while(p1)
{
p1=p1->next; //计算有多少项
cnt++;
}
printf("%d ",cnt);
while(p2)
{
printf("%4.2f ",p2->c);
printf("%d ",p2->e);
p2=p2->next;
}
printf("\n");
}
void SubbPolyn(struct Node *&p1,struct Node *&p2)
{
struct Node *p=p2;
while(p)
{
p->c=(-1.0)*(p->c);
p=p->next;
}
AddPolyn(p1,p2);
p=p2;
while(p)
{
p->c=(-1.0)*(p->c);
p=p->next;
}
}
void main()
{
int flag=1;
while(flag)
{
struct Node* head;
head=NULL;
static struct Node *pa,*pb;
pa=pb=NULL;
printf("************************************************************\n");
printf("请选择功能:1建立并输出多项式 2多项式相加 3多项式相减 4退出\n");
printf("************************************************************\n");
scanf("%d",&flag);
switch(flag)
{
case 1:{
head=CreatPolyn(head);
OutputPolyn(head);break;
}
case 2:
{
if(!pa)
{
printf("请先建立多项式a\n");
pa=CreatPolyn(pa);
}
if(!pb)
{
printf("请先建立多项式b\n");
pb=CreatPolyn(pb);
}
printf("多项式相加后为\n");
AddPolyn(pa,pb);
}break;
case 3:
{
if(!pa)
{
printf("请先建立多项式a\n");
pa=CreatPolyn(pa);
}
if(!pb)
{
printf("请先建立多项式b\n");
pb=CreatPolyn(pb);
}
printf("多项式相减后为\n");
SubbPolyn(pa,pb);
}break;
case 4:flag=0;break;
default:printf("输入有错");
}
}
}
详细请登录:[url=http://www.cpbdqn.com]网络工程师培训[/url]
#include<stdio.h>
#include<malloc.h>
struct Node
{
float c;
int e;
struct Node* next;
};
struct Node* CreatPolyn(struct Node* &head)
{
int flag=0;
struct Node *p1,*p2;
p1=p2=head=(struct Node*)malloc(sizeof(Node));
printf("请输入多项式的系数和指数\n");
printf("系数:");
scanf("%f",&(p1->c));
printf("指数:");
scanf("%d",&(p1->e));
printf("\n");
printf("是否继续输入:1继续 0终止\n");
scanf("%d",&flag);
while(flag)
{
p1->next=(struct Node*)malloc(sizeof(Node));
p1=p1->next;
printf("请输入多项式的系数和指数\n");
printf("系数:");
scanf("%f",&(p1->c));
printf("指数:");
scanf("%d",&(p1->e));
printf("\n");
printf("是否继续输入:1继续 0终止\n");
scanf("%d",&flag);
}
p1->next=NULL;
p1=p2=head;
while(p1->next) ///将输入的多项式按指数升序排列
{
p2=p1;
while(p2->next)
{
if(p2->e>(p2->next)->e)
{
float temp1=p2->c;
int temp2=p2->e;
p2->c=p2->next->c;
p2->next->c=temp1;
p2->e=p2->next->e;
p2->next->e=temp2;
}
p2=p2->next;
}
p1=p1->next;
}
return head;
}
void AddPolyn(struct Node *&p1,struct Node *&p2)
{
struct Node *pa,*pb,*pc,*head;
pa=p1;pb=p2;
head=(struct Node*)malloc(sizeof(Node));
pc=head;
while(pa&&pb)
{
if(pb->e<pa->e)
{
pc->c=pb->c;
pc->e=pb->e;
pb=pb->next;
}
else
if(pb->e==pa->e)
{
if(!(pa->c+pb->c))
{
free(pc);
pa=pa->next;
pb=pb->next;
}
else
{
pc->c=pa->c+pb->c;
pc->e=pa->e;
pa=pa->next;
pb=pb->next;
}
}
else
if(pb->e>pa->e)
{
pc->c=pa->c;
pc->e=pa->e;
pa=pa->next;
}
pc->next=(struct Node*)malloc(sizeof(Node));
pc=pc->next;
}
if(pa==NULL&&pb==NULL)
{
pc->next=NULL;
}
else
{
pa=pa?pa:pb;
pc->c=pa->c;
pc->e=pa->e;
pa=pa->next;
while(pa)
{
pc->next=(struct Node*)malloc(sizeof(Node));
pc=pc->next;
pc->c=pa->c;
pc->e=pa->e;
pa=pa->next;
}
pc->next=NULL;
}
pc=head;
while(pc)
{
printf("%4.2f %d ",pc->c,pc->e);
pc=pc->next;
}
printf("\n");
}
void OutputPolyn(struct Node* head)
{
struct Node *p1,*p2;
if(!head)
{
printf("多项式不存在,请先建立\n");
}
p1=p2=head;
int cnt=0;
while(p1)
{
p1=p1->next; //计算有多少项
cnt++;
}
printf("%d ",cnt);
while(p2)
{
printf("%4.2f ",p2->c);
printf("%d ",p2->e);
p2=p2->next;
}
printf("\n");
}
void SubbPolyn(struct Node *&p1,struct Node *&p2)
{
struct Node *p=p2;
while(p)
{
p->c=(-1.0)*(p->c);
p=p->next;
}
AddPolyn(p1,p2);
p=p2;
while(p)
{
p->c=(-1.0)*(p->c);
p=p->next;
}
}
void main()
{
int flag=1;
while(flag)
{
struct Node* head;
head=NULL;
static struct Node *pa,*pb;
pa=pb=NULL;
printf("************************************************************\n");
printf("请选择功能:1建立并输出多项式 2多项式相加 3多项式相减 4退出\n");
printf("************************************************************\n");
scanf("%d",&flag);
switch(flag)
{
case 1:{
head=CreatPolyn(head);
OutputPolyn(head);break;
}
case 2:
{
if(!pa)
{
printf("请先建立多项式a\n");
pa=CreatPolyn(pa);
}
if(!pb)
{
printf("请先建立多项式b\n");
pb=CreatPolyn(pb);
}
printf("多项式相加后为\n");
AddPolyn(pa,pb);
}break;
case 3:
{
if(!pa)
{
printf("请先建立多项式a\n");
pa=CreatPolyn(pa);
}
if(!pb)
{
printf("请先建立多项式b\n");
pb=CreatPolyn(pb);
}
printf("多项式相减后为\n");
SubbPolyn(pa,pb);
}break;
case 4:flag=0;break;
default:printf("输入有错");
}
}
}