c 언어: 길이 순 서 를 정 하 는 기본 작업 의 실제 실현

//           .cpp :              。
//
#include "stdafx.h"
#include<iostream>
#include<stdio.h>
#include<string>
#include<iomanip>
#define STRING_SIZE 255
using namespace std;
typedef unsigned char SString[STRING_SIZE + 1];
//      
void Error(char *s);                                 //      
void Strassign_string(SString &S, char chars[]);     //    
void Clear_string(SString &s);                       //    
void Destroy_string(SString &S);                     //    
int Getlength_string(SString &s);                    //          
int Compare_string(SString &s, SString &t);          //            
void Concat_string(SString &t, SString &s1, SString &s2); //       
void Substring_string(SString &sub, SString &s, int pos, int len); //       
void Insert_string(SString &S, SString &T, int pos);   //    
void Delete_string(SString &S, int t, int pos);        //    
//      
void Error(char *s)                                 //      
{
	cout << s << endl;
	exit(1);
}
/*      :1、            ;2.            0,         
3.                ,           ,       ,      
             ,       */
void Strassign_string(SString &S, char chars[])    //    
{
	int i = 0;
	int chars_length = 0;     //     0
	while (chars[i] != '\0')  //         
	{
		chars_length++;     
		i++;
	}
	if (!chars_length)   S[0] = 0;
	else
	{
		int j = 1;
		int k = 0;
		if (chars_length > STRING_SIZE)
		{
			while(j<=STRING_SIZE)
			{
				S[j++] = chars[k++];
			}
			S[0] = STRING_SIZE;
			cout << "           ,      !" << endl;
		}
		else
		{
			while (j <= chars_length)
			{
				S[j++] = chars[k++];
			}
			S[0] = chars_length;
		}
	}
}
void Clear_string(SString &s)                     //    
{
	s[0] = 0;
}
void Destroy_string(SString &S)                    //    
{
	for (int i = 1; i <= S[0]; i++)
	{
		delete &S[i];
	}
	delete &S[0];
}
int Getlength_string(SString &s)                    //          
{
	return s[0];
}
/*              :     1  1        
1.   1   2,    ;2.   1   2,  0;3,   1   2,    */
int Compare_string(SString &s, SString &t)          //            
{
	for (int i = 1; (i <= s[0]) && (i <= t[0]); i++)
	{
		if (s[i] != t[i])
			return (s[i] - t[i]);
		else
			return (s[0] - t[0]);
	}
}
/*              :       S1 S2     ,                   。
  S1 s2     ,            :
1.S1[0]+S2[0]<=STRING_SIZE,          ;
2.S1[0]+S2[0]>STRING_SIZE, S1[0]<STRING_SIZE,      S1   ,S2   ,S2     ;
3.S1[0]=STRING_SIZE,      S1 ;
4.S1[0]>STRING_SIZE,      S1   ,S1     ;*/
void Concat_string(SString &t, SString &s1, SString &s2)//       
{
	if ((s1[0] + s1[0]) <= STRING_SIZE)
	{
		int j = 1;
		int k = 1;
		while (j <= s1[0])
		{
			t[k++] = s1[j++];
		}
		j = 1;
		while (j <= s2[0])
		{
			t[k++] = s2[j++];
		}
		t[0] = s1[0] + s2[0];
		cout << "    ,      !" << endl;
	}
	else if ((s1[0]) <= STRING_SIZE)
	{
		int j = 1;
		int k = 1;
		while (j <= s1[0])
		{
			t[k++] = s1[j++];
		}
		j = 1;
		while (j <= s2[0])
		{
			t[k++] = s1[j++];
		}
		t[0] = STRING_SIZE;
		cout << "     ,S2    !" << endl;
	}
	else if ((s1[0])== STRING_SIZE)
	{
		int j = 1;
		int k = 1;
		while (j <= s1[0])
		{
			t[k++] = s1[j++];
		}
		t[0] = STRING_SIZE;
		cout << "     ,S2     !" << endl;
	}
	else 
	{
		int j = 1;
		int k = 1;
		while (j <= s1[0])
		{
			t[k++] = s1[j++];
		}
		t[0] = s1[0];
		cout << "     ,S1      !" << endl;
	}

}
void Substring_string(SString &sub, SString &s, int pos, int len) //       
{
	if ((pos<1) || (pos>s[0]) || (len < 0) || (len>(s[0] - pos + 1)))
		Error("      ,     !");
	for (int i = 1; i <= len; i++)
	{
		sub[i] = s[pos + i - 1];
	}
	sub[0] = len;
}
void Insert_string(SString &S, SString &T, int pos)//        pos   
{
	if ((S[0] + T[0]) <= STRING_SIZE)
	{
		int j = 0;
		for (int i = (S[0]+T[0]); i >= pos; i--) //         ,       
		{   
			S[i] = S[S[0] - j];
			j++;
		}
		j = 1;
		for (int i = pos; i < (pos + T[0]); i++)  //      pos      
		{
			S[i] = T[j];
			j++;
		}
		S[0] = S[0] + T[0];
	}
} 
void Delete_string(SString &S, int t, int pos)  //    pos     t   
{
    for (int i = pos; i < (S[0] - pos + 1); i++)
		{
			S[i] = S[i+t];
		}
		S[0] = S[0] - t;
}
int _tmain(int argc, _TCHAR* argv[])
{
	char a[50] = { "abc" };
	SString S1;
	Strassign_string(S1, a);
	cout << " S1    :" << Getlength_string(S1) << endl;
	char b[50] = { "ABCDEFGHIJKLMN"};
	SString S2;
	Strassign_string(S2, b);
	cout << " S2    :" << Getlength_string(S2) << endl;
	cout << "         :" << Compare_string(S1, S2) << endl;
	Insert_string(S2, S1, 3);
	cout << "        :";
	for (int i = 1; i <= S2[0]; i++)
	{
		cout << S2[i];
	}
	cout << endl;
	Delete_string(S2, 3, 2);
	cout << "        :";
	for (int i = 1; i <= S2[0]; i++)
	{
		cout << S2[i];
	}
	cout << endl; 
	SString S3;
	Concat_string(S3, S1, S2);
	cout<<"          :" << Getlength_string(S3) << endl;
	cout << "        :";
	for (int i = 1; i <= S3[0]; i++)
	{
		cout << S3[i];
	}
	cout << endl;
	SString S4;
	Substring_string(S4, S3, 3, 5);
	cout << "      :";
	for (int i = 1; i <= S4[0]; i++)
	{
		cout << S4[i];
	}
	cout << endl;
	return 0;
}

좋은 웹페이지 즐겨찾기