N组相同固定长度字符数组成员统计 C/C++语言实现
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了N组相同固定长度字符数组成员统计 C/C++语言实现相关的知识,希望对你有一定的参考价值。
用C/C++语言实现,N组固定长度,且长度相同的字符数组,均为7个字符的字符数组,统计数组中相同字符的个数,要求N组数组输入后,能够返回各成员字符在N组数组中出现的总次数,比如'a','b','c','d','e','f'其他数组中也有相同的,也有不同,能够统计所有成员出现的个数
#include <stdexcept>#include <fstream>
#include <iostream>
#include <iomanip>
#include <map>
#include <utility>
#include <string>
#include <set>
#include <ctime>
#include <windows.h>
#define FULLPROGRESS 1107568.0
class RBGroup
public:
// construction.
RBGroup(int r0, int r1, int r2, int r3, int r4, int r5, int blue)
: m_Blue(blue)
m_arrRed[0] = r0;
m_arrRed[1] = r1;
m_arrRed[2] = r2;
m_arrRed[3] = r3;
m_arrRed[4] = r4;
m_arrRed[5] = r5;
// sort: large to small.
SortRedBall();
// duplication check.
for(size_t n = 0; n != 5; ++n)
if (m_arrRed[n] == m_arrRed[n + 1])
std::cerr << "\nError:Duplicated value for red ball, Application quited!"
<< std:: endl;
system("color c");
system("pause");
exit(EXIT_FAILURE);
// red ball range check.
if(m_arrRed[0] > 34 || m_arrRed[5] < 1)
std::cerr << "\nError:Invalid value for red ball(1 ~ 34), Application quited!"
<< std:: endl;
system("color c");
system("pause");
exit(EXIT_FAILURE);
// blue ball number check.
if (m_Blue > 16 || m_Blue < 1)
std::cerr << "\nError:Invalid value for blue ball(1 ~ 16), Application quited!"
<< std:: endl;
system("color b");
system("pause");
exit(EXIT_FAILURE);
public:
// for common use
static void Sort( int *arr, size_t Num )
int tmp = 0;
for (size_t i = Num - 1; i != 0; --i)
for (size_t j = 0; j != (Num - 1); ++j)
if (arr[j] < arr[j + 1])
tmp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = tmp;
void RBReset ( int r0, int r1, int r2, int r3, int r4, int r5, int blue )
m_Blue = blue;
m_arrRed[0] = r0;
m_arrRed[1] = r1;
m_arrRed[2] = r2;
m_arrRed[3] = r3;
m_arrRed[4] = r4;
m_arrRed[5] = r5;
// sort: large to small.
SortRedBall();
// duplication check.
for(size_t n = 0; n != 5; ++n)
if (m_arrRed[n] == m_arrRed[n + 1])
std::cerr << "\nError:Duplicated value for red ball, Application quited!"
<< std:: endl;
system("color c");
system("pause");
exit(EXIT_FAILURE);
// red ball range check.
if(m_arrRed[0] > 34 || m_arrRed[5] < 1)
std::cerr << "\nError:Invalid value for red ball(1 ~ 34), Application quited!"
<< std:: endl;
system("color c");
system("pause");
exit(EXIT_FAILURE);
// blue ball number check.
if (m_Blue > 16 || m_Blue < 1)
std::cerr << "\nError:Invalid value for blue ball(1 ~ 16), Application quited!"
<< std:: endl;
system("color b");
system("pause");
exit(EXIT_FAILURE);
// for itself
void SortRedBall()
Sort(m_arrRed, 6);
int NumRedFit( const RBGroup &ref ) const
size_t sumFit = 0;
for (size_t n = 0; n != 6; ++n)
for(size_t m = 0; m != 6; ++m)
if (m_arrRed[n] == ref.m_arrRed[m])
sumFit++;
return sumFit;
bool BlueFit( const RBGroup &ref ) const
return m_Blue == ref.m_Blue;
public:
// operators
friend bool operator==( const RBGroup &lhs, const RBGroup &rhs )
return (lhs.BlueFit(rhs) && (lhs.NumRedFit(rhs) == 6)) ? true : false;
friend bool operator<( const RBGroup &lhs, const RBGroup &rhs )
for(size_t n = 0; n != 6; ++n)
if (lhs.m_arrRed[n] < rhs.m_arrRed[n])
return true;
if (lhs.m_arrRed[n] > rhs.m_arrRed[n])
return false;
// all red equal then compare blue.
return lhs.m_Blue < rhs.m_Blue;
friend bool operator>( const RBGroup &lhs, const RBGroup &rhs )
return (lhs == rhs || lhs < rhs) ? false : true;
friend std::ostream &operator<<( std::ostream &ostrm, const RBGroup &rbg)
ostrm.fill('0');
for(size_t n = 5; n != -1; --n)
ostrm << std::setw(2) << rbg.m_arrRed[n] << " ";
ostrm << "\b:" << std::setw(2) << rbg.m_Blue;
ostrm.fill(' ');
return ostrm;
// set RBGroup obj from input, style like: 1 2 3 4 5 6:7
friend std::istream &operator>>( std::istream &istrm, RBGroup &rbg )
char ignore;
for(size_t n = 0; n != 6; ++n)
istrm >> rbg.m_arrRed[n];
rbg.SortRedBall();
// duplication check
for(size_t n = 0; n != 5; ++n)
if (rbg.m_arrRed[n] == rbg.m_arrRed[n + 1])
std::cerr << "\nError:Duplicated value for red ball, Application quited!"
<< std:: endl;
system("color c");
system("pause");
exit(EXIT_FAILURE);
// red ball range check
if(rbg.m_arrRed[0] > 34 || rbg.m_arrRed[5] < 1)
std::cerr << "\nError:Invalid value for red ball(1 ~ 34), Application quited!"
<< std:: endl;
system("color c");
system("pause");
exit(EXIT_FAILURE);
istrm >> ignore; // abandon ':'
istrm >> rbg.m_Blue;
// blue ball number check.
if (rbg.m_Blue > 16 || rbg.m_Blue < 1)
std::cerr << "\nError:Invalid value for blue ball(1 ~ 16), Application quited!"
<< std:: endl;
system("color b");
system("pause");
exit(EXIT_FAILURE);
return istrm;
public:
int RBWon( const RBGroup &ref ) const
// first prize.
if (BlueFit(ref) && (6 == NumRedFit(ref)))
return 1;
// second prize.
if (6 == NumRedFit(ref))
return 2;
// third prize.
if (BlueFit(ref) && (5 == NumRedFit(ref)))
return 3;
// fourth prize.
if ((BlueFit(ref) && (4 == NumRedFit(ref))) || (5 == NumRedFit(ref)))
return 4;
// fifth prize.
if ((BlueFit(ref) && (3 == NumRedFit(ref))) || (4 == NumRedFit(ref)))
return 5;
// sixth prize.
if (BlueFit(ref))
return 6;
// Not winning, welcome back!
return 0;
private:
int m_arrRed[6];
int m_Blue;
;
class WinNum
public:
static double totalBets; // 'double' type just for precision of later division operators
// default constructor.
WinNum() : m_CandidateRBG(1, 2, 3, 4, 5, 6, 7)
for(size_t n = 0; n != 6; ++n)
m_arrPrizeLevel[n] = 0;
// style like: 1 2 3 4 5 6:7 (not wrapped by '\n')
void resetCandidateRBG ( int r0, int r1, int r2, int r3, int r4, int r5, int blue )
m_CandidateRBG.RBReset(r0, r1, r2, r3, r4, r5, blue);
for(size_t n = 0; n != 6; ++n)
m_arrPrizeLevel[n] = 0;
const RBGroup &getCandidateRBG() const return m_CandidateRBG;
void addWinning( unsigned level, unsigned num )
// no check for level & num.
m_arrPrizeLevel[level - 1] += num;
unsigned getSumBets() const
return
(m_arrPrizeLevel[0] + m_arrPrizeLevel[1] + m_arrPrizeLevel[2]
+ m_arrPrizeLevel[3] + m_arrPrizeLevel[4] + m_arrPrizeLevel[5]);
unsigned getPrizeDetail(unsigned level) const
// level range check.
if (level < 1 || level > 6)
std::cout << "Error:Invalid value for prize level, value 0 returned!" << std::endl;
return 0;
return m_arrPrizeLevel[level - 1];
// operators.
public:
friend bool operator<(const WinNum &lhs, const WinNum &rhs)
return (lhs.getCandidateRBG() < rhs.getCandidateRBG());
// style like: (wrapped by '\n' because of multi-line)
// Group: 1 2 3 4 5 6:7 12 (0.005%) in total.
//------------------------------------------------------
// 1st prize: 2
// 2nt prize: 1
// 3rd prize: 4
// 4th prize: 3
// 5th prize: 1
// 6th prize: 1
friend std::ostream &operator<<( std::ostream &ostrm, const WinNum &wn )
std::cout << "\nGroup: " << std::left << std::setw(12) << wn.getCandidateRBG()
<< std::setw(8) << std::right << wn.getSumBets()
<< std::fixed << std::setprecision(4)
<< " (" << (wn.getSumBets() / WinNum::totalBets * 100) << "%) in total.\n"
<< std::setw(48) << std::setfill('-') << "-" << std::setfill(' ') << std::endl
// 1st prize.
<< std::left << std::setw(35) << " 1st prize:"
<< std::right << std::setw(8) << wn.getPrizeDetail(1) << std::endl
// 2nd prize.
<< std::left << std::setw(35) << " 2nd prize:"
<< std::right << std::setw(8) << wn.getPrizeDetail(2) << std::endl
// 3rd prize.
<< std::left << std::setw(35) << " 3rd prize:"
<< std::right << std::setw(8) << wn.getPrizeDetail(3) << std::endl
// 4th prize.
<< std::left << std::setw(35) << " 4th prize:"
<< std::right << std::setw(8) << wn.getPrizeDetail(4) << std::endl
// 5th prize.
<< std::left << std::setw(35) << " 5th prize:"
<< std::right << std::setw(8) << wn.getPrizeDetail(5) << std::endl
// 6th prize.
<< std::left << std::setw(35) << "6th prize:"
<< std::right << std::setw(8) << wn.getPrizeDetail(6) << std::endl;
return ostrm;
private:
RBGroup m_CandidateRBG;
unsigned m_arrPrizeLevel[6];
;
double WinNum::totalBets = 0.0;
int main()
system("@echo off");
std::cout << "================= Static Release v1.0 =================" << std::endl;
// get file.
// std::string path;
// std::cout << "Bets file Path:\n";
// std::cin >> path;
// std::cin.sync();
// std::ifstream betsfile(path.c_str());
// while (!betsfile)
//
// betsfile.clear();
// std::cerr << "File open failed, please input a invalid file path:\n";
// std::cin >> path;
// std::cin.sync();
// betsfile.open(path.c_str());
//
std::ifstream betsfile("bets.txt");
// get total number of bets
betsfile >> WinNum::totalBets;
// read and analyze file.
RBGroup tmpRBG(1, 2, 3, 4, 5, 6, 7);
std::map<RBGroup, size_t> mapRBGSum;
std::map<RBGroup, size_t>::iterator mapRBGSum_iter;
std::cout << "Reading bet item: ";
for(size_t n = 0; n != WinNum::totalBets; ++n)
std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b" << std::setw(12) << n;
betsfile >> tmpRBG;
if ( mapRBGSum.end() != (mapRBGSum_iter = mapRBGSum.find(tmpRBG)))
(mapRBGSum_iter->second)++;
else
mapRBGSum.insert(std::make_pair(tmpRBG, 1));
/*std::cout << mapRBGSum.size() << std::endl;*/
std::cout << "\n" << WinNum::totalBets << " bets item added successfully!";
WinNum tmpWN;
std::multimap<unsigned, WinNum> mmapWinNumStatistics;
unsigned tmpPrizeLevel = 0;
double progress1 = 0.0, progress2 = 0.0;
char percent[100];
unsigned curProgress = 0;
DWORD dwStart = 0, dwEnd = 0, dwInterval = 0, dwMaxinterval = 0; // for time consumption testing.
for (size_t n1 = 1; n1 < 29; ++n1)
for (size_t n2 = n1 + 1; n2 < 30; ++n2)
for (size_t n3 = n2 + 1; n3 < 31; ++n3)
// total progress show
progress1 = curProgress / FULLPROGRESS;
sprintf_s(percent, 100, "< Totally %.2f%% completed! >", progress1);
system("cls");
std::cout << "\nData analyzing begin, it may take a long: \t" << percent << std::endl;
printf("Analyzing [%2d %2d %2d ...:.] serial: ", n1, n2, n3);
for (size_t n4 = n3 + 1; n4 < 32; ++n4)
for (size_t n5 = n4 + 1; n5 < 33; ++n5)
for (size_t n6 = n5 + 1; n6 < 34; ++n6)
// time record
// dwStart = GetTickCount();
// printf("\r%d", dwMaxinterval);
// progress show.
curProgress++;
progress2 = 100.0 * ((n4 - 4)*28*28 + (n5 - 5)*28 + (n6 - 6)) / (28 * 28 * 28);
sprintf_s(percent, 100, "%5.2f%% completed!", progress2);
std::cout << "\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b" << std::setw(20) <<percent;
// get to business
for(size_t nBlue = 1; nBlue < 17; ++nBlue)
tmpWN.resetCandidateRBG(n1, n2, n3, n4, n5, n6, nBlue);
for(mapRBGSum_iter = mapRBGSum.begin();
mapRBGSum_iter != mapRBGSum.end();
++mapRBGSum_iter)
if(tmpPrizeLevel = (mapRBGSum_iter->first.RBWon(tmpWN.getCandidateRBG())))
tmpWN.addWinning(tmpPrizeLevel, mapRBGSum_iter->second);
/*std::cout << tmpWN.getCandidateRBG() << std::setw(12) << tmpWN.getSumBets() << std::endl;*/
mmapWinNumStatistics.insert(std::make_pair(tmpWN.getSumBets(), tmpWN));
dwEnd = GetTickCount();
dwInterval = dwEnd - dwStart;
dwMaxinterval = max(dwMaxinterval, dwInterval);
system("cls");
system("color b");
std::cout << "\n\nCongradulations data analysis done!" << std::endl;
// figure out probability range.
std::multimap<unsigned, WinNum>::const_iterator mm_citer1 = mmapWinNumStatistics.begin();
std::multimap<unsigned, WinNum>::const_iterator mm_citer2 = mmapWinNumStatistics.end();
double probCeiling = ((--mm_citer2)->first) / WinNum::totalBets;
double probFloor = (mm_citer1->first) / WinNum::totalBets;
std::cout << "\nThe min. probability : " << std::setprecision(6)
<< probFloor << std::endl;
std::cout << "\nThe max. probability : " << std::setprecision(6)
<< probCeiling << std::endl;
// get max and min probability.
double maxProb = 0.0, minProb = 0.0, swapProb;
std::cout << "Please input max. and min. probability between the range above: ";
std::cin >> minProb >> maxProb;
std::cin.sync();
if (maxProb < minProb) // adjust max & min
swapProb = maxProb;
maxProb = minProb;
minProb = swapProb;
while (maxProb > probCeiling || minProb < probFloor)
std::cout << "Error:Invalid range of probability specified, must be in the range ["
<< probFloor << ", " << probCeiling
<< "]!\nPlease re-input a valid probability range:";
std::cin >> maxProb >> minProb;
if (maxProb < minProb) // adjust max & min
swapProb = maxProb;
maxProb = minProb;
minProb = swapProb;
unsigned maxNumBets = static_cast<unsigned>(floor(WinNum::totalBets * maxProb));
unsigned minNumBets = static_cast<unsigned>(ceil(WinNum::totalBets * minProb));
// get final result set of bi-color ball groups
std::multimap<unsigned, WinNum>::const_iterator iterLB = mmapWinNumStatistics.lower_bound(minNumBets);
std::multimap<unsigned, WinNum>::const_iterator iterUB = mmapWinNumStatistics.upper_bound(maxNumBets);
unsigned tmpSum = 0;
if (iterLB != iterUB)
while (iterLB != iterUB)
tmpSum++;
std::cout << tmpSum << "group(s) of numbers that fulfill your requirement:" << std::endl;
tmpSum = 0;
while (iterLB != iterUB)
std::cout << "No." << tmpSum << std::endl;
std::cout << (iterLB->second) << std::endl;
iterLB++;
system("PAUSE");
// output analyze result
// for(iter = mapRBGSum.begin(); iter != mapRBGSum.end(); ++iter)
//
// std::cout << (iter->first) << "\ttotal: " << std::setw(5) << (iter->second) << std::endl;
//
//RBGroup a;
//try
//
// std::cin >> a;
//
//catch (std::logic_error* e)
//
// std::cerr << e->what() << std:: endl;
// std::cout << "Application quit!" << std:: endl;
// return EXIT_FAILURE;
//
//std::cout << a << std::endl;
return 0;
参考技术A #include<iostream>
#include<cstring>
using namespace std;
int main()
int n,b[128]=0,i,j;
char **p;
cin>>n;
cin.get();
p=new char*[n];
for(i=0;i<n;i++)
p[i]=new char[7];
for(i=0;i<n;i++)
gets(p[i]);
for(i=0;i<n;i++)
for(j=0;j<7;j++)
b[p[i][j]-1]++;
for(i=0;i<128;i++)
if(b[i]!=0)
cout<<static_cast<char>(i+1)<<":"<<b[i]<<endl;
return 0;
这个程序实现输出的时候时候是按ASCII码排的,不知是不是符合要求
以上是关于N组相同固定长度字符数组成员统计 C/C++语言实现的主要内容,如果未能解决你的问题,请参考以下文章
C语言 数组的问题,书上的例子,说a = sizeof(mu) / sizeof(mu[0])可以统计数组的元素个数,为啥?