C++ primer plus chapter 5 (循环与关系表达式)

这篇文章展示了C++编程的基础知识,包括使用for和while循环,变量的作用域,表达式的值,以及运算符如++的前后置用法。同时,还涵盖了字符串操作和数组,特别是二维数组的处理。此外,提到了运算符重载和函数的延迟执行概念。
摘要由CSDN通过智能技术生成
#include <iostream>

int main(void)
{
	using namespace std;

	int i;

	for(i = 0; i < 5; i++)
		cout << "C++ knows loop" << endl;
	
	cout << "C++ knows when to stop." << endl;

	return 0;
}

 

 

#include <iostream>

int main(void)
{
	using namespace std;

	cout << "Enter the starting countdown value: ";
	int limit;

	cin >> limit;

	int i;

	for(i = limit; i; i--)
		cout << "i = " << i << endl;

	cout << "Done, now that i = " << i << endl;

	return 0;
}

 表达式的值为等号左侧的值

#include <iostream>

int main(void)
{
	using namespace std;

	int x;

	cout << "The expression x = 100 has the value ";
	cout << (x = 100) << endl;
	cout << "Now x = " << x << endl;
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << endl;
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << endl;

	cout.setf(ios_base::boolalpha);
	cout << "The expression x < 3 has the value ";
	cout << (x < 3) << endl;
	cout << "The expression x > 3 has the value ";
	cout << (x > 3) << endl;

	return 0;
}

#include <iostream>

int main(void)
{
	using namespace std;

	int i = 100;

	for(int i = 0; i < 5; i++)
		cout << "C++ knows loop" << endl;
	
	cout << "i = " << i << endl;
	cout << "C++ knows when to stop." << endl;

	return 0;
}

 计算阶乘

#include <iostream>

const int ArSize = 16;

int main(void)
{
	long long factorials[ArSize];
	
	factorials[0] = factorials[1] = 1;

	for(int i = 2; i < ArSize; i++)
		factorials[i] = i * factorials[i-1];

	for(int i = 0; i < ArSize; i++)
		std::cout << i << "! = " << factorials[i] << std::endl;

	return 0;
}

修改步长 声明命名空间的另一种方式

#include <iostream>

int main(void)
{
	using std::cout;
	using std::cin;
	using std::endl;

	cout << "Enter an integer: ";
	int by;
	cin >> by;

	cout << "Counting by " << by << endl;

	for(int i = 0; i < 100; i = i + by)
		cout << i << endl;
}

使用for循环访问字符串

#include <iostream>

int main(void)
{
	using namespace std;

	cout << "Enter a word:";
	string word;
	cin >> word;

	for(int i = word.size() - 1; i >= 0; i--)
		cout << word[i];

	cout << endl;

	return 0;
}

++i与i++

#include <iostream>

int main(void)
{
	using namespace std;

	int a = 20;
	int b = 20;

	cout << "a   = " << a << ", " << "b   = " << b << endl;
	cout << "a++ = " << a++ << ", " << "++b = " << ++b << endl;
	cout << "a   = " << a << ", " << "b   = " << b << endl;

	return 0;
}

使用运算符重载时前置和后置的区别

指针与递增递减运算符

//1)*++pt  2)++*pt   3)(*pt)++  4)*pt++

#include <iostream>

int main(void)
{
	using namespace std;

	double arr[5] = {21.1, 32.8, 23.4, 45.2, 37.4};
	double *pt = arr;

//	cout << *pt << endl;

	cout << "*++pt = " << *++pt << endl; //运算符同级且都在左侧 从右至左执行 指针先跨过一个元素再取值 32.8
	cout << "++*pt = " << ++*pt << endl;//从右向左执行 先取值再前置自增33.8
	cout << "(*pt)++ = " << (*pt)++ << endl;//括号优先级最高先取出 33.8 再修改为34.8
	cout << "*pt = " << *pt << endl;//34.8
	cout << "*pt++ = " << *pt++ << endl;//优先级相同 后缀优先级高 但是后缀++是先用后增 所以*先执行 但是是后++ 所以先用再自增  34.8 在将pt 后指一位
//因为后缀++优先级高于*所以是++在pt上而不是(*pt)上,这里并不是看起来优先级小了,是因为后置运算符的先用后加性质,如果是优先级小的话,应该是先取值,对取的值+1,但是不是的,输出来的是指针所指地址+1
	cout << "*pt = " << *pt << endl; //23.8

	return 0;
}

复合语句

#include <iostream>

int main(void)
{
	using namespace std;

	double number, sum = 0.0;

	cout << "Calculate five numbers sum and average." << endl;
	cout << "Please enter five values:" << endl;
		
	for(int i = 1; i <= 5; i++)
	{
		cout << "Value " << i << ": " ;
		cin >> number;
		sum += number;
	}

	cout << "The sum = " << sum << endl;
	cout << "Average = " << sum / 5 << endl;

	return 0;
}

语句块与变量作用范围

如果在语句块中定义一个新的变量,则仅当程序执行该语句块中的语句时,该变量才存在,执行完该语句块后,变量将被释放。

 逗号运算符允许将两个表达式放到C++句法中只允许放一个表达式的地方。

 逗号有时候是分隔符。

#include <iostream>
#include <string>

int main(void)
{
	using namespace std;

	cout << "Please enter a word:";
	string word;
	cin >> word;

	int i, j;
	char temp;
	for(j = 0, i = word.size()-1; j < i; j++, i--)
	{
		temp = word[i];
		word[i] = word[j];
		word[j] = temp;
	}

	cout << word << endl;

	return 0;
}

逗号表达式从左至右执行,其值是第二部分的值。

 关系表达式

#include <iostream>

int main(void)
{
	using namespace std;

	int arr[10] = {20, 20, 20, 20, 20, 19, 20, 18, 20, 20};

	cout << "Doing it right:" << endl;
	int i;
	for(i = 0; arr[i] == 20; i++)
		cout << "arr " << i << " is a 20." << endl;

	cout << "Doing it dangerously wrong: " << endl;
	for(i = 0; arr[i] = 20; i++)
		cout << "arr " << i << " is a 20." << endl;//使用数组时编译器不会去检查访问是否越界

	return 0;
}

C风格字符串的比较

 

#include <iostream>
#include <cstring>

int main(void)
{
	using namespace std;

	char word[5] = "?ate";

	for(char ch = 'a'; strcmp(word, "mate"); ch++)
	{
		cout << word << endl;
		word[0] = ch;
	}

	cout << "After loop ends, word is " << word << endl;

	return 0;
}

string类字符串的比较——运算符重载

#include <iostream>
#include <string>

int main(void)
{
	using namespace std;

	string word = "?ate";

	for(char ch = 'a'; word != "mate"; ch++)
	{
		cout << word << endl;
		word[0] = ch;
	}

	cout << "After loop ends, word is " << word << endl;

	return 0;
}

while循环——当根据循环条件而不是循环次数来确定是否执行时,while更合适

#include <iostream>

const int ArSize = 20;

int main(void)
{
	using namespace std;

	char name[ArSize];

	cout << "Your first namem please: ";
	cin >> name;

	cout << "Here is your name: " << endl;

	int i = 0;

	while(name[i] != '\0')  //while(name[i])
	{
		cout << name[i] << ": " << (int)name[i] << endl;
		i++;
	}

	return 0;
}

 

 

 while编写延时函数

#include <iostream>
#include <ctime>

int main(void)
{
	using namespace std;

	cout << "Enter the delay time, in seconds:";

	float secs;
	cin >> secs;
	clock_t delay = secs * CLOCKS_PER_SEC;

	clock_t start = clock();

	while(clock() - start < delay)
		;
	
	cout << "done!\n";

	return 0;
}

 

 do while循环——while后面有分号

#include <iostream>

int main(void)
{
	using namespace std;
	int n;

	cout << "Enter numbers in the range 1~10 to find my favorite number" << endl;
/*
	cin >> n;

	while(n != 7)
	{
		cin >> n;
	} 
*/
	do
	{
		cin >> n;
	}while(n != 7);
	
	cout << "Yes, 7 is my favorite." << endl;

	return 0;
}

嵌套循环与二维数组

#include <iostream>

const int Cities = 5;
const int Years = 4;

int main(void)
{
	using namespace std;

	const char * cities[Cities] = //字符串数组 字符指针数组
	{
		"Gribble City",
		"Gribbletown",
		"New Gribble",
		"San Gribble",
		"Gribble Vista"
	};

	int maxtemps[Years][Cities] = 
	{
		{96, 100, 87, 101, 105},
		{96, 98, 91, 107, 104},
		{97, 101, 93, 108, 107},
		{98, 103, 95, 109, 108}
	};

	cout << "City: Maximum temperature for 2008 - 2011" << endl;
	for(int city = 0; city < Cities; city++)
	{
		cout << cities[city] << ":\t";
		for(int year = 0; year < Years; year++)
			cout << maxtemps[year][city] << "\t";

		cout << endl;
	}

	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值