main()函数-程序入口函数
main()函数-程序入口函数
#include <iostream>
#include <string>
#include <array>
using namespace std;
// 创建数组actior, 30个char
// 创建数组chuck,13个float
void CreationArray(void);
// 创建一个结构体糖块CandyBar,包含三个成员,品牌,重量(浮点型),卡路里(整形)
// 声明结构体创建变量名为snack, 初始化值为Mocha Munch, 2.3, 500,最后显示变量
typedef struct CandyBar {
string brank;
double weight;
int calorie;
}sweets;
sweets CandyBarFunc(sweets snackNumber);
// typedef sweets *(candy)(sweets snackNumber);
// 编写一个程序,用户输入三次50的成绩,显示次数和平均成绩。使用array对象来存储数据
void GradeFunc(int* num, double* ave);
int main(void)
{
// char website[] = "sikiedu";
// cout << website << endl;
// c++
//int* p = NULL;
//if (NULL == p)
//{
// p = new int;
//}
//*p = 20;
//delete p;
//// c
//int* p1 = NULL;
//if (NULL == p1)
//{
// p1 = (int*)malloc(sizeof(int));
//}
//*p1 = 20;
//free(p1);
// c++11中新的数组创建方式:数组的模板类创建
//array<int, 20> a1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
//array<int, 20> a2 = { 0 };
//a2 = a1;
// 第二题
sweets snack;
snack.brank = "0";
snack.weight = 0.0;
snack.calorie = 0;
snack = CandyBarFunc(snack);
cout << "糖块的品牌:" << snack.brank << ",糖块的重量为:" << snack.weight << ",糖块的卡路里为:" << snack.calorie << "." << endl;
// 第三题
int num = 0;
double ave = 0;
GradeFunc(&num, &ave);
cout << "次数:" << num << ",平均成绩:" << ave << endl;
return 0;
}
void CreationArray(void)
{
char actior[30];
float chuck[13];
array<char, 30> actior1;
array<float, 13> chuck1;
char* actior2 = new char[30];
float* chuck2 = new float[13];
delete[] actior2;
delete[] chuck2;
char* actior3 = (char*)malloc(sizeof(char) * 30);
float* chuck3 = (float*)malloc(sizeof(float) * 13);
free(actior3);
free(chuck3);
}
sweets CandyBarFunc(sweets snackNumber)
{
snackNumber.brank = "Mocha Munch";
snackNumber.weight = 2.3;
snackNumber.calorie = 500;
return snackNumber;
}
void GradeFunc(int* num, double* ave)
{
const int temp = 3;
array<int, temp> number;
cout << "输入三次成绩:" << endl;
cout << "第一次:";
cin >> number[0];
cout << "第二次:";
cin >> number[1];
cout << "第三次:";
cin >> number[2];
*num = temp;
*ave = (number[0] + number[1] + number[2]) / temp;
}
数组
array<int, 20> a1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
array<int, 20> a2 = { 0 };
a2 = a1;
int *arr = NULL;
arr = new int[20];
arr[0] = 90;
*(arr + 1) = 80;
delete[] p;
c++申请堆内存
int *p = NULL;
if(NULL == p)
{
// 申请内存空间
p = new int;
}
*p = 20;
cout << *p << endl;
释放内存空间
delete p;
c申请堆内存
int *p = NULL;
if(NULL == p)
{
p = (int*)malloc(sizeof(int));
}
*p = 20;
printf("*p = %d\n", *p);
free(p);
默认情况下cin是通过指定特殊字符来判断是否输入结束的
按字符串大小输入字符串
cin,getline(字符串变量名,字符串大小)
例子:
cin.getline(name, 20);
#include <iostream>
using namespace std;
int Height();
bool TimeRoutine(struct timeStruct* timeRoutine);
float ClassAndGrade(int boyNumber, int girlNumber);
// 第二题使用的结构体
struct timeStruct {
int day;
int hour;
int minute;
int second;
};
int main()
{
//// 第一题
//int height = 0;
//height = Height();
//cout << "身高是:" << height << "cm." << endl;
/*********************************************************************************************************************************************************/
// 第二题
// 编写一个程序,让用户输入秒,然后把它转换成多少天,多少小时,多少分钟和多少秒显示出来
//bool timeBool = false;
//timeStruct timeStr;
//timeStr.day = 0;
//timeStr.hour = 0;
//timeStr.minute = 0;
//timeStr.second = 0;
//timeBool = TimeRoutine(&timeStr);
//if (true == timeBool)
//{
// cout << "时间是:" << timeStr.day << "天," << timeStr.hour << "小时," << timeStr.minute << "分钟," << timeStr.second << "秒." << endl;
//}
//else
//{
// cout << "ERROR!" << endl;
//}
/*********************************************************************************************************************************************************/
// 第三题
// 要求用户输入一个班级的男生女生的数量,并输出女生的比例(百分比)
//int boy = 0;
//int girl = 0;
//if (0 == boy || 0 == girl)
//{
// cout << "输入男生的人数:";
// cin >> boy;
// cout << endl;
// cout << "输入女生的人数:";
// cin >> girl;
// cout << endl;
//}
//float number = ClassAndGrade(boy, girl);
//cout << "比例是:" << number << "%" << endl;
return 0;
}
// 第三题
float ClassAndGrade(int boyNumber, int girlNumber)
{
int sum = 0;
float girl = 0;
sum = boyNumber + girlNumber;
girl = ((float)girlNumber / (float)sum) * 100;
if (0 == girl)
{
return 0;
}
return girl;
}
// 第二题
bool TimeRoutine(struct timeStruct *timeRoutine)
{
long int secondNumber = 0;
if (0 == secondNumber)
{
cout << "输入一个时间秒:";
cin >> secondNumber;
}
timeRoutine->day = (int)(secondNumber / 60 / 60 / 24);
timeRoutine->hour = (int)(secondNumber / 60 / 60 % 24);
timeRoutine->minute = (int)(secondNumber / 60 % 60);
timeRoutine->second = (int)(secondNumber % 60);
if (0 == timeRoutine->day && 0 == timeRoutine->hour && 0 == timeRoutine->minute && 0 == timeRoutine->second)
{
return false;
}
else
{
return true;
}
}
// 第一题
int Height()
{
float heightM = 0;
int heightCM = 0;
cout << "输入身高:" << endl;
cin >> heightM;
cout << endl;
heightCM = (int)(heightM * 100);
//cout << "身高是:" << heightCM << "cm" << endl;
return heightCM;
}
#include <iostream>
#include <stdio.h>
int main()
{
// 命名空间std
/*
cout or endl就包含在std这个命名空间中
而std就包含在iostream这个头文件中
printf包含在stdio.h这个头文件中中
*/
using namespace std;
//using std::cout;
//using std::endl;
//using std::cin;
//
// cout << "..." << endl;等于
// c语言中的printf("...\n");
cout << "Hello world!" << endl;
printf("hello world\n");
return 0;
}
main函数在一个程序中有且只有一个,它是操作系统调用程序的入口
ctrl+k ctrl+c 注释
ctrl+k ctrl+u 取消注释
#include <iostream>
#include <stdio.h>
int main()
{
// 命名空间std
/*
cout or endl就包含在std这个命名空间中
*/
using namespace std;
// cout << "..." << endl;等于
// c语言中的printf("...\n");
cout << "Hello world!" << endl;
printf("hello world\n");
return 0;
}
int main() {} 函数是程序入口
格式化:ctrl+k >>ctrl+f
注释:快捷键快速注释 Ctrl+k+c 快速取消注释 Ctrl+k+u
using std::cout;
using std::endl;
using std::cin;
namespace的个别引用
float i = 0, total = 0;
do
{
cout << "请输入一个数字:";
cin >> i;
total += i;
cout << "当前输入数字之和:" << total << endl;
} while (i!=0);
cout << "退出程序";
C++11中for的进阶使用
for(int temp : 数组名)
{
cout << temp << " ";
}
可让数组全部输出
for(int& temp : 数组名)
{
cout << temp << " ";
temp = 10;
}
&引用,可将数组名对应的地址存储里的值通过temp进行修改
as
全选,快捷键ctrl+k,ctrl+c 取消注释ctrl+k,ctrl+u
#include 预处理指令
<iostream> 输入输出流,i表示输入,o表示输出
如果不引入<iostream>,就无法使用cout
系统内置的用<>
自己创建的用"" 并且加.h
cout << "你好";
如果不引用命名空间则需要在cout前加入明明空间,例如 std::cout << "你好";
ctal+k+f
宏可以代表任意字符串
定义宏后不可以有分号
类型起别名