一、一元稀疏多项式计算器 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

}

if(pos->e!=0)

{

cout << "x^";

cout

}

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

{

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;

不知道是不是你需要的

一元稀疏多项式计算器C语言编写

二、php如何用strstr查找多个字符比如$aa="1234567@0"...

strstr — 查找字符串的首次出现string strstr ( string $haystack , mixed $needle [, bool $before_needle = false ] )返回 haystack 字符串从 needle 第一次出现的位置开始到 haystack 结尾的字符串。 $needle只能是字符(串)或者数字.不然会提醒:needle is not a string or an integer如果一定要用strstr查询字符串 $aa中是否包含1或者2或者@ 可以下面这样:$aa = "1234567@0";$search = array(1,2,'@');function test($str , $array_search){ foreach($array_search as $value){ if(strstr($str , $value)!==false){ return true; } } return false;}//使用方法$test = test($aa,$search);//true如果你仅仅想确定 needle 是否存在于 haystack 中,请使用速度更快、耗费内存更少的 strpos() 函数。建议使用正则表达式简单些如:$aa = "1234567@0";$search = '/[12@]/';if(preg_match($search , $aa)){ //包含1或者2或者@}else{ //不包含}参考PHP手册网址:http://php.net/manual/zh/function.strstr.php

三、php通过strpos查找字符串出现位置的方法

本文实例讲述了php通过strpos查找字符享给大家供大家参考。具体分析如下:

strpos用来查找一个字符串位置,strpos区分大小写,如果没有找,所以strpos有两种类形,一种是bool型,开发过程中需要注意

输出结果:6

由于strpos有所以在判断是否找到子字符串的的时候最好使用===三个等号进行严格类型的相等比较

上面

This script will print: Not found based (==) test Found based (===) test The (===) test is c

希望本文所述对大家的php程序设计有所帮助。

本站所有文章资讯、展示的图片素材等内容均为注册用户上传(部分报媒/平媒内容转载自网络合作媒体),仅供学习参考。 用户通过本站上传、发布的任何内容的知识产权归属用户或原始著作权人所有。如有侵犯您的版权,请联系我们反馈本站将在三个工作日内改正。