C++学习笔记02

01

int main()
{
	double donation[10];
	int a;
	int i;
	int sum=0;
	double ave;
	int n = 0;
	for (i = 0; i <10; i++)
	{
		cin >> a;
		if (a!=0) 
			donation[i] = a;
		else
		{
			for (int j = 0; j < i; j++)
				sum = sum + donation[j];
			ave = sum / i;
			for (int j = 0; j < i; j++)
			{
				if (donation[j] > ave)
					n++;
				cout <<"数组: "<< donation[j]<<endl;
			}
			cout << "平均值" << ave << "有" << n << "个数大于平均值";
			break;
		}

		
	}
}

在这里插入图片描述
01,int a;cin>>a;
键入M,返回0,用此控制输入的格式.(输入数字时,cin先获取对应的ASCLL码,由于目标是int型,再将其转化为数字保存在内存单元a中。
02,int a;char b;cin>b;’;a=b
键入M,在内存单元b中保存的是M对应的ASCLL码,赋值号可将其赋值给a
小结:cin要先看给什么类型的数据服务,给int型服务时只接受数字输入,给char型服务时都接受。cin的力量是在服务类型正确时将输入转换成可以保存在内存的单元中的类型(ASCLL码)。

02

int main()
{
	//制表符是8个字符,当在一个制表符开始的位置(明白什么位置是制表符开始的位置)输出一个字符串时,
	//如果字符串不到8个字符,加一个"\t",会补充空格,达到一个制表符
	char res[] = "hello\tworld";
	cout << res << endl;//不满8个字符,会用空格填充达到8个字符

	cout << "hello___world" << endl;//中间3个下划线_

	char res2[] = "helloworld\thello";//开始第一个字符串10个字符,加一个制表符,会补充6个空格
	//完成第二个制表符
	cout << res2 << endl;
	cout << "helloworld______hello" << endl;//中间6个下划线_
	return 0;
}

在这里插入图片描述

03

int main()
{
	int vow=0;
	int con=0;
	int oth=0;
	cout << "Enter words (q to quit):";
	string ch;
	while ((cin >> ch) && (ch != "q"))
	{
		if (isalpha(ch[0]))
			if (ch[0] == 'a' && ch[0] == 'e' && ch[0] == 'i' && ch[0] == 'o' && ch[0] == 'u')
				vow++;
			else
				con++;
		else
			oth++;
	}
	cout << vow << " words beginning with vowels" << endl;
	cout << con << " words beginning with consonants" << endl;
	cout << oth << "others";
}
```![在这里插入图片描述](https://img-blog.csdnimg.cn/20210421153604434.png)
cin会忽略缓冲队列中的空白符(空格,回车,制表符等),输入完This is my apple并按回车时会将其输入到缓存队列,while中的两个条件都满足,因此一直在循环中还未打印,此时cin读取到了apple,读取到回车是会自动跳过,便无法进入循环(此时缓存队列中没有回车,当输入非空字符时将再进入循环进行计数。

## 04

```cpp
	for (int j = 0; j < i; j++)
	{   
		getline(ifile, pt[j].name);
		cout << "读取"<<j<<"号姓名:"<< pt[j].name<<endl;
		ifile >> pt[j].money;
		cout << "读取"<<j<<"号金额:"<< pt[j].money<<endl;
		ifile.get();
	}

在这里插入图片描述
读取姓名时,如用ifile>>,遇到空白符便会停止,Stone不会读取,故用getline读取一整行,读取到末尾的回车时,将其丢弃,故在读取金额时可以成功读取,用ifile读取金额时,遇到空白符就会停止,末尾的回车还在缓存队列中,要用ifile.get()将其跳过。

04

struct student {
	char fullname[SLEN];
	char hobby[SLEN];
	int ooplevel;
};
student* ptr_stu = new student[class_size];
int entered = getinfo(ptr_stu, class_size);
int getinfo(student *pa, int n)
//int getinfo(student pa[], int n)
{
	int i;
	for (i = 0; i < n; i++)
	{
		cout << "全名:";
		cin >> (*(pa+i)).fullname;
		cout << "爱好:";
		cin >> pa[i].hobby;
		cout << "等级:";
		cin >> pa[i].ooplevel;
	}
	return i;
}

动态结构数组传参时,形参列表可用pa或者pa[ ],二者等价,都代表动态结构数组的首地址,在访问其中元素时((pa+i)).fullname或者pa[i].hobby,二者皆可,一种是表示地址的pa视为指针,一种是将其视为数组。

05

double calculate(double, double, double(*)(double, double));
double add(double x, double y);
double sub(double x, double y);
int main()
{
	double a = calculate(1, 2,add);
	cout << a;
	double b = calculate(3, 1, sub);
	cout << b;
}
double calculate(double x, double y, double(*pt)(double a, double b))
{
	return (*pt)(x, y);
}
double add(double x, double y)
{
	return x + y;
}
double sub(double x, double y)
{
	return x - y;
}

声明时:double calculate(double, double, double(*)(double, double))即可,就算是函数指针也不用标识符。
调用参数为函数指针的函数时,实参为函数名,形参为double(*pt)(double, double)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值