|
#11
| ||||
| ||||
| goto這種東西 我覺得能不用就是盡量不要用.. ![]() 而且只用兩個迴圈 這樣的情況就不會很複雜... |
|
#12
| |||
| |||
| 老師說過不能用goto 請問有其他的辦法嗎? 真苦惱~ |
|
#13
| ||||
| ||||
| 引用:
最重要要靠想像力.. 以下是我寫的 三個迴圈就搞定.. 用條件判斷反而會增加複雜度.. 不能用goto的原因是這種東西用多了 程式會寫的非常難看. 但不是說他不能用. 只是不要常用.. 代碼: #include "stdafx.h"
#include <iostream>
using namespace std;
#define size 1000
void main()
{
int g1,cht[size],eng[size],math[size],v1=0,v2,rec[size],v3=1;
char g2='2';
for( ; g2!='1'; v3=v3+v1)
{
cout<<"請問要輸入多少比成績值組??"<<endl;
cin >>g1;
for( v1=0 ; v1<g1 ; v1++ )
{
cout <<"請輸入第 "<<v1+1 <<" 筆國文成績"<<endl;
cin>> cht[v1];
cout <<"請輸入第 "<<v1+1 <<" 筆英文成績"<<endl;
cin>> eng[v1];
cout <<"請輸入第 "<<v1+1 <<" 筆數學成績"<<endl;
cin>> math[v1];
rec[v1]=(cht[v1]+eng[v1]+math[v1])/3;
}
for(v2=v1-1 ; v2>=0 ; v2--)
{
cout<<"第 "<<v1-v2<<"筆成績值組輸出結果為 " <<" 國文= "<<cht[v2]<<" 英文= "<<eng[v2]<<" 數學= "<<math[v2]<<" 結算平均為 "<<rec[v2]<<endl;
}
cout<<"請問是否還要繼續計算?? (YES=Any key[number!=1] ; No=1) ";
cin >>g2;
}
cout<<"閣下總共填入 "<<v3-1 <<" 筆成績值組"<<endl;
cout<<endl;
system("pause");
}
此文章於 2005-12-12 03:05 AM 被 vxr 編輯. |
|
#14
| |||
| |||
| 平均怎麼弄? 我在最後的平均出現問題 大大幫幫忙解說一下 代碼: #include <stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,j,x,y,z,p;
j=0;
printf("輸入成績請按1,按任意鍵輸出成績:");
scanf("%d", &i);
int index;
index=0;
switch(i)
{
case 1:
while(i==1){
j=j+1;
printf("請輸入%d號的英文成績:", j);
scanf("%d", &x);
printf("請輸入%d號的數學成績:", j);
scanf("%d", &y);
printf("請輸入%d號的國文成績:", j);
scanf("%d", &z);
if(index<10)
{
a[index]=x;
b[index]=y;
c[index]=z;
index++;
}
printf("輸入成績請按1,按任意鍵輸出成績:");
scanf("%d", &i);
}
default:
printf("號碼\t英\t數\t國\t總分\n");
j=0;
while(i!=1){
for(j=0;j<index;j++)
{
printf("%d\t", j+1);
printf("%d\t", a[j]);
printf("%d\t", b[j]);
printf("%d\t", c[j]);
printf("%d\n", a[j]+b[j]+c[j]);
i=1;
}
}
}
printf("----------------------------------------------\n");
printf("平均\t");
printf("%d\t", x/j);
printf("%d\t", y/j);
printf("%d\t\n", z/j);
return 0;
}
|
|
#15
| |||
| |||
| 平均設浮整數會比較好吧!你的問題大概是沒有小數點吧! 所以變數設float應該就可以出現小數點了 不然也可以使用轉碼 |
|
#16
| ||||
| ||||
| 好奇怪的平均成績計算??!! 如果我輸出的成績值組達到2筆以上.. 請問要怎麼算出平均成績??? 你那個只能算一筆成績.. 如果我ㄧ開始根本不想算.. 一開始就按i>1的值以上 請問怎麼辦?? array裡面擺的值全都空的.. 程式就掛在那邊了 這邊也是要解決的 麻煩請再檢查吧.. |
|
#17
| |||
| |||
| 平均 = 總和 / 個數 所以 總和 = 陣列中所有值相加 個數 = index 程式修改如下: 代碼: printf("----------------------------------------------\n");
printf("平均\t");
int total;
float avg_a, avg_b, avg_c;
total = 0;
for (j=0; j<index; j++)
total = total + a[j]; // 用迴圈將 a 陣列中所有值相加, 得到總和
avg_a = (float) total / index; // avg_a 即 a 的平均
/* 同理可得 */
total = 0;
for (j=0; j<index; j++)
total = total + b[j];
avg_b = (float) total / index;
total = 0;
for (j=0; j<index; j++)
total = total + c[j];
avg_c = (float) total / index;
/* 最後將 avg_a, avg_c, avg_c 印出 */
printf("%f/t%f/t%f\n", avg_a, avg_b, avg_c);
|
|
#18
| |||
| |||
| 太感謝 tim.kang 提供的修改後的程式~今天終於可以吧作業交出去了~感謝啊~ |