⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cstring.cpp

📁 C/C++程序设计导论(第二版)》程序源文件
💻 CPP
字号:
// CString.cpp   C++ String implementation file#include <math.h>#include <string.h>#include <stdlib.h>#include "CString.h"String::String ()          							// simple constructor { 	cells[0] = NULLC; }String::operator char* ()							// outward cast conversion {	return (cells); }String::String (char arg[ ])							// literal constructor { 	strncpy (cells, arg, MAX-1); }String String::operator= (String a)							// assignment of strings { 	strcpy (cells, a.cells);   	return (*this); }int String::Atoi ()							// converts text to int { 	return (atoi(cells));    					 		// just a wrapper for atoi(char*) }float String::Atof ()							// converts text to float { 	return (atof(cells));							// just a wrapper for atof(char*) }int String::Length ()							// returns length of string { 	return (strlen(cells));							// just a wrapper for strlen(char*) }void String::InputStr (istream& in)							// input function { 	in >> cells; }void String::OutputStr (ostream& out)							// output function { 	out << cells; }int String::operator== (String a)							// test for equality { 	return (!strcmp (cells, a.cells)); }int String::operator!= (String a)    							// test for inequality { 	return (strcmp (cells, a.cells)); }int String::operator< (String a)							// test for less than { 	if (strcmp (cells, a.cells) < 0)		return (1);	else return (0); }int String::operator> (String a)	   						// test for greater than { 	if (strcmp (cells, a.cells) > 0)		return (1);	else return (0); }int String::operator<= (String a)							// test for LEQ {	 if (strcmp (cells, a.cells) <= 0)		return (1);	else return (0); }int String::operator>= (String a)							// test for GEQ { 	if (strcmp (cells, a.cells) >= 0)		return (1);	else return (0); }String String::Substr (int n, int size)							// substring copy {	String temp;	if (n < MAX-1)	 {	strncpy (temp.cells, cells+n, size);		temp.cells[size] = NULLC;	 }	return (temp); }int String::Contains (String a)							// test for substring {	char *ptr;	ptr = strstr (cells, a.cells);	if (ptr == NULL) return (-1);	else return ((int)ptr - (int)cells); }String String::operator+ (String s)							// concatenation{ 	int n;  	String temp;  	strcpy (temp.cells, cells);  	n = strlen(cells);  	if (MAX-n-1 > 0) 		strncpy (temp.cells+n, s.cells, MAX-n-1);  	return (temp);}char String::Onechar (int n)							// return a single character{	if (n < strlen(cells))		return (cells[n]);	else return (-1);}void String::Onechar (int n, char c)							// replace a single character{	if (n < strlen(cells))		cells[n] = c;}istream& operator>> (istream& in, String& a)	   // prototype for overloaded >> { 	a.InputStr(in);                              							// simply calls the right	return (in);						//   member function }ostream& operator<< (ostream& out, String a)	   // prototype for overloaded << { 	a.OutputStr(out);				                  			//   simply calls the right	return (out);				                     		//   member function }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -