C++ Exercise_4

Test_1

#include <iostream>
//maybe we can use use 枚举
enum{white,black,yeelow};
int main()
{
	using namespace std;
	
	int code;
	cout << "Enter plaese: ";
	cin >> code;
	if (code == white)
		cout << "OMG" << endl;
	else if (code==black)
		cout << "Really?" << endl;
	else
		cout << "Ohhhhhhh" << endl;
}
  1. INT_MAX INT_MIN 两个常量。
  2. 字符函数库<cctype>中有:isalnum(); isalpha(); iscntrl(); isdigit(); isgraph(); islower(); isprint(); ispunct(); ispace(); issupper(); isxdigit(); tolower(); toupper();

Test_2

if (!cin) {
		cin.clear();
		cin.get();
	}
	cin.get();     //read end of line after last input
	cin.get();     //wait for user to press <Enter>
  1. 首先我并不知道这啥玩意儿= =

Test_3

#include <iostream>
const int MAX = 5;
int main()
{
	using namespace std;
	int number[MAX];
	
	cout << "Enter please: " << endl;
	for (int i = 0; i < MAX; i++) {
		cout << "The " << i << " : ";
		while (!(cin >> number[i])) {    //must number
			cin.clear();   //reset input
			while (cin.get() != '\n')
				continue;
			cout << "Must enter a number : ";
		}
	}
	double sum = 0;
	for (int i = 0; i < MAX; i++)
		sum += number[i];
	cout << "The average is : " << sum / MAX << endl;
}
  1. 对错误输入的处理

Test_4

#include <iostream>
#include <fstream>
int main()
{
	using namespace std;
	
	char name[20];

	ofstream hiahia;    //amzing character
	hiahia.open("fuckyou.txt");

	cout << "Enter your name : ";
	cin.get(name, 20);

	cout << fixed;   //what's the meaning of fixed
	cout.precision(2);  //精确的
	cout.setf(ios_base::showpoint);
	cout << "Your name : " << name << endl;

	hiahia << fixed; 
	hiahia.precision(2); 
	hiahia.setf(ios_base::showpoint);
	hiahia << "Your name : " << name << endl;

	hiahia.close();
	return 0;
}
  1. 如果文件原已经存在,则将其删除,如果你的命名和系统文件同名呢?哈哈哈,然后不小心把系统文件全部变成了.txt。人才啊。放在projext1里面。那个fixed && precision 啥意思??格式化方法....

Test_5

#include <iostream>
#include <fstream>
#include <cstdlib>   //for use exit();
int main()
{
	using namespace std;

	char filename[20];
	char name[20];
	ifstream hiahia;

	cout << "Enter filename : ";
	cin.get(filename, 20);
	hiahia.open(filename);
	if (!hiahia.is_open()) {
		cout << "Can't open this file!";
		exit(EXIT_FAILURE);
	}
	int count = 0;
	char ch;
	hiahia >> ch;
	while (hiahia.good()) {
		name[count] = ch;
		hiahia >> ch;
		count++;
	}
	if (hiahia.eof())
		cout << "End of reached\n";
	else if (hiahia.fail())
		cout << "Input terminated by data mismatch\n";
	else
		cout << "Input terminated for unknown reason\n";
	if (count == 0) {
		cout << "No data\n";
	}
	else {
		cout << "The number is : " << count << endl;
		cout << "The result is : ";
		for (int i = 0; i < count; i++) {
			cout << name[i];
		}
	}

	hiahia.close();
	return 0;
}
  1. 字符串输出??
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值