如何读C++文件中的数字读入到数组中
2个回答
展开全部
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
class numbers
{
public:
bool isRepeated;
int repeatedTimes;
int num;
numbers();
};
numbers::numbers()
{
isRepeated = false;
repeatedTimes = 0;
num = 0;
}
int main()
{
ifstream input;
input.open("document.txt");
vector<numbers> vec;
numbers obj;
int i = 0;
int k = 0;
while(!input.eof())
{
input >> obj.num;
vec.push_back(obj);
for(int j = 0; j < i; j++)
{
if(vec[i].num == vec[j].num)
{
vec[i].isRepeated = true;
vec[j].repeatedTimes++;
break;
}
}
i++;
}
for(k = 0; k < i; k++)
{
if(vec[k].isRepeated)
{
break;
}
}
if(k == i)
{
cout << "没有重复的数" << endl;
}
else
{
for(k = 0; k < i; k++)
{
if(vec[k].repeatedTimes > 0)
{
cout << vec[k].num << "重复" << vec[k].repeatedTimes << "次, 一共出现" << (vec[k].repeatedTimes + 1) << "次" << endl;
}
}
}
return 0;
}
#include<fstream>
#include<vector>
using namespace std;
class numbers
{
public:
bool isRepeated;
int repeatedTimes;
int num;
numbers();
};
numbers::numbers()
{
isRepeated = false;
repeatedTimes = 0;
num = 0;
}
int main()
{
ifstream input;
input.open("document.txt");
vector<numbers> vec;
numbers obj;
int i = 0;
int k = 0;
while(!input.eof())
{
input >> obj.num;
vec.push_back(obj);
for(int j = 0; j < i; j++)
{
if(vec[i].num == vec[j].num)
{
vec[i].isRepeated = true;
vec[j].repeatedTimes++;
break;
}
}
i++;
}
for(k = 0; k < i; k++)
{
if(vec[k].isRepeated)
{
break;
}
}
if(k == i)
{
cout << "没有重复的数" << endl;
}
else
{
for(k = 0; k < i; k++)
{
if(vec[k].repeatedTimes > 0)
{
cout << vec[k].num << "重复" << vec[k].repeatedTimes << "次, 一共出现" << (vec[k].repeatedTimes + 1) << "次" << endl;
}
}
}
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |