C++ 面向对象编程实验题:类与成员函数 字符串统计类

实验内容

  1. 功能要求:
    (1)从键盘上输入一行字符串(以回车键结束),统计该字符串中英文字符数(大小写不分)、数字字符数、空格字符数及其他字符数。
    (2)在主程序中能够产生不断循环输入字符串、统计字符的效果,如:

    1:继续输入字符串进行统计(计算不清零)
    2:继续输入字符串进行统计(清零后重新统计)
    3:退出系统

  2. 类的UML见下图

StringStatistic
-CharCount:int
-DigitCount:int
-SpaceCount:int
-OtherCount:int
+StringStatistic()
~StringStatistic()
+Analyzer(const char * s):void
+GetCharCount():int
+GetDigitCount():int
+GetSpaceCount():int
+GetOtherCount():int
+Reset():void
+DataOut():void
  • 数据成员的含义

    CharCount:统计英文字母的个数
    DigitCount:统计数字的个数
    SpaceCount:统计空格的字符个数
    OtherCount:统计其他字符的个数

  • 成员函数的含义

    StringStatistic():构造函数,涉及数据的初始值置0。
    ~StringStatistic():析构函数,仅仅输出结束的标志即可。
    Analyzer(const char * s):从键盘上读取字符串,并分析字符串s内容,分类统计的数据存入对象相应的数据中。
    GetCharCount(): 得到英文字母的个数
    GetDigitCount():得到数字字符的个数
    GetSpaceCount():得到空格字符的个数
    GetOtherCount():得到其他字符的个数
    Reset():将所有的统计数据清零
    DataOut():输出统计的结果

  1. 写成完整的程序。要求以项目的方式实现,即程序有3个文件组成,分别是类的定义、成员函数的定义、主函数的定义。

参考答案

main.cpp

#include <bits/stdc++.h>
#include <Windows.h>
#include "StringStatistic.h"
using namespace std;

int main()
{
    
    
	StringStatistic str;
	string s = "";
	string option = "1";

	while (option == "1" || option == "2")
	{
    
    
		cout << "请输入字符串:";
		getline(cin, s);

		str.analyzer(s);
		str.dataOut();

		cout <<"请输入选项:不清除结果继续统计(1);开始新一轮统计(2);退出程序(3):";
		cin >> option;

		while (option != "1" && option != "2" && option != "3")
		{
    
    
			cout << "请输入正确选项:不清除结果继续统计(1);开始新一轮统计(2);退出程序(3):";
			cin >> option;
		}

		if (option == "2")
		{
    
    
			str.reset();
		}

		cin.ignore();
		cin.clear();
	}

	cout << "Bye" << endl;
}

StringStatistic.h

#pragma once
#include <iostream>
#include <string>
using namespace std;

class StringStatistic
{
    
    
private:
	int charCount;						// 统计英文字母的个数
	int digitCount;						// 统计数字的个数
	int spaceCount;						// 统计空格的字符个数
	int otherCount;						// 统计其他字符的个数

public:
	// 构造函数,涉及数据的初始值置0
	StringStatistic();

	// 析构函数,仅输出结束的标志
	~StringStatistic();

	// 从键盘上读取字符串,并分析字符串s内容
	void analyzer(const string& s);		// C++11已经弃用char*用于声明可变长字符串了,只能使用const char*
	
	// 返回得到英文字母的个数
	inline int getCharCount();

	// 返回得到数字字符的个数
	inline int getDigitCount();

	// 返回得到空格字符的个数
	inline int getSpaceCount();

	// 返回得到其他字符的个数
	inline int getOtherCount();

	// 将所有的统计数据清零
	void reset();

	// 输出统计的结果
	void dataOut();
};

StringStatistic.cpp

#include "StringStatistic.h"

StringStatistic::StringStatistic()
{
    
    
	this->charCount = 0;
	this->digitCount = 0;
	this->otherCount = 0;
	this->spaceCount = 0;
}

StringStatistic::~StringStatistic()
{
    
    
	cout << "Class StringStatistic Destructor" << endl;
}

void StringStatistic::analyzer(const string& s)
{
    
    

	for (auto ch : s)
	{
    
    
		if (ch >= 'A' && ch <= 'Z' || ch >= 'a' && ch <= 'z')
		{
    
    
			this->charCount++;
			continue;
		}

		else if (ch >= '0' && ch <= '9')
		{
    
    
			this->digitCount++;
			continue;
		}

		else if (ch == ' ')
		{
    
    
			this->spaceCount++;
			continue;
		}

		else
		{
    
    
			this->otherCount++;
		}
	}
}

int StringStatistic::getCharCount()
{
    
    
	return this->charCount;
}

int StringStatistic::getDigitCount()
{
    
    
	return this->digitCount;
}

int StringStatistic::getSpaceCount()
{
    
    
	return this->spaceCount;
}

int StringStatistic::getOtherCount()
{
    
    
	return this->otherCount;
}

void StringStatistic::reset()
{
    
    
	this->charCount = 0;
	this->digitCount = 0;
	this->otherCount = 0;
	this->spaceCount = 0;
}

void StringStatistic::dataOut()
{
    
    
	cout << "英文字母个数:" << this->charCount << endl;
	cout << "数字个数:" << this->digitCount << endl;
	cout << "空格个数:" << this->spaceCount << endl;
	cout << "其他字符个数:" << this->otherCount << endl;
}

猜你喜欢

转载自blog.csdn.net/Ouchdex/article/details/116720409