ETH官方钱包

切換
舊版
前往
大廳
主題

C++上課筆記(4)

查理 | 2013-12-10 20:54:33 | 巴幣 0 | 人氣 206

輸入兩值做加總,詢問是否繼續(y/n)

#include <iostream>
using namespace std;
int main()
{
    int a,b,sum;
    char ans;
    do
    {
       cout<<"Input a,b:";
       cin>>a>>b;
       cout<<a<<"+"<<b<<"="<<sum<<endl;
       cout<<"continue?(y/n)";
       cin>>ans;
    }while(ans=='y' ||ans=='Y');
    cout<<"謝謝使用"<<endl;
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------
輸入數值(1~6)
若錯誤列印訊息,並在輸入一次

#include <iostream>
using namespace std;
int main()
{
    int n;
    do
    {
       cout<<"Input n(1~6):";
       cin>>n;
       if(n<1 || n>6)
          cout<<"輸入錯誤"<<endl;
    } while(n<1 || n>6);
      cout<<"你輸入的是:"<<n<<endl;
system("pause");
return 0;
}
-------------------------------------------------------------------------------------
巢狀迴圈
****
****
****
****
****
****

#include <iostream>
using namespace std;
int main()
{
    int i,j;
    for(i=1;i<=6;i++)
    {
       for(j=1;j<=4;j++)
       {
          cout<<"*";
       }
       cout<<endl;
    }

system("pause");
return 0;
}
----------------------------------------------------------------------------------------------
12345
  12345
    12345
      12345
#include <iostream>
using namespace std;
int main()
{
    int i,j,k;
    for(i=1;i<=7;i++)
    {
       for(j=1;j<=i-1;j++)
       {
          cout<<" ";
       }
       for(k=1;k<=5;k++)
       {
          cout<<k;
       }
       cout<<endl;
    }

system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------------
continue and break

#include <iostream>
using namespace std;
int main()
{
    int n;
    
    do{
        cout<<"input n(1~6):";
        cin>>n;
        if(n<1 || n>6)
        {   cout<<"輸入錯誤,再輸入一次"<<endl;;
           continue;
        }
        else
          break;
    }while(1);   //無限迴圈
    cout<<"你輸入的是:"<<n<<endl;

    system("pause");
    return 0;
}
----------------------------------------------------------------------------------------------------------
輸入6值找出最大值

#include <iostream>
using namespace std;
int main()
{
    int i,max=0;
    int a[6];
    for(i=0;i<6;i++)
    {
       cout<<"a["<<i<<"]=";
       cin>>a[i];
       if(a[i]>max)
       {
          max=a[i];
       }
    }
    cout<<"max="<<max<<endl;
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------
已知資料,做處理

#include <iostream>
using namespace std;
int main()
{
    int h[]={175,158,168,180};
    int w[]={85,60,45,83};
    double BMI[4];
    int i;
    double t;
    for(i=0;i<4;i++)
    {
       t=h[i]/100.0;
       BMI[i]=w[i]/(t*t);
       cout<<BMI[i]<<endl;
    }
system("pause");
return 0;
}
------------------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
    int y,m;
    int days[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
    cout<<"year:";
    cin>>y;
    cout<<"month:";
    cin>>m;
    if(y%4==0&&y%100!=0||y%400==0)
       days[2]=29;
    cout<<"有"<<days[m]<<"天"<<endl;
system("pause");
return 0;
}
--------------------------------------------------------------------------------------------------------------------
高公局今天將先公告國道計程收費方案,前20公里免費
小客車、小貨車每公里一.二元、兩百公里以上○.九元,
大貨車每公里一.五元、兩百公里以上一.一二元,
大客車及聯結車每公里一.八元、兩百公里以上一.三五元。

1.2,1.5,1.8
0.9,1.12,1.35

#include <iostream>
using namespace std;
int main()
{
    double cost[]={1.2,1.5,1.8};
    double discount[]={0.9,1.12,1.35};
    int km,item;
    double tal=0;
    cout<<"1.小客車,小貨車"<<endl;
    cout<<"2.大貨車"<<endl;
    cout<<"3.大客車,聯結車"<<endl;
    cout<<"請選擇:";
    cin>>item;
    
    cout<<"公里數:";
    cin>>km;
    if(km>20)
    {
       km-=20;
       if(km<200)
         tal=km*cost[item-1];
       else
         tal=200*cost[item-1]+(km-200)*discount[item-1];
       cout<<"國道費用:"<<tal<<endl;         
    }
    else
      cout<<" 20公里以下免費"<<endl;
    




    system("pause");
    return 0;
}

創作回應

更多創作