C++基础入门---不会写代码的Robotic Newbie(1)

前记:问:为什么要记录这些?

           答:还不是因为自己依旧是个菜鸡*_* //

1)各种语言入门参考:https://www.runoob.com/cplusplus/cpp-data-structures.html 

2)运行报错----->缺少ucrtbased.dll文件的解决参考:https://blog.csdn.net/so_geili/article/details/53009680

#include <iostream>     //for using cout
#include <stdlib.h>     //for using the function Sleep
#include <windows.h>   //for using the function Sleep都可以
#include <windows.h>
#include <iomanip> // 标识符setw(2),默认对齐?
#include<string>  //用C++风格字符串
#include <ctime>//使用系统的时间
#define _CRT_SECURE_NO_DEPRECATE;
using  namespace std;
//基本框架
/*
这是多行注释。
*/
#define DAY 7  //宏常量,一般先定义
#define month 30
//函数声明和变量声明都可以在头部编写。在主函数代码块或子函数中被定义和初始化
// 函数声明 
void func(void);
void funcmath(void);
int max1(int num1, int num2);
int bestLucky();//红包
int Array();
char String(void);
void Pointer(void);
void  Get_Current_Date();//当下时间
static int count1 = 10; /* 全局变量 */

int main()
{
	system("color 3A"); //可用于设置颜色,其中color后面的1是背景色代号,A是前景色代号
	/*0=黑色,1=蓝色,2=绿色,3=湖蓝色,4=红色,5=紫色;
    6=黄色,7=白色,8=灰色,9=淡蓝色,A=淡绿色,B=浅绿色;
    C=淡红色,D=淡紫色,E=淡黄色,F=亮白色。*/

	printf("This next row code will open the web.\n");
	Sleep(1000);
	cout << "下面一行代码打开对应的网址" << endl;
	Sleep(1000);         //make the programme waiting for 1 seconds
ShellExecute(0, "open", "https://www.runoob.com/cplusplus/cpp-variable-scope.html", 0, 0, 1); 
	//若出现加载combase.dll,可以设置工具--选项--调试--符合--取消选项pdb指定的远程符合文件地址。
	
	const int year = 365;//const修饰的变量--常量
	//system("color 1B"); //可用于设置颜色,其中color后面的1是背景色代号,A是前景色代号
//  cout<<用于输出字符
	Sleep(1000);
	cout << "cout<<用于输出且匹配对应类型,\n进行换行,cout也可以通过<<符合持续输出如下。"<<endl;
	Sleep(1000);
	cout << "hello world\n"<<"大家好才是真的好!\n" << "梦想就是让人感到坚持就是幸福的东西!\n" << "出自电影《中国合伙人》!\n"; //输出文字
	
  // \是用于表示转义字符:如:\n表示换行;\t表示整齐输出
	cout << "aa\tThis is OK"<<endl;
	cout << "aaaabb\tThis is OK"<<endl;
	cout << "abb\tThis is OK" << endl;
	Sleep(1000);

	cout << "一周有:" << DAY << "天\n";
	Sleep(1000);
	cout << "一个月有:" << month << "天\n";
	Sleep(1000);
	cout << "一年大概有:" << year << "天"<<endl;
	Sleep(1000);         //make the programme waiting for 2 seconds
	// 变量初始值
	int num1 = 10;//科学计数法30000=3e4;
	double num4 = 3e-4;
	cout << "计数法的值num4="<<num4<<endl;
	double num5 = 3e4;
	cout << "计数法的值num5=" << num5 << endl;
	float num2 = 20.89f;
	double num3 = 2.00034123;
	float sum = num1 + num2 + num3;
	double sum1 = num1 + num2 + num3;
	Sleep(1000);
	//setw()默认填充的内容为空格,可以setfill()配合使用设置其他字符填充。
	cout << setfill('*') << setw(5) << 'a' << endl;
	//字符,字符串
	char ch2 = 'a';               //int强制转换为整型
	cout <<"a对应的ASCII码值为:"<< (int)ch2 << endl;//字符A对应的ascii码值
	Sleep(1000);
	char ch1 = 'A';
	cout << "A对应的ASCII码值为:" << (int)ch1 << endl;//字符A对应的ascii码值
	string str= "你好啊";//C++风格,要包含头文件#include<string>
	char str1[]= "谢谢你,我很好!";//加[],双引号C风格
	cout << str << endl;
	cout << str1 << endl;
// bool类型
	Sleep(1000);
	bool flag = true;
	cout <<"真的布尔数据值:"<< flag<< endl;
	bool flag1 = false;
	cout << "假的布尔数据值:" << flag1 << endl;
	//默认情况输出6位有效数字
	cout << "计算结果sum=" << sum << endl;
	cout << "计算结果sum1=" << sum1 << endl;
	Sleep(1000);
	cout << "输出空行的方式cout << endl和下面两种)";
	cout << endl;
	cout << "\n";
	printf("\n");

    Sleep(1000);
	//system("color 4D"); //可用于设置颜色,其中color后面的1是背景色代号,A是前景色代号
	//  使用了 endl,这将在每一行后插入一个换行符。相当于在每个“”中加\n.
//  << 运算符用于向屏幕传多个值,
//  sizeof() 函数用来获取各种数据类型的大小
	cout << "不同数据类型占用不同的内存空间\n";
	cout << "sizeof()函数用来变量占用的内存空间大小\n";
	Sleep(1000);
	cout << "type: \t\t" << "************size**************" << endl;
	cout << "int: \t\t" << "所占字节数:" << sizeof(int);
	cout << "\t最大值:" << (numeric_limits<int>::max)();
	cout << "\t最小值:" << (numeric_limits<int>::min)() << endl;
	Sleep(1000);
	cout << "float: \t\t" << "所占字节数:" << sizeof(float);
	cout << "\t最大值:" << (numeric_limits<float>::max)();
	cout << "\t最小值:" << (numeric_limits<float>::min)() << endl;
	Sleep(1000);
	cout << "size_t: \t" << "所占字节数:" << sizeof(size_t);
	cout << "\t最大值:" << (numeric_limits<size_t>::max)();
	cout << "\t最小值:" << (numeric_limits<size_t>::min)() << endl;
	Sleep(1000);
	cout << "string: \t" << "所占字节数:" << sizeof(string) << endl;
	// << "\t最大值:" << (numeric_limits<string>::max)() << "\t最小值:" << (numeric_limits<string>::min)() << endl;  
	cout << "type: \t\t" << "************size**************" << endl;
	cout << endl;
	Sleep(1000);
	//system("cls");//清除输出
	//system("color 3C"); //可用于设置颜色,其中color后面的1是背景色代号,A是前景色代号
	Sleep(1000);
	std::cout << "测试全局变量count,循环调用函数func" << endl;
	while (count1--)
	{
		func();
		Sleep(1000);
	}
	//system("cls");//清除输出
	//system("color 4E"); //可用于设置颜色,其中color后面的1是背景色代号,A是前景色代号
	funcmath();
	Sleep(1000);
	int ret;
	ret=max1(num3,num4); // 调用函数来获取最大值
	cout << "两个数的最大值为:" << ret << endl;
	Sleep(1000);
	bestLucky();
	Sleep(1000);
	cout << "测试数组定义和赋值" << endl;
	Array();
	Sleep(1000);
		String();
		Sleep(1000);
		std::cout << "测试指针(*)和引用(&)" << endl;
		Pointer();
		Sleep(1000);
		std::cout << "测试指针(*)和引用(&)" << endl;

//时间显示
// 将当前日期以 20** - ** - ** 格式输出
     cout<<"使用函数Get_Current_Date()进行时间输出"<<endl;
		for (int i = 0; i <=10; i++)
		{
			Get_Current_Date();
		}
		Sleep(1000);
		cout << "cls进行清除屏幕" << endl;
	system("pause");
	return 0;
}
	// 函数定义
	void func(void) //void表示函数不返回数值;
	// 函数格式 return_type function_name( parameter list ),return_type 是函数返回的值的数据类型
	{
		static int i = 5; // 局部静态变量
		i++;
		std::cout << "变量 i 为 " << i;//std::cout标准输出,std::cin标准输入
		std::cout << " , 变量 count1 为 " << count1 << std::endl;
	}
//************************************************************************
	void funcmath(void)
	{ //乘法表显示
		int i, j;
		for (i = 1; i < 10; i++) 
		{
			for (j = 1; j <= i; j++) //后置++,先运算再对变量进行加1;如果是前置递增,先让变量加1,再进行运算
			{
				cout << j << " × " << i << " = " << setw(2) << i * j << "  ";//setw(2)默认右对齐的情况下限制输出字符的域宽
			}
			cout << endl << endl;
		}
		int A, B;
			printf("请输入两个数字进行比较");
			cin >> A >> B;//输入数字且为对应变量的初始值
			A>B ? cout << A << "大于" << B << endl 
			:A==B ? cout << A << "等于" << B << endl
			: cout << B << "大于" << A << endl;
//***************************************************************************//
	/* 四个数值中的最大值;使用? : 运算符,可以用if else来代替
    格式--->Exp1 ? Exp2 : Exp3;
	? : 表达式的值取决于 Exp1 的计算结果。如果 Exp1 为真,则计算 Exp2 的值,
	且 Exp2 的计算结果则为整个 ? : 表达式的值。如果 Exp1 为假,则计算 Exp3 的值,
	且 Exp3 的计算结果则为整个 ? : 表达式的值。
    ? 被称为三元运算符,因为它需要三个操作数
	*/
			int a[5], max;//声明变量
			cout << "请输入四个数字:";
			cin >> a[1] >> a[2] >> a[3] >> a[4];
			max = a[a[a[1]>a[2] ? 1 : 2]>a[a[3]>a[4] ? 3 : 4] ? a[1]>a[2] ? 1 : 2 : a[3]>a[4] ? 3 : 4];
			cout << "最大值为:" << max << endl;

// 运算
			int q=100;
		    q = 12;
			q+= 34;
			cout <<"运算测试即:a+=1;等价于a=a+1;" <<q << endl;
	}
//表示函数返回整型的数值******************************************************
	int max1(int num3, int num4)
	{
		Sleep(1000);
		cout << "请输入两个数值进行比较:" << endl;
		cin >> num3 >> num4;
		// 局部变量声明
		int result=0;

		if (num3 > num4)
			result = num3;
		else
			result = num4;
		
		return result;
	}
//生成随机数,构建抢红包机制******************************************
	int bestLucky()
	{
		int i, number;
		int best;//手气最佳
		float total;

		cout << "请输入红包金额:";
		cin >> total;
		cout << "请输入抢红包人数:";
		cin >> number;
		/* 生成随机数 */
		// 设置种子
		srand((unsigned)time(NULL));
		float a[1024];//保存每个人的随机数。最多支持1024个人抢红包。
		float b[1024];//保存每个人获得的红包金额。
		float suma = 0;//随机数总和。
		float sumb = 0;//红包总和。
		int max = 0;
		for (i = 0; i < number; i++)
		{
			// 生成实际的随机数
			a[i] = rand() % 100;

			if (a[i] > max) {
				max = a[i];
				best = i;//获取手气最佳
			}
			suma += a[i];
		}

		for (i = 0; i < number - 1; i++)
		{
			b[i] = a[i] / suma * total;//按照随机数计算每个人实际获得的金额
			sumb += round(b[i] * 100) / 100.0;//将红包金额保留两位小数
											  //输出信息
			cout << "第" << setiosflags(ios::right) << setw(3) << i + 1 <<
				"个人的红包是:" << setiosflags(ios::right) << setw(6) <<
				setiosflags(ios::fixed) << setprecision(2) <<
				round(b[i] * 100) / 100.0;
			if (best == i) {
				cout << "(手气最佳)" << endl;
			}
			else {

				cout << endl;
			}
		}
		//最后一人的红包金额等于总金额减去前面的金额。
		cout << "第" << setiosflags(ios::right) <<
			setw(3) << number << "个人的红包是:" <<
			setiosflags(ios::right) << setw(6) << setiosflags(ios::fixed) <<
			setprecision(2) << round((total - sumb) * 100) / 100.0;
		if (best == number - 1) {
			cout << "(手气最佳)" << endl;
		}
		else {

			cout << endl;
		}
		return 0;
	}
//数组
	int Array()//数组
	{
		int n[10]; // n 是一个包含 10 个整数的数组

				   // 初始化数组元素          
		for (int i = 0; i < 10; i++)
		{
			n[i] = i + 100; // 设置元素 i 为 i + 100
		}
		cout << "Element" << setw(13) << "Value" << endl;
		// 输出数组中每个元素的值                     
		for (int j = 0; j < 10; j++)
		{
			// setw() 函数 来格式化输出
			cout << setw(7) << j << setw(13) << n[j] << endl;
		}

		return 0;
	}
// 字符
 char String(void)
	{
		string str1 = "runoob";
		string str2 = "google";
		string str3;
		int  len;

		// 复制 str1 到 str3
		str3 = str1;
		cout << "str3 : " << str3 << endl;

		// 连接 str1 和 str2
		str3 = str1 + str2;
		cout << "str1 + str2 : " << str3 << endl;

		// 连接后,str3 的总长度
		len = str3.size();
		cout << "str3.size() :  " << len << endl;
		return 0;
 }
//指针	
void Pointer()
{
	int  var = 20;   // 实际变量的声明
	int  *ip;        // 指针变量的声明
	ip = &var;       // 在指针变量中存储 var 的地址;不能ip=var;进行赋值,两个变量数据类型不一样。
	cout << "Value of var variable: "<<var << endl;
		// 输出在指针变量中存储的地址
	cout << "Address stored in ip variable: "<< ip << endl;
   // 访问指针中地址的值
	cout << "Value of *ip variable: "<< *ip << endl;;
}
// 时间
void  Get_Current_Date()
{
	struct tm tmnow;
	char dt[100];
	// 基于当前系统的当前日期/时间
	time_t now = time(0);
	// 把 now 转换为字符串形式
	ctime_s(dt, 100, &now);
	cout << "本地日期和时间:" << dt << endl;
	Sleep(1000);
}

3)结果演示部分:

猜你喜欢

转载自blog.csdn.net/weixin_39090239/article/details/115275031