c++转义字符、指针

时间:2023-03-09 14:24:20
c++转义字符、指针

上篇博客的答案:

   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++中的作用域和变量的声明使用,这里我不写了,太简单了。

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 的大小)