#include "pch.h"
#include <stdio.h>
#include <math.h>
#include <string>
#include <stdlib.h>
#define MAXLEN 225
#define FALSE 0
#define TRUE 1
/*字符串順序表示(靜態存儲)*/
typedef struct {
char ch[MAXLEN];
int len;
}SString;
/*構造串*/
int StrAssign(SString *s,const char *ch)
{
int i;
if (strlen(ch)>MAXLEN) {
return FALSE;
}
else {
for (i = 0; i < strlen(ch);i++) {
s->ch[i] = ch[i];
}
s->len = strlen(ch);
return TRUE;
}
}
/*數據串插入*/
int StrInsert(SString *s,SString t,int pos) //在字符串s序號爲pos之前插入串t
{
int i;
if (pos<0||pos>s->len) {
return FALSE;
}
if (s->len+t.len<=MAXLEN) {
for (i = s->len + t.len - 1;i>=pos+t.len;i--) {
s->ch[i] = s->ch[i-t.len];
}
for (int i = 0; i < t.len;i++) {
s->ch[pos + i] = t.ch[i];
}
s->len = s->len + t.len;
}
else if (pos+t.len<=MAXLEN) {/*插入後串長>MAXLEN,但串t可以全部插入*/
for (i = MAXLEN - 1; i >= t.len + pos ;i--) {
s->ch[i] = s->ch[i-t.len];
}
for (i = 0; i < t.len;i++) {
s->ch[i + pos] = t.ch[i];
}
s->len = MAXLEN;
}
else {/*串的部分序列需要“截斷”*/
for (i = 0; i < MAXLEN - pos;i++) {
s->ch[pos+i] = t.ch[i];
}
s->len = MAXLEN;
}
return TRUE;
}
/*串刪除函數,在串中刪除從序號pos起len個字符*/
int StrDelete(SString *s,int len,int pos)
{
int i;
if (pos<0||pos>(s->len-len)) {
return FALSE;
}
for (i = pos + len; i < s->len;i++) {
s->ch[i - len] = s->ch[i];
}
s->len = s->len - len;
return TRUE;
}
/*串複製函數*/
int StrCopy(SString *s,SString t)
{
int i;
for (int i = 0; i < t.len;i++) {
s->ch[i] = t.ch[i];
}
s->len = t.len;
return TRUE;
}
/*判空函數*/
int StrEmpty(SString s)
{
if (0 == s.len) {
return TRUE;
}
else
return FALSE;
}
/*串比較函數*/
/*求串長*/
int StrLength(SString s)
{
return s.len;
}
/*清空函數*/
int StrClear(SString *s)
{
s->len = 0;
return TRUE;
}
/*串連接函數*/
int StrCat(SString *s,SString t)
{
int i;
if (s->len + t.len <= MAXLEN) {/*連接後串長小於MAXLEN*/
for (i = s->len; i < s->len + t.len; i++) {
s->ch[i] = t.ch[i - s->len];
}
s->len += t.len;
return TRUE;
}
else if (s->len < MAXLEN) {/*s的長度小於MAXLEN,但是連接後串長大於MAXLEN*/
for (i = s->len; i < MAXLEN; i++) {
s->ch[i] = t.ch[i - s->len];
}
s->len = MAXLEN;
return FALSE;/*表示截斷,無法正確連接*/
}
else
return FALSE;
}
/*求子串函數*/
int SubString(SString *sub,SString s,int pos,int len)
/*將串s中序號爲pos起,len個字符複製到sub中*/
{
int i;
if (pos<0 || pos>s.len || len < 0||len>s.len-pos) {
sub->len = 0;
return FALSE;
}
else {
for (i = 0; i < len;i++) {
sub->ch[i] = s.ch[pos+i];
}
sub->len = len;
return TRUE;
}
}
/*定位函數*/
int Index(SString s,SString t,int pos)
/*求串t在串s中的位置*/
{
int i,j;
if (s.len == 0||t.len==0) {
return 0;
}
i = pos; j = 0;
while (i<s.len&&j<t.len) {
if (s.ch[i] == t.ch[j]) {
i++;
j++;
}
else {
i = i - j + 1;
j = 0;
}
}
if (j >= t.len) {
return (i - j);
}
else
return 0;
}
void StrPrint(SString s)
{
int i;
for (i = 0; i < s.len;i++) {
printf("%c",s.ch[i]);
}
printf("\n");
}
int main()
{
/*part test*/
SString s, t,temp,sub;
int pos,i;
StrAssign(&s,"abcd");
StrAssign(&t,"efgh");
StrPrint(s);
StrPrint(t);
//StrInsert(&s,t,0);
StrCat(&s,t);
StrPrint(s);
StrAssign(&temp,"cdef");
pos = Index(s,temp,0);
printf("%d\n",pos);//2
SubString(&sub,s,0,4);
StrPrint(sub);//abcd
}
串的定長順序存儲表示與實現(c語言)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.