Skip to content

Instantly share code, notes, and snippets.

Created December 14, 2009 15:19
Show Gist options
  • Select an option

  • Save anonymous/256109 to your computer and use it in GitHub Desktop.

Select an option

Save anonymous/256109 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
const int SIZE=100;
const int SIZE1=26;
void fill_array(char a[],int SIZE,int& n);
void out_put(char a[],int& n);
void calculate(char a[],int& n,int b[]);
void swap(int& v1,int& v2);
void sort(int b[],int SIZE1);
int main()
{
char a[SIZE];
int n;
int b[SIZE1]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
fill_array(a,SIZE,n);
cout<<endl;
cout<<"以下是你輸入的英文字母:"<<endl;
out_put(a,n);
cout<<endl<<endl;
calculate(a,n,b);
sort(b,SIZE1);
cout<<"結果:"<<endl;
for(int q=0;q<SIZE1;q++)
if(b[q]>0)
cout<<b[q]<<endl;
cout<<endl;
return 0;
}
void fill_array(char a[],int SIZE,int& n)
{
char letter;
int index=0;
cout<<"輸入最多"<<SIZE<<"個小寫字母並以句點表示輸入結束:"<<endl;
cin>>letter;
while((letter!='.')&&(index<SIZE))
{
a[index]=letter;
index++;
cin>>letter;
}
n=index;
}
void out_put(char a[],int& n)
{
int i;
for(i=0;i<n;i++)
cout<<a[i];
}
void calculate(char a[],int& n,int b[])
{
int s;
for(s=0;s<n;s++)
switch(a[s])
{
case'a':
{
b[0]++;
break;
}
case'b':
{
b[1]++;
break;
}
case'c':
{
b[2]++;
break;
}
case'd':
{
b[3]++;
break;
}
case'e':
{
b[4]++;
break;
}
case'f':
{
b[5]++;
break;
}
case'g':
{
b[6]++;
break;
}
case'h':
{
b[7]++;
break;
}
case'i':
{
b[8]++;
break;
}
case'j':
{
b[9]++;
break;
}
case'k':
{
b[10]++;
break;
}
case'l':
{
b[11]++;
break;
}
case'm':
{
b[12]++;
break;
}
case'n':
{
b[13]++;
break;
}
case'o':
{
b[14]++;
break;
}
case'p':
{
b[15]++;
break;
}
case'q':
{
b[16]++;
break;
}
case'r':
{
b[17]++;
break;
}
case's':
{
b[18]++;
break;
}
case't':
{
b[19]++;
break;
}
case'u':
{
b[20]++;
break;
}
case'v':
{
b[21]++;
break;
}
case'w':
{
b[22]++;
break;
}
case'x':
{
b[23]++;
break;
}
case'y':
{
b[24]++;
break;
}
case'z':
{
b[25]++;
break;
}
}
}
void swap(int& v1,int& v2)
{
int temp;
temp=v1;
v1=v2;
v2=temp;
}
void sort(int b[],int SIZE1)
{
int max,r;
for(r=0;r<SIZE1-1;r++)
{
max=r;
for(int j=r+1;j<SIZE1;j++)
if(b[j]>b[max])
max=j;
swap(b[r],b[max]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment