|
发表于 2004-3-5 10:17:00
|
显示全部楼层
多谢大家的帮助. 献丑一下, 将最后的代码发上来, 请多指教.
[code:1]//****************************************************************
// SPECIFICATION FILE (prog1.h)
// This file gives the specification of a CharType abstract data
// type and provides I/O port for accessing the class member.
//****************************************************************
#ifndef CHAR_H // Avoid multiple inclusion
#define CHAR_H // of header files
using namespace std;
class VowelType
{
public:
int countVowels(int count, char str[]); // read from the array
// count the vowel
void outString(char str[]); // output the result
private:
int count;
char str;
};
#endif
[/code:1]
[code:1]//********************************************
// SPECIFICATION FILE (prog1.cpp)
// This file reads the input from the screen
// and counts how many vowels in that input.
// output the result to the screen
//
// Tues & Thur
//********************************************
#include <iostream>
#include "prog1.h"
using namespace std;
int main()
{
VowelType Vowel;
char str[255];
int i; //control the array length
int count=0;
int loopCount;
int loop = 0;
cout << "How many strings you would want to input-->: ";
cin >> loopCount;
cin.get();
// control how many strings have to do.
while(loop < loopCount)
{
cout << "\nEnter your sentence here--> ";
fgets(str, 255, stdin);
i = strlen(str)-1;
if(str[i]=='\n')
str[i] = '\0';
cout << endl << loop+1;
cout << "\nThe sentence " <<'"'<< str << '"'<< " has " << Vowel.countVowels(count, str) << " vowels."<< endl;
Vowel.outString(str);
cout << endl;
loop++;
}
cin.get();cin.get();
return 0;
}
[/code:1]
[code:1]// Implimentation File
// Define Class function members
#include <iostream>
#include "prog1.h"
using namespace std;
// Read from the array, compare every character with vowel letters one by one.
int VowelType::countVowels(int count, char str[])
{
int i;
for (i=0; i < 255; i++)
{
if (str[i]=='A' || str[i]=='a' || str[i]=='E' || str[i]=='e'
|| str[i]=='I' || str[i]=='i' || str[i]=='O'
|| str[i]=='o' || str[i]=='U' || str[i]=='u' )
count++;
}
return count;
}
// Output the result
void VowelType::outString(char str[])
{
int i;
cout << "The words are:\n";
for (i=0; i < strlen(str); i++)
{
if ((str[i] >= 65 && str[i] <= 90) ||
(str[i] >= 97 && str[i] <= 122))
cout << str[i];
else if (str[i]==' ')
cout << '\n';
}
}[/code:1] |
|