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

📄 strarray.cpp

📁 《C++程序设计习题及解答》配套代码VC版
💻 CPP
字号:
// strarray.cpp

#include "stdafx.h"
#include "strarray.h"
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

// -------------------------------------------------------------
const int Max = 100;
String stringList[Max];
int size = 0;

// -------------------------------------------------------------
int ReadString( String& s )
{
	const int bufsize = 100;
	static char buffer[ bufsize ];

	cin.getline( buffer, 100 );
	if ( cin.eof() )
		return 0;

	if ( !s )
	{
		delete s;
		s = NULL;
	}

	s = new char[ strlen(buffer) + 1 ];
	if ( !s )
	{
		cerr << "内存分配出错!" << endl;
		return 0;
	}

	strcpy( s, buffer );

	return 1;
}

// -------------------------------------------------------------
void Output( String a[] )
{
	cout << "\nSorted string list:\n";
	for ( int i = 0; i < size; i++ )
		cout << a[i] << endl;
}

// -------------------------------------------------------------
void Insert( String strList[], String& str )
{
	if ( size == Max )
	{
		cerr << "数组溢出!\n";
		exit(1);
	}

	int i, j;
	i = Search( strList, str );

	for ( j = size-1; j >= i; j-- )
		strList[j+1] = strList[j];

	strList[i] = str;

	size++;
}

// -------------------------------------------------------------
int Search( String strList[], String& str )
{
	for ( int i = 0; i < size; i++ )
		if ( strcmp( strList[i], str ) > 0 )
			return i; // found
	return size; // not found
}

⌨️ 快捷键说明

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