| 
 | 
 
 
发表于 2014-4-12 10:23:04
|
显示全部楼层
 
 
 
没加排序功能 即兴写了下 勿喷啊 O(∩_∩)O~- #include<stdio.h>
 
 - #include<string.h>
 
  
- int Input();
 
 - void print(int n);
 
 - void copy(char * ch1,char * ch2,int i);
 
 - void tp(int n);
 
  
- struct per
 
 - {
 
 -         char name[20];
 
 -         int n;
 
 - }person[50],t[50];
 
  
- int main()
 
 - {
 
 -         int n;
 
 -         printf("请输入候选人名字(按0结束最多支持50个):");
 
 -         n = Input();
 
 -         tp(n);
 
 -         print(n);
 
 -         return 0;
 
 - }
 
  
- int Input()
 
 - {
 
 -         char name[20];
 
 -         int k = 0;
 
 -         int i;
 
 -         char ch;
 
 - s1:        for(i = 0;i<20;i++)
 
 -         {
 
 -                 name[i] = " ";
 
 -         }
 
 -         scanf("%s",name);
 
 -         if(*name != '0')
 
 -         {
 
 -                 copy(person[k].name ,name,20);
 
 -                 k++;
 
 -                 while((ch = getchar())!='\n');
 
 -                 goto s1;
 
 -         }
 
 -         return k;
 
 - }
 
  
- void print(int n)
 
 - {
 
 -         int i;
 
 -         for(i = 0;i < n;i++)
 
 -         {
 
 -                 printf("%-3s :   %-3d \n",person[i].name,person[i].n);
 
 -         }
 
 - }
 
  
- void copy(char * ch1,char * ch2,int i)
 
 - {
 
 -         int k;
 
 -         for(k = 0;k<i;k++)
 
 -         {
 
 -                 *(ch1+k) = *(ch2+k);
 
 -         }
 
 - }
 
  
- void tp(int n)
 
 - {
 
 -         int i;
 
 -         char t_name[20];
 
 -         char ch;
 
 -         printf("候选人如下:\n");
 
 -         for(i = 0;i < n;i++)
 
 -         {
 
 -                 printf("%-3s\n",person[i].name);
 
 -         }
 
 - s2:        printf("请输入你要投票的名字(输入0投票结束):");
 
 -         scanf("%s",t_name);
 
 -         
 
 -         if(*t_name == '0')
 
 -         {
 
 -                 return;
 
 -         }
 
 -         else 
 
 -                 for(i = 0;i<n;i++)
 
 -                 {
 
 -                         if(strcmp(t_name,person[i].name)==0)
 
 -                         {
 
 -                                 person[i].n++;
 
 -                                 while((ch = getchar())!='\n');
 
 -                                 goto s2;
 
 -                         }        
 
 -                 }
 
 -                 printf("你输入的名字不是候选人员,请重新输入!\n");
 
 -                 while((ch = getchar())!='\n');
 
 -                 goto s2;
 
 - }
 
  复制代码 |   
 
 
 
 |