C++ Exercise_3

Test_1

#include <iostream>
#include <cstring>
int main()
{
	using namespace std;
	
	//for (cin>>x; x==0; cin>>x)
	//----------------------------------//
	//for (char ch = 'a'; ch <= 'z'; ch++)
	//    cout<<ch;
	char word[5] = "?uck";  //we need 5 cause \0
	for (char ch = 'a'; strcmp(word,"fuck"); ch++) {
		cout << word << endl;
		word[0] = ch;
	}
	cout <<"After : "<< word << endl;

	return 0;
}

Test_2

#include <iostream>
#include <ctime>
int main()
{//当然我看不懂这个程序= =
	using namespace std;
	
	//while (name[i] != '\0') == while (name[i])
	cout << "Enter the delay time ,in seconds : ";
	float secs;
	cin >> secs;
	clock_t delay = secs * CLOCKS_PER_SEC;
	cout << "Starting\a\n";
	clock_t start = clock();
	while (clock() - start < delay)
		;
	cout << "done\a\n";

	return 0;
}
  1. for中可声明局部变量,while不阔以。
  2. 延时循环可还行。该程序以系统时间单位为单位(而不是秒),避免了在每轮循环中将系统时间转换为秒。

Test_3

#include <iostream>
#include <ctime>
int main()
{
	using namespace std;
	
	int number[5] = { 1,2,3,4,5 };
	for (int x : number) {
		cout << x << std::endl;
	}
	for (int& x : number) {
		x *= 2;
	}
	for (int x : number) {
		cout << x << std::endl;
	}
	for (int y : {5, 4, 3, 2, 1}) {
		cout << y << " ";
	}
	cout << '\n';
}
  1. 这个是目前我看不懂的操作,貌似是因为容器?基于范围的for循环。

Test_3

#include <iostream>

int main()
{
	using namespace std;
	
	char ch;
	int count = 0;
	cout<<"Enter your fucking number fuck!"<<endl;
	cin >> ch;
	while (ch != '#') {           //fuck you#hiahai
		cout << ch;
		count++;
		cin >> ch;                //fuckyou
	}
	cout << endl << count << " Has been read" << endl; //7 Has been read
}
  1. cin读取char和其他类型一样,会忽略空格和换行,所以空格没有被回显,也没有被计数!!!
  2. 发送给cin的输入被缓冲,amazing!!!因此可以在#后面继续输入。

Test_4

#include <iostream>

int main()
{
	using namespace std;
	
	char ch;
	int count = 0;
	cout<<"Enter your fucking number fuck!"<<endl;
	cin.get(ch);
	while (ch != '#') {           //fuck you the first
		cout << ch;               //fuck you the second#woc
		count++;
		cin.get(ch);              //fuck you the first
	}                             //fuck you the second
	cout << endl << count << " Has been read" << endl; //38 Has been read
}
  1. 通常我们使用 cin.gei(ch) 或者 cin.get(name,20);

Test_5

cin.get(ch);
	while (cin.fail()== false) { 
		
		cin.get(ch);             
	} 
  1. many操作系统支持重定向,允许用户用文件代替键盘输入。命令如下:file.exe<file.txt 其中<是重定向运算符。其次,很多系统允许通过键盘模拟文件尾条件很多PC编程环境将Ctrl + Z视为模拟的EOF
  2. cin.get(); cin.eof(); cin.fail(); cin.clear()://我没写出来 vs按了ctrl+z 不阔以???
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值