大学时一份作业代码备案
这是大学的一份作业,最近整理时在网上搜到,备份到这里。用TurboC编译,运行显示一个 柱状图。还记得当时这个作业是拷贝在3.5寸软盘里执行的。临交作业前软盘损坏,紧急的 重新输入了一次。当时对C语言是多么的有兴趣。
#include<stdio.h> #define SC scanf #define PR printf struct person { char name[20]; int count,code; float pec; } leader[20]; int main() { int n,i; char c; void scan(struct person leader[],int n); void arith(struct person leader[],int n); void diagram(struct person leader[],int n); PR("The Course Is Made By Tongxin'Yiban Yan Yonggang.\nNow Press Enter For Running The Course.\n"); while(getchar()!='\n'); while(1) {PR("\nInput the number of candidates:"); do {SC("%d",&n); if(n>1&&n<=20)break; else if(n<=1)PR("The candidates'number is too small.Input again!"); else if(n>20)PR("The candidates'number is too large.Input again!"); }while(1); scan(leader,n); arith(leader,n); PR("\nNow press any key for looking diagram."); getchar(); getchar(); diagram(leader,n); for(;;) { PR("\nContinued(Y/N)?"); c=getchar(); getchar(); if(c=='Y'||c=='y'||c=='N'||c=='n')break; } if(c=='y'||c=='Y'){for(i=0;i<20;i++)leader[i].count=0;continue;} else break; } } void scan(struct person leader[],int n) { int i,j,k,flag=1; static char a1[67][35]; char c; for(j=0;j<34;j++) for(i=0;i<67;i+=3) a1[i][j]=196; for(i=0;i<67;i++) { a1[i][0]=179;a1[i][20]=179;a1[i][34]=179; } for(i=3;i<=3*n;i+=3) { a1[i][0]=195;a1[i][20]=197;a1[i][34]=180; } a1[0][0]=218;a1[0][20]=194;a1[0][34]=191;a1[3*n+3][0]=192;a1[3*n+3][20]=193;a1[3*n+3][34]=190; while(flag) { PR("\nInput the candidates name and code.If only input name,system will \ngive code one by one.\n"); PR("If only input name,press S,else press others."); getchar(); c=getchar(); if(c=='s'||c=='S') { PR("Input name:"); for(i=0;i<n;i++){SC("%s",&leader[i].name);leader[i].code=i+1;} } else {PR("Input name and code:"); for(i=0;i<n;i++)SC("%s%d",&leader[i].name,&leader[i].code);} PR("This is candidates'name and code:"); a1[1][9]='N';a1[1][10]='a';a1[1][11]='m';a1[1][12]='e'; a1[1][27]='C';a1[1][28]='o';a1[1][29]='d';a1[1][30]='e'; for(i=0;i<2;i++) {PR("\n\t\t"); for(j=0;j<35;j++)PR("%c",a1[i][j]);} for(i=0;i<4;i++){a1[1][i+9]=0;a1[1][i+27]=0;} for(i=3;i<=3*n+3;i+=3) for(j=0;leader[i/3-1].name[j]!='\0';j++)a1[i+1][j+1]=leader[i/3-1].name[j]; for(i=3;i<=3*n+3;i++) {PR("\n\t\t"); if((i+2)%3!=0||i<3)for(j=0;j<35;j++)PR("%c",a1[i][j]); else PR("%c%-19s%c%10d %c",a1[i][0],leader[i/3-1].name,a1[i][20],leader[i/3-1].code,a1[i][34]); } for(;;) { PR("\nRight(Y/N)?"); getchar(); c=getchar(); if(c=='Y'||c=='y'){flag=0;break;} else if(c=='N'||c=='n')break; } } } void arith(struct person leader[],int n) { int i,j,k,s=0,x,flag=1; float m,p; static char c,a1[67][55]; for(j=0;j<55;j++) for(i=0;i<67;i+=3) a1[i][j]=196; for(i=0;i<67;i++) { a1[i][0]=179;a1[i][20]=179;a1[i][34]=179;a1[i][44]=179;a1[i][54]=179; } for(i=3;i<=3*n;i+=3) { a1[i][0]=195;a1[i][20]=197;a1[i][34]=197;a1[i][44]=197;a1[i][54]=180; } a1[0][0]=218;a1[0][20]=194;a1[0][34]=194;a1[0][44]=194;a1[0][54]=191;a1[3*n+3][0]=192;a1[3*n+3][20]=193;a1[3*n+3][34]=193;a1[3*n+3][44]=193;a1[3*n+3][54]=190; while(flag) { PR("Input the total of vote(an integar):"); SC("%d",&k); PR("Input the needed revery(a persontage):"); SC("%f%c",&m,&c); PR("Now beginning vote:\n"); for(;;) {SC("%d",&x); if(x==0)break; for(j=0;j<n;j++) if(x==leader[j].code){leader[j].count++;break;} s++; if(s>=k)break; } p=(float)s/k*100; if(p>=m) { for(i=0;i<n;i++)leader[i].pec=(float)leader[i].count/s*100; a1[1][5]='N';a1[1][6]='a';a1[1][7]='m';a1[1][8]='e'; a1[1][24]='C';a1[1][25]='o';a1[1][26]='d';a1[1][27]='e'; a1[1][37]='C';a1[1][38]='o';a1[1][39]='u';a1[1][40]='n';a1[1][41]='t'; a1[1][46]='P';a1[1][47]='e';a1[1][48]='r';a1[1][49]='c';a1[1][50]='e';a1[1][51]='e';a1[1][52]='n';a1[1][53]='t'; for(i=3;i<=3*n+3;i+=3) for(j=0;leader[i/3-1].name[j]!='\0';j++)a1[i+1][j+1]=leader[i/3-1].name[j]; for(i=0;i<=3*n+3;i++) {PR("\n\t"); if((i+2)%3!=0||i<3)for(j=0;j<55;j++)PR("%c",a1[i][j]); else PR("%c%-19s%c%10d %c%7d %c%8.2f%%%c ",a1[i][0],leader[i/3-1].name,a1[i][20],leader[i/3-1].code,a1[i][34],leader[i/3-1].count,a1[i][44],leader[i/3-1].pec,a1[i][54]); } } else {PR("Less of needed revery,It's a invalid vote,input again!\n"); for(i=0;i<20;i++)leader[i].count=0;s=0;continue;} break; } } void diagram(struct person leader[],int n) { int i,j,k=0; static char a[21][66],*p1,*p2; p1="Percent"; p2="Code"; a[0][0]=24;a[20][65]=62; for(i=1;i<21;i++)a[i][0]=179; for(i=1;i<65;i++)a[20][i]=196; for(j=5;j<=5*n;j+=5) {for(i=20-(int)leader[k].pec/5;i<20;i++) a[i][j]=219;k++;} a[20][0]=192; PR(" %s\n",p1); for(i=0;i<21;i++) { if(i%2==0)PR(" "); else PR("%6.2f%%",(float)(20-i)*5); for(j=0;j<66;j++) PR("%c",a[i][j]); if(i==20)PR("%s",p2); PR("\n"); } PR(" "); for(i=0;i<5*n;i+=5)PR("%5d",leader[i/5].code); for(j=5;j<=5*n;j+=5) for(i=1;i<20;i++) a[i][j]=0; PR("\n"); }