如何用c语言实现一元稀疏多项式计算器。。。

来自:韩乐坊    更新日期:早些时候
一元稀疏多项式计算器 C语言编写~

datastruct.h

typedef struct list
{
int c; //多项式的项数
int e; //多项式的指数
struct list *next; //下一结点
};

typedef struct list *LinkList;
typedef struct list Node;

下面是线性表的操作相关函数声明,对应文件ListOper.h:
//File: ListOper.h


#ifndef DATASTRUCT
#define DATASTRUCT
#include "datastruct.h"
#endif

//some functions declaretioin

bool CreateList(LinkList &L);
Node *CreateNode(int e, int c);
void FreeList(LinkList &L);
void SortList(LinkList &L);
void DeleteNextNode(Node *d);
void SweepNextNode(Node *s);
void OutPutList(LinkList &L);

相关函数的实现,对应文件ListOper.cpp:
//File: ListOper.cpp

#include
#include
#ifndef DATASTRUCT
#define DATASTRUCT
#include "datastruct.h"
#endif
#include "ListOper.h"

using namespace std;

bool CreateList(LinkList &L)
{
//TODO: 创建线性链表
Node *head;
head=(Node *)malloc(sizeof(Node));
if(head==NULL)
{
cout << "内存分配错误" << endl;
return false;
}
head->next=NULL;
head->c=0;
head->e=0;
L=head;
return true;
}

Node *CreateNode(int e, int c)
{
//TODO: 创建结点
Node * pos;
pos=(Node *)malloc(sizeof(Node));
if(pos==NULL)
{
cout << "内存分配错误" << endl;
exit(1);
}
pos->e=e;
pos->c=c;
pos->next=NULL;
return pos;
}

void FreeList(LinkList &L)
{
//TODO: 释放整个线性表所占用的内存空间
Node *pos;
Node *next;
pos=L;
while(pos!=NULL)
{
next=pos->next;
free(pos);
pos=next;
}
}

void SortList(LinkList &L)
{
bool flag=true; //是否需要排序标志
Node *head=L->next;
Node *pos;
Node *last;
Node *temp;
if(head->next==NULL)
{
return;
}
while(flag)
{
flag=true;
last=head;
pos=last->next;
if(last==NULL||last->next==NULL)
{
break;
}
while(last!=NULL && last->next!=NULL)
{
flag=false;
pos=last->next;
if(last->ee) //哈哈哈哈哈,HTML代码
{
SweepNextNode(last);
flag=true;
}
if(last->e==pos->e)
{
last->c+=pos->c;
DeleteNextNode(last);
flag=true;
/*last=last->next;
pos=last->next;*/
}
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 c;
}
if(pos->e!=0)
{
cout << "x^";
cout 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'; //状态指示符,我省略了系数为负的情况
/*
n: 非运算状态
c: 正在计算系数
e: 正在计算指数
p: 指数为0
f: 完成了一个项目的输入
*/
for(int i=0;i<aLength;i++)
{
temp[0]=aString[i];
tp=temp[0];
switch(status)
{
case 'n':
{
c=0;e=0;
status='c';
if(tp=='-')
{
plus=false;
continue;
}
if(isdigit(tp))
{
c=atoi(temp);
continue;
}
if(tp=='x')//多项式以x开头
{
c=1;
status='e';
continue;
}
}
case 'c':
{
if(isdigit(aString[i]))
{
if(plus)
{
c=c*10+atoi(temp);
}
else
{
c=c*10-atoi(temp);
}
continue;
}
if(tp=='x')
{
if(c==0)
{
c=1;
}
status='e';
e=0;
continue;
}
//此处考虑了常数项出现在其他位置的可能
if(tp=='+')
{
plus=true;
status='p';
continue;
}
if(tp=='-')
{
plus=false;
status='p';
continue;
}
/*if(temp[0]=='^')
{
status='e';
e=0;
continue;
}*/ //此种情况不可能出现
continue;
} //正在解析系数
case 'e':
{
if(tp=='^')
{
continue;
}
if(isdigit(tp))
{
e=e*10+atoi(temp);
continue;
}
if(tp=='+')
{
plus=true;
status='f';
continue;
}
if(tp=='-')
{
plus=false;
status='f';
continue;
}
} //正在解析系数
case 'p':
{
e=0;
status='f';
continue;
}
case 'f':
{
pos=CreateNode(e,c);
last->next=pos;
last=pos;
c=0;e=0;
status='c';
i--;
continue;
}
}
}
pos=CreateNode(e,c);
last->next=pos;
return L;


不知道是不是你需要的

一元多项式简单的计算器的功能:1)输入并建立多项式;2)输出多项式;3)两个多项式相加,输出和多项式;4)两个多项式相减,输出差多项式。
例程
#include /*DOS接口函数*/#include /*数学函数的定义*/#include /*屏幕操作函数*/#include /*I/O函数*/#include /*库函数*/#include /*变量长度参数表*/#include /*图形函数*/#include /*字符串函数*/#include /*字符操作函数*/#define UP 0x48 /*光标上移键*/#define DOWN 0x50 /*光标下移键*/#define LEFT 0x4b /*光标左移键*/#define RIGHT 0x4d /*光标右移键*/#define ENTER 0x0d /*回车键*/void *rar; /*全局变量,保存光标图象*/struct palettetype palette; /*使用调色板信息*/int GraphDriver; /* 图形设备驱动*/int GraphMode; /* 图形模式值*/int ErrorCode; /* 错误代码*/int MaxColors; /* 可用颜色的最大数值*/int MaxX, MaxY; /* 屏幕的最大分辨率*/double AspectRatio; /* 屏幕的像素比*/void drawboder(void); /*画边框函数*/void initialize(void); /*初始化函数*/void computer(void); /*计算器计算函数*/void changetextstyle(int font, int direction, int charsize); /*改变文本样式函数*/void mwindow(char *header); /*窗口函数*/int specialkey(void) ; /*获取特殊键函数*/int arrow(); /*设置箭头光标函数*//*主函数*/int main(){initialize();/* 设置系统进入图形模式 */computer(); /*运行计算器 */closegraph();/*系统关闭图形模式返回文本模式*/return(0); /*结束程序*/}/* 设置系统进入图形模式 */void initialize(void){int xasp, yasp; /* 用于读x和y方向纵横比*/GraphDriver = DETECT; /* 自动检测显示器*/initgraph( &GraphDriver, &GraphMode, "" );/*初始化图形系统*/ErrorCode = graphresult(); /*读初始化结果*/if( ErrorCode != grOk ) /*如果初始化时出现错误*/{printf("Graphics System Error: %s
",grapherrormsg( ErrorCode ) ); /*显示错误代码*/exit( 1 ); /*退出*/}getpalette( &palette ); /* 读面板信息*/MaxColors = getmaxcolor() + 1; /* 读取颜色的最大值*/MaxX = getmaxx(); /* 读屏幕尺寸 */MaxY = getmaxy(); /* 读屏幕尺寸 */getaspectratio( &xasp, &yasp ); /* 拷贝纵横比到变量中*/AspectRatio = (double)xasp/(double)yasp;/* 计算纵横比值*/}/*计算器函数*/void computer(void){struct viewporttype vp; /*定义视口类型变量*/int color, height, width;int x, y,x0,y0, i, j,v,m,n,act,flag=1;float num1=0,num2=0,result; /*操作数和计算结果变量*/char cnum[5],str2[20]={""},c,temp[20]={""};char str1[]="1230.456+-789*/Qc=^%";/* 定义字符串在按钮图形上显示的符号 */mwindow( "Calculator" ); /* 显示主窗口 */color = 7; /*设置灰颜色值*/getviewsettings( &vp ); /* 读取当前窗口的大小*/width=(vp.right+1)/10; /* 设置按钮宽度 */height=(vp.bottom-10)/10 ; /*设置按钮高度 */x = width /2; /*设置x的坐标值*/y = height/2; /*设置y的坐标值*/setfillstyle(SOLID_FILL, color+3);bar( x+width*2, y, x+7*width, y+height );/*画一个二维矩形条显示运算数和结果*/setcolor( color+3 ); /*设置淡绿颜色边框线*/rectangle( x+width*2, y, x+7*width, y+height );/*画一个矩形边框线*/setcolor(RED); /*设置颜色为红色*/outtextxy(x+3*width,y+height/2,"0."); /*输出字符串"0."*/x =2*width-width/2; /*设置x的坐标值*/y =2*height+height/2; /*设置y的坐标值*/for( j=0 ; j=x0+6*width)/*如果右移,移到尾,则移动到最左边字符位置*/{x=x0;m=0;}else{x=x+width+width/2;m++;} /*否则,右移到下一个字符位置*/if(v==LEFT) /*左移箭头时新位置计算*/if(x=7*height){y=y0;n=0;} /*如果移到尾,再下移,则移动到最上边字符位置*/else{y=y+height+height/2;n++;} /*否则,移到下边一个字符位置*/putimage(x,y,rar,XOR_PUT); /*在新的位置显示光标箭头*/}c=str1[n*5+m]; /*将字符保存到变量c中*/if(isdigit(c)||c=='.') /*判断是否是数字或小数点*/{if(flag==-1) /*如果标志为-1,表明为负数*/{strcpy(str2,"-"); /*将负号连接到字符串中*/flag=1;} /*将标志值恢复为1*/sprintf(temp,"%c",c); /*将字符保存到字符串变量temp中*/strcat(str2,temp); /*将temp中的字符串连接到str2中*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,str2); /*显示字符串*/}if(c=='+'){num1=atof(str2); /*将第一个操作数转换为浮点数*/strcpy(str2,""); /*将str2清空*/act=1; /*做计算加法标志值*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='-'){if(strcmp(str2,"")==0) /*如果str2为空,说明是负号,而不是减号*/flag=-1; /*设置负数标志*/else{num1=atof(str2); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将str2清空*/act=2; /*做计算减法标志值*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/outtextxy(5*width,height,"0."); /*显示字符串*/}}if(c=='*'){num1=atof(str2); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将str2清空*/act=3; /*做计算乘法标志值*/setfillstyle(SOLID_FILL,color+3); bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='/'){num1=atof(str2); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将str2清空*/act=4; /*做计算除法标志值*/setfillstyle(SOLID_FILL,color+3);bar(2*width+width/2,height/2,15*width/2,3*height/2);outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='^'){num1=atof(str2); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将str2清空*/act=5; /*做计算乘方标志值*/setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='%'){num1=atof(str2); /*将第二个操作数转换为浮点数*/strcpy(str2,""); /*将str2清空*/act=6; /*做计算模运算乘方标志值*/setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/bar(2*width+width/2,height/2,15*width/2,3*height/2); /*画矩形*/outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='='){num2=atof(str2); /*将第二个操作数转换为浮点数*/switch(act) /*根据运算符号计算*/{case 1:result=num1+num2;break; /*做加法*/case 2:result=num1-num2;break; /*做减法*/case 3:result=num1*num2;break; /*做乘法*/case 4:result=num1/num2;break; /*做除法*/case 5:result=pow(num1,num2);break; /*做x的y次方*/case 6:result=fmod(num1,num2);break; /*做模运算*/}setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/sprintf(temp,"%f",result); /*将结果保存到temp中*/outtextxy(5*width,height,temp); /*显示结果*/}if(c=='c'){num1=0; /*将两个操作数复位0,符号标志为1*/num2=0;flag=1;strcpy(str2,""); /*将str2清空*/setfillstyle(SOLID_FILL,color+3); /*设置用淡绿色实体填充*/bar(2*width+width/2,height/2,15*width/2,3*height/2); /*覆盖结果区*/outtextxy(5*width,height,"0."); /*显示字符串*/}if(c=='Q')exit(0); /*如果选择了q回车,结束计算程序*/}putimage(x,y,rar,XOR_PUT); /*在退出之前消去光标箭头*/return; /*返回*/}/*窗口函数*/void mwindow( char *header ){int height;cleardevice(); /* 清除图形屏幕 */setcolor( MaxColors - 1 ); /* 设置当前颜色为白色*/setviewport( 20, 20, MaxX/2, MaxY/2, 1 ); /* 设置视口大小 */height = textheight( "H" ); /* 读取基本文本大小 */settextstyle( DEFAULT_FONT, HORIZ_DIR, 1 );/*设置文本样式*/settextjustify( CENTER_TEXT, TOP_TEXT );/*设置字符排列方式*/outtextxy( MaxX/4, 2, header ); /*输出标题*/setviewport( 20,20+height+4, MaxX/2+4, MaxY/2+20, 1 ); /*设置视口大小*/drawboder(); /*画边框*/}void drawboder(void) /*画边框*/{struct viewporttype vp; /*定义视口类型变量*/setcolor( MaxColors - 1 ); /*设置当前颜色为白色 */setlinestyle( SOLID_LINE, 0, NORM_WIDTH );/*设置画线方式*/getviewsettings( &vp );/*将当前视口信息装入vp所指的结构中*/rectangle( 0, 0, vp.right-vp.left, vp.bottom-vp.top ); /*画矩形边框*/}/*设计鼠标图形函数*/int arrow(){int size;int raw[]={4,4,4,8,6,8,14,16,16,16,8,6,8,4,4,4}; /*定义多边形坐标*/setfillstyle(SOLID_FILL,2); /*设置填充模式*/fillpoly(8,raw); /*画出一光标箭头*/size=imagesize(4,4,16,16); /*测试图象大小*/rar=malloc(size); /*分配内存区域*/getimage(4,4,16,16,rar); /*存放光标箭头图象*/putimage(4,4,rar,XOR_PUT); /*消去光标箭头图象*/return 0;}/*按键函数*/int specialkey(void){int key;while(bioskey(1)==0); /*等待键盘输入*/key=bioskey(0); /*键盘输入*/key=key&0xff? key&0xff:key>>8; /*只取特殊键的扫描值,其余为0*/return(key); /*返回键值*/}

先把稀疏多项式转成矩阵,如果计算


如何用c语言实现一元稀疏多项式计算器。。。视频

相关评论:
  • 15917932319用C语言实现一元多项式相乘
    阙桂乔printf("请输入一元N次多项式的N:");scanf("%d",&i);for(j=0;j<=i;j++){ printf("P(x) %d次项系数:",j);scanf("%d",&x[j]);printf("Q(x) %d次项系数:",j);scanf("%d",&y[j]);} printf("\\nP(x) = ");for (k=i;k>=0;k--){printf("%dX^%d + ",...

  • 15917932319C语言 一元多项式的计算 关于x的一元多项式函数化简器 给定一个关于x...
    阙桂乔这位是在别人的地方copy来的,这是地址:创建链表头head->next=NULL;scanf("%f%d",&x,&y);\/\/实现用户输入的第一个项,包括其指数和系数while(x!=0)\/\/当用户没有输入结束标志0时可一直输入多项式的项,且输入一个创建一个结点{input=(PLOYList*)malloc(sizeof(PLOYList));\/\/创建新链节input...

  • 15917932319大鱼5元,中鱼3元,小鱼一元三条,100元买一百条,怎么买,用C语言
    阙桂乔include<stdio.h> \/\/大鱼5元,中鱼3元,小鱼一元三条,100元买一百条,怎么买,用C语言 void main(){ int i, j, k;for(i=0; i<100; i++)for(j=0; j<100; j++)for(k=0; k<100; k++)if(i*5+j*3+k\/3==100&&k%3==0&&i+j+k==100) printf("%d, %d, %d\\n", i, ...

  • 15917932319急求一元线性回归的C语言程序
    阙桂乔一元线性回归的C语言程序是:利用最小二乘法来估计线性回归方程的参数,然后用这些参数来预测因变量的值1。例如,你可以参考下面的代码:include <stdio.h>#include <math.h>\/\/定义一个函数,计算一元线性回归方程的参数a和bvoid linear_regression(double x[], double y[], int n, double *a, ...

  • 15917932319C语言的一元多项式相乘问题,高手救急!
    阙桂乔include "stdio.h"include "alloc.h"include "conio.h"typedef struct node { int e,c;struct node *next;}PN;PN *createLink(){ int n;PN *head, *p;p=head=(PN*)malloc(sizeof(PN));printf("n=");scanf("%d",&n);while(n){ p->next=(PN*)(PN*)malloc(sizeof(PN));p=p...

  • 15917932319c语言编程用一元五角人民币兑换五分两分和一分的硬币,每一种方案硬币总...
    阙桂乔13种。1、有三种形式的C + +语言整数常数:十进制,八和十六进制。(1)十进制整数是由数字0至9的数据不以0开始。(2)八进制整数是数字0~7从0开始的构成的数据。(3)十六进制整数是从0号的组成的9个数据开始以0x或0X和字母从a到f(大写和小写字母)。2、输入是一个正整数n,指示第一N个...

  • 15917932319c语言用穷举法实现一元人民币换成一分、两分、五分的硬币共五十枚的硬币...
    阙桂乔要用穷举法实现一元人民币换成一分、两分和五分的硬币共五十枚的硬币方案,可以使用三个循环来遍历所有可能的组合。下面是一个用C语言实现该程序的示例:```include <stdio.h> int main() { int count = 0; \/\/ 记录方案数量 \/\/ 穷举所有可能的组合 for (int oneFen = 0; oneFen <= 100;...

  • 15917932319如何用c语言编程解一元七次方程组?
    阙桂乔如下:include<stdio.h> include<math.h> disc=b*b-4*a*c;p=-b\/(2.0*a);q=sqrt(disc)\/(2.0*a);x1=p+q;x2=p-q;printf("x1=%7.2f\\nx2=%7.2f\\n",x1,x2);return 0。

  • 15917932319用C语言,一张一元钞票换成一分 二分 五分硬币 每种至少八枚 一共有...
    阙桂乔每种至少8枚,一共是1*8+2*8+5*8=64,那剩下的就是36就够了 int flag=0;\/\/统计方案的变量 for(i=0;i<=36;i++) \/\/这个变量是统计1分的,最多只能有36个 for(j=0;j<=18;j++) \/\/统计2分,最多只能有18个 for(k=0;k<=7;j++) \/\/统计5分,最多只能有7个 if((...

  • 15917932319数据结构c语言 用单链表储存一元多项式,并实现两个多项式的相加运算...
    阙桂乔同时void Add(LinkList *P1,LinkList *P2,LinkList *&P3做了一点修改!也许,程序的可读性不好,你可以自己再修改!代码如下:include<stdio.h> include <malloc.h> define MAX 20 \/\/多项式最多项数 typedef struct{ double ratio;int exp;} ElemType;typedef struct LNode { ElemType data;st...

  • 相关主题精彩

    版权声明:本网站为非赢利性站点,内容来自于网络投稿和网络,若有相关事宜,请联系管理员

    Copyright © 喜物网