| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2009 人关注过本帖
标题:郁闷!这道题目怎么编?
只看楼主 加入收藏
ElfDN
Rank: 4
等 级:贵宾
威 望:11
帖 子:291
专家分:0
注 册:2005-11-13
收藏
得分:0 

习惯不好,一直是在写字板上写程序,没编译


2006-01-07 19:43
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
收藏
得分:0 
那你编译一下好不好?我加了分号还是有错:
.cpp(19): error C2679: 二进制“<<” : 没有找到接受“templates”类型的右操作数的运算符(或没有可接受的转换)

我水平太差,不知道要怎么改,你帮忙看一下吧,谢谢你!
2006-01-07 20:09
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 

楼上的 头文件中加 #include<string> 就可以了编译成功了

不过好像ElfDN程序的结果必须输出每个单词呢 测试的是每一个单词的长度...应该只输出最长的单词才能满足要求吧


unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-01-07 21:40
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
收藏
得分:0 

是呀,要输出长度最大的,我加了#include<string>后,运行结果如下:
Some states use income tax in addition to sales tax to raise the their revenues.
最长单词:Some
单词长度:4
最长单词:states
单词长度:6
最长单词:states
单词长度:6
最长单词:states 最长单词:income
单词长度:6
最长单词:states 最长单词:income
单词长度:6
最长单词:states 最长单词:income
单词长度:6
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:addition
单词长度:8
最长单词:revenues.
单词长度:9

2006-01-08 04:04
unicorn
Rank: 4
等 级:贵宾
威 望:14
帖 子:1066
专家分:0
注 册:2005-10-25
收藏
得分:0 

我倒是想出一个简单的算法 不过程序有问题 请大家帮忙改下吧

#include<iostream>
#include<string>
using namespace std;
void main()
{
int max=0;//单词的最大长度,初始值为0
char m[20];//保存在循环结束后的单词
char s[20];//保存输入的字符串
char c[1];//保存字符串中的每个字符
char *temp=new char[];//保存每个单词
gets(s);//输入字符串
for(int i=0;s[i]!='\0';i++)
{

c[0]=s[i];
if(c[0]==' ')
{
if(strlen(temp)>max)//判断单词长度,并保存单词
{
max=strlen(temp);
strcpy(m,temp);
delete []temp;
char *temp=new char[];

}
}
strcat(temp,c);//把字符串中的单词一个字母一个字母的保存到temp字符数组中
}
delete []temp;
cout<<"the longest word is "<<m<<endl;
cout<<"length is "<<max<<endl;

}


unicorn-h.spaces. ◇◆ sava-scratch.spaces.  noh enol ! pue pu!w hw u! shemle aq ll!m noh 
2006-01-08 13:29
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 

#include <iostream>
#include <cstdlib>
#include <cctype> //isalpha()
#include <vector>
using namespace std;

int main()
{
vector<pair<string,int> > text; //hold all word
string word;
int max=0;
int lengh;
char ch;

cout<<"please input text :"<<endl;

//read line......

while(1)
{
cin.get(ch);

if(isalpha(ch)) //is a letter ?
word+=ch;
else
{
if(word!="")
{
lengh=word.size();
if(max < lengh)
max = lengh;
text.push_back( pair<string,int>(word, lengh) );
word="";
}
}

if(ch=='\n') break;
}

//output.............

int size=text.size();
cout<<"longest word : \n";
for(int i=0; i<size; i++)
{
if(text[i].second==max)
cout<<'\t'<<text[i].first<<'\n';
}
cout<<"lengh : "<<max<<endl;

system("pause");
return 0;
}

有简单的方法吗?

[此贴子已经被作者于2006-1-8 21:52:22编辑过]


2006-01-08 21:40
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
收藏
得分:0 
在visual studio.NET里,有语法错误。
.cpp(59): error C2676: 二进制“!=” : “std::string”不定义该运算符或到预定义运算符可接收的类型的转换

[此贴子已经被作者于2006-1-9 13:01:06编辑过]

2006-01-09 06:06
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 

我没VC++编译器,看看行不

#include <iostream>
#include <cstdlib>
#include <cctype> //isalpha()
#include <vector>
using namespace std;

int main()
{
vector<pair<string,int> > text; //hold all word
string word;
const string null_s;
int max=0;
int lengh;
char ch;

cout<<"please input text :"<<endl;

//read line......

while(1)
{
cin.get(ch);

if(isalpha(ch)) //is a letter ?
word+=ch;
else
{
if(word!=null_s)
{
lengh=word.size();
if(max < lengh)
max = lengh;
text.push_back( pair<string,int>(word, lengh) );
word="";
}
}

if(ch=='\n') break;
}

//output.............

int size=text.size();
cout<<"longest word : \n";
for(int i=0; i<size; i++)
{
if(text[i].second==max)
cout<<'\t'<<text[i].first<<'\n';
}
cout<<"lengh : "<<max<<endl;

system("pause");
return 0;
}

[此贴子已经被作者于2006-1-9 14:32:55编辑过]


2006-01-09 14:32
Grace_TT
Rank: 1
等 级:新手上路
威 望:1
帖 子:324
专家分:0
注 册:2005-12-21
收藏
得分:0 
不行。。。
2006-01-09 16:44
woodhead
Rank: 3Rank: 3
等 级:新手上路
威 望:9
帖 子:1124
专家分:0
注 册:2005-7-18
收藏
得分:0 
投降

2006-01-09 16:50
快速回复:郁闷!这道题目怎么编?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017083 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved