小程序集.docx

上传人:b****0 文档编号:18084214 上传时间:2023-08-13 格式:DOCX 页数:19 大小:18.40KB
下载 相关 举报
小程序集.docx_第1页
第1页 / 共19页
小程序集.docx_第2页
第2页 / 共19页
小程序集.docx_第3页
第3页 / 共19页
小程序集.docx_第4页
第4页 / 共19页
小程序集.docx_第5页
第5页 / 共19页
小程序集.docx_第6页
第6页 / 共19页
小程序集.docx_第7页
第7页 / 共19页
小程序集.docx_第8页
第8页 / 共19页
小程序集.docx_第9页
第9页 / 共19页
小程序集.docx_第10页
第10页 / 共19页
小程序集.docx_第11页
第11页 / 共19页
小程序集.docx_第12页
第12页 / 共19页
小程序集.docx_第13页
第13页 / 共19页
小程序集.docx_第14页
第14页 / 共19页
小程序集.docx_第15页
第15页 / 共19页
小程序集.docx_第16页
第16页 / 共19页
小程序集.docx_第17页
第17页 / 共19页
小程序集.docx_第18页
第18页 / 共19页
小程序集.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

小程序集.docx

《小程序集.docx》由会员分享,可在线阅读,更多相关《小程序集.docx(19页珍藏版)》请在冰点文库上搜索。

小程序集.docx

小程序集

/************************小程序一**************************/

/*文档名:

C++编写的fibonacci数列*/

/*作者:

赵军伟版本1.0*/

#include

#include

voidmain()

{

longinti,n,*p=newlongint[500];

*p=1;

*(p+1)=1;

cout<<"enteryounumber:

";

cin>>n;

cout<<*p<<"\t\t"<<*(p+1)<<"\t\t";

p=p+2;

for(i=3;i<=n;i++)

{

*p=*(p-1)+*(p-2);

cout<<*p<<"\t\t";

if(i%5==0)

cout<

p++;

}

}

/**********************************************************/

/************************小程序二**************************/

/**********************************************************/

/*文档名:

C编写的fibonacci数列*/

/*作者:

赵军伟版本1.0*/

#include

voidmain()

{

longinti,n,a[500];

a[1]=1;

a[2]=1;

printf("pleaseinputn:

\n");

scanf("%d",&n);

printf("%ld\t\t%ld\t\t",a[1],a[2]);

p=p+2;

for(i=3;i<=n;i++)

{

a[i]=a[i-1]+a[i-2];

printf("%ld\t\t",a[i]);

if(i%5==0)

printf("\n");;

p++;

}

}

 

fibonacci数列的特点

11235

813213455

89144233377610

9871597258441816765

F1=F2=1;

F(n)=F(n-1)+F(n-2);(n>=3)

/**********************************************************/

/************************小程序三**************************/

/**********************************************************/

/*文件名:

条件语句的使用——求一元二次方程的根*/

/*作者:

赵军伟*/

/*版本:

1.0*/

/*日期:

2009.6.17*/

#include

#include

main()

{

floata,b,c,x1,x2;

floatr,s;

printf("\n\tpleaseinputthevalueofa!

\n\ta=");

scanf("%f",&a);

printf("\n\tpleaseinputthevalueofb!

\n\tb=");

scanf("%f",&b);

printf("\n\tpleaseinputthevalueofc!

\n\tc=");

scanf("%f",&c);

printf("\n\tthevalueofabcis:

");

printf("\n\ta=%f\tb=%f\tc=%f\n",a,b,c);

r=b*b-4.0*a*c;

if(r>0.0)

{

s=sqrt(r);

x1=(-b+s)/(2.0*a);

x2=(-b-s)/(2.0*a);

printf("\n\treal:

x1=%15.7f\n\tx2=%15.7f\n\t",x1,x2);

}

elseif(r==0)

{

printf("\n\tdouble:

x1=x2=%15.7f\n\t",-b/(2.0*a));

}

else

{

x1=-b/(2.0*a);

x2=sqrt(-r)/(2.0*a);

printf("\n\tcomplex:

Re=%15.7f\n\tIm=%15.7f",x1,x2);

}

while

(1);

}

/**********************************************************/

/************************小程序四**************************/

/**********************************************************/

/*文档名:

汉诺塔问题的c语言实现*/

/*作者:

赵军伟版本1.0*/

#include

voidmain()

{

voidhanoi(intn,charone,chartwo,charthree);

intm;

printf("inputthenumberofdiskes:

");

scanf("%d",&m);

printf("thestepatomoving%ddiskes:

\n",m);

hanoi(m,'A','B','C');

printf("\n");

}

voidhanoi(intn,charone,chartwo,charthree)

{

voidmove(intn,charx,chary);

if(n==1)

move(n,one,three);

else

{

hanoi(n-1,one,three,two);

move(n-1,one,three);

hanoi(n-1,two,one,three);

}

}

voidmove(intn,charx,chary)

{

inti;

for(i=1;i<=n;i++)

{

printf("%c-->%c\t",x,y);

if(i%10==0)

printf("\n");

}

}

注:

试的时候不要输入过大的数,我输了20,等了有5分钟,才计算完毕。

 

/**********************************************************/

/************************小程序五**************************/

/**********************************************************/

/*文件名:

关于幂数的计算*/

/*作者:

赵军伟*/

/*版本:

1.0*/

/*日期:

2009.7.14*/

#include

#include

 

main()

{

intx,n,p;

intpower(intx,intn);

printf("\n\tcalculateXtothepowerofN\n\tpleaseinputXandN:

\n\tX=");

scanf("%d",&x);

printf("\tN=");

scanf("%d",&n);

p=power(x,n);

printf("\n\t%dtothepowerof%dis:

%d",x,n,p);

while

(1);

}

 

intpower(intx,intn)

{

inti=0,p=1;

for(i=0;i

{

p=p*x;

}

/*do---while语句实现

do

{

p=p*x;

++i;

}

while(i

return(p);

}

/**********************************************************/

/************************小程序六**************************/

/**********************************************************/

/*文档名:

用c语言实现1~100求和的问题*/

/*作者:

赵军伟*/

/*版本:

1.0*/

/*时间:

2009..5.27*/

 

/*用do—while语句构成的循环计算自然数1~100的累加*/

#include

 

main()

{

inti,s=0;

i=1;

do/*复合语句循环体*/

{

s=s+i;

i++;

}/*循环体结束*/

while(i<=100);

printf("\n\t1+2+3+......+99+100=%d\n",s);

while

(1);

}

/*用while语句构成的循环计算自然数1~100的累加*/

#include

 

main()

{

inti,s=0;

i=1;

while(i<=100)/*复合语句循环体*/

{

s=s+i;

i++;

}/*循环体结束*/

printf("\n\t1+2+3+......+99+100=%d\n",s);

while

(1);

}

 

/*用for语句构成的循环计算自然数1~100的累加*/

#include

 

main()

{

inti,s=0;

for(i=1;i<=100;i++)

s=s+i;

printf("\n\t1+2+3+......+99+100=%d\n",s);

while

(1);

}

 

/*用for语句缺省结构构成的循环计算自然数1~100的累加*/

#include

 

main()

{

inti,s=0;

i=1;/*设置循环初值*/

for(;i<=100;)/*缺省初值设定表达式和更新表达式*/

{

s=s+i;/*循环体语句*/

i++;/*循环控制变量更新*/

}

printf("\n\t1+2+3+......+99+100=%d\n",s);

while

(1);

}

/**********************************************************/

/************************小程序七**************************/

/**********************************************************/

/******************************************************************************

*文件名:

实验3.c

*文件描述:

试编写一程序,实现KMP算法,输入三组主串S和模式串P,输出模式串的

*Next(j)函数值,

*以及该P在S中的位置的定位函数值,即序号值。

其中S的长度为15~25,P的长度为5~8。

*创建人:

赵军伟2009.11.15

******************************************************************************/

#include

#include

structnode

{

chardata;

structnode*next;

};

structnode*input()

{

structnode*str;

structnode*p,*r;

charx;

intn=0;

/*str=NULL;*/

scanf("%c",&x);

while(x!

='\n')

{

n=n+1;

p=(structnode*)malloc(sizeof(structnode));

p->data=x;

p->next=NULL;

if(n==1)

{

str=p;

}

else

{

r->next=p;

}

r=p;

scanf("%c",&x);

}

returnstr;

}

voidOutput(structnode*str)

{

structnode*p;

p=str;

while(p!

=NULL)

{

printf("%c",p->data);

p=p->next;

}

printf("\n");

}

structnode*LinkStrMatch(structnode*T,structnode*P)

{

structnode*shift,*t,*p;

inti=0;

shift=T;

t=shift;p=P;

while(t&&p)

{

if(t->data==p->data)

{

t=t->next;

p=p->next;

i++;

}

else

{

shift=shift->next;

t=shift;

p=P;

i++;

}

}

i++;

if(p==NULL)

{

printf("\tKMPsuccess!

!

!

\tTheaddressis%d\n",i-StrLen(P)+1);

returnshift;

}

else

{

printf("\tKMPFail!

!

!

\n");

returnNULL;

}

}

intGetNext(structnode*T)

{

inti=1,j=0,k=0,t;

inttLen=StrLen(T);

charp[100];

charnext[100]="";

for(k=0;k<=tLen;k++)

{

p[k]=T->data;

T=T->next;

}

next[1]=0;

while(i

{

if(j==0||p[i]==p[j])

{

++i;

++j;

next[i]=j;

}

else

j=next[j];

}

for(t=1;t<=StrLen(T);t++)

{

printf("%d",next[t]);

}

printf("\n");

return1;

}

intStrLen(structnode*T)

{

intlen=0;

while(T->next!

=NULL)

{

T=T->next;

len++;

}

return(len);

}

voidmain()

{

structnode*T1,*T2,*T3,*P,*C1,*C2,*C3;

intt;

T1=input();

T2=input();

T3=input();

P=input();

GetNext(P);

printf("\tT1:

");

LinkStrMatch(T1,P);

printf("\tT2:

");

LinkStrMatch(T2,P);

printf("\tT3:

");

LinkStrMatch(T3,P);

getch();

}

 

/**********************************************************/

/************************小程序八**************************/

/**********************************************************/

/******************************************************************************

*文件名:

KMP算法(顺序存储结构).c

*实验题目:

试编写一程序,实现KMP算法,输入三组主串S和模式串P,输出模式串的Next(j)函数值,以及该P在S中的位置的定位函数值,即序号值。

其中S的长度为15~25,P的长度为5~8。

*文件描述:

匹配可如下进行:

假设一指针i和j分别指示主串和模式串中的待比较字符,令i的初值为pos,j的初值为1.若在匹配过程中s[i]=t[j],则i和j分别增加1;若s[i]!

=t[j]匹配失败后,则i不变,j退到next[j]位置再进行比较,依次类推。

知道下列两种情况:

一种是j退到某个next值时字符比较相等,则i和j分别增1继续进行匹配;另一种是j退到值为零(即模式的第一个字符失配),则此时i和j也要分别增1,表明从主串的下一个字符起和模式重新开始匹配。

*创建人:

赵军伟2009.11.28

******************************************************************************/

#include

#include

#defineMaxStrLen100

typedefunsignedcharString[MaxStrLen+1];/*用定长顺序存储来表示串*/

intKMP_Matching(StringT,StringP,intnext[])

{

/*KMP模式匹配算法*/

inti=1,j=1;

while(i<=T[0]&&j<=P[0])

{

if(j==0||T[i]==P[j])

{

++i;

++j;

}

else

{

j=next[j];

}

}

if(j>T[0])

{

returni-T[0];

}

else

{

return0;

}

}

voidFindNext(StringT,intnext[])

{

/*求模式串T的next函数值并存入next数组中*/

inti=1,j=0;

next[1]=0;

while(i

{

if(j==0||T[i]==T[j])

{

++i;

++j;

next[i]=j;

}

else

{

j=next[j];

}

}

}

voidmain()

{

intre,i,next[100];

StringS1,S2;

printf("\nEnterlengthoftheStringS1andtheStringS1:

");

scanf("%d",&S1[0]);

for(i=1;i<=S1[0];i++)

{

scanf("%c",&S1[i]);

}

printf("\nEnterlengthoftheStringS1andtheStringS2:

");

scanf("%d",&S2[0]);

for(i=1;i<=S2[0];i++)

{

scanf("%c",&S2[i]);

}

FindNext(S2,next);

re=KMP_Matching(S1,S2,next);

printf("\n%d\n",re);

getch();

}

/**********************************************************/

/************************小程序九**************************/

/**********************************************************/

/*文档名:

编写一个读入一个字符串,把它存入一个链表,并按相反的次序打印的程序。

*/

/*作者:

赵军伟版本1.0*/

#include

#defineMAX10

structSalaryInfo{

charname[10];

chardepartment[15];

intbasepay;

intallowance;

inttotal;

}stuff[MAX],*q;

main()

{

FILE*fp;

inti,n;

printf("Pleaseinputthenumbersofstuffs:

\n");

scanf("%d",&n);

printf("PleaseinputtheSalaryInformationofthestuff!

\n\n");

for(i=0;i

{

scanf("%s%s%d%d",stuff[i].name,stuff[i].department,&stuff[i].basepay,&stuff[i].allowance);

stuff[i].total=stuff[i].basepay+stuff[i].allowance;}

if((fp=fopen("SalaryInfo","wt+"))==NULL)

{

printf("Cannotopenfilepressanykeyexit!

");

getch();

exit

(1);

}

q=stuff;

fwrite(q,sizeof(structSalaryInfo),1,fp);

rewind(fp);

fread(q,sizeof(structSalaryInfo),1,fp);

printf("namedepartmentbasepayallowancetotal\n");

for(i=0;i

{

q->basepay=q->basepay+100;

printf("%5s\t%5s\t%5d\t%5d\t%5d\n",q->name,q->department,q->basepay,q->allowance,

q->total=q->basepay+q->allowance);

}

getch();

fclose(fp);

}

 

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 法律资料

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2