佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 903|回复: 9

C++的问题!!

[复制链接]
发表于 14-3-2006 08:19 PM | 显示全部楼层 |阅读模式
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
回复

使用道具 举报


ADVERTISEMENT

 楼主| 发表于 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?
小弟真的很笨,对不起哦!
回复

使用道具 举报

Follow Us
发表于 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, 应付考试。。而做。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 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之间的联系,不知哪位大大可以解释一下吗?
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 22-9-2024 04:15 AM , Processed in 0.098658 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表