我想让该程序在allchar上划分char的数量,但是不这样做? [英] i want that this program divide the number of char on allchar but it doesnt do?

查看:143
本文介绍了我想让该程序在allchar上划分char的数量,但是不这样做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include "StdAfx.h"
#include<string>
#include<iostream>
using namespace std ;

int main()
{
 
	string sentence = " " ;
	
	string aa[100]  ;
	float a=0,b=0,c=0,d=0,e=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0,r=0,s=0,t=0,u=0,v=0,w=0,x=0,y=0,z=0;
	int ii=0;
	//int counter = 0 ;
	float maxlenght = sentence.length() - 1 ;

	cout << "Enter your sentences ( End the sentence with . ): " << endl ;
	getline(cin,sentence);


	cout << " Your sentence has " << sentence.length() - 1 << " charactures ." << endl ;

	for(ii=0 ; ii<sentence.length();ii++)
	{
		
	if( sentence[ii] == ''a'' )
	{
		a++;	
	}
	if( sentence[ii] == ''b'' )
	{
		b++;	
	}
	if( sentence[ii] == ''c'' )
	{
		c++;	
	}
	if( sentence[ii] == ''d'' )
	{
		d++;	
	}
	if( sentence[ii] == ''e'' )
	{
		e++;	

	}
	if( sentence[ii] == ''f'' )
	{
		f++;	
	}
	if( sentence[ii] == ''g'' )
	{
		g++;	
	}
	if( sentence[ii] == ''h'' )
	{
		h++;
	}
	if( sentence[ii] == ''i'' )
	{
		i++;	
	}
	if( sentence[ii] == ''j'' )
	{
		j++;	
	}
	if( sentence[ii] == ''k'' )
	{
		k++;	
	}
	if( sentence[ii] == ''l'' )
	{
		l++;
	}
	if( sentence[ii] == ''m'' )
	{
		m++;	
	}
	if( sentence[ii] == ''n'' )
	{
		n++;	
	}	
	if( sentence[ii] == ''o'' )
	{
		o++;	
	}	
	if( sentence[ii] == ''p'' )
	{
		p++;	

	}
    if( sentence[ii] == ''q'' )
	{
		q++;	
	}
    if( sentence[ii] == ''r'' )
	{
		r++;	
	}
	if( sentence[ii] == ''s'' )
	{
		s++;	
	}
	if( sentence[ii] == ''t'' )
	{
		t++;	
	}
					
	if( sentence[ii] == ''u'' )
	{
		u++;	
	}
	if( sentence[ii] == ''v'' )
	{
		v++;	
	}
	if( sentence[ii] == ''w'' )
	{
		w++;	
	}
	if( sentence[ii] == ''x'' )
	{
		x++;	
	}
	if( sentence[ii] == ''y'' )
	{
		y++;	
	}
	if( sentence[ii] == ''z'' )
	{
		z++;	
	}
	}
	cout<<"a "<<a<<endl;
	cout<<"b "<<b<<endl;
	cout<<"c "<<c<<endl;
	cout<<"d "<<d<<endl;
	cout<<"e "<<e<<endl;
	cout<<"f "<<f<<endl;
	cout<<"g "<<g<<endl;
	cout<<"h "<<h<<endl;
	cout<<"i "<<i<<endl;
	cout<<"j "<<j<<endl;
	cout<<"k "<<k<<endl;
	cout<<"l "<<l<<endl;
	cout<<"m "<<m<<endl;
	cout<<"n "<<n<<endl;
	cout<<"o "<<o<<endl;
	cout<<"p "<<p<<endl;
	cout<<"q "<<q<<endl;
	cout<<"r "<<r<<endl;
	cout<<"s "<<s<<endl;
	cout<<"t "<<t<<endl;
	cout<<"u "<<u<<endl;
	cout<<"v "<<v<<endl;
	cout<<"w "<<w<<endl;
	cout<<"x "<<x<<endl;
    cout<<"y "<<y<<endl;
	cout<<"z "<<z<<endl;
	float hh  ;
	 hh = a / maxlenght  ;
	cout << "yay" << hh;
}

推荐答案

此代码有太多错误.我建议您花更多的时间阅读编程指南并学习:
如何使用整数进行计数,
如何将字符(或计算值)用于数组索引
如何设计合理的循环和决策块
等等.
There are too many things wrong with this code. I suggest you spend a lot more time reading your programming guides and learn:
how to use integers for counting,
how to use characters (or calculated values) for array indices
how to design sensible loops and decision blocks
etc.


哦,亲爱的.

给您的建议:设置两个数组:一个数组中有26个字符,由字符串"abcdefgh ..."填充.另一个有26个整数,都预设为零.
对于输入字符串中的每个字符,请遍历字符串并查看是否在其中.如果是,则递增数组中相应的整数.
然后在打印它们时,再次循环遍历,打印字符和计数:
Oh dear.

A suggestion for you: Set up two arrays: one with 26 characters in, filled from a string "abcdefgh...". The other with 26 integers, all preset to zero.
For each character in the input string, loop through the characters string and see if it is in there. Increment the corresponding integer in the array if it is.
Then when you print them, loop through again, printing the character and the count:
for (int iCount = 0; iCount < acCharsLengthl iCount++)
   {
   cout<<acChars[iCount]<<" appeared "<<aiCounts[iCount]<<" times."<<endl;
   }

您的代码将更小,更快,更易于维护.然后尝试找出另一个问题是什么!

Your code will be smaller, quicker, and easier to maintain. Then try to work out what your other problem is!


您知道有更紧凑的方法可以完成工作(并且永远不要使用float来计数整数).例如

You know there are more compact ways to get the job done (and never use float to count integers quantities). For instance

int count[256] = {0};
for(int n=0 ; n<sentence.length();n++)>
{
  count[sentence[n]]++;
}
for (int n=0; n<256; n++)
{
  if ( isalpha((char)n))
  {
    cout << (char) n << " has " <<  count[n] << " occurrences " << endl;
  }
}


这篇关于我想让该程序在allchar上划分char的数量,但是不这样做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆