【2018.3.2】實驗二
阿新 • • 發佈:2018-03-03
小孩 ret pos 給定 包括 飲食 eight 結構 main
1 //******************************************************** 2 //*程序作者:兔至 3 //*完成日期:2018年3月2日 4 //*章 節:第一章 5 //*題 號:習題4 6 //*題 目:學生成績統計 7 // 從鍵盤輸入一個班(全班最多不超過30人)學生某門課 8 // 的成績,當輸入成績為負值時,輸入結束,分別實現下列功能: 9 // (1)統計不及格人數並打印不及格學生名單; 10 // (2)統計成績在全班平均分及平均分之上的學生人數,並打印這些學生的名單;11 // (3)統計各分數段的學生人數及所占的百分比。 12 // 註:將成績分為六個分數段, 13 // 60分以下為第0段, 14 // 60~69為第1段, 15 // 70~79為第2段, 16 // 80~89為第3段, 17 // 90~99為第4段, 18 // 100分為第5段。 19 // 編程要求: 20 // (1).較好的用戶輸入輸出提示信息21 // (2).使用子函數來實現上述各個功能 22 // (3).最好不要使用全局變量 23 //******************************************************** 24 25 #include<iostream> 26 #include<string> 27 using namespace std; 28 struct score 29 { 30 string name; 31 float point; 32 }; 33 void fail(score Score);34 void exce(score Score); 35 void per(score Score); 36 int main() 37 { 38 score Score[30]; 39 int i; 40 char code; 41 for(i=0;Score[i].point >=0;i++) 42 { 43 cout << "請輸入學生姓名:"; 44 cin >> Score[i].name; 45 cout << endl; 46 cout << "請輸入該學生成績:"; 47 cin >> Score[i].point; 48 cout << endl; 49 } 50 cout << "A、統計不及格人數並打印不及格學生名單\nB、統計成績在全班平均分及平均分之上的學生人數,並打印這些學生的名單\nC、統計各分數段的學生人數及所占的百分比\n請輸入您想進行的操作代號:"; 51 cin >> code; 52 switch(code) 53 { 54 case(‘A‘):fail(*Score);break; 55 case(‘B‘):exce(*Score);break; 56 case(‘C‘):per(*Score);break; 57 } 58 return 0; 59 } 60 61 void fail(score Score) 62 { 63 int i,count=0; 64 cout << "不及格的學生為:"; 65 for(i=0;Score[i].name !=‘\0‘;i++) 66 { 67 if(Score[i].point<60) 68 { 69 count++; 70 cout << Score[i].name<<" "; 71 } 72 } 73 cout << endl; 74 cout << "不及格的人數為:"<<count; 75 } 76 77 void exce(score Score) 78 { 79 float ave; 80 int sum=0,count=0,i,Count=0; 81 for(i=0;Score[i].name!=‘\0‘;i++) 82 { 83 count++; 84 sum+=Score[i].point; 85 } 86 ave=sum/count; 87 cout << "成績在平均分及其上的學生為:"; 88 for(i=0;Score[i].name!=‘\0‘;i++) 89 { 90 if(Score[i].point>=ave) 91 { 92 cout << Score[i].name<<" "; 93 Count++; 94 } 95 } 96 cout << endl; 97 cout << "成績在平均分及其上的學生人數為:"<<Count<<endl; 98 } 99 100 void per(score Score) 101 { 102 int count=0,count_0=0,count_1=0,count_2=0,count_3=0,count_4=0,count_5=0,i; 103 float per0,per1,per2,per3,per4,per5; 104 for(i=0;Score[i].name!=‘\0‘;i++) 105 { 106 count++; 107 if(Score[i].point<60) 108 count_0++; 109 else if(Score[i].point>=60&&Score[i].point<=69) 110 count_1++; 111 else if(Score[i].point>=70&&Score[i].point<=79) 112 count_2++; 113 else if(Score[i].point>=80&&Score[i].point<=89) 114 count_3++; 115 else if(Score[i].point>=90&&Score[i].point<=99) 116 count_4++; 117 else if(Score[i].point==100) 118 count_5++; 119 } 120 per0=count_0/(100*count); 121 per1=count_1/(100*count); 122 per2=count_2/(100*count); 123 per3=count_3/(100*count); 124 per4=count_4/(100*count); 125 per5=count_5/(100*count); 126 cout << "各分數段的學生人數及所占的百分比:\n"; 127 cout << "第0段:" <<per0<<"%"<<endl; 128 cout << "第1段:" <<per1<<"%"<<endl; 129 cout << "第2段:" <<per2<<"%"<<endl; 130 cout << "第3段:" <<per3<<"%"<<endl; 131 cout << "第4段:" <<per4<<"%"<<endl; 132 cout << "第5段:" <<per5<<"%"<<endl; 133 }
想要傳遞結構數組到子函數裏,但是子函數裏用“Score[i].point”就會提示格式不相稱……
1 //******************************************************** 2 //*程序作者:兔至 3 //*完成日期:2018年3月1日 4 //*章 節:第一章 5 //*題 號:習題3 6 //*題 目: 身高預測:每個做父母的都關心自己孩子成人後的身高, 7 // 據有關生理衛生知識與數理統計分析表明,影響小孩成人後 8 // 的身高的因素包括遺傳、飲食習慣與體育鍛煉等。小孩成人 9 // 後的身高與其父母的身高和自身的性別密切相關。 10 // 設faHeight為其父身高,moHeight為其母身高,身高預測公式為 11 // 男性成人時身高=(faHeight + moHeight)×0.54cm 12 // 女性成人時身高=(faHeight×0.923 + moHeight)/2cm 13 // 此外,如果喜愛體育鍛煉,那麽可增加身高2%;如果有良好的衛 14 // 生飲食習慣,那麽可增加身高1.5%。編程從鍵盤輸入用戶的性別 15 // (用字符型變量sex存儲,輸入字符F表示女性,輸入字符M表示男性) 16 // 父母身高(用實型變量存儲,faHeight為其父身高,moHeight為其母身高)、 17 // 是否喜愛體育鍛煉(用字符型變量sports存儲,輸入字符Y表示喜愛, 18 // 輸入字符N表示不喜愛)、是否有良好的飲食習慣等條件 19 // (用字符型變量diet存儲,輸入字符Y表示良好,輸入字符N表示不好) 20 // ,利用給定公式和身高預測方法對身高進行預測。 21 // 編程要求:有用戶輸入輸出提示信息。 22 //******************************************************** 23 24 #include<iostream> 25 using namespace std; 26 int main() 27 { 28 float kheight,faheight,moheight; 29 char sex,diet,sports; 30 cout << "【兒童身高神預測】"<<endl; 31 cout << endl; 32 cout << "請輸入父親身高:"; 33 cin >> faheight; 34 cout << endl; 35 cout << "請輸入母親身高:"; 36 cin >> moheight; 37 cout << endl; 38 cout << "請輸入孩子性別:"; 39 cin >> sex; 40 cout << endl; 41 if(sex == ‘F‘) 42 kheight=(faheight*0.923+moheight)/2; 43 else 44 kheight=(faheight + moheight)*0.54; 45 cout << "請問您的孩子喜愛運動嗎?"; 46 cin >> sports; 47 if(sports==‘Y‘) 48 kheight*=1.02; 49 cout << endl; 50 cout << "請問您的孩子是否有良好的飲食習慣?"; 51 cin >> diet; 52 cout << endl; 53 if(diet==‘Y‘) 54 kheight*=1.015; 55 cout << "您的孩子的預測身高為:"<<kheight<<"cm\n[註:此預測僅供娛樂。]"; 56 return 0; 57 }
這個倒是挺好玩??
【2018.3.2】實驗二