當前位置:編程學習大全網 - 編程軟體 - 編程實現候選人得票情況的統計及排序輸出~~~~求高手~~~~大壹的VC++

編程實現候選人得票情況的統計及排序輸出~~~~求高手~~~~大壹的VC++

#include <iostream.h>

#include <string.h>

struct Node{

char name[12]; //候選人姓名

int count; //計數候選人的得票

Node * next;

};

Node *Statistic(Node *head, char *name) {

Node *p1=head,*p2;

if(head==0){

head=new Node;

strcpy(head->name, name);

head->count=1; head->next=0;

}

else{

while(p1){

if(strcmp(p1->name,name)==0) { p1->count++; break; }

else{ p2=p1;p1=p1->next; }

}

if(p1==0){

p1=new Node;

strcpy(p1->name,name); p1->count=1;

p1->next=0;

p2->next=p1; ;

}

}

return head;

}

void List(Node *head)

{ while(head){

cout<< head->name<<" :\t"<< head->count<<endl;

head=head->next;

}

}

void Free(Node *head)

{ Node *p;

while(head){

p=head;

head=head->next;

delete p;

}

}

void main(void) //連續輸入得票候選人姓名,以輸入"0"結束

{ Node *head=0;

char name[12];

cout<<"輸入得票候選人的姓名:";

cin>>name;

while( strcmp(name,"0")!=0 ) {

head = Statistic(head,name);

cout<<"輸入得票候選人的姓名:";

cin>>name;

}

cout<<"統計得票結果:\n姓名 得票數\n";

List(head); Free(head);

下次註意點

  • 上一篇:妳安全嗎豆瓣評分
  • 下一篇:簡單做快樂吐司的做法?
  • copyright 2024編程學習大全網