7706人加入学习
(18人评价)
C++编程系列 第一季编程基础

制作于2018年2月7日

价格 免费

#include <iostream>

#include <string>

#include <array>

using namespace std;

 

int main()

{

 

//1,创建数组actor,里面有30个char

// 创建数组chuck,里面有13个float

char actor[30];

float chuck[13];

 

 

//2,创建一个结构体糖块CandyBar,里面包含三个成员,

//第一个成员存储了糖块的品牌,

//第二个成员存储糖块的重量(可以有小数),

//第三个成员存储了糖块的卡路里(整数)。

//编写一个程序,声明这个结构,创建一个名为snack的CandyBar变量,

//初始化为Mocha Munch,2.3,500,初始化应该声明snack的时候进行。最后程序显示snack变量的内容。

struct CandyBar {

string brand;

float weight;

int cal;

};

CandyBar snack = { "Mocha Munch",2.3,500 };

cout << "Snack:" << endl;

cout << snack.brand << " " << snack.weight << " " << snack.cal << endl;

 

 

//3,编写一个程序,然给用户输入三次50米跑的成绩,显示次数和平均成绩。使用一个array对象来存储数据。

array<float, 3> a;

cout << "请输入第1次成绩:";

cin >> a[0];

cout << "请输入第2次成绩:";

cin >> a[1];

cout << "请输入第3次成绩:";

cin >> a[2];

float total = a[0] + a[1] + a[2];

cout << total / 3 << endl;

 

return 0;

}

 

自写:

char actor[30];
 float chuck[13];
 char* pactor = new char[30];
 float* pchuck = new float[13];
 array<char, 30> aactor;
 array<float, 13> afloat;
 struct CandyBar
 {
  string brand;
  float weight;
  int calorie;
  CandyBar(string brand1, float weight1, int calorie1)
  {
   brand = brand1;
   weight = weight1;
   calorie = calorie1;
   cout << "品牌:" << brand << endl;
   cout << "重量:" << weight << endl;
   cout << "卡路里:" << calorie << endl;
  }
 };
 CandyBar snack{ "Mocha Munch",2.3f,500 };
 
 cout << endl;
 array<float, 3> score;
 short count = 0;
 while (count<3)
 {
  cout << "请输入50米第 " << (count+1) << " 次成绩:";
  cin >> score[count];
  count++;
 }
 cout << "次数:" << count << endl;
 cout << "平均成绩:" << (score[0] + score[1] + score[2]) / count << endl;
[展开全文]

全选后,ctrl+k ctrl+f可以格式化,但是有局限性

 

[展开全文]

若是吧命名空间放在函数外面,其下的所有函数都可以使用,若放在一个函数下方,只能让这个函数使用它。

固定模式:cout<<"";

std下的函数,输出的意思

换行:endl,cout<<"...../n"

cout后可以持续输出,例如:cout<<"你好"<<endl<<"葛赛飞是个坏蛋";

c语言输出函数:  printf(.....);

例如:printf("我得年龄是%s","17");

        printf("我得年龄是%d",17);

[展开全文]

函数前的字母例如

main前的int(整数)为返回值,代码最后有返回语句(return +整数)

main下用大括号抱起来的是函数体

动态链接库自己不会运行,作为一个库,模块,作为一个工具给别人使用,不需要main函数

 

[展开全文]

单行注释:在一行代码前加上//其后的代码无效

/*   中间全部注释   */

全选中后先按 ctrl+k再按ctrl+c

ctrl+k ctrl+u 取消注释

 

 

 

[展开全文]

#include:预处理指令(引入接下来的指令)

iostream:输入 输出

i:input   o:output 

cout只属于iostream,别的用会报错

 iostream属于系统内置的,所以用尖括号<>来引用

若是自己创建头文件要用引号"",而且要加.h

namespace:命名空间

[展开全文]

01
 -在解决方案下面创建项目

 

[展开全文]

 

// unsigned short e = -3;
// cout << e << endl; // 输出:65533 
// unsigned short 类型 范围:0~65535
// 0 65535 65534 65533
// 0 -1        -2        -3

#include <iostream>

#include <climits>

using namespace std;

 

int main()

{

// 类似:超市购物买袋子,袋子大小放的东西多大

short a = 3;

int b = 100;

long c = 900;

long long d = 100;

 

cout << INT_MAX << endl; // 输出:2147483647 // 21亿

cout << INT_MAX << endl; // 输出:-2147483648

 

cout << SHRT_MAX << endl; // 输出:32767 // 3万

cout << SHRT_MAX << endl; // 输出:-32768

 

short level = 8;

 

unsigned short e = 3; // unsigned 无符号的

// unsigned short e = -3;

// cout << e << endl; // 输出:65533

// unsigned short 类型 范围:0~65535

// 0 65535 65534 65533

// 0 -1 -2 -3

 

// int b = 100000000000;

// cout << b << endl; // 输出:1215752192

 

int b = 400000000;

unsigned int f = 4000000000; // unsigned 比原来扩大2倍

cout << b << endl; // 输出:-294967296

cout << f << endl; // 输出:4000000000

 

int t;

cin >> t;

return 0;

}

 

百度:

C++ 整形 范围

https://blog.csdn.net/kazama_kenji/article/details/52229533

[展开全文]

这里要输出string,需要预加载

#include<string>

[展开全文]

新标准使用char 要加数组 

[展开全文]
  1. float a = 3; // int
  2. int b = 4.3;//double 

 float res = 4 = 4.4

float f  = 34.4

int d = int(23.8);

 

 

[展开全文]

定义字符串数组时,要加\0空字符结束

 

[展开全文]

除法运算,两个数是什么类型的,得到的值就是什么类型

 

int a=7

int b=3

int c=a/b;   c=2

float d=a/b; d=2

[展开全文]

\n 和endl 都是换行

cout <<"**\n" ==cout<<"**"<<"\n"==cout<<"**"<<endl

 

[展开全文]

ctrl+k ctrl+c 注释 ctrl+k ctrl+u 除去注释

[展开全文]

授课教师

问问题加入A计划,有专门负责答疑的老师哦!!!

课程特色

下载资料(1)
视频(58)