数据结构与算法分析 第5章 数据结构与算法分析 Larry N.pdf

上传人:qwe****56 文档编号:69621460 上传时间:2023-01-07 格式:PDF 页数:6 大小:56.85KB
返回 下载 相关 举报
数据结构与算法分析 第5章 数据结构与算法分析 Larry N.pdf_第1页
第1页 / 共6页
数据结构与算法分析 第5章 数据结构与算法分析 Larry N.pdf_第2页
第2页 / 共6页
点击查看更多>>
资源描述

《数据结构与算法分析 第5章 数据结构与算法分析 Larry N.pdf》由会员分享,可在线阅读,更多相关《数据结构与算法分析 第5章 数据结构与算法分析 Larry N.pdf(6页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、Chapter 5 20 Chapter 5:Standard C+Input/Output and String ClassesExercises 5.21.int stringCount(const string&str,const string&target)/*-Find the number of times string str appears in string target.-*/int matches=0;int position=target.find(str);while(position!=string:npos)matches+;position=target.fin

2、d(str,position+1);return matches;2.string monthName(int monthNumber)/*-Convert a month number to its corresponding name.-*/switch(monthNumber)case(1):return January;case(2):return February;case(3):return March;case(4):return April;case(5):return May;case(6):return June;case(7):return July;case(8):re

3、turn August;case(9):return September;case(10):return October;case(11):return November;case(12):return December;default:cout nMonth:illegal month number:monthNumber endl;return;3.#include /isupper(),tolower()int monthNumber(string monthName)/*-Convert a month name to its corresponding number.-*/for(i

4、nt i=0;i monthName.length();i+)if(isupper(monthNamei)monthNamei=tolower(monthNamei);Chapter 5 21 if(monthName=january)return 1;if(monthName=february)return 2;if(monthName=march)return 3;if(monthName=april)return 4;if(monthName=may)return 5;if(monthName=june)return 6;if(monthName=july)return 7;if(mon

5、thName=august)return 8;if(monthName=september)return 9;if(monthName=october)return 10;if(monthName=november)return 11;if(monthName=december)return 12;cout nMonthNumber:illegal month name:monthName endl;return 0;4.#include /islower(),toupper(),isupper(),tolower()string lowerToUpper(string str)/*-Find

6、 uppercase equivalent of a string str.-*/for(int i=0;i str.length();i+)if(islower(stri)stri=toupper(stri);return str;string upperToLower(string str)/*-Find lowercase equivalent of a string str.-*/for(int i=0;i str.length();i+)if(isupper(stri)stri=tolower(stri);return str;Chapter 5 22 5.string replac

7、eAll(string str,string substring,string newSubstring)/*-Find a string str with all occurrences of substring replaced by newSubstring.-*/int pos=-1;for(;)pos=str.find(substring,pos+1);if(pos=string:npos)return str;str.replace(pos,substring.length(),newSubstring);6.string nameChange(string firstName,s

8、tring middleName,string lastName)/*-Construct a string of the form lastName,firstName,middle-initial.from strings firstName,middleName,lastName.-*/return lastName+,+firstName+middleName.substr(0,1)+.;7.#include /isspacestring nameChange(string name)/*-Construct a string of the form LastName,FirstNam

9、e MiddleInitial.from a string name of the form FirstName MiddleName LastName-*/unsigned endFirst,startMiddle,startLast;endFirst=name.find(,0);startMiddle=endFirst+1;while(isspace(namestartMiddle)startMiddle+;startLast=name.find(,startMiddle);while(isspace(namestartLast)startLast+;return name.substr(

10、startLast,name.length()-1)+,+name.substr(0,endFirst+1)+name.substr(0,1)+.;Chapter 5 23 8.int parseInt(const string&s,bool&success)/*-Convert a string into an integer Precondition:s contains a string of digits,possibly preceded by+or-.Postcondition:Corresponding integer is returned and success is tru

11、e if conversion is possible;otherwise,-1 is returned and success is false.-*/success=true;char sign=+;string local=s;if(s0=+|s0=-)sign=s0;local.erase(0,1);/strip sign bool allDigits=true;for(int index=0;index local.length();index+)if(!isdigit(localindex)allDigits=false;break;if(allDigits)int holder=

12、atoi(local.data();if(sign=-)return-holder;else return holder;else success=false;/indicate poorly formed string return -1;9./*Convert string to real number.Note:scientific or decimal format may be used*/#include#include Chapter 5 24 double parseReal(const string&s,bool&success)/*-Convert a string int

13、o a real number.Note:scientific or decimal format may be used.Precondition:s contains a string of characters in a real value Postcondition:Corresponding real value is returned and success is true if conversion is possible;otherwise,-1 is returned and success is false.-*/success=true;char sign=+;stri

14、ng local=s;if(s0=+|s0=-)sign=s0;local.erase(0,1);/strip sign bool allDigits=isdigit(local0);int numE=0;int numP=0;for(int index=0;index 1|numE 1)break;if(allDigits&numP=1&numE=1)double holder=atof(local.data();if(sign=-)return-holder;else return holder;else success=false;/indicate poorly formed stri

15、ng return -1;Chapter 5 25 10.bool isAPalindrome(const string&str)/*-Determine if string str is a palindrome;that is,it does not change when the order of the characters in the string is reversed.-*/int strLength=str.length(),limit=strLength/2;for(int i=0;i limit;i+)if(stri!=strstrLength-i-1)return fa

16、lse;return true;11./*-Determine if two strings are anagrams;that is,one string str1 is a permutation of the characters of the other string str2.-*/bool areAnagrams(string str1,string str2)string temp=str2;int pos;for(int i=0;i str1.length();i+)pos=temp.find(str1.substr(i,1);if(pos=string:npos)return false;temp.erase(pos,1);return true;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 财经金融

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁