C++编程学习阶段性总结

例1:输入3个整数,按由小到大的顺序输出。

#include <iostream>
using namespace std;

int main()
{
	void sort(int&, int&, int&);
	int a, b, c;
	cout << "请输入三个整数:";
	cin >> a >> b >> c;
	sort(a, b, c);
	cout << "输出结果为:" << a << " " << b << " " << c << endl;
	return 0;
}

void sort(int& a, int& b, int& c)
{
	void change(int&, int&);
	if (a > b) change(a, b);
	if (a > c) change(a, c);
	if (b > c) change(b, c);
}

void change(int& a, int& b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}

例2:输入10个整数,将其中最小的数与第一个数互换,把最大的数与最后一个互换。

#include <iostream>
using namespace std;

int a[10];
void cin_num();
void func_num();
void cout_num();

int main()
{
	cin_num();
	func_num();
	cout_num();
	return 0;
}

void cin_num()
{
	cout << "请输入10个数:";
	for (int i = 0; i < 10; i++)
		cin >> a[i];
}

void func_num()
{
	void change(int&, int&);
	int m = 0,n = 0;
	for (int i = 0; i < 10; i++)
		if (a[i] < a[m])
			m = i;
	change(a[0], a[m]);
	for (int i = 0; i < 10; i++)
		if (a[i] > a[n])
			n = i;
	change(a[9], a[n]);
}

void cout_num()
{
	cout << "输出结果为:";
	for (int i = 0; i < 10; i++)
		cout << a[i] << " ";
}

void change(int& a, int& b)
{
	int temp;
	temp = a;
	a = b;
	b = temp;
}

例3:有n个人围城一圈,顺序排号,从第一个人开始报数,从1-3报数,凡报到3的人退出圈子,问最后留下来的人原来排在第几号?

#include <iostream>
using namespace std;
const int NUM = 20;

int main()
{
	int a[NUM];
	int i = 0, j, m = 0, n = 0, k = 0;
	cout << "请输入不超过20人的总人数:";
	cin >> m;
	for (j = 0; j < m; j++)
		a[j] = j + 1;	//给每个人先取个位置编号
	while (n < m - 1)
	{
		if (a[i] != 0) k++;
		if (k == 3)
		{
			a[i] = 0;
			k = 0;
			n++;
		}
		i++;
		if (i == m) i = 0;
	}
	for (j = 0; j < m; j++)
		if (a[j] != 0)
			cout << " 最后一人的位置为:" << a[j] << endl;
	return 0;

例4:写一个函数,求字符串的长度,在main函数中输入字符串,并输出长度。

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

int main()
{
	char str[20];
	int cal_len(char *str);
	cout << "请输入一个字符串:";
	cin >> str;

	cout << "字符串长度为:" << cal_len(str) << endl;
	return 0;
}

int cal_len(char *str)
{
	int len = 0;
	char* ptr = str;
	while (*ptr != '\0')
	{
		ptr++;
		len++;
	}
	return len;
}

例5:输入一行文字,找出其中的大写字母、小写字母、空格、数字以及其它字符各有多少。

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

int main()
{
	void countCharacters(const string & str, int* upper, int* lower, int* num, int* space, int* other);
	string input;
	cout << "请输入一行字符:";
	getline(cin, input);
	int upper = 0, lower = 0, num = 0, space = 0, other = 0;
	countCharacters(input, &upper, &lower, &num, &space, &other);
	cout << "大写字母:" << upper << endl;
	cout << "小写字母:" << lower << endl;
	cout << "数字:" << num << endl;
	cout << "空格:" << space << endl;
	cout << "其它:" << other << endl;
	return 0;
}

void countCharacters(const string& str, int* upper, int* lower, int* num, int* space, int* other)
{
	for (const char& ch : str)
	{
		if (ch >= 'A' && ch <= 'Z')
			(*upper)++;
		else if (ch >= 'a' && ch <= 'z')
			(*lower)++;
		else if (ch >= '0' && ch <= '9')
			(*num)++;
		else if (ch == ' ')
			(*space)++;
		else
			(*other)++;
	}
}

结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

武当豆豆

为国家GDP快速增长做贡献

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值