一个有关于C++的问题

zhangyongnihao1 2009-06-21 10:58:49
录入一篇英文文章(放在字符数组a[n]中),统计单词个数。并依次输出各个单词(用指针的一维数组编程)。
假定文章不超过1000个字符,单词不超过240个。
请自我录入结束条件。
我需要一篇完整的程序,谢谢,这道题的原题还需要按照单词的长度依次输出,但是我原题的程序有错误改不了。我希望能帮我先把上面简单的程序解决了 我好改下面得代码。或者告诉我下面代码的错误。谢谢


最好是能帮我重新做一个上面简单的代码。谢谢


这是原代码:
#include <iostream>
#include <cctype>
#define N 1000
using namespace std;
int main()
{
char a[N+1], ch;
int i, j, k, m, n;
int anum[240], loc[240];
//anum[i]存放第i个单词的长度,loc[i]存放第i个单词在a[]中的开始位置
i = 0;
while ((ch=getchar())!='#')//输入
{
a[i] = ch;
i++;
}
a[i] = 0;
k = 0;
for (j=0; j <i; j++)
{
while (!isalpha(a[j]) && j <i)//排除非字符
{
j++;
}
if (j>=i)//输入最后为非字符的时候在此退出循环
{
break;
}
loc[k] = j;//记录第k个单词的位置
m=0;
while (isalpha(a[j]) && j <i)//求第k个单词的长度
{
j++;
m++;
}
anum[k] = m;//记录长度
k++;
}
j = 1;
m = k;//m=k为总的单词个数
while (m)//查询anum[],分别输出长度为1、2、3.。。的单词
{//所以只是按照单词长度和出现次序排序,没管大小,而且没动原数组
for (i=0; i <k; i++)
{
if (anum[i] == j)
{
for (n=0; n <j; n++)
{
putchar(*(a+loc[i]+n));
}
putchar('\n');
m--;
}
}
j++;
}

return 0;
}
...全文
48 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
jn989 2009-06-21
  • 打赏
  • 举报
回复
或者lz网上搜搜词法分析方面的代码,很多的,就不帮lz写了,如:
http://www.136z.com/download/VC/1463.html
jn989 2009-06-21
  • 打赏
  • 举报
回复
这就是一个词法分析程序嘛,看看编译方面的书就会了。
pysjp 2009-06-21
  • 打赏
  • 举报
回复
SF 帮楼主顶了
lpf000 2009-06-21
  • 打赏
  • 举报
回复
putchar 应该是 stdio.h中的
lpf000 2009-06-21
  • 打赏
  • 举报
回复
修改了楼主的一点点,VS08运行没问题呢
#include <stdio.h> 
#include <cctype>
#define N 1000
using namespace std;
int main()
{
char a[N+1], ch;
int i, j, k, m, n;
int anum[240], loc[240];
//anum[i]存放第i个单词的长度,loc[i]存放第i个单词在a[]中的开始位置
i = 0;
while ((ch=getchar())!='#')//输入
{
a[i] = ch;
i++;
}
a[i] = 0;
k = 0;
for (j=0; j <i; j++)
{
while (!isalpha(a[j]) && j <i)//排除非字符
{
j++;
}
loc[k] = j;//记录第k个单词的位置
m=0;
while (isalpha(a[j]) && j <i)//求第k个单词的长度
{
j++;
m++;
}
anum[k] = m;//记录长度
k++;
}
j = 1;
m = k;//m=k为总的单词个数
while (m)//查询anum[],分别输出长度为1、2、3.。。的单词
{//所以只是按照单词长度和出现次序排序,没管大小,而且没动原数组
for (i=0; i <k; i++)
{
if (anum[i] == j)
{
for (n=0; n <j; n++)
{
putchar(*(a+loc[i]+n));
}
putchar('\n');
m--;
}
}
j++;
}

return 0;
}
yang_e_2009 2009-06-21
  • 打赏
  • 举报
回复
写的不好 LZ随便看看

#include <iostream>
#include <cctype>
#include <stdio.h>
#define N 1001
#define W 101
using namespace std;
int main()
{
char all[N]; //全部的字符
int word[W] = {0}; //单词的位置
int size_word[W] = {0}; //单词的大小
int a_cnt,word_cnt = 0;
for(a_cnt = 0; (a_cnt == 0 || all[a_cnt-1]!= '#') && a_cnt != N; ++a_cnt)
{
all[a_cnt] = getchar(); //读入单词
if(a_cnt == 0) //是否第一个单词
{
if(isalpha(all[a_cnt]))
{
++word_cnt; //单词数加一
word[word_cnt-1] = a_cnt; //记录位置
size_word[word_cnt-1] = 1; //大小为1
}
continue;
}
if(isalpha(all[a_cnt])) //是否为一个字符
if(!isalpha(all[a_cnt-1])) //前一个是否不为字符 不是字符则是新的单词
{
++word_cnt; //单词数加一
word[word_cnt-1] = a_cnt; //记录位置
size_word[word_cnt-1] = 1; //大小还原为1
}else{
++size_word[word_cnt-1]; //大小加1
}
}
all[a_cnt] = '\0';
int t = word_cnt; //剩余单词
for(int size = 1; true; ++size) //输出
{
for(int it = 0; it != word_cnt; ++it)
if(size_word[it] == size)
{
for(int ix = word[it]; ix != word[it]+size_word[it]; ++ix)
putchar(all[ix]);
--t;
putchar('\n');
}
if(t == 0)
break;
}
system("PAUSE");
return 0;
}
  • 打赏
  • 举报
回复


/*textQuery.h*/

#include <iostream>
#include <vector>
#include <map>
#include <set>
#include <string>

using namespace std;

class TextQuery
{
public:
typedef vector< string >::size_type line_no;
typedef map< string,set<line_no> >::iterator map_word_iterator;
void read_file(ifstream &is)
{
store_file(is);
build_map();
}
set< line_no > run_query(const string &) const;
string text_line(line_no) const;
private:
void store_file(ifstream &);
void build_map();
vector< string > lines_of_text;
map< string,set< line_no > > word_map;
};


/*textQuery.cpp*/

#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <string>

#include "textQuery.h"

using namespace std;

void TextQuery::store_file(ifstream &is)
{
string textline;
while (getline(is, textline))
lines_of_text.push_back(textline);
}

void TextQuery::build_map()
{
for(line_no line_num = 0; line_num != lines_of_text.size(); ++line_num)
{
istringstream line(lines_of_text[line_num]);
string word;
while (line >>word)
word_map[word].insert(line_num);
}
}

set< TextQuery::line_no > TextQuery::run_query(const string &query_word) const
{
map< string, set< line_no > >::const_iterator loc = word_map.find(query_word);
if (loc == word_map.end())
return set<line_no>();
else
return loc->second;
}

string TextQuery::text_line(line_no line) const
{
if (line <lines_of_text.size())
return lines_of_text[line];
throw out_of_range("line number out of ranger");
}

/*main.cpp*/

#pragma warning(disable:4786)
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <map>
#include <set>
#include <string>

#include "textQuery.h"

using namespace std;

typedef set<TextQuery::line_no> line_nums;

void print_results(const set<TextQuery::line_no> &locs, const string &sought, const TextQuery &file);

ifstream& open_file(ifstream &in, const string &file);

int main()
{
ifstream infile;
if ( !open_file(infile, "text_in.txt") )
{
cerr << "No input file!" << endl;
return EXIT_FAILURE;
}
TextQuery tq;
tq.read_file(infile);
while (true)
{
cout << "enter word to look for,or q for qiut: ";
string s;
cin >> s;
if(!cin || s == "q")
break;
set<TextQuery::line_no> locs =tq.run_query(s);
print_results(locs, s, tq);
}
return 0;
}

void print_results(const set<TextQuery::line_no> &locs, const string &sought, const TextQuery &file)
{
line_nums::size_type size = locs.size();
cout << "\n" <<sought << " occurs " << size << " " <</*make_plural(size, "time" , "s") <<*/endl;
line_nums::const_iterator it = locs.begin();
for( ; it != locs.end(); ++it)
{
cout <<"\t(line " << (*it) +1 << ")" <<file.text_line(*it) <<endl;
}

}


ifstream& open_file(ifstream &in, const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}

  • 打赏
  • 举报
回复
用C写太累,用C++容器做,倒是可以贴代码给你。

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧