|
#1
| |||
| |||
| [問題][C&C++]輸出問題(已解決) 號碼 英 數 國 總分 1 75 70 70 220 2 . . . . 3 . . . . . . 10 ------------------------------ 以上如何輸出 我做的~ 已經定義英 數 國 分數 輸出卻沒有像範例一樣 代碼: int p;
p=1;
for(p=1;p<100;p++)
{
printf("號碼 英 數 國 總分\n");
printf("%d %d %d %d %d", p a[x] b[y] c[z] a[x]+b[y]+c[z]);
}
此文章於 2005-12-13 06:48 AM 被 hikenluck 編輯. |
|
#2
| ||||
| ||||
| 引用:
該不會這就是全部??? |
|
#3
| |||
| |||
| 引用:
你們老師也太狠了 上幾題都還沒搞定 又來了一題 代碼: int p;
printf("號碼 英 數 國 總分\n");
for(p=1;p<100;p++)
printf("%4d %4d %4d %4d %4d", p, a[p], b[p], c[p], a[p]+b[p]+c[p]);
例如 " %4d%3d " , 12,45----->bb12b45 (b表示空格) p為三個陣列的索引值,這樣才可以 印出同一個人的國,英,數的三科成績 此文章於 2005-12-09 07:04 AM 被 hcl0203 編輯. |
|
#4
| ||||
| ||||
| 代碼: #include <iostream>
using namespace std;
int main()
{
//首先必須先為三個成績定義陣列,以方便存放資料
const int maxlength = 3;
double chinese_grade[maxlength]={0};
double english_grade[maxlength]={0};
double math_grade[maxlength]={0};
int i,j,total;
//接下來跑迴圈來輸入學生資料
for(i = 0;i < maxlength; i++)
{
j = i + 1;
cout<<"請輸入第"
<<j
<<"位學生中文成績:";
cin>>chinese_grade[i];
cout<<"請輸入第"
<<j
<<"位學生英文成績:";
cin>>english_grade[i];
cout<<"請輸入第"
<<j
<<"位學生數學成績:";
cin>>math_grade[i];
}
//先列標題
cout<<"====================================="<<endl;
cout<<"學號\t中文\t英文\t數學\t總分"<<endl;
cout<<"====================================="<<endl;
//將所有陣列裡的值都列出來
for(i = 0; i < maxlength; i++)
{
j = i + 1;
total = chinese_grade[i] + english_grade[i] + math_grade[i];
cout<<j
<<"\t"
<<chinese_grade[i]
<<"\t"
<<english_grade[i]
<<"\t"
<<math_grade[i]
<<"\t"
<<total
<<endl;
}
return 0;
}
1. 定義儲存資料的陣列 2. 將資料放進去 3. 排版並將資料讀出來 在範例中我設為三筆而已,要增加請自行修改 |
|
#5
| |||
| |||
| 每個禮拜都一個作業 這次的繳交期限可以到月底 bojack大大 您寫的我不太懂 是我沒學過VC++ 我試試看 vxr兄 程式我補給您 代碼: #include <stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,x,y,z;
printf("輸入成績請按1,輸出請按2");
scanf("%d", &i);
if(i==1)
{
printf("請輸入英文成績:");
scanf("%d", &x);
printf("請輸入數學成績:");
scanf("%d", &y);
printf("請輸入國文成績:");
scanf("%d", &z);
for(x=0;x<=100;x++)
{
a[x]=x;
}
for(y=0;y<=100;y++)
{
b[y]=y;
}
for(z=0;z<=100;z++)
{
c[z]=z;
}
}else if(i==2){
int p;
p=1;
for(p=1;p<100;p++)
{
printf("號碼 英 數 國 總分\n");
printf("%d %d %d %d %d", p a[x] b[y] c[z] a[x]+b[y]+c[z]);
}
}else{
printf("請重新輸入\n");
}
return 0;
}
此文章於 2005-12-09 10:20 PM 被 hikenluck 編輯. |
|
#6
| ||||
| ||||
| 引用:
cin就像是scanf... 個人也是偏好用C 因為可以少打一些東西... 個人覺得閣下寫的程式有些部份是多此一舉.. 這個不需要太多迴圈去跑.. 不然反而會增加時間複雜度.. 我覺得可以不用條件判斷.. 此文章於 2005-12-10 12:39 AM 被 vxr 編輯. |
|
#7
| |||
| |||
| 因為小弟的底子不好 且聽老師講不一定懂 不能當場時做我覺得是缺點 因為回宿舍反而會忘記怎麼弄 有些可以少打自己卻沒辦法做到 或許真的沒想到吧! 讓我想想~ |
|
#8
| |||
| |||
| printf裡頭當然可以做運算... printf("%d %2d %lf %f..." i, j, i*j, i/j, ...),這是可行的。 cin、cout和prinf、scanf好像還是有點差別吧~~ cin、cout是直接輸出輸入,printf則可以做一些格式化的輸出~~
__________________ 掬一把人間淨土,造千年人間仙境 |
|
#9
| |||
| |||
| 引用:
是指x,y,z嗎 代碼: #include <stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,x,y,z;
printf("輸入成績請按1,按任意鍵輸出成績:");
scanf("%d", &i);
switch(i)
{
case 1:
printf("請輸入英文成績:");
scanf("%d", &x);
printf("請輸入數學成績:");
scanf("%d", &y);
printf("請輸入國文成績:");
scanf("%d", &z);
for(x=0;x<=100;x++)
{
a[x]=x;
}
for(y=0;y<=100;y++)
{
b[y]=y;
}
for(z=0;z<=100;z++)
{
c[z]=z;
}
break;
default:
int p;
p=1;
for(p=1;p<100;p++)
{
printf("號碼\t英\t數\t國\t總分\n");
printf("%3d\t", p);
printf("%3d\t", x);
printf("%3d\t", y);
printf("%3d\t", z);
printf("%3d\t", x+y+z);
}
}
printf("輸入成績請按1,按任意鍵輸出成績:");
scanf("%d", &i);
return 0;
}
![]() 如果哪裡打錯, 請糾正我 謝謝 |
|
#10
| |||
| |||
| 第一個問題 你要在輸入1時就能再跑一次 你要用一個迴圈把第六行到倒數第四行包起來 或 程式的倒數第三,四行拿掉, 用 goto 到程式第六行 如下 代碼: #include <stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,x,y,z;
loop_begin: <=== 加上 goto 用的 label
printf("輸入成績請按1,按任意鍵輸出成績:");
scanf("%d", &i);
switch(i)
{
case 1:
printf("請輸入英文成績:");
scanf("%d", &x);
printf("請輸入數學成績:");
scanf("%d", &y);
printf("請輸入國文成績:");
scanf("%d", &z);
for(x=0;x<=100;x++)
{
a[x]=x;
}
for(y=0;y<=100;y++)
{
b[y]=y;
}
for(z=0;z<=100;z++)
{
c[z]=z;
}
break;
default:
int p;
p=1;
for(p=1;p<100;p++)
{
printf("號碼\t英\t數\t國\t總分\n");
printf("%3d\t", p);
printf("%3d\t", x);
printf("%3d\t", y);
printf("%3d\t", z);
printf("%3d\t", x+y+z);
}
}
goto loop_begin; <=== 修改這裡
return 0;
}
你在輸入成績時 printf("請輸入英文成績:"); scanf("%d", &x); <=== 這樣沒錯 for(x=0;x<=100;x++) <=== 這裡錯了 { a[x]=x; } 因為你又叫 x 從 0 到 100, 並且把 x 的值給 a[x] 此時 a 陣列的值如下 0, 1, 2, 3, ....., 100 就不是你輸入的數字了 第三個問題 輸出時 你的迴圈 index 只有用到 p 你並沒有用到 a, b, c 陣列 這裡請參考 hcl0203 的回答 完整程式如下 紅色是修改的部份 代碼: #include <stdio.h>
int main()
{
int a[10],b[10],c[10];
int i,x,y,z;
int index;
index = 0;
loop_begin:
printf("輸入成績請按1,按任意鍵輸出成績:");
scanf("%d", &i);
switch(i)
{
case 1:
printf("請輸入英文成績:");
scanf("%d", &x);
printf("請輸入數學成績:");
scanf("%d", &y);
printf("請輸入國文成績:");
scanf("%d", &z);
if (index >= 10) <== 因為你的陣列只有 10 個
break;
a[index] = x;
b[index] = y;
c[index] = z;
index++;
break;
default:
int p;
p=1;
for(p=0;p<index;p++) <=== 你輸入幾個人, 就輸出幾筆成績
{
printf("號碼\t英\t數\t國\t總分\n");
printf("%3d\t", p);
printf("%3d\t", a[p]);
printf("%3d\t", b[p]);
printf("%3d\t", c[p]);
printf("%3d\t", a[p] + b[p] + c[p]);
}
}
goto loop_begin;
return 0;
}
此文章於 2005-12-11 10:39 PM 被 tim.kang 編輯. |