# C 程式

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

GPA=4時,印出excellent student

GPA=3時,印出good student

GPA=2時,印出satisfactory

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

(請利用else....if)

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

Update:

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

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

Rating

(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): 自己