c++笔记(一)基本数据类型和表达式

开始做一些c++编程例题恢复一下自己的编程能力

记录一下积累的零散知识

1.取整函数

floor()是向负无穷大舍入,floor(-10.5) == -11;
ceil()是向正无穷大舍入,ceil(-10.5) == -10;

2.大小写转换

小写字母ascⅡ?比大写字母大32;
所以转换公式可以是char=char-0x20//char=char-‘a’+‘A’//(-32)

3.string类

1)为了在程序中使用string类型,必须包含头文件
注意这里不是string.h,string.h是C字符串头文件。
2)可以用 ==、>、<、>=、<=、和!=比较字符串,可以用+或者+=操作符连接两个字符串,并且可以用[]获取特定的字符。
3)特性函数
int capacity()const; //返回当前容量(即string中不必增加内存即可存放的元素个数)
int max_size()const; //返回string对象中可存放的最大字符串的长度
int size()const; //返回当前字符串的大小
int length()const; //返回当前字符串的长度
bool empty()const; //当前字符串是否为空
void resize(int len,char c); //把字符串当前大小置为len,多去少补,多出的字符c填充不足的部分
4)常用函数
string &insert(int p,const string &s); //在p位置插入字符串s
string &replace(int p, int n,const char *s); //删除从p开始的n个字符,然后在p处插入串s
string &erase(int p, int n); //删除p开始的n个字符,返回修改后的字符串
string substr(int pos = 0,int n = npos) const; //返回pos开始的n个字符组成的字符串
void swap(string &s2); //交换当前字符串与s2的值
string &append(const char *s); //把字符串s连接到当前字符串结尾
void push_back(char c) //当前字符串尾部加一个字符c

const char data()const; //返回一个非null终止的c字符数组,data():与c_str()类似, 用于string转const char其中它返回的数组是不以空字符终止,

const char c_str()const; //返回一个以null终止的c字符串,即c_str()函数返回一个指向正规C字符串的指针, 内容与本string串相同,用于string转const char

size_type find( const basic_string &str, size_type index ); //返回str在字符串中第一次出现的位置(从index开始查找),如果没找到则返回string::npos
size_type find( const char *str, size_type index ); // 同上
size_type find( const char *str, size_type index, size_type length ); //返回str在字符串中第一次出现的位置(从index开始查找,长度为length),如果没找到就返回string::npos
size_type find( char ch, size_type index ); // 返回字符ch在字符串中第一次出现的位置(从index开始查找),如果没找到就返回string::npos

4.setw()函数

此函数是用来 控制输出间隔的
作用于紧随其后的输出(例如cout<<a<<setw(3)<<b;表示b占3个位置不足用空格填充)
默认的就是空格,可以用setfill()配合使用设置其他字符填充

5.常用的操纵符

dec 按十进制显示整数 输入/ 输出
hex 按十六进制显示整数 输入/ 输出
oct 按八进制显示整数 输入/ 输出
ws 跳过前导空白 输入
endl 输出一个换行符并刷新流 输出
ends 输出空字符(‘\0’) 并刷新流 输出
setprecision(int p)
设置精度位数 p 输出
setw(int w) 设置字段宽度为 w 输出
注意: 如果使用 I/ O 流类库中某些特定操纵符的话, 应该在程序顶部增加预处理命
#include

6.<<相关

<<在c艹中出了表示输出流外
也有向左移位符号的意思,如a<<8表示左移8位
同理可得>>

7.文件读写

头文件#include
对文件的三种操作
ofstream 写操作
ifstream 读操作
fstream 读写操作

文件读写的步骤:
1、包含的头文件:#include
2、创建流
3、打开文件(文件和流关联)
4、读写 (写操作:<<,put( ), write( ) 读操作: >> , get( ),getline( ), read( ))
5、关闭文件:把缓冲区数据完整地写入文件, 添加文件结束标志, 切断流对象和外部文件的连接

写入文件:
1)用<<运算符写入
以行为单位写入
2)用put()
从内存中写入一个字节到文件
3)函数write( ):
功能:把buf指向的内容取n个字节写入文件
函数声明:ostream & ostream :: write ( char * buf , int n ) ;
参数说明:buf表示要写入内存的地址,传参时要取地址。n表示要读入字节的长度

读文件:
1)>>
总是以空格、Tab、回车结束,因而不能以行为单位读,而是以单词为单位。
2)getline()
以行为单位 读入内存,能一次读入一行
3)get()
从文件读取一个字节到内存
4)read( ):
功能:从文件中提取 n 个字节数据,写入buf指向的地方中
函数声明:istream & read ( char * buf , int n ) ;

附上练习的代码:

#include "stdafx.h"
#include<iostream>
#include<string>
#include<fstream>

using namespace std;


void t21()
{
	char c;
	cin >> c;
	c = c - 'a' + 'A';
	cout << c << endl;
	
}

void t22()
{
	int n;
	cin >> n;
	n = n % 100;
	n = n / 10;
	cout << n << endl;
	

}

void t23()
{
	float a, b, h, s,bs,roll;
	cout << "输入长度:";
	cin >> a;
	cout << "输入宽度:";
	cin >> b;
	cout << "输入高度:";
	cin >> h;
	cout << "输入壁纸面积";
	cin >> bs;

	s = (a + b) * 2 * h;
	roll = s / bs;
	roll = ceil(roll);
	cout <<"共需壁纸数:"<< roll << endl;

}
void t24()
{
	const int HFEE = 100;
	const int RFEE = 1800;
	string sname;
	int hour, total;
	cout << "输入名字:";
	cin >> sname;
	cout << "输入学时:";
	cin >> hour;
	total = hour * HFEE + RFEE;

	cout << sname << ":" << total << endl;

}
void t25()
{
	int year;
	cin >> year;
	if ((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
		cout << year << "年是闰年";
	else
		cout << year << "年不是闰年";
}
void t26()
{
	typedef unsigned long u_long;
	u_long b1, b2, b3, b4;
	u_long addr = 0;
	char ch;
	cin >> b4 >> ch >> b3 >> ch >> b2 >> ch >> b1;
	addr = addr | b4;
	addr = (addr << 8) | b3;
	addr = (addr << 8) | b2;
	addr = (addr << 8) | b1;
	cout << hex << addr << endl;
}
void t27()
{
	ofstream ofile;
	ofile.open("odata.txt");
	if (ofile.is_open())
	{
		cout << "请输入电文ctrl+z结束" << endl;
		char ch;
		while ((ch = cin.get()) != EOF)
		{
			if ((ch >= 'a') && (ch <= 'z'))
				ch = 'a' + (ch + 4 - 'a') % 26;
			else if ((ch >= 'A') && (ch <= 'Z'))
				ch = 'A' + (ch + 4 - 'A') % 26;
			ofile << ch;
		}
		ofile.close();
	}
	else {
		cout << "文件不能打开";
	}
}
void t28()
{
	ifstream ifile;
	ifile.open("odata.txt");
	if (ifile.is_open())
	{
		cout << "电文原码:" << endl;
		char ch;
		while ((ch = ifile.get()) != EOF)
		{
			if ((ch >= 'a') && (ch <= 'z'))
				ch = 'z' - ('z' - ch + 4) % 26;
			else if ((ch >= 'A') && (ch <= 'Z'))
				ch = 'Z' - ('Z' - ch + 4) % 26;
			cout << ch;
		}
		ifile.close();
	}
	else {
		cout << "文件不能打开";
	}
	system("pause");
}
void t29()
{
	int n;
	cin >> n;
	n = n | 0x0f;
	cout << n << endl;

}
int main()
{
	//t21();
	t29();
	system("pause");
	return 0;
	
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值