|
Write a C++ program that reads input a word at a time until alone q is entered. The program
should then report the number of words that began with vowels, the number that began with
consonants, and the number that fit neither of those categories. One approach is to use
isalpha() to discriminate between words beginning with letters and those that don’t and then
use an if or switch statement to further identify those passing the isalpha() test that begin with
vowels. A sample run might look like this:
Enter words (q to quit)
:
The 12 awesome oxen ambled
quietly across 15 meters of lawn.
q
5 words beginning with vowels
4 words beginning with consonants
2 others |
|
|
|
|
|
|
|

楼主 |
发表于 14-3-2006 08:20 PM
|
显示全部楼层
小弟做的!
#include <iostream>
#include <cstring>
#include <conio.h>
using namespace std;
int main()
{
string v="AEIOU",b;
bool z;
int vovel=0,con=0, other=0;
char g;
int i=0;
while(2==2)
{
b+=static_cast<char>(getch());
if(b[i]=='q')
break;
else
{ cout<<b[i];
i++;}
}
cout<<b<<endl;
cout<<b.length()<<endl;
system("pause");
// b="The 12 awesome oxen ambled quietly across 15 meters of lawn.";
for(int i=0;i<b.length();i++)
{
b[i]=toupper(b[i]);
z=true;
if(i==0)
{ if(isalpha(b[i]))
{ for(int j=0;j<v.length();j++)
{ cout<<b[i]<<" "<<v[j]<<endl;
if(b[i]==v[j])
{ vovel++;z=false; } }
if(z) con++;}
else
other++;
}
else if(b[i]==' ')
{
i++;
b[i]=toupper(b[i]);
if(isalpha(b[i]))
{ for(int j=0;j<v.length();j++)
{ cout<<b[i]<<" "<<v[j]<<endl;
if(b[i]==v[j])
{ vovel++;z=false; } }
if(z) con++;}
else
other++;
}
}
cout<<"VOVEL = "<<vovel<<endl;
cout<<"CON= "<<con<<endl;
cout<<"other = "<<other<<endl;
system("pause");
return 0;
} |
|
|
|
|
|
|
|

楼主 |
发表于 14-3-2006 08:22 PM
|
显示全部楼层
到底要怎样才能做到题目要的呢?
小弟是新手,这是我的assignment 1其中一题哦!很难了! |
|
|
|
|
|
|
|
发表于 15-3-2006 11:56 AM
|
显示全部楼层
你应该用C++的 token concept (to count how many word in sentence and break it to pieces/word), then 用strcmp() (compare the pieces/word with first string element only to determine vokel or cons...)
问题不难,deitel 写的program with c 有。 |
|
|
|
|
|
|
|
发表于 18-3-2006 05:52 PM
|
显示全部楼层
使用VECTOR功能。
利用PUSH_BACK来记录各STRING。
然后,可以使用STRNCPY将CSTRING换去CHAR。
不用说应该都知道,ISALPHA ISDIGIT ISPUNCT怎样用吧?
或用STRCMP。
我对asimo所说的TOKEN METHOD没有概念,好像更复杂,因为需要建立很多STRING来做比较。 |
|
|
|
|
|
|
|

楼主 |
发表于 19-3-2006 01:05 AM
|
显示全部楼层
原帖由 轩辕民 于 18-3-2006 05:52 PM 发表
使用VECTOR功能。
利用PUSH_BACK来记录各STRING。
然后,可以使用STRNCPY将CSTRING换去CHAR。
不用说应该都知道,ISALPHA ISDIGIT ISPUNCT怎样用吧?
或用STRCMP。
我对asimo所说的TOKEN METHOD没有概 ...
版主,
使用VECTOR功能。
利用PUSH_BACK来记录各STRING。
然后,可以使用STRNCPY将CSTRING换去CHAR。
这些我都不会哦!要怎样弄才可以呢?
怎样用PUSH_BACK来记录各STRING?
小弟真的很笨,对不起哦! |
|
|
|
|
|
|
|
发表于 19-3-2006 01:57 AM
|
显示全部楼层
原帖由 vwyk 于 19-3-2006 01:05 发表
版主,
使用VECTOR功能。
利用PUSH_BACK来记录各STRING。
然后,可以使用STRNCPY将CSTRING换去CHAR。
这些我都不会哦!要怎样弄才可以呢?
怎样用PUSH_BACK来记录各STRING?
小弟真的很笨,对不起 ...
有一个东西叫GOOGLE。
有一本课本叫PROBLEM SOLVING WITH C++。
你没有买课本,是你的错。
因为那本书很有用,而且是基础入门的必要课本。
在CP2也有用到。
我希望你学IT不是只为了应付考试,而是真的能了解如何学习。
更希望你在之后能了解,IT不是WEBPAGE而已。
我们是ENGINEER,不是随随便便找来写一些程序的阿猫阿狗。
如果我们读IT,特别是SE和ISE的,那么,NASA的太空计划,PARIS的TRAIN SYSTEM等都不需要找SOFTWARE ENGINEER,他们随便找一个只学过PRACTICLE的中学生也可以了。 |
|
|
|
|
|
|
|
发表于 19-3-2006 03:07 AM
|
显示全部楼层
原帖由 轩辕民 于 19-3-2006 01:57 AM 发表
有一个东西叫GOOGLE。
有一本课本叫PROBLEM SOLVING WITH C++。
你没有买课本,是你的错。
因为那本书很有用,而且是基础入门的必要课本。
在CP2也有用到。
我希望你学IT不是只为了应付考试,而是真的能 ...
说的也是,,,,
目前一直都是为了assignment, 应付考试。。而做。。。 |
|
|
|
|
|
|
|
发表于 19-3-2006 03:31 AM
|
显示全部楼层
This sample from http://planetsourcecode.com/vb/s ... d=9295&lngWId=3 is about how to use token. This concept is break the sentence to pieces(word).
// This code split a given string into tokens
// author: Gonzales Cenelia
#include <string.h>
#include <stdio.h>
#include <assert.h>
#define MAX_TOKENS 20
#define MAX_SIZE 30
// list of delimiters
char *delim = " ?.!,";
int tokenize(char *str, char token[][MAX_SIZE]);
bool isdelim(char c);
int main()
{
char token[MAX_TOKENS][MAX_SIZE] = {0};
char *str = "I ate my dog.";
// spliting the current string "str"
int nTokNum = tokenize(str, token);
// prints the result on the screen
for(int i = 0; i < nTokNum; ++i)
{
printf("%s\n", token);
}
return 0;
}
// check to see if a given character is a delimiter
bool isdelim(char c)
{
bool bRetVal = 0;
for(int i = 0; delim != 0; ++i)
{
if(delim == c)
{
bRetVal = 1;
break;
}
}
return bRetVal;
}
// this function split a given string into tokens
// the tokens are then saves into an array: "token"
int tokenize(char *str, char token[][MAX_SIZE])
{
char buffer[MAX_SIZE] = {0};
char temp[2] = {0};
int toknum = 0;
for(int i = 0, j = 0; str != 0; ++i)
{
if(!isdelim(str))
{
// to avoid buffer overflow
assert(i < MAX_SIZE);
sprintf(temp, "%c", str);
strcat(buffer, temp);
}
else if(strlen(buffer) > 0)
{
// the number of tokens needs to be
// less than "MAX_TOKENS"
assert(j < MAX_TOKENS);
strcpy(token[j], buffer);
buffer[0] = 0; j++;
}
}
// updating number of tokens
toknum = j;
// returning the number of tokens found
return toknum;
} |
|
|
|
|
|
|
|

楼主 |
发表于 19-3-2006 08:52 PM
|
显示全部楼层
不好意思哦!我lec要得课本是object-oriented programming in C++,所以我就买这个而已!
i got refer back bout vector, but stil dono hw it function, when we need to use vector ? hw it function as array tat grows n shrinks as needeD?
刚刚search了lib geoweb,知道lib有这本书,明天会去看!
vector n push_back之间的联系,不知哪位大大可以解释一下吗? |
|
|
|
|
|
|
| |
本周最热论坛帖子
|