P109.3.(1)寫一個算法統計在輸入字符串中各個不同字符出現的頻度並將結果存入文件(字符串的合法字符爲A~Z這26個字母和0~9這10個數字)

#include <iostream>
#include <stdlib.h>
using namespace std;

int main()
{
	int times[36];
	char ch;
	int num;

	for(int i = 0; i < 36; i++)
	{
		times[i] = 0;
	}
	cout<<"請輸入字符"<<endl;
	cin>>ch;
	while(ch != '#')//'#'用於標誌字符串的結束
	{
		if(ch >= '0' && ch <= '9')
		{
			num = ch - '0';//times[0]~times[9]用於記錄'0'~‘9’的頻度
			times[num]++;
		}
		else if(ch >= 'A' && ch <= 'Z')
		{
			num = ch - 'A' + 10;//times[10]~times[35]用於記錄A到Z的頻度
			times[num]++;
		}
		else
		{
			cout<<"輸入字符錯誤!"<<endl;
		}
		cin>>ch;
	}
	for(int j = 0; j < 36; j++)//輸出輸入字符串的頻度值
	{
		cout<<times[j];
	}


	system("pause");
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章