c++逸出字元、指標

來源:互聯網
上載者:User

標籤:

上篇部落格的答案:

   1: // DataTypeDemo.cpp : 定義控制台應用程式的進入點。
   2: //
   3:  
   4: #include "stdafx.h"
   5: #include <iostream>
   6: /*
   7: 1\輸入成績,告訴我們等級
   8: 
   9: 自己定A  B   C 區間
  10: 
  11: 2\輸入一個班級的成績,也可以不輸入,直接用數組表示  20人
  12: 
  13: 輸出90分以上的百分比
  14: 
  15: 輸出80--90的百分比
  16: */
  17:  
  18: int _tmain(int argc, _TCHAR* argv[])
  19: {
  20:     using std::cout;
  21:     using std::cin;
  22:     using std::endl;
  23:     //第一題
  24:     char rank;
  25:     double gread = 0.0;
  26:     cout << "請輸入你的分數:" << endl;
  27:     scanf_s("%d", &gread);
  28:     if (gread >= 90)
  29:     {
  30:     rank = ‘A‘;
  31:     }
  32:     else if (gread >= 85)
  33:     {
  34:     rank = ‘B‘;
  35:     }
  36:     else if (gread >= 60)
  37:     {
  38:     rank = ‘c‘;
  39:     }
  40:     else{
  41:     rank = ‘d‘;
  42:     }
  43:     cout << "您的等級是:" << rank << endl;
  44:  
  45:     //第二題 這裡我就寫五個
  46:     int stuarray[] = {22,44,88,98,89};
  47:     int acout=0, bcout=0, ccout=0,dcout=0;
  48:     int stulength = sizeof(stuarray) / sizeof(int);
  49:     for (int index = 0; index < stulength;index++)
  50:     {
  51:         if (stuarray[index] >= 90)
  52:             acout++;
  53:         else if (stuarray[index] >= 80)
  54:             bcout++;
  55:         else if (stuarray[index] >= 60)
  56:             ccout++;
  57:         else
  58:             dcout++;
  59:     }
  60:     cout << "90分以上的百分比是:%" << acout *100/ stulength<< endl;
  61:     cout << "80--90的百分比是:%" << bcout*100 / stulength  << endl;
  62:     system("pause");
  63:     return 0;
  64: }
  65:  

 

關於c++中的範圍和變數的聲明使用,這裡我不寫了,太簡單了。

c++運算式:

1、求位元組數: sizeof()

2、下標運算子  []

3、賦值運算子  =

4、算數運算子  +  -  *  /

5、關係運算子  ++ – 等

 

c++指標:

我們從一個數組來進行講解:

   1: // pointDemo.cpp : 定義控制台應用程式的進入點。
   2: //
   3:  
   4: #include "stdafx.h"
   5: #include <iostream>
   6:  
   7: int _tmain(int argc, _TCHAR* argv[])
   8: {
   9:     using std::cout;
  10:     using std::cin;
  11:     using std::endl;
  12:  
  13:     int nArray[] = {1,5,3,4,5,6,7,8,};
  14:     cout << sizeof(nArray) / sizeof(int) << endl;//sizeof 求大小
  15:     int *pArray = nArray;//nArray 指向數組的第一個指標
  16:     cout << pArray << endl;//因此這裡列印地址
  17:     cout << *pArray <<"****"<<pArray[0]<< endl;//這個就=nArray[0]
  18:     cout << "*-********" << endl;
  19:     cout << nArray + 1 << endl;
  20:     cout << *(nArray + 1) << endl;//此處是nArray地址+1也就是nArray[1]
  21:     cout << *pArray + 1 << endl;//此處輸出的是nArray[0]+1=2
  22:     system("pause");
  23:     return 0;
  24: }
  25:  

demo:

1、對所有資料類型定義一個數組,然後將他們的地址列印出來

2、不使用下標,將數組中的值改變

3、將所有的基礎資料類型定義為指標,並通過指標操縱裡面的值

4、int *  換成short *  並列印出相同的結果  (輸入的數字不能超過short 的大小)

c++逸出字元、指標

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.