C++第一部分基础语法(hello world,数据类型,程序流程结构)

这里是基础的语法知识,包括(hello world,数据类型,程序流程结构)

有些代码没有放进main函数里,大家记得放进去运行!

1.hello world的书写

#include<iostream>
using namespace std;

#define day 7
const int b = 10;


int main()
{
	int a = 10;
	day = day+ 1;//错误
	b = b + 1;//错误
	cout << "hello world" << endl << a<< endl;
	cout << "b=" << b  << endl;
	system("pause");
	return 0;


}

2.数据类型

#include<iostream>
using namespace std;
#include<string>
/************************************整形***********************************************/
//int main()
//{
//	//short num1 = 32767; //( - 32768  -- - 32767)
//
//	//int num2 = 10;
//
//	//long num3 = 10;
//
//	//long long num4 = 10;
//	//cout << "num1=" << num1 << endl;
//	//cout << "num2=" << num2 << endl;
//	//cout << "num3=" << num3 << endl;
//	//cout << "num4=" << num4 << endl;
//
//	sizeof
//	//cout << "int的内存大小=" << sizeof(int) << endl;
//
//	//cout << "long的内存大小=" << sizeof(long) << endl;
//	//保留六位有效数字
//	float f1 = 3.14f;//加了f表示单精度
//	cout << "f1=" << f1 << endl;
//	cout << "float的内存大小=" << sizeof(float) << endl;
//	/*科学计数法*/
//	float f2 = 3e2;
//	cout << "f2=" << f2 << endl;
//	/*char*/
//	char ch = 'a';//单字符
//	cout << "ch=" << ch << endl;
//	cout << "char的内存大小=" << sizeof(char) << endl;
//	cout << (int)ch << endl;
//
//	system("pause");
//	return 0;
//}

/***********************************************/
int main()
{
	//int num2 = 10;
	///*转义字符*/

	//cout << "hello world\n";
	//cout << "hello world" << endl;
	//cout << "\\" << endl;
	//cout << "\\\n";
	///*字符串*/
	//char str1[] = "hello world!!";
	//cout << str1[0] << endl;
	//cout << str1 << endl;
	///*c++类型*/
	//string str2 = "hello world!!";
	//cout << str2<< endl;

	///*bool类型*/
	//bool flag = true;
	//cout << flag << endl;
	//flag = !flag;
	//cout << flag << endl;
	//cout <<"bool所占"<< sizeof(bool)<<endl;


	///*数据输入*/
	//int a = 0;
	//cout << "请输入a的值" << endl;
	//cin >> a;
	//cout << "a=" << a << endl;

	//int a = 10;
	//int b = a++ * 10;
	//cout << a << endl;
	//cout << b<< endl;

	//cout <<( a<b) << endl;

	int a = 10;
	cout << !a << endl;
	cout << !!a << endl;
	cout << !!!a << endl;
	cout << !!!!a << endl;

	system("pause");
	return 0;
}

3.程序流程结构

3.1 选择

int score;
	cout << "please input score" << endl;
	cin >> score;
	if (score>=600)
	{
		
		cout << "一本" << endl;
		if (score >= 700)
		{
			cout << "北大" << endl;
		}
		else if (score < 700 && score >= 650)
		{
			cout << "清华" << endl;
		}
		else cout << "人大" << endl;

	}
	else if(score< 600 && score>=500)
	{
		cout << "二本" << endl;
	}
	else if (score < 500 && score >= 400)
	{
		cout << "三本" << endl;
	}
	else if (score <=400)
	{
		cout << "专科" << endl;
	}

3.2 三只小猪案例

int weight1;
	int weight2;
	int weight3;
	int max;
	cout << "please input weight of first pig" << endl;
	cin >> weight1;
	cout << "please input weight of second pig" << endl;
	cin >> weight2;
	cout << "please input weight of third pig" << endl;
	cin >> weight3;
	if (weight1 > weight2)
	{
		max = weight1;
	}
	else max = weight2;
	if (max > weight3)
	{
		cout << "最重的小猪是" << max << endl;
	}
	else cout << "最重的小猪是" << weight3 << endl;

	if (weight1 == weight2 == weight3)
	{
		cout << "三者一样重" <<  endl;
	}

3.3 switch

	int score = 0;
	cout << "input score" << endl;
	cin >> score;

	switch (score)
	{
	case 10:cout << "10分好电影" << endl; break;
	case 9:cout << "9分好电影" << endl; break;
	default:cout << "一般" << endl; break;

	}

3.4 while

	int a = 0;
	while (a<10)
	{
		cout << a << endl;
		a++;
	}

3.5 猜数字案例

srand((unsigned int)time(NULL));
	int a = rand() % 100 + 1;
	cout << a << endl;
	int val = 0;
	int b = 4;
	cout << "您还有" << b << "次机会" << endl;
	cin >> val;
	while (1)
	{
		if (a > val)
		{
			cout << "你输小了" << endl;
			cout << "还有" << b<<"次机会"<<endl;
			cin >> val;
			b--;
		}
		else if (a < val)
		{
			cout << "你输大了" << endl;
			cout << "还有" << b -1 << "次机会" << endl;
			cin >> val;
			b--;
		}
		else if (a = val)
		{
			cout << "您输对了" << endl; break;
		}
		if (b ==0 && a!=val )
		{
			cout << "输错了,您没有次数了" << endl;
			break;
		}

 	}

3.6 do while

   int a = 0;
   do
   {
	   cout << a << endl;
	   a++;
   } while (a<10 );

3.7 水仙花案例

int a = 100;
  while (a)
  {
	  int a1 = a / 100;//百位
	  int a2 = a % 100 / 10;//十位
	  int a3 = a % 100 % 10;//个位
	  if ((a1*a1*a1 + a2*a2*a2 + a3 *a3*a3) == a)
	  {
		  cout << a << endl;
	  }
	  a++;
	  if (a >= 1000)
	  {
		  break;
	  }
	 
  }


  do
  {
	   int a1 = a / 100;//百位
	   int a2 = a % 100 / 10;//十位
	   int a3 = a % 100 % 10;//个位
	    if ((a1*a1*a1 + a2*a2*a2 + a3 *a3*a3) == a)
	    {
	     cout << a << endl;
	    }
		a++;
  } while (a <1000);

3.8 for循环

 for (int i = 0;i<10; i++)
  {
	  cout << i << endl;
  }

3.9 敲桌子案例

int num = 0;
  for (int i = 1; i <=100; i++)
  {
	  if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
	  {
		  cout << "敲桌子" << endl;
		  num++;
	  }
	  else  cout << i << endl;
	  
  }
  cout << "敲了"<<num<<"次"<<endl;

3.10 嵌套循环 打印*图

 for (int i = 0; i < 10; i++)
  {
	  for (int j = 0; j < 10; j++)
	  {
		 
		  if (j < 9)
		  {
			  cout << "* ";
		  }
		  else
		  {
			  cout << "*" << endl;
		  }
	  } 
	  
  }

3.11 九九乘法表

  for (int i = 1; i < 10; i++)
  {
	  for (int j = 1; j <= i; j++)
	  {
		  cout << j << " * " << i << " = "  << j*i <<" ";
	  }
	  cout << endl;
  }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值