//
// main.cpp
// myFourthCptimerplusplus
//
// Created by 炒饭 on 2019/4/12.
// Copyright © 2019 炒饭. All rights reserved.
//
//1)创建和使用数组
//2)创建和使用C-风格的字符串
//3)创建和使用String类
//4)使用方法getline()和get()读取字符串
//5)混合输入字符串和数字
//6)创建和使用结构
//7)创建和使用共同体
//8)创建和使用枚举
//注意:数组的i索引是从l0开始的,编译器不会检查使用的下标是否有效,但是会破坏程序数据结构
//c++标准模版库(STL)提供了一种数组替代品---模版类vector
//用引号阔起来的字符串隐士的包含结尾空字符,键盘输入字符串,自动结尾加空字符“\0”
//合理区分字符常量和字符串常量如:“S” 'S'
//地址是C++中的一种独立的类型,不能随便赋值
#include <iostream>
int main()
{
// insert code here...
using namespace std;
int yams[3];
yams[0] = 7;
yams[1] = 8;
yams[2] = 6;
int yamcosts[3] = {20,30,5};
//注意如果的我是c++的编译器或者解释器不能这样取声明
cout << "Toatal yams = ";
cout << yams[0] + yams[1] + yams[2] << endl;
cout << "The package with "<< yams[1]<< "yams costs ";
cout << yamcosts[1] << "cents per yam.\n";
int total = yams[0]* yamcosts[0]+ yams[1]* yamcosts[1];
total =total +yams[2]* yamcosts[2];
cout << "The total yam expense is "<< total << "cents.\n";
cout <<"\nSize of yams array = "<< sizeof yams;
cout <<"bytes.\n";
cout <<"Size of one element =" << sizeof yams[0];
cout <<" bytes.\n";
// int cards[4] = {3,6,8,10};允许
// int hand[4];//允许
// hand[4]={5,6,7,8};//错误
// hand = cards;//错误
//long things[]={1,2,3.0};//允许,系统自己计算个数:sizeof things/sizeof(short)
//只有在定义数组时才能使用初始化,也不能将一个数组赋给另一个数组,可以通过下标给元素赋值
float help[5]={1,2,3};
cout<<"the array help[5] is: "<<endl;
cout<<help[0] <<help[1] <<help[2] <<help[3] <<help[4] <<endl;
//C++帝国
//首先,初始化数组时,l可以省略等号(=)
double earnings[4] {1,2,3,4};
//其次,可不在大括号内包含任何东西,这将把所有元素都设置为零
//sizeof返回数组的长度,strlen返回字符串的长度,不把空字符计算在内
unsigned int counts[10] = {};
char fish[10] = "abcde";
cout << "The fish arry length is : " << sizeof fish << endl;
cout << "The fish arry length is : " << strlen(fish)<< endl;
//字符串拼接,被拼接后,不会在拼接位置之间添加空格等等,第一个对象的空字符也会被取代
cout<< "i an a big boy" "what are you."<<endl;
cout<< "i an a big boy"
"what are you."<<endl;
const int size = 20;
char name[size];
char citys[size];
cout << "enter your name :\n";
//cin()使用空白(空格,TAB,换行符)来确定结束的位置
//cin.getline(数组,个数)读取一行数据,这个地方要注意!!!!!遇见换行符终止,不保存换行符
cin.getline(citys,size);
cout <<"the citys are: "<<citys <<endl;
cin >>name;
cout <<"my name is :"<<name <<endl;
cin.getline(citys,size);
cout <<"the citys are: "<<citys <<endl;
cin.get(name,size);
cout <<"my name is :"<<name <<endl;
cin.get(name,size);
std::cout << "Hello, World!\n";
return 0;
}
实验结果:
Toatal yams = 21
The package with 8yams costs 30cents per yam.
The total yam expense is 410cents.
Size of yams array = 12bytes.
Size of one element =4 bytes.
the array help[5] is:
12300
The fish arry length is : 10
The fish arry length is : 5
i an a big boywhat are you.
i an a big boywhat are you.
enter your name :
zhou jian
the citys are: zhou jian
zhoujian
my name is :zhoujian
the citys are:
zhou jian
my name is :zhou jian
Hello, World!
Program ended with exit code: 0