ETH官方钱包

切換
舊版
前往
大廳
主題

C++上課筆記(2)

查理 | 2013-12-05 21:46:52 | 巴幣 4 | 人氣 1982

兩值比大小

#include <iostream>
using namespace std;

int main()
{
    float a,b;
    printf("兩值");
    scanf("%f %f",&a,&b);
    if(a>b)
    printf("bigger%.1f\n",a);
    else
    printf("bigger%.1f\n",b);
    
system("pause");
return 0;
}

----------------------------------------------------------------------------------------------------------------------
判斷偶數(shù)奇數(shù)

#include <iostream>
using namespace std;

int main()
{
    int n;
    printf("input n:");
    scanf("%d",&n);
    if(n%2==0)
    printf("偶數(shù)\n");
    else
    printf("奇數(shù)\n");
system("pause");
return 0;
}

-------------------------------------------------------------------------------------------------------------------------
輸入成績(jī)

#include <iostream>
using namespace std;
int main()
{
    int s;
    printf("input score:");
    scanf("%d",&s);
    if(s>=0 && s<=100)  //  ! not ,   && and  ||  or   
       if(s>=60)
         printf("及格\n");
       else
         printf("不及格\n");  
    else
      printf("輸入錯(cuò)誤\n");
      
     system("pause");
     return 0;
}

----------------------------------------------------------------------------------------------------------------------
輸入里程,並計(jì)算出車(chē)費(fèi)。假設(shè)里程在1500公尺以下皆為70元,
每超過(guò)500公尺加5元,不足500公尺以500公尺計(jì)算。

#include <iostream>
using namespace std;

int main()
{
    int m,money,diff;
    cout<<"里程:";
    cin>>m;
    if(m<=1500)
     money=70;
    else
    {
     diff=m-1500;
     if(diff%500==0)
      money=70+(diff/500)*5;
     else
      money=70+((diff/500)+1)*5;
    }
    cout<<"費(fèi)用:"<<money<<endl;
    
    system("pause");
    return 0;
}

-------------------------------------------------------------------------------------------------------------------------

綜合所得稅速算公式
所得淨(jìng)額 x 稅率 - 累進(jìn)差額 = 應(yīng)納稅額
0 ~ 370,000 x 6% - 0

370,001 ~ 990,000 x 13% - 25,900

990,001 ~ 1,980,000 x 21% - 105,100

1,980,001~3,720,000 x 30% - 283,300

3,720,001 以上 x 40% - 655,300

#include <iostream>
using namespace std;

int main()
{   
    int salary;
    double tax;
    cout<<"Input salary:";
    cin>>salary;
    
    if(salary<=370000)
     tax=salary*0.06;
    else if(salary<=990000)
     tax=salary*0.13-25900;
    else if(salary<=1980000)
     tax=salary*0.21-105100;
    else if(salary<=3720000)
     tax=salary*0.3-283300;
    else
     tax=salary*0.4-655300;
    
    cout<<"應(yīng)繳稅額"<<tax<<endl;

system("pause");
return 0;
}

-----------------------------------------------------------------------------------------------------------------------
輸入身高、體重
計(jì)算BMI
BMI=體重/身高*身高(M)
            BMI<18.5 太輕
18.5<=BMI<24    正常
   24<=BMI<27    過(guò)重
   27<=BMI<30    輕度肥胖
   30<=BMI          肥胖

#include <iostream>
using namespace std;
#include <math.h>
int main()
{   
    double w,h,BMI;
    cout<<"身高: 體重:";
    cin>>h>>w;
    h/=100;
    BMI=w/pow(h,2);
    cout<<"BMI:"<<BMI<<endl;
    if(BMI<18.5)
    cout<<"太輕"<<endl;
    else if(BMI<24)
    cout<<"正常"<<endl;
    else if(BMI<27)
    cout<<"過(guò)重"<<endl;
    else if(BMI<30)
    cout<<"輕度肥胖"<<endl;
    else
    cout<<"肥胖"<<endl;
     
system("pause");
return 0;
}

-----------------------------------------------------------------------------------------------------------------------
輸入時(shí)間(分鐘),並計(jì)算出停車(chē)費(fèi)。假設(shè)停車(chē)時(shí)間在 30 分鐘
以下免費(fèi)。超過(guò)時(shí)間,每半小時(shí)為 15 元,不滿(mǎn)半小時(shí)以半
小時(shí)計(jì)算,每日最高以 240 元計(jì)算。
輸入值:0 ~ 720 分鐘之間任意值
輸出值:價(jià)格

#include <iostream>
using namespace std;

int main()
{   
    int m,out;
    int money=0;
    cout<<"輸入時(shí)間:";
    cin>>m;
    if(m>=0&&m<=720)
    {
     if(m<=30)
      cout<<"30分鐘以下免費(fèi)"<<endl;
     else
     {
      out=m-30;
      if(out%30==0)
       money=(out/30)*15;
      else
       money=((out/30)+1)*15;
      cout<<"停車(chē)金額:"<<money<<endl;
     }
     if(money>=240)
     {
      money=240;
      cout<<"最高停車(chē)費(fèi):"<<money<<endl;
     }
    }
    else
    cout<<"輸入錯(cuò)誤"<<endl;
   
system("pause");
return 0;
}

創(chuàng)作回應(yīng)

更多創(chuàng)作