C++案例 利用基本语法实现程序

目录

1、嵌套if语句

三只小猪称体重

2、while循环

猜数字

 3、do...while循环语句

水仙花

4、for循环语句

敲桌子

5、for嵌套循环

乘法口诀表

6、一维数组

五只小猪称体重

数组元素逆置

7、二维数组

考试成绩统计

8、函数分文件编写


1、嵌套if语句

三只小猪称体重

    介绍:输入三只小猪体重,判断那只最重

#include<iostream>
using namespace std;

int main()
{
	//1、创建三只小猪的体重变量
	int num1 = 0;
	int num2 = 0;
	int num3 = 0;

	//2、让用户输入三只小猪的重量
	cout << "请输入小猪A的体重:" << endl;
	cin >> num1;

	cout << "请输入小猪B的体重:" << endl;
	cin >> num2;

	cout << "请输入小猪C的体重:" << endl;
	cin >> num3;
    
    //打印查看体重
	cout << "小猪A的体重:" << num1<<endl;
	cout << "小猪B的体重:" << num2 << endl;
	cout << "小猪C的体重:" << num3 << endl;

	//3、判断哪只最重
	//先判断A和B的体重
	if (num1 > num2)//A比B重
	{
		if (num1 > num3)//A比C重
		{
			cout << "小猪A最重" << endl;
		}
		else
		{
			cout << "小猪C最重" << endl;
		}
	}
	else
	{
		if (num2 > num3)//B比C重
		{
			cout << "小猪B最重" << endl;
		}
		else
		{
			cout << "小猪C最重" << endl;
		}
	}
	system("pause");
	return 0;
}

 

2、while循环

猜数字

介绍:系统随机生成一个数字,玩家输入的数字,系统来判断是否输入正确

# include<iostream>
# include<ctime>
using namespace std;

int main()
{
	//添加随机数种子,作用:利用当前系统时间生成随机数,防治每次随机数都一样
	srand((unsigned int)time(NULL));

	//1、系统生成随机数
	int num = rand() % 100 + 1; //rand() % 100 + 1 生成0+1~99+1的随机数
	//cout << num << endl;

	//2、玩家进行猜测
	int val = 0;//玩家输入的数据

	while (1)
	{
        cout << "请输入猜测的数字:" << endl;
		cin >> val;

		//3、判断玩家的猜测

		//猜错 提示猜的结果 过大或者过小 重新返回第二步
		if (val > num)
		{
			cout << "猜测过大" << endl;
		}
		else if (val < num)
		{
			cout << "猜测过小" << endl;
		}
		//猜对  退出游戏
		else
		{
			cout << "恭喜猜对了" << endl;
			break;//break.可以用该关键字退出循环
		}
	}

	system("pause");
	return 0;
}

 

进阶:限制玩家猜测的次数

# include<iostream>
# include<ctime>
using namespace std;

int main()
{
	//添加随机数种子,作用:利用当前系统时间生成随机数,防治每次随机数都一样
	srand((unsigned int)time(NULL));

	//1、系统生成随机数
	int num = rand() % 100 + 1; //rand() % 100 + 1 生成0+1~99+1的随机数
	//cout << num << endl;

	//2、玩家进行猜测
	int val = 0;//玩家输入的数据
	int count = 0; //玩家输入的次数

	while (1)
	{
		cout << "请输入猜测的数字:" << endl;
		cin >> val;
		count++;
	

		//3、判断玩家的猜测

		//猜错 提示猜的结果 过大或者过小 重新返回第二步
		if (val > num)
		{
			cout << "猜测过大" << endl;
		}
		else if (val < num)
		{
			cout << "猜测过小" << endl;
		}
		//猜对  退出游戏
		else
		{
			cout << "恭喜猜对了" << endl;
			break;//break.可以用该关键字退出循环
		}

		//允许猜测的次数为6次
		if (count > 5)
		{
			cout << "超过猜测次数,退出游戏" << endl;
			break;
		}
	}

	system("pause");
	return 0;
}

 

 3、do...while循环语句

水仙花

介绍:水仙花数是指一个3位数,它的每个位上的数字的3次幂之和等于它本身,例如:1^3+5^3+3^3=153

#include<iostream>
using namespace std;

int main()
{

	//1、打印所有三位数字
	int num = 100;
	do 
	{
		//2、从所有三位数总找到水仙花数
		int a = 0;//个位
		int b = 0;//十位
		int c = 0;//百位
		
		a = num % 10;
		b = num / 10 % 10;
		c = num / 100;

		if (a*a*a + b*b*b + c*c*c == num)//如果是水仙花数就打印
		{
			cout << num << endl;
		}
		num++;
	} while (num < 1000);

	system("pause");
	return 0;
}

 

4、for循环语句

敲桌子

介绍:从1开始到数字100,如果数字中个位含有7或者数字十位含有7,或者该数字是7的倍数,我们打印敲桌子,其余数字直接打印输出

#include<iostream>
using namespace std;

int main()
{
	//1、先输出1~100数字
	for (int i = 1; i <= 100; i++)
	{
		//2、从100个数字中找到特殊数字,打印”敲桌子“
		//如果是7的倍数。个位有7,十位有7打印敲桌子
		if (i % 7 == 0 || i %10 == 7 || i /10==7)   //特殊数字
		{
			cout << "敲桌子" << endl;
		}
		else
		{
			cout << i << endl;
		}
	}

	system("pause");
	return 0;
}

5、for嵌套循环

乘法口诀表

介绍:打印出乘法口诀表

#include<iostream>
using namespace std;

int main()
{
	//行
	for (int i = 1; i <= 9; i++)
	{	
		//列
		for (int j = 1; j <= i; j++)
		{
			cout << j <<" * "<<i<<" = "<<j*i<<"  ";
		}
		cout << endl;
	}
	system("pause");
	return 0;
}

6、一维数组

五只小猪称体重

介绍:在一组数组中记录五只小猪体重,如:int arr[5] = {300,350,200,400,250};找到并打印最重的小猪体重

#include<iostream>
using namespace std;

int main()
{
	
	//1、创建5只小猪体重的数组
	int arr[5] = { 300,350,200,400,250 };

	//2、从数组中找到最大值
	int max = 0;//先认定一个最大值
	for (int i = 0; i < 5; i++)
	{
		//cout << arr[i] << endl;
		//如果访问的数组中的元素比我认定的最大值还要大,更新最大值
		if (arr[i] > max)
		{
			max = arr[i];
		}
	}

	//3、打印最大值
	cout << "最重小猪的体重为:" << max << endl;
	system("pause");
	return 0;
}

 

数组元素逆置

介绍:先声明一个5个元素,并且将元素逆置,(如原数组为:1,2,3,5,4;逆置后的数组为:4,5,3,2,1)

#include<iostream>
using namespace std;

int main()
{
	//1、创建一个数组
	int arr[5] = { 1,3,2,5,4 };
	cout << "逆置前的数组:" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i]<<",";
	}
	cout << endl;

	//2、实现逆置
	//2.1记录起始下表的位置
	int start = 0;

	//2.2记录结束下标的位置
	int end = sizeof(arr)/sizeof(arr[0]) - 1;

	//2.5循环执行2.1操作,知道气质位置 >= 结束位置
	while (start < end)
	{
		//2.3起始下标与结束下标的元素互换
		int temp = arr[start];//临时存放数组数据
		arr[start] = arr[end];
		arr[end] = temp;

		//2.4起始位置++,结束位置--
		start++;
		end--;
	}

	//3、打印逆置后的数组
	cout << "逆置后的数组:" << endl;
	for (int i = 0; i < 5; i++)
	{
		cout << arr[i] << ",";
	}
	cout << endl;
	system("pause");
	return 0;
}

7、二维数组

考试成绩统计

介绍:有三名同学(张三,李四,王五),在一次考试中的成绩分别如下表,请分别输出三名同学的总成绩
                    语文            数学            英语
    张三         100             100              100
    李四          90               50               100
    王五          60               70                80

#include<iostream>
using namespace std;
#include<string>

int main()
{
	//1、创建二维数组
	int scores[3][3] = {
		{100,100,100},
		{90,50,100},
		{60,70,80}
	};
	string names[3] = {"张三","李四","王五"};

	//2、统计考试成绩
	for (int i = 0; i < 3; i++)
	{
		int sum = 0;//统计分数总和变量
		for (int j = 0; j < 3; j++)
		{
			sum += scores[i][j];
		}
		cout << names[i]<<"的总分数为:"<<sum<<endl;
	}

	system("pause");
	return 0;
}

 

8、函数分文件编写

函数分文件编写—般有4个步骤:

1、创建后缀名为.h的头文件

2、创建后缀名为.cpp的源文件

3、在头文件中写函数的声明

4、在源文件中写函数的定义

 

 swsp.h

#include<iostream>
using namespace std;

//函数声明
void swap(int a, int b);

 swap.cpp

#include"swap.h" //" ":表示自定义的头文件

//函数定义
void swap(int a, int b)
{
	int temp = a;
	a = b;
	b = temp;

	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
}

 主文件

#include<iostream>
using namespace std;
#include"swap.h"

int main()
{
	int a = 10;
	int b = 20;

	swap(a, b);

	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_48764574/article/details/123128258