ETH官方钱包

切換
舊版
前往
大廳
主題

C++上課筆記(7)

查理 | 2013-12-19 19:35:25 | 巴幣 0 | 人氣 236

void型態(tài)沒有回傳值

#include <iostream>
using namespace std;

int main()
{
    void print(int);
    print(25);
    

system("pause");
return 0;
}

void print(int age)
{
     cout<<"我今年"<<age<<"歲"<<endl;
}

-------------------------------------------------------------------------------------------
印星星

#include <iostream>
using namespace std;

int main()
{
    void square(int,int);
    square(5,6);
    

system("pause");
return 0;
}

void square(int a,int b)
{
     int i,j;
     for(i=1;i<=a;i++)
     {
        for(j=1;j<=b;j++)
           cout<<"*";
        cout<<endl;
     }
     
}
------------------------------------------------------------------------------------------------
印框的星星
#include <iostream>
using namespace std;

int main()
{
    void printstar(int,int);
    printstar(5,6);
system("pause");
return 0;
}

void printstar(int a,int b)
{
     int i,j,k;
     for(i=1;i<=a;i++)
     {
        if(i==1||i==a)
          for(j=1;j<=b;j++)
             cout<<"*";
        else
          for(k=1;k<=b;k++)
             if(k==1||k==b)
               cout<<"*";
             else
               cout<<" ";
        cout<<endl;
     }
}
------------------------------------------------------------------------------------------
定義常數(shù)

#include <iostream>
using namespace std;
#include <math.h>
#define BMI(h,w) w/pow(h/100.0,2)
int main()
{
    cout<<BMI(172,68)<<endl;

system("pause");
return 0;
}
-------------------------------------------------------------------------------------------------
使用define定義價錢
Menu()列印選單
total(項目,數(shù)量)計算小計
sum=0定義全域變數(shù)

#include <iostream>
using namespace std;
#define  red  20
#define  green 25
#define  coffee 45
int sum=0;   //總計
void  Menu();   
int total(int ,int );
int main()
{
    int item,qty;
    do{
    Menu();
    cin>>item;
    if(item>=1 && item<=3)
    {  cout<<"數(shù)量:";
       cin>>qty;
    }
    cout<<total(item,qty)<<"元"<<endl;
   }while(item!=4);


    system("pause");
    return 0;
}
void Menu()
{
       cout<<"1.紅茶"<<red<<"元"<<endl;
       cout<<"2.綠茶"<<green<<"元"<<endl;
       cout<<"3.咖啡"<<coffee<<"元"<<endl;
       cout<<"4.結束"<<endl;
       cout<<"請選擇:";
}
int total(int item,int qty)
{
     switch(item)
     {
        case 1:
             sum+=red*qty;
             break;
        case 2:
             sum+=green*qty;
             break;
        case 3:
             sum+=coffee*qty;
             break;
        case 4:
             cout<<"總計:"<<sum<<"元"<<endl;
             system("pause");
             return 0;
     }
     return sum;
}
------------------------------------------------------------------------------------------------------------
指標

#include <iostream>
using namespace std;
int main()
{
    int a=7;
    int *p;    //指標
    p=&a;      //儲存a變數(shù)記憶體位址 ,p指標指向a變數(shù)
    cout<<a<<"  "<<*p<<endl;      
    cout<<p<<endl;                      //位址
    *p+=5;
    cout<<a<<endl;
    
    int b=23;
    p=&b;
    cout<<*p<<endl;
    cout<<p<<endl;
    
    
    system("pause");
    return 0;
}
---------------------------------------------------------------------------

#include <iostream>
using namespace std;
int main()
{

   int *p=new int;
    *p=15;
    cout<<*p<<endl;
    
    int *k;  //宣告      
    k=new int;
    *k=100;
    cout<<*k<<endl;
    
    double *x=new double;
    *x=10.7;
    cout<<*x<<endl;
    
    system("pause");
    return 0;
}
-------------------------------------------------------------------------------------------------------
//指標與陣列
#include <iostream>
using namespace std;
int main()
{
    int var[]={7,3,2,1};
    int sum=0,i;
    for(i=0;i<4;i++)
    {
      cout<<*(var+i)<<endl;
      sum+=*(var+i);   //指標寫法
     // sum+=*var++;   //錯誤,不可以改變陣列的起始位址       
     }  
     cout<<sum<<endl;
     system("pause");
     return 0;
    
}
-----------------------------------------------------------------------------------------------
//指標與陣列
#include <iostream>
using namespace std;
int main()
{
    int var[]={7,3,2,1};
    int sum=0,i;
    int *p;
    p=var;
    for(i=0;i<4;i++)
    {
       cout<<*p<<endl;
       sum+=*p;    //sum+=*p++;
       p++;
    }
    cout<<sum<<endl;
    system("pause");
    return 0;
}
-----------------------------------------------------------------------------------------------
#include <iostream>
using namespace std;
int main()
{
   
   int *a=new int;
   int *b=new int;
   int *c=new int;
   cout<<"input a,b:";
   cin>>*a>>*b;
   *c=*a+*b;
   cout<<*c<<endl;

   system("pause");
   return 0;
}

創(chuàng)作回應

更多創(chuàng)作