第8週実践プロジェクト2—チェーン列のアルゴリズムライブラリの構築
9929 ワード
/*
Copyright (c)2015,
All rights reserved.
: 2.cbp
:
:2015 11 15
:v1.0
: , , 。
:
:
*/
listring.h :
<pre name="code" class="cpp">#ifndef LISTRING_H_INCLUDED
#define LISTRING_H_INCLUDED
#include <stdio.h>
#include <malloc.h>
typedef struct snode
{
char data;
struct snode *next;
} LiString;
void StrAssign(LiString *&s,char cstr[]); // cstr s
void StrCopy(LiString *&s,LiString *t); // t s
bool StrEqual(LiString *s,LiString *t); //
int StrLength(LiString *s); //
LiString *Concat(LiString *s,LiString *t); //
LiString *SubStr(LiString *s,int i,int j); //
LiString *InsStr(LiString *s,int i,LiString *t) ; //
LiString *DelStr(LiString *s,int i,int j); //
LiString *RepStr(LiString *s,int i,int j,LiString *t); //
void DispStr(LiString *s); //
#endif // LISTRING_H_INCLUDED
listring.cppファイルコード:
main.cppファイルコード:<pre name="code" class="cpp">#include "listring.h" void StrAssign(LiString *&s,char cstr[]) // cstr s { int i; LiString *r,*p; s=(LiString *)malloc(sizeof(LiString)); r=s; //r for (i=0;cstr[i]!='\0';i++) { p=(LiString *)malloc(sizeof(LiString)); p->data=cstr[i]; r->next=p;r=p; } r->next=NULL; } void StrCopy(LiString *&s,LiString *t) // t s { LiString *p=t->next,*q,*r; s=(LiString *)malloc(sizeof(LiString)); r=s; //r while (p!=NULL) // t s { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } r->next=NULL; } bool StrEqual(LiString *s,LiString *t) // { LiString *p=s->next,*q=t->next; while (p!=NULL && q!=NULL && p->data==q->data) { p=p->next; q=q->next; } if (p==NULL && q==NULL) return true; else return false; } int StrLength(LiString *s) // { int i=0; LiString *p=s->next; while (p!=NULL) { i++; p=p->next; } return i; } LiString *Concat(LiString *s,LiString *t) // { LiString *str,*p=s->next,*q,*r; str=(LiString *)malloc(sizeof(LiString)); r=str; while (p!=NULL) // s str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } p=t->next; while (p!=NULL) // t str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } r->next=NULL; return str; } LiString *SubStr(LiString *s,int i,int j) // { int k; LiString *str,*p=s->next,*q,*r; str=(LiString *)malloc(sizeof(LiString)); str->next=NULL; r=str; //r if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s)) return str; // for (k=0;k<i-1;k++) p=p->next; for (k=1;k<=j;k++) // s i j str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } r->next=NULL; return str; } LiString *InsStr(LiString *s,int i,LiString *t) // { int k; LiString *str,*p=s->next,*p1=t->next,*q,*r; str=(LiString *)malloc(sizeof(LiString)); str->next=NULL; r=str; //r if (i<=0 || i>StrLength(s)+1) // return str; for (k=1;k<i;k++) // s i str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } while (p1!=NULL) // t str { q=(LiString *)malloc(sizeof(LiString)); q->data=p1->data; r->next=q;r=q; p1=p1->next; } while (p!=NULL) // *p str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } r->next=NULL; return str; } LiString *DelStr(LiString *s,int i,int j) // { int k; LiString *str,*p=s->next,*q,*r; str=(LiString *)malloc(sizeof(LiString)); str->next=NULL; r=str; //r if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s)) return str; // for (k=0;k<i-1;k++) // s i-1 str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } for (k=0;k<j;k++) // p next j p=p->next; while (p!=NULL) // *p str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data; r->next=q;r=q; p=p->next; } r->next=NULL; return str; } LiString *RepStr(LiString *s,int i,int j,LiString *t) // { int k; LiString *str,*p=s->next,*p1=t->next,*q,*r; str=(LiString *)malloc(sizeof(LiString)); str->next=NULL; r=str; //r if (i<=0 || i>StrLength(s) || j<0 || i+j-1>StrLength(s)) return str; // for (k=0;k<i-1;k++) // s i-1 str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data;q->next=NULL; r->next=q;r=q; p=p->next; } for (k=0;k<j;k++) // p next j p=p->next; while (p1!=NULL) // t str { q=(LiString *)malloc(sizeof(LiString)); q->data=p1->data;q->next=NULL; r->next=q;r=q; p1=p1->next; } while (p!=NULL) // *p str { q=(LiString *)malloc(sizeof(LiString)); q->data=p->data;q->next=NULL; r->next=q;r=q; p=p->next; } r->next=NULL; return str; } void DispStr(LiString *s) // { LiString *p=s->next; while (p!=NULL) { printf("%c",p->data); p=p->next; } printf("
"); }<span style="line-height: 17.27272605895996px; background-color: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none;">#include "listring.h" int main() { LiString *s1,*s2,*s3,*s4,*s5; printf(" :
"); printf(" (1) s1 s2
"); StrAssign(s1,"ABCDEFGHIJKLMN"); printf(" (2) s1:"); DispStr(s1); printf(" (3) s1 :%d
",StrLength(s1)); StrAssign(s2,"^_^"); printf(" (4) s2:"); DispStr(s2); printf(" (5) s2 :%d
",StrLength(s2)); printf(" (6) s1 9 s2 s3
"); s3=InsStr(s1,9,s2); printf(" (7) s3:"); DispStr(s3); printf(" (8) s3 :%d
",StrLength(s3)); printf(" (9) s1 2 3 s3
"); s3=DelStr(s1,2,3); printf(" (10) s3:"); DispStr(s3); printf(" (11) s3 :%d
",StrLength(s3)); printf(" (12) s1 2 5 s2 s3
"); s3=RepStr(s1,2,5,s2); printf(" (13) s3:"); DispStr(s3); printf(" (14) s2 :%d
",StrLength(s3)); printf(" (15) s1 2 10 s4
"); s4=SubStr(s1,2,10); printf(" (16) s4:"); DispStr(s4); printf(" (17) s4 :%d
",StrLength(s4)); printf(" (18) s2 s3 s5
"); s5=Concat(s2,s3); printf(" (19) s5:"); DispStr(s5); printf(" (20) s5 :%d
",StrLength(s5)); return 0; } </span><span style="line-height: 17.27272605895996px; background-color: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none;"> :</span>
<span style="line-height: 17.27272605895996px; background-color: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; margin: 0px; padding: 0px; border: none;"><img src="http://img.blog.csdn.net/20151115213613058" alt="" /> </span><span style="line-height: 17.27272605895996px; background-color: inherit; font-family: Consolas, 'Courier New', Courier, mono, serif; font-size: 11.818181991577148px; color: rgb(85, 85, 85);"> </span>