1 / 8

Coding Reference for First Computer Exam

Provides guidance and examples for a programming assignment related to lottery, power calculation, and poker game, implemented in C language. Includes sample code and explanations.

wardsandra
Télécharger la présentation

Coding Reference for First Computer Exam

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第一次上機考參考答案 僅供參考,同學可自行再想更好的方法.

  2. #include<stdio.h> main() { int num[6]={3,8,15,22,25,41}; int num2[6]; int i,j,count=0; char flag,flag2; printf("============================\n"); printf(" 歡迎來到樂透彩對獎系統\n"); printf("============================\n"); do{ do{ for(i=0;i<6;i++) { printf("Please input your %d numbers:",i+1); scanf("%d",&num2[i]); if((num2[i]>42)||(num2[i]<1)) { printf("輸入錯誤,請重新輸入!\n"); flag2=1;break; } flag2=0; } }while(flag2==1); for(i=0;i<6;i++) for(j=0;j<6;j++) { if(num2[i]==num[j]) count++; } printf("本期中獎號碼為:3,8,15,22,25,41\n"); switch(count) { case 0: case 1: case 2:printf("您未中獎!\n");break; case 3:printf("恭喜您中普獎!\n");break; case 4:printf("恭喜您中參獎!\n");break; case 5:printf("恭喜您中二獎!\n");break; case 6:printf("恭喜您中頭獎!\n");break; } printf("再對一次獎嗎(y/n)?\n"); scanf(" %c",&flag); }while((flag=='y')||(flag=='Y')); } 1.請寫一程式,可以輸入您的彩券號碼,並輸出中獎情形

  3. 2.試寫一程式計算x的 y 次方 • #include <stdio.h> • #include <stdlib.h> • int power(int xx,int yy) • { • int i; • int total=1 ; • for(i=1;i<=yy;i++) • { • total=total*xx ; • } • return(total) ; • } • int main(void) • { • int x,y ; • printf("Please use a comma\n"); • printf("Pls. input the x and y for power(x,y)") ; • scanf("%d,%d",&x,&y) ; • printf("Result of power(x,y)=%d\n",power(x,y)) ; • system("pause") ; • return 0 ; }

  4. #include<stdlib.h> #include<stdio.h> #include<time.h> #include<math.h> int poker[4][14],user[4][2],sys[4][2],sequence[4]; int main(void) { int card(int,int); void create(void); int compare(void); void show_result(int a[4][2]); int i,j,win=0; printf("------------------------------\n"); printf(" 歡迎來到POKER SYSTEM\n"); printf("------------------------------\n"); for(i=0;i<4;i++) { for(j=0;j<14;j++) poker[i][j]=0; } for(i=0;i<4;i++) { for(j=0;j<2;j++) sys[i][j]=0; } for(i=0;i<4;i++) { for(j=0;j<2;j++) user[i][j]=0; } printf(" 請等候系統發牌\n\n"); create(); win=compare(); printf(" 比賽結果:\n\n"); if(win==0) printf(" 你輸了!!\n"); else if(win==1) printf(" 你贏了!!\n"); else printf(" 平手!!\n"); /*printf("\n 你的牌:\n"); show_result(user); */ printf("\n\n 系統的牌:\n"); show_result(sys); printf("\n------------------------------\n"); system("pause"); return 0; } 3.請將作業1新增加一個功能:可讓使用者輸入出牌的順序。

  5. void create(void) { int i=0,j=0,count=0,x=0,y=0,flag=0; do{ x=0; y=0; srand(time(0)); x=(rand()%13)+1; /*x為數字*/ y=(rand()%4); /*y為花色*/ flag=card(x,y); if(flag==1) continue; else { count++; if(count<=4) /*前四次是user的牌*/ { user[i][0]=x; user[i][1]=y; i++; } else /*後四次是system的牌*/ { sys[j][0]=x; sys[j][1]=y; j++; } } }while(count<=8); } int card(int x1,int y1) /*確認不會發出一樣的牌*/ { int redo=0; if(poker[x1][y1]==0) poker[x1][y1]=1; else redo=1; return redo; }

  6. int compare(void) /*比大小*/ { int i,j,k=0,sys_count=0,user_count=0,win=0; printf("\n 你的牌:\n"); show_result(user); printf("\n\n 請問您的出牌順序是?(輸入時請以空白隔開)"); for(k=0;k<4;k++) scanf("%d",&sequence[k]); printf("\n"); for(i=0,k=0,j=0;i<4;i++) { j=sequence[i]; j--; if(user[j][0]>sys[i][0]) /*先比數字*/ user_count++; else if(user[j][0]==sys[i][0]) /*若數字相同再比花色*/ { if(user[j][1]>sys[i][1]) user_count++; else sys_count++; /*系統贏一次*/ } else sys_count++; } if(sys_count>user_count) win=0; else if(sys_count<user_count) win=1; else win=2; return win; } void show_result(int a[][2]) { int i,j; for(i=0;i<4;i++) { printf(" (%d)",i+1); switch(a[i][1]) { case 3: printf("\006"); break; /*黑桃*/ case 2: printf("\003"); break; /*紅心*/ case 1: printf("\004"); break; /*方塊*/ case 0: printf("\005"); break; /*梅花*/ } switch(a[i][0]) { case 1: printf("A "); break; case 11: printf("J "); break; case 12: printf("Q "); break; case 13: printf("K "); break; default: printf("%d ",a[i][0]); break; } } }

  7. #include<stdio.h> void in(void),count(void); int salary[5]={0},t[5]={0}; char name[5][15]; int main() { int flag=1,num; do{ printf("===================\n"); printf("| employee system |\n"); printf("| 1.input data |\n"); printf("| 2.count salary |\n"); printf("| 3.exit |\n"); printf("===================\n"); printf("Please input number(1-3):"); scanf("%d",&num); printf("\n"); switch(num) { case 1:in();break; case 2:count();break; case 3:flag=0;break; default:printf("You input error!!\n"); } }while(flag); return 0; } void in() { int i,num; printf("Please input number of employee(Max5):"); scanf("%d",&num); fflush(stdin); printf("\n"); for(i=0;i<num;i++) { printf("%d. name:",i+1); gets(name[i]); printf("%d. salary:",i+1); fflush(stdin); scanf("%d",&salary[i]); printf("%d. work hour:",i+1); fflush(stdin); scanf("%d",&t[i]); fflush(stdin); } printf("\n"); } 4.撰寫「員工薪資系統」

  8. void count() • { • int num; • char query; • do{ • printf("Please input Query number of employee{"); • printf("If exit, please input \"q\"}: \n"); • fflush(stdin); • scanf("%c",&query); • if(query>='1' && query<='5') • { • num=query-48; • printf("-----------------------------\n"); • printf("number of employee: %d\n",num); • printf("name: %s\n",name[num-1]); • printf("salary: %d\n",salary[num-1]); • printf("work hour: %d\n",t[num-1]); • printf("Total salary: %d\n",salary[num-1]*t[num-1]); • printf("-----------------------------\n"); • } • else if(query='q') • { • printf("QUIT!!\n\n"); • } • else • { • printf("You input error!!\n"); • } • }while(query!='q'); • }

More Related