C++程序测量一个字符串中的单词个数(多种方法).doc_第1页
C++程序测量一个字符串中的单词个数(多种方法).doc_第2页
C++程序测量一个字符串中的单词个数(多种方法).doc_第3页
C++程序测量一个字符串中的单词个数(多种方法).doc_第4页
C++程序测量一个字符串中的单词个数(多种方法).doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

#includevoid main()char str81;int i,num=0,word=0;char c;coutplease input the string:n;gets(str);for(i=0;(c=stri)!=0;i+)if(c= )word=0;else if(word=0)word=1;num+;coutThere are %d words in the line.nnum;这个练习题不错,如果把空格换成非字符符号,可以用来统计文章中的单词数。#include#includeint main()char str50;int word;int n=0;int i;printf(Input:);gets(str);for(i=0;i50-1;i+)if(stri= )word=1;if(word)n+;word=0;printf(Output: There are is %d in teh line.n,n);system(pause);return 0;给你个思路吧。设定一个字符数组,或者直接用string对象,从键盘接收一个字符串到该字符数组或字符串对象中。然后设一个变量i用以遍历字符串,如果遇到第i位是空格或者标点,则空格或标点数加1,并检查第i-1位是否为字母,如果i-1位是字母,说明刚刚遍历过去的是一个单词,则单词数加1,否则(i-1位不是字母)就继续往下走。当然你还要设三个变量存储空格、标点和单词的数量。期间还要注意一些问题,比如字符串首位是标点或者空格的问题等等。#includeint main() char a,t; int wd=0,sp=0,pc=0,temp=0; cout请输入一行任意字符:; a=cin.get(); if (a =n) coutWord, space bar, punctuationendl; cout0, 0, 0=a&a=A&a=Z); else other+; t=a; a=cin.get(); if (t!=,|t!=.|t!=;|t!=?|t!=|t!=|t!=!) wd = sp+pc+1-temp; else wd = sp+pc-temp; coutWord, space bar, punctuationendl; coutwd, sp, pcendl; return 0; #include #include #include using namespace std;int WordsCount(string &pText);int CountWordsInText(string &pFileName) ifstream ifile; string text; int words = 0; ifile.open(pFileName.c_str(); if (ifile.is_open() while (!ifile.eof() getline(ifile, text); words += WordsCount(text); ifile.close(); return words;int WordsCount(string &pText) int words = 0; string:size_type pos = 0; pos = pText.find_first_of( , pos); while (pos != pText.npos) pos+; words+; pos = pText.find_first_of( , pos); words+; return words;int main() string file = test.txt; coutCountWordsInText(file)endl; return 0;将要测试的文本文件放在工程目录下即可。不限定候选人的情况下,可以采用链表(或者类),这样可以随时增加候选人(或类对象)。选票就采用一维数组好了,这样线性时间就可以完成。查找时间O(n),排序可以用各种排序算法,如冒泡排序。用c+编写的选票系统,并可统计相关得票情况-#include using namespace std struct Person/声明结构体类型Person; char name20 int count int main() Person leader3=aaa,0,bbb,0,ccc,0 /定义Person类型的数组,内容为当前候选人的姓名及得票数; char leader_name20 /leader_name为投票人所选的人姓名; int i,j for(i=0 ileader_name /先后输入十张票上所选的人的姓名; for(j=0 j3 j+) if(strcmp(leader_name,)=0) leaderj.count+ /所选人与候选人的姓名相同,则该候选人的票数加1; coutendl double round for(i=0 i3 i+) round=leaderi.count/10 :得票数:leaderi.count得票率:roundendl return 0 1.问题描述设有n个候选人参加选举,统计每个人最后的得票情况。2.基本要求(1)以数组作为存储结构;(2)设计统计得票算法,将最后的得票情况统计并输出。3.设计思想:可以将每个候选人设计作为一个结构类型,包括名字和得票数,将n个候选人组成一个结构数组,其存储结构定义如下:const int n=10;/假设有10个人参加选举struct Personchar *name;int count;Leadern;可以从键盘依次输入选举情况,每次输入一个人的名字,将输入的名字与结构数组Leader进行比较,将对应候选人的得票数增加1 【思考题】将该问题用C+中的类实现。代码后面最好有解释。个人建议既然你学C+就不要用char* 应该用string替换,以下是我刚才写的基于类的实现,我已经在VC6.0上测试过无误代码如下:#include#includeusing namespace std;typedef struct candidate string name; /候选人名 int votecount; /选票stCand,*pstCand;class voteprivate: pstCand pcand; /候选人数组 int candnum; /总的候选人 int nowcandnum; /现在又几个候选人public: vote(int n = 0); vote(); void addVote(const string &st); void output();vote:vote(int n) /n表示有多少个获选人 if(n=0) candnum = 0; pcand = NULL; else candnum = n; pcand = new stCandn; nowcandnum = 0;vote:vote() delete pcand; pcand = NULL;void vote:output() /输出选举结果 当然如果需要可以在输出结果后进行排序 for (int i=0;inowcandnum;i+) pcandi.votecountendl; void vote:addVote(const string &st) int flag = 0; /当前所存名单是否有该人for(int i=0;inowcandnum;) /顺序查找该人在所存储的位置 if(st = ) flag = 1; /已经找到该人 break; i+; if(flag = 1) pcandi.votecount+; else if(i candnum) /如果没有出现过 增加该人名 = st; pcandi.votecount=1; nowcandnum +; else /候选人名单已经超过所要求的候选人个数 couterror! the candidate num is overflow!endl; int main() int m = 0;coutm; string stemp; vote svote(m); /创建选举类cou

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论