C++언어 데이터 구조 문자열 의 기본 동작 인 스 턴 스 코드

C 언어 데이터 구조 열의 기본 조작 실례 코드
출력 결과:

구현 코드:

#include<iostream>
using namespace std;
typedef int Status;
#define Max 20
#define OK 1
#define ERROR 0
#define OVERLOE -2
typedef struct//      
{
 char *ch;
 int length;
}HString;
//===================================================================
Status CreatHString(HString &H)//     
{
 H.length = 0;
 H.ch = (char *)malloc(Max*sizeof(char));
 for (int i = 0; i < Max; i++)
 {
 H.ch[i]=getchar();
 H.length++;
 if (getchar() == '
') break; } return OK; }//CreatHString //=================================================================== Status PrintHString(HString H)// { if (H.length == 0) { cout << " !" << endl; return ERROR; } else for (int i = 0; i < H.length; i++) cout << H.ch[i] << " "; cout << endl; return OK; }//PrintHString //=================================================================== Status HStringLength(HString H)// { cout << " :" << endl; cout << H.length << endl; return OK; }//HStringLength //=================================================================== Status HStringCompare(HString H, HString T)// ( ) { cout << " :" << endl; int L; L = H.length - T.length; if (L<0) cout << -L << endl; if (L>=0) cout << L << endl; return OK; }//HStringCompare //=================================================================== Status ConcatHString(HString &S, HString H, HString T)// H T { if (!(S.ch = (char *)malloc((H.length + T.length)*sizeof(char)))) exit(OVERLOE); for (int i = 0; i < H.length; i++) S.ch[i] = H.ch[i]; S.length = H.length + T.length; for (int j = H.length; j < S.length; j++) S.ch[j] = T.ch[j-H.length]; return OK; }//ConcatHString //=================================================================== Status SubHString(HString &Sub, HString S, int pos,int len) {// Sub S pos len if (pos<1 || pos>S.length) { cout << " !" << endl; return ERROR; } if (len<0 || len>S.length - pos + 1) { cout << " !" << endl; return ERROR; } if (!len) { Sub.ch == NULL; Sub.length = 0; } else { Sub.ch = (char *)malloc(len*sizeof(char)); for (int i = 0; i < len ; i++) Sub.ch[i] = S.ch[pos + i - 1]; Sub.length = len; } return OK; }//SubHString //=================================================================== Status ClearHString(HString &H)// H { if (H.ch) { free(H.ch); H.ch = NULL; } H.length = 0; return OK; }//ClearHString //=================================================================== void main() { HString S,H,T; cout << " ( ):" << endl; CreatHString(H); cout << " :" << endl; PrintHString(H); HStringLength(H); cout << " ( ):" << endl; CreatHString(T); HStringCompare(H, T); ConcatHString(S, H, T); cout << " :" << endl; PrintHString(S); HString Sub; int pos, len; cout << " pos len:" << endl; cin >> pos >> len; SubHString(Sub, S, pos, len); cout << " :" << endl; PrintHString(Sub); ClearHString(S); cout << " S :" << endl; PrintHString(S); }//main

좋은 웹페이지 즐겨찾기