C++第五次作業

劉鑫磊rr發表於2020-10-24

文章目錄:

一:C++統計一段英文文字中英文單詞的個數(主函式中實現功能)

程式碼實現

執行結果

二:C++用一個string型別的變數(準確來講,稱為物件)s儲存一大段中文或英文文字,實現在其中查詢並統計某詞語t出現的次數。基本演算法思想(使用陣列下標訪問的方法:通過下標的線性移動來訪問字串的各個字元)統計某單詞例如”the”在文中出現的頻率

方法一:通過下標的線性移動來訪問字串的各個字元

 程式碼實現

執行結果

方法二:直接用函式

執行結果

三:C++在文字中查詢某個字詞並替換為另一個字詞

方法一:直接利用函式

程式碼實現

執行結果 

方法二:使用陣列下標訪問的方法通過下標的線性移動來訪問字串的各個字元


一:C++統計一段英文文字中英文單詞的個數(主函式中實現功能)

	char s[]=	"Space, \"tab\", linefeed, carriage-return, formfeed, vertical-tab, and newline "
	"characters are called \"white-space characters\" because they serve the same "
	"purpose as the spaces between words and lines on a printed page --- they make "
	"reading easier. Tokens are delimited (bounded) by white-space characters and by "
	"other tokens, such as operators and punctuation. When parsing code, the C compiler "
	"ignores white-space characters unless you use them as separators or as components "
	"of character constants or string literals. Use white-space characters to make a program "
	"more readable. Note that the compiler also treats comments as white space Yirenxp.";

程式碼實現

#include <iostream> 
#include <cmath>
#include <cstring>
#include <ctype.h> 
using namespace std; 


int main()
{
	
	int i=0;//通過陣列下標訪問字元
	bool readalpha=0;//表示讀到的是否是字元
	unsigned words=0;//統計單詞個數
	
	char s[1000]="Space, \"tab\", linefeed, carriage-return, formfeed, vertical-tab, and newline "
	"characters are called \"white-space characters\" because they serve the same "
	"purpose as the spaces between words and lines on a printed page --- they make "
	"reading easier. Tokens are delimited (bounded) by white-space characters and by "
	"other tokens, such as operators and punctuation. When parsing code, the C compiler "
	"ignores white-space characters unless you use them as separators or as components "
	"of character constants or string literals. Use white-space characters to make a program "
	"more readable. Note that the compiler also treats comments as white space Yirenxp.";


	for (i = 0; i < 1000; i++)
	{
		if (  isalpha( s[i] ))
		{
			readalpha=1;
			words++;
		}else
		{
			 readalpha=0;
		}
	}
	cout <<"單詞的個數為:"<<words<<endl;
	system("pause");
    return 0;
}


執行結果

二:C++用一個string型別的變數(準確來講,稱為物件)s儲存一大段中文或英文文字,實現在其中查詢並統計某詞語t出現的次數。基本演算法思想(使用陣列下標訪問的方法:通過下標的線性移動來訪問字串的各個字元)統計某單詞例如”the”在文中出現的頻率

方法一:通過下標的線性移動來訪問字串的各個字元

 程式碼實現

#include <iostream>
#include <cstring>
#include <cmath>
using namespace std;

int main(){	
	string s="As promised in the first edition of this book, C++ has been evolving to meet "
			"the needs of its users.This evolution has been guided by the experience of users of widely "
			"varying backgrounds working in a great range of application areas. The C++ user community "
			"has grown a hundredfold during the six years since the first edition of this book; many "
			"lessons have been learned, and many techniques have been discovered and/or validated "
			"by experience. Some of these experiences are reflected here. The primary aim of the "
			"language extensions made in the last six years has been to enhance C++ as a language "
			"for data abstraction and objectoriented programming in general and to enhance it as a tool "
			"for writing highquality libraries of user defined types in particular. A \"highquality library,\" is "
			"a library that provides a concept to a user in the form of one or more classes that are "
			"convenient, safe, and efficient to use. In this context, safe means that a class provides "
			"a specific typesafe interface between the users of the library and its providers; efficient "
			"means that use of the class does not impose significant overheads in runtime or space on "
			"the user compared with handwritten C code.";

	string t="the";

	bool flag=0; 
	unsigned count=0;
	int i_s=0,i_a=0,i_t=0;

	do
	{	
		i_a = i_s;						// i_a為輔助,最初跟隨i_s
		i_t =0;							// i_t作為訪問t串字元的下標,先指向其首部
			do
			{	
				if(s[i_a]==t[i_t])		//此處還要考慮大小寫的問題,思考怎麼改?
				{	
					flag=1;				//bool型別的flag,若對應字元相等則賦值為1

					i_a++ ;	
					i_t ++;
				}
				else					//對應字元不等,說明不匹配,跳出內層do-while
				{	flag=0;
					i_s++; 
					i_a = i_s; 
					i_t =0;
					break;
				}
			}while(i_t <t.length());	//i_t還沒指向t串的尾部

		//內層迴圈結束後判斷flag為何值
			if(flag)
			{	
				count++;				//count統計t出現的次數
				i_s = i_s +t.length();	//i_s下移一個t串長度
				i_a = i_s; 
				i_t =0;
			}else{
				i_s ++;
				}
	}while(i_s < (s.length()-t.length()));//i_s還沒走到s尾部,為什麼要減//掉一個t.length()?


	 cout<<"單詞the"<<"出現的次數為:"<<count<<"次"<<endl;
	system("pause");
	return 0;
}

執行結果

方法二:直接用函式

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

 

int main()

{

            int n=0,sum=0;

            string s1("Space, \"tab\", linefeed, carriage-return, formfeed, vertical-tab, and newline "
	"characters are called \"white-space characters\" because they serve the same "
	"purpose as the spaces between words and lines on a printed page --- they make "
	"reading easier. Tokens are delimited (bounded) by white-space characters and by "
	"other tokens, such as operators and punctuation. When parsing code, the C compiler "
	"ignores white-space characters unless you use them as separators or as components "
	"of character constants or string literals. Use white-space characters to make a program "
	"more readable. Note that the compiler also treats comments as white space Yirenxp.");

            string s2("the");

            string temp=s1;

            while (1){ //迴圈:查詢到子串就把子串刪去

                        n=temp.find(s2);//返回子串的位置

                        if (n!=-1)//n=-1表示未找到子串
						{ 
							//開一個新的字串變數temp儲存刪去子串後的字串
                             temp=temp.substr(n+s2.length(),temp.length()-s2.length());   

                              sum++;//出現次數
                        }
						else
						{
							break;
						}
            }

            cout<<"單詞"<<s2<<"出現的次數為:"<<sum<<"次"<<endl;
			system("pause");
			return 0;

}

執行結果

三:C++在文字中查詢某個字詞並替換為另一個字詞

方法一:直接利用函式

程式碼實現

#include <iostream> 
#include <cmath>
#include <string>
using namespace std; 





int main()
{
    
	int p;//儲存找到的字元位置
	string str,world;//儲存要查詢的字串,查詢的單詞變數

	cout << "請輸入一個字串:"<<endl;
	cin>>str;
	cout <<"請輸入要查詢的字串:"<<endl;
	cin>>world;



	//查詢單詞在字串中的起始位置
	p=str.find(world);//儲存在字元變數p中
	while (p==-1)
	{
		cout << "抱歉,字串中沒有你想要查詢的字串:"<<endl;
		str.replace(p,world.size(),"-------");
	}
	cout <<str<<endl;
	system("pause");
    return 0;
}


執行結果 

 

方法二:使用陣列下標訪問的方法通過下標的線性移動來訪問字串的各個字元

相關文章