《數(shù)據(jù)結(jié)構(gòu)(C語言版)》串的堆分配存儲
清華大學(xué)計算機系列教材? ?累計發(fā)行超400萬冊
嚴(yán)蔚敏 吳偉民 編著
數(shù)據(jù)結(jié)構(gòu)(C語言版)
以下是本人對該紫皮書第四章串中4.2.2節(jié)串的堆分配存儲表示的代碼實現(xiàn),把課本上提到的關(guān)于這一節(jié)的全部函數(shù)均實現(xiàn)了,函數(shù)部分與課本完全相同
(貌似專欄把我縮進吃了,懶得加了,另外建議用visual studio編譯,會幫你自動調(diào)整縮進)
(預(yù)計如果全部更新完會出一個代碼合集的壓縮包,附上鏈接以供下載,里面格式為.txt便于大家閱讀,希望大家多多支持點個免費的贊)
代碼如下:
//串的堆分配存儲表示
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1
#define FALSE 0
#define OK 1
#define ERROR 0
#define INFEASIBLE -1
#define OVERFLOW -2
typedef int Status;
typedef struct
{
char* ch;//若是非空串,則按串長分配存儲區(qū),否則ch為NULL
int length;//串長度
}HString;
Status StrAssign(HString& T, char* chars);//生成一個其值等于串常量的串T
int StrLength(HString S);//返回S的元素個數(shù),稱為串的長度
int StrCompare(HString S, HString T);//依據(jù)ASCII碼值比較兩個字符串大小并返回其差值
Status ClearString(HString& S);//將S清為空串
Status Concat(HString& T, HString S1, HString S2);//用T返回S1和S2聯(lián)接而成的新串
Status SubString(HString& Sub, HString S, int pos, int len);//返回串S的第pos個字符起長度為len的字串
void ShowString(HString S);//打印字符串
void Menu()
{
printf("************************\n");
printf("********0.退? 出********\n");
printf("********1.生成串********\n");
printf("********2.求串長********\n");
printf("********3.比較串********\n");
printf("********4.清空串********\n");
printf("********5.連接串********\n");
printf("********6.求子串********\n");
printf("************************\n");
printf("請輸入你想使用的功能:");
}
int main()
{
int option;
char strA[100];
char strB[100];
HString MyStringA;
MyStringA.ch = NULL;?
MyStringA.length = 0;
HString MyStringB;
MyStringB.ch = NULL;
MyStringB.length = 0;
HString ConnectStr;
ConnectStr.ch = NULL;
ConnectStr.length = 0;
Menu();
while (scanf("%d", &option) != EOF)
{
while (getchar() != '\n');
switch (option)
{
case 1:
printf("請輸入字符串A:");
scanf("%100s", strA);
StrAssign(MyStringA, strA);
printf("字符串A為:");
ShowString(MyStringA);
break;
case 2:
printf("字符串A的長度為%d\n", StrLength(MyStringA));
break;
case 3:
printf("請輸入字符串B:");
scanf("%100s", strB);
StrAssign(MyStringB, strB);
printf("字符串A為:");
ShowString(MyStringA);
printf("字符串B為:");
ShowString(MyStringB);
printf("兩者之差為:%d\n", StrCompare(MyStringA, MyStringB));
break;
case 4:
ClearString(MyStringA);
printf("字符串A已清空!\n");
break;
case 5:
printf("請輸入字符串B:");
scanf("%100s", strB);
StrAssign(MyStringB, strB);
Concat(ConnectStr, MyStringA, MyStringB);
printf("連接后的字符串為:");
ShowString(ConnectStr);
break;
case 6:
printf("尋找A的給定長度的子串,請輸入子串的位置和長度:");
int position, length;
scanf("%d%d", &position, &length);
HString sub;
sub.ch = NULL;
if (SubString(sub, MyStringA, position, length))
{
ShowString(sub);
}
else
{
printf("子串位置錯誤!\n");
}
break;
case 0:
printf("感謝使用!");
exit(0);
default:
printf("非法輸入,請重試!\n");
}
Menu();
}
return 0;
}
Status StrAssign(HString& T, char* chars)//生成一個其值等于串常量的串T
{
if (T.ch)//釋放T原有空間
{
free(T.ch);
}
int i;
char* c;
for (i = 0, c = chars; *c; i++, c++);//求chars的長度i
if (!i)//如果串常量為空
{
T.ch = NULL;
T.length = 0;
}
else
{
if (!(T.ch = (char*)malloc(i * sizeof(char))))
{
exit(OVERFLOW);
}
for (int j = 0; j < i; j++)
{
T.ch[j] = chars[j];
}
T.length = i;
}
return OK;
}
int StrLength(HString S)//返回S的元素個數(shù),稱為串的長度
{
return S.length;
}
int StrCompare(HString S, HString T)//依據(jù)ASCII碼值比較兩個字符串大小并返回其差值
{
for (int i = 0; i < S.length && i < T.length; i++)
{
if (S.ch[i] != T.ch[i])
{
return S.ch[i] - T.ch[i];
}
}
return S.length - T.length;
}
Status ClearString(HString& S)//將S清為空串
{
if (S.ch)
{
free(S.ch);
S.ch = NULL;
}
S.length = 0;
return OK;
}
Status Concat(HString& T, HString S1, HString S2)//用T返回S1和S2聯(lián)接而成的新串
{
if (T.ch)
{
free(T.ch);
}
if (!(T.ch = (char*)malloc((S1.length + S2.length) * sizeof(char))))
{
exit(OVERFLOW);
}
for (int i = 0; i < S1.length; i++)
{
T.ch[i] = S1.ch[i];
}
T.length = S1.length + S2.length;
for (int i = S1.length; i < T.length; i++)
{
T.ch[i] = S2.ch[i - S1.length];
}
return OK;
}
Status SubString(HString& Sub, HString S, int pos, int len)//返回串S的第pos個字符起長度為len的字串
{
if (pos<1 || pos>S.length || len<0 || len>S.length - pos + 1)
{
return ERROR;
}
if (Sub.ch)
{
free(Sub.ch);
}
if (!len)
{
Sub.ch = NULL;
Sub.length = 0;
}
else
{
if (!(Sub.ch = (char*)malloc(len * sizeof(char))))
{
exit(OVERFLOW);
}
for (int i = 0; i < len; i++)
{
Sub.ch[i] = S.ch[i + pos - 1];//由于c語言數(shù)組從0開始存取,故要向前移一位
}
Sub.length = len;
}
return OK;
}
void ShowString(HString S)//打印字符串
{
if (!S.ch)
{
printf("該字符串為空串!\n");
}
for (int i = 0; i < S.length; i++)
{
putchar(S.ch[i]);
}
printf("\n");
}