#include <stdlib.h>
struct student{
int xh; //學號
char xm[10]; //姓名
int flag; //已投票數
int flag_p; //被票數
int xp_xh[6]; //被選人學號
}stu[20];
//函數功能:錄入學生信息
void lr()
{
int i;
for(i=0;i<=20;i++)
{
printf("學號:%d\n",i);
printf("請輸入姓名:");
stu[i].xh=(i+1);
scanf("%c",&stu[i].xm);
stu[i].flag=0;
stu[i].flag_p=0;
stu[i].xp_xh[6]=0;
printf("\n");
printf("繼續輸入? ");
system("pause");
system("cls");
}
printf("學號\t");
printf("姓名\t");
printf("票數\n");
for(i=0;i<=20;i++)
{
printf("%d\t",stu[i].xh);
printf("%c\t",&stu[i].xm);
printf("%d",stu[i].flag_p);
}
}
//函數功能:投票,對數據進行操作 ,輸出結果
void tp(int tpr,int tp)
{
int q=0;
if (stu[tpr-1].flag<6)
{
if (tpr>20 || tpr<0)
{
printf("輸入有誤!\n");
system("pause");
}
else{
q = stu[tpr-1].flag;
stu[tpr-1].xp_xh[q]=(tp-1);
stu[tpr-1].flag++;
stu[tp-1].flag_p++;
}
}
else{
printf("您已經投了6票!");
system("cls");
printf("投票結果:\t");
printf("學號\t姓名\t票數\t投票數\n");
for (int k=0;k<=20;k++)
printf("%d\t%c\t%d\t%d",stu[k].xh,&stu[k].xm,stu[k].flag_p,stu[k].flag);
system("/npause");
exit(1);
}
}
int mian()
{
int xh_tpr; //投票人學號
int xh_tp; //被投票人學號
lr();
start:
printf("請輸入妳的學號:");
scanf("%d",&xh_tpr);
printf("\n請輸入妳要投票人的學號:");
scanf("%d",&xh_tp);
tp(xh_tpr,xh_tp);
goto start;
}