#include <iostream>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
void part1 (char(&str)[100] )
{
ifstream myfile ("test1.txt");
myfile>>str;
myfile.close();
}
int main()
{
char str [100];//定义一个大小100的字符型数组
part1 (str);//利用part1函数将文本test1中的内容赋值给str.
typedef struct
{
char b ;//出现的字符
int c;//出现的次数
}ye,*yezi;//定义了一个结构体
int n=0;
int j=0;
int i=0;
int h=0;
yezi str1 [100];//利用结构体开辟结构体数组,大小100;
while(str[j]!='0')//如果上述字符型数组没有读到结束标志。,则进入while循环。
{
for(i=0;i<100;i++)//对结构体数组str1进行遍历。
{
if (str[j]==str1[i]->b)//如果字符型数组某一元素与结构体数组某一个元素中的b相等。
{
str1[i]->c++;//将结构体数组str1中的C加一
}
if (i=100)//如果都不相等,说明这是一个新字符
{
str1[n]->b=str[j];
str1[n]->c=1;
n++;
}
}
j++;
}
for(i=0;i<100;i++)
{
cout<<str1[i]->b<<str1[i]->c<<endl;
}
}
相似问题