【捡起C++】分支语句和逻辑运算符

//if.cpp -----use the if statement
#include <iostream>
int main() {
	using std::cin;
	using std::cout;
	char ch;
	int spaces = 0;
	int totals = 0;
	cin.get(ch);
	while (ch != '.'){
		if (ch == ' ')
			++spaces;
		++totals;
		cin.get(ch);
	}
	cout << spaces << " spaces, " << totals;
	cout << " characters total in sentence\n";
	return 0;
}
//cctypes.cpp -- using the ctype.h
#include <iostream>
#include <cctype>
int main() {
	using namespace std;
	cout << "Enter text for analysis, and type @"
		<< " to  terminate input.\n";
	char ch;
	int whitespace = 0;
	int digits = 0;
	int chars = 0;
	//标点符号
	int punct = 0;
	int others = 0;
	
	cin.get(ch);
	while (ch != '@') {
		if (isalpha(ch)) {
			chars++;
		}
		else if (isspace(ch)) {
			whitespace++;
		}
		else if (isdigit(ch)){
			digits++;
		}
		else if (ispunct(ch)) {
			punct++;
		}
		else {
			others++;
		}
		cin.get(ch);
	}
	cout << chars << " letters, "
		<< whitespace << " whitespace, "
		<< digits << " digits, "
		<< punct << " punctuations, "
		<< others << " others.\n";
	return 0;
}
函数名称返回值
isalnum()如果参数是字母或数字,该函数返回true
isalpha()如果参数是字母,该函数返回true
iscntrl()如果参数是控制字符,该函数返回true
isdigit()如果参数是数字(0~9),该函数返回true
isgraph()如果参数是除空格之外的打印字符,该函数返回true
islower()如果参数是小写字母,该函数返回true
isprint()如果参数是打印字符(包括空格),该函数返回true
ispunct()如果参数是标点符号,该函数返回true
isspace()如果参数是标准空白字符,如空格、进纸、换行符、回车、水平制表符或者垂直制表符,该函数返回true
isupper()如果参数是大写字母,该函数返回true
isxdigit()如果参数是十六进制数字,即09、af或A~F,该函数返回true
tolower()如果参数是大写字符,则返回其小写,否则返回该参数
toupper()如果参数是小写字符,则返回其大写,否则返回该参数
/*
	当用户输入的不是数字时,该程序将不再读取输入,并要求继续输入数字。
*/
#include <iostream>
const int Max = 5;
int main() {
	using namespace std;
	int golf[Max];
	cout << "Please enter your golf scores.\n";
	cout << "You must enter " << Max << " rounds.\n";
	int i;
	for (i = 0; i < Max; i++) {
		cout << "round #" << i + 1 << ": ";	
		while (!(cin >> golf[i])) {
			cin.clear();           //清除错误状态
             //读取行尾之前的所有输入,从而删除这一行中的错误输入
			while (cin.get() != '\n'){
				continue;
			}
			cout << "Please enter a number: ";
		}
	}
	// calculate average
	double total = 0.0;
	for (i = 0; i < Max; i++) {
		total += golf[i];
	}
	//report results
	cout << total / Max << " = average score " << Max << " rounds\n";
	return 0;
}
简单文件输入/输出

​ 使用文件输出的主要步骤如下:

  1. 包含头文件fstream

  2. 创建一个ofstream对象

  3. 将该ofstream对象与一个文件关联起来

  4. 就像使用cout那样使用ofstream对象

//outfile.cpp --- writing to a file
#include <iostream>
#include <fstream>

int main() {
	using namespace std;
	char automobile[50];
	int year;
	double a_price, d_price;
	ofstream outFile;
	outFile.open("carinfo.txt");   //create object for output
	//如果该文件已存在,会丢弃原有的内容,然后将新的输出加入到该文件中
	cout << "Enter the make and model of automobile: ";
	cin.getline(automobile, 50);
	cout << "Enter the model year:";
	cin >> year;
	cout << "Enter the original asking price: ";
	cin >> a_price;
	d_price = 0.913 * a_price;


	//display information on screen with cout
	cout << fixed;
	cout.precision(2);
	cout.setf(ios_base::showpoint);
	cout << "Make and model: " << automobile << endl;
	cout << "Year: " << year << endl;
	cout << "Was asking $" << a_price << endl;
	cout << "Now asking $" << d_price << endl;

	//using outFile instead of cout;
	outFile << fixed;
	outFile.precision(2);
	outFile.setf(ios_base::showpoint);
	outFile << "Make and model: " << automobile << endl;
	outFile << "Year: " << year << endl;
	outFile << "Was asking $" << a_price << endl;
	outFile << "Now asking $" << d_price << endl;
	outFile.close();

	return 0;
}
//sumafile.cpp  -- functions with an array argument
#include <iostream>
#include <fstream>
#include <cstdlib>
const int SIZE = 60;
int main() {
	using namespace std;
	char filename[SIZE];
	ifstream inFile;          //object for handling file input
	cout << "Enter name of data file: ";
	cin.getline(filename, SIZE);
	inFile.open(filename);     //associate inFile with a file
	if (!inFile.is_open()) {   //failed to open file
		cout << "Could not open the file " << filename << endl;
		cout << "Program terminating.\n";
		exit(EXIT_FAILURE);
	}
	
	double value, sum = 0.0;
	int count = 0;                // number of items read
	inFile >> value;              //get first value
	while (inFile.good()){        // while input good and not at eof
		++count;		          // one more item read
		sum += value;             // calculate running total
		inFile >> value;          // get next value
	}
	if (inFile.eof()) {
		cout << "End of file reached.\n";
	}
	else if (inFile.fail()) {
		cout << "Input terminated by data mismatch.\n";
	}
	else {
		cout << "Input terminated for unknown reason.\n";
	}
	if (count == 0) {
		cout << "No data processed.\n";
	}
	else {
		cout << "Items read: " << count << endl;
		cout << "Sum: " << sum << endl;
		cout << "Average: " << sum / count << endl;
	}
	inFile.close();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值