E企盈营销工具技术服务商 热线:4006-838-530

选举小程序

E企盈直播平台营销卖货系统

#includeiostream#includefstream#includestring#includeiomanipusing namespace std;const int noOfCandidates=6;const int noOfRegions=4;void printHeading();//预申明函数void initialize(int vbRegion[noOfCandidates][noOfRegions],int tVotes[],int noOfRows);void getCandidatesName(ifstream& inp,string cNames[],int noOfRows);void sortCandidatesName(string cNames[],int noOfRows);int binsearch(string cNames[],int noOfRows,string);void processVotes(ifstream& inp,string cNames[],int vbRegion[noOfCandidates][noOfRegions],int noOfRows);void addRegionsVote(int vbRegion[noOfCandidates][noOfRegions],int tVotes[],int noOfRows);void printResults(string cNames[],int vbRegion[noOfCandidates][noOfRegions],int tVotes[],int noOfRows);int main(){string candidatesName[noOfCandidates];//申明变量int votesByRegion[noOfCandidates][noOfRegions];int totalVotes[noOfCandidates];ifstream infile;infile.open(“C:\Users\diyhoo\Desktop\candData.txt”);//打开输入文件if(!infile){cout”Input file does not exist.”endl;return 1;}cout”setp 1 comple”endl;getCandidatesName(infile,candidatesName,noOfCandidates);//将数据读入数组candidatesNamecout”setp 2 comple”endl;sortCandidatesName(candidatesName,noOfCandidates);//对数组candidatesName进行排序cout”setp 3 comple”endl;infile.close();//关闭文件infile.clear();//清除数据流infile.open(“C:\Users\diyhoo\Desktop\voteData.txt”);if(!infile){cout”Input file does not exist.”endl;return 1;}initialize(votesByRegion,totalVotes,noOfCandidates);//初始化数组votesByRegion,totalVotescout”setp 4 comple”endl;processVotes(infile,candidatesName,votesByRegion,noOfCandidates);//处理投票结果并存储在数组votesByRegion中cout”setp 5 comple”endl;addRegionsVote(votesByRegion,totalVotes,noOfCandidates);//计算每名候选人的总票数并存储在totalvotes中cout”setp 6 comple”endl;printHeading();//输出题头printResults(candidatesName,votesByRegion,totalVotes,noOfCandidates);//输出结果return 0;}void initialize(int vbRegion[noOfCandidates][noOfRegions],int tVotes[],int noOfRows){int i,j;for(i=0;inoOfRows;i++)for(j=0;jnoOfRegions;j++)vbRegion[i][j]=0;for(i=0;inoOfRows;i++)tVotes[i]=0;}void getCandidatesName(ifstream& inp,string cNames[],int noOfRows){int i;for(i=0;inoOfRows;i++)inpcNames[i];}void sortCandidatesName(string cNames[],int noOfRows){int i,j;int min;for(i=0;inoOfRows-1;i++){min=i;for(j=i+1;jnoOfRows;j++)if(cNames[j]cNames[min])min=j;cNames[i].swap(cNames[min]);}}int binSearch(string cNames[],int noOfRows,string name){int first,last,mid;bool found;first=0;last=noOfRows-1;found=false;while(!found&&first=last){mid=(first+last)/2;if(cNames[mid]==name)found=true;elseif(cNames[mid]name)first=mid+1;elselast=mid-1;}if(found)return mid;elsereturn -1;}void processVotes(ifstream& inp,string cNames[],int vbRegion[noOfCandidates][noOfRegions],int noOfRows){string candName;int region;int noOfVotes;int loc;inpcandNameregionnoOfVotes;while(inp){loc=binSearch(cNames,noOfRows,candName);if(loc!=-1)vbRegion[loc][region-1]=vbRegion[loc][region-1]+noOfVotes;inpcandNameregionnoOfVotes;}}void addRegionsVote(int vbRegion[noOfCandidates][noOfRegions],int tVotes[],int noOfRows){int i,j;for(i=0;inoOfRows;i++)for(j=0;jnoOfRegions;j++)tVotes[i]=tVotes[i]+vbRegion[i][j];}void printHeading(){cout”     —————ElectionResults—————-     “endlendl;cout”Candidate            votes”endl;cout”Name       Region1    Region2    Region3      Region4     Total”endl;cout”—-       ——-    ——-     ——      ——-      —-“endl;}void printResults(string cNames[],int vbRegion[][noOfRegions],int tVotes[],int noOfRows){int i,j;int largestVotes=0;int winLoc=0;int sumVotes=0;for(i=0;inoOfRows;i++){if(largestVotestVotes[i]){largestVotes=tVotes[i];winLoc=i;}sumVotes=sumVotes+tVotes[i];coutleft;coutsetw(9)cNames[i]” “;coutright;for(j=0;jnoOfRegions;j++)coutsetw(8)vbRegion[i][j]” “;cout”   “setw(10)tVotes[i]endl;}coutendlendl”Winner:”cNames[winLoc]”, votes received:”tVotes[winLoc]endlendl;cout”Total votes polled:”sumVotesendl;}

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » 选举小程序
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏