C 程式

1. 輸入某一同學的GPA,當

GPA=4時,印出excellent student

GPA=3時,印出good student

GPA=2時,印出satisfactory

GPA=1時,則直接印出score=50,

其他則印出Are you a fool or a genius?

(請利用else....if)

2. 將第三題的程式,改成switch....case的形式

Update:

用C++程式

*輸入年份,並判斷此年份是否為閏年或平年

*輸入某一整數,判斷它是為2的倍數,或3的倍數或5的倍數

2 Answers

Rating
  • Favorite Answer

    (1)

    #include<iostream>

    using namespace std;

    int main()

    {

    int GPA;

    while (cin>>GPA)

    {

    if (GPA==4) cout<<"excellent student"<<endl;

    else if (GPA==3) cout<<"good student"<<endl;

    else if (GPA==2) cout<<"satisfactory"<<endl;

    else if (GPA==1) cout<<"score=50"<<endl;

    else cout<<"Are you a fool or a genius"<<endl;

    }

    }

    (2)

    #include<iostream>

    using namespace std;

    int main()

    {

    int GPA;

    while (cin>>GPA)

    {

    switch (GPA)

    {

    case 4: cout<<"excellent student"<<endl; break;

    case 3: cout<<"good student"<<endl; break;

    case 2: cout<<"satisfactory"<<endl; break;

    case 1: cout<<"score=50"<<endl; break;

    default: cout<<"Are you a fool or a genius"<<endl; break;

    }

    }

    }

    (3)

    #include<iostream>

    using namespace std;

    int main()

    {

    int year;

    while (cin>>year)

    {

    if (year%4==0)

    {

    if (year%100!=0) cout<<"西元 "<<year<<" 是閏年"<<endl;

    else cout<<"西元 "<<year<<" 是平年"<<endl;

    }

    else cout<<"西元 "<<year<<" 是平年"<<endl;

    }

    }

    (4)

    #include<iostream>

    using namespace std;

    int main()

    {

    int n;

    while (cin>>n)

    {

    bool flag[3]={false};

    int judge=0;

    S1: if (n%2==0)

    {

    cout<<n<<"是2的倍數"<<endl;

    flag[0]=true;

    judge++;

    if (flag[1]==false&&judge<3) goto S2;

    if (flag[2]==false&&judge<3) goto S3;

    else goto S;

    }

    else if (flag[1]==true) goto S3;

    else if (flag[2]==true) goto S2;

    S2: if (n%3==0)

    {

    cout<<n<<"是3的倍數"<<endl;

    flag[1]=true;

    judge++;

    if (flag[0]==false&&judge<3) goto S1;

    if (flag[2]==false&&judge<3) goto S3;

    else goto S;

    }

    else if (flag[2]==true) goto S;

    S3: if (n%5==0)

    {

    cout<<n<<"是5的倍數"<<endl;

    flag[2]=true;

    judge++;

    if (flag[0]==false&&judge<3) goto S1;

    if (flag[1]==false&&judge<3) goto S2;

    else goto S;

    }

    if (n%2!=0&&n%3!=0&&n%5!=0) cout<<n<<"不是2、3、5的倍數"<<endl;

    S: n=n;

    }

    }

    Source(s): 自己
  • 1 decade ago

    不好意思第一題也就是第三題,所以第2題裡面我所打的第三題就是第一題的部份

    2005-11-01 01:02:19 補充:

    也就是將第一題的程式,在把它改成switch....case的形式

Still have questions? Get your answers by asking now.