C++ primer plus第六版课后编程题答案6.7


 

6.7

#include<iostream>
#include<cctype>
using namespace std;

void main67()
{
	const int Size=10;
	int vowels=0;	//存储个数
	int consonants=0;
	int other=0;
	cout<<"Enter words (q to quit)"<<endl;
	char temp[Size];
	char ch='0';
	char t;	//读取数组内单个字符
	do{
		//while(ch!='13'&&ch!='\n')
		//{
			cin>>temp;
			cout<<temp<<" "<<endl;
			t=temp[0];
			//int j=0;
			//while(t!='\0') //判断元音开头
			//{ 这里我默认输入的都是单词或者数字,即不会出现如 aag125sa这类的输入
			if(!isalpha(t))
					other++;
			else if(t=='a'||t=='e'||t=='i'||t=='o'||t=='u')
					vowels++;
			else
					consonants++;
			//}
			
			if(temp[0]=='q'&&strlen(temp)==1)	//判断是否为q至关重要的一步
				break;
			
		//}
		/**
		cout<<"vowels="<<vowels<<endl;
		cout<<"consonants="<<consonants<<endl;
		cout<<"other="<<other<<endl;
		cout<<"ch== "<<ch<<endl;
		*/
	}while(true);

	cout<<vowels<<"  words beginning with vowels"<<endl;
	cout<<consonants<<" words beinning with consonants"<<endl;
	cout<<other<<" others"<<endl;
	system("pause");


}

 

 


发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注