瀏覽單個文章
flowofsoul
Major Member
 
flowofsoul的大頭照
 

加入日期: Jul 2004
文章: 292
引用:
作者jackyepson
我是寫這樣子的!!!這真的是作業,我也有問我們老師,可是講的不是很明白!!! 喔......對了也要可以比較負數才行!!!
.....
if(a>b)
b=a;
if(b>a)
if(a>c)
c=a;
}
printf("輸入中最大者和次大者依序是:%d和%d\n",(signed)b,(signed)c);
}


老實說,你貼的這源碼能編譯得過都很難.....而且邏輯上有誤。但你貼的源碼根本編譯不過,所以我沒辦法指出你錯在哪。

不過有個很明顯的地方,如果 a>b is true那下一步比較 b>a 根本無意義,因為你已經把 a 指派給 b 了。

後面的源碼殘缺不全小弟就不多說了。

現在給你一個c++的解答,因為我是學c++的,c的語法不熟(有了cout跟cin,誰還想用scanf跟printf),你自己代換。

cin 用 scanf 代換,cout 用 printf 代換。

源碼如下

//////////////////////////////////////////////

int test1,test2,test3;

cout << "請輸入整數,若輸入0則程式結束" << endl;

cin >> test1;



while(test1){
cout << "請輸入第二個整數" << endl;

cin >> test2 ;

test3 = test1 > test2 ? test1 : test2;

cout << "最大為" << test3 ;

test3 = test1 < test2 ? test1 : test2;

cout << "次大為" << test3 << endl;

cout << "請輸入整數,若輸入0則程式結束" << endl;

cin >> test1;
}

////////////////////////////

如果你想把這個程式拿去編譯,記得main的前面要加上
#include <iostream>
#include <string>

這個程式已經在我的visual c++ .net上確認執行正確無誤,負數也可比較。參考一下吧。

要是想比較不只兩個整數,那就麻煩一點。
     
      
舊 2004-10-28, 08:57 PM #11
回應時引用此文章
flowofsoul離線中