小甲鱼C++快速入门——第二天

04视频

题目:

答案C版本(程序有问题)

#include <stdio.h>
#include <stdlib.h>

//argc程序的参数数量,包含自身
//argv[]的每隔指针指向命令行的一个字符串。
//argv[0]指向字符串“copyFile.exe”
//argv[1]指向字符串“sourceFile”
//argv[2]指向字符串“destFile”
int main( int argc, char* argv[] )
{
	FILE *in, *out;
	int ch;

	if( argc != 3)
	{
		fprintf( stderr, "输入的形式:copyFile 源文件名 目标文件名 \n");
		exit( EXIT_FAILURE);
	}

	if( (in = fopen(argv[1], "rb")) == NULL)//二进制可读形式打开
	{
		fprintf(stderr, "打不开文件:%s \n", argv[1]);
		exit( EXIT_FAILURE);
	}

	if( (out = fopen(argv[2], "wb")) == NULL)//二进制可写形式打开
	{
		fprintf(stderr, "打不开文件:%s \n", argv[2]);
		fclose(in);//关闭文件不可少
		exit( EXIT_FAILURE);
	}
	
	while( ( ch = getc(in) ) != EOF)//EOF=end of file(宏定义为-1)
	//getc()函数一次从输入流stdin读取一个字符,返回值为int;
	//putc()函数把这个字符写入到输出流stdout
	{
		if( putc(ch, out) == EOF)
			break;
	}
	if( ferror(in))
	{
		printf("读取文件 %s 失败! \n", argv[1]);
	}
	if( ferror(out))
	{
		printf("写入文件 %s 失败! \n", argv[2]);
	}
	
	printf("成功复制1个文件!\n");

	fclose(in);
	fclose(out);

	return 0;
}

C++版本—将文件中的数据输出到控制台

//运行程序之前需要在程序当前文件夹中建立一个文件“test.txt”,
//并写入数据【可以为字母,数字,中文】
#include <iostream.h>
#include <fstream.h>

int main()
{
	ifstream in;//文件输入流

	in.open("test.txt");
	if( !in )
	{
		cerr << "打开文件失败" <<endl;
		return 0;
	}

	char x;
	 while( in >> x)
	 {
		 cout << x;
	 }

	 cout << endl;
	 in.close();//关闭文件
	 return 0;
}

C++版本—通过控制台向文件中写入数据

#include <iostream.h>
#include <fstream.h>

int main()
{
	ofstream out;//文件写入类

	out.open("test.txt");
	if( !out )
	{
		cerr << "打开文件失败!" << endl;
		return 0;
	}

	for(int i = 0; i < 10; i++)
	{
		out << i;
	}
	out << endl;
	out.close();

	return 0;
}

常用文件打开模式

#include <iostream.h>
#include <fstream.h>

int main()
{
	fstream fp("test.txt", ios::in | ios::out);//以可读可写方式打开文件
	if( !fp )
	{
		cerr << "打开文件失败" << endl;
		return 0;
	}

	fp << "I love you very much !!!";
	static char str[10];

	fp.seekg(ios::beg);//使得文件指针指向文件头;ios::end是文件尾
	fp >> str;
	cout << str << endl;

	fp.close();

	return 0;
}

05视频

问题1

答案

#include <iostream.h>

int main()
{
	char answer;

	cout << "Are you sure???" 
			<< "请输入Y/y or N/n:";
	cin >> answer;

	switch( answer)
	{
	case 'Y':
	case 'y':
		cout << "是的!!!";
			break;
	case 'N':
	case 'n':
		cout << "不是!!!";
		break;
	default:
		cout << "你输入的结果符合要求!!!";
		break;
	}
	
	cin.ignore(100, '\n');//忽略100个字符,除非遇到回车
	cout << endl << "输入任何字符结束程序!" << endl;
	cin.get();//不加cin.ignore(100, '\n')时,此时cin.get()得到的是'\n'

	return 0;
}

问题2

答案

#include <iostream.h>

int main()
{
	const double N1 = 9.0 / 5.0;
	const unsigned short N2 = 32;
	//华氏温度 = 摄氏温度*9.0/5.0+32
	double tempIn, tempOut;
	char typeIn, typeOut;

	cout << "请输入一个温度【xx.xC或xx.xF】:";
	cin >> tempIn >> typeIn;

	cin.ignore (100, '\n');//忽略回车
	cout << endl;

	switch( typeIn)
	{
	case 'C':
	case 'c':
		typeOut = 'F';
		typeIn = 'C';
		tempOut = tempIn * N1 + N2;
		break;
	case 'F':
	case 'f':
		typeOut = 'C';
		typeIn = 'F';
		tempOut = (tempIn - N2) / N1;
		break;
	default:
		typeOut = 'E';
		break;
	}
	if( typeOut != 'E')
	{
		cout <<  tempIn << typeIn << "="
			<< tempOut << typeOut << endl;
	}
	else
		cout << "输入错误!!!" << endl;

	cout << "请输入任何字符结束" << endl;
	cin.get();//上面已经有了cin.ignore (100, '\n');

	return 0;
}

对输入数据进行合法性检查

06视频函数重载

函数重载不是一个真正的面向对象特征,只是可以简化编程工作的一种方案;

只能通过不同参数进行重载,但是不能通过不同的返回值重载;

重载和覆盖不同:https://blog.youkuaiyun.com/qiuchaoxi/article/details/79790736

1)  覆盖是子类和父类之间的关系,是垂直关系;重载是同一个类中方法之间的关系,是水平关系。

2)  覆盖只能由一对方法产生关系,重载是多个方法之间的关系。

3)  覆盖要求参数列表相同,重载要求参数列表不同。

4)  覆盖关系中,调用方法是根据对象的类型来决定;而重载关系是根据调用时的实参表与形参表来选择方法体的。

#include<iostream.h>
 
//不要忘记函数重载需要在前面,声明
void convertTemperature( double tempin, char typein);
void convertTemperature( int tempin, char typein);
 
int main()
{
	double tempin;
	char typein;
	int tempinint;
 
	cout<<"请以【xx.x C】或者【xx.x F】格式输入一个温度:";
	cin >> tempin >> typein;
	cin.ignore(100,'\n');
	cout<<"\n";
	convertTemperature( tempin,typein );
 
	cout<<"请以【xx C】或者【xx F】格式输入一个温度:";
	cin >> tempinint >> typein;
	cin.ignore(100,'\n');
	cout<<"\n";
	convertTemperature( tempinint,typein );

	return 0;
}
void convertTemperature( double tempin, char typein)
{
	//华氏温度=摄氏温度*9.0/5.0+32
	const unsigned short ADD_SUBTRACT = 32;
	const double RADIO = 9.0/5.0;
	double tempout;
	char typeout;
	switch( typein )
	{
	case 'c' :
	case 'C' :
		tempout=tempin * RADIO+ADD_SUBTRACT;
		typein = 'C';
		typeout = 'F';
		break;
	case 'F' :
	case 'f' :
		tempout = (tempin - ADD_SUBTRACT)/RADIO;
		typein = 'F';
		typeout = 'C';
		break;
	default:
		typeout='E';
	}
	if (typeout!='E')
	{
		cout<<tempin <<typein <<"="<<tempout<<typeout<<"\n\n";
	}
	else 
	{
		cout<<"输入格式错误!!!"<<"\n";
	}
	cout<<"请输入任意字符结束程序";
	cin.get();
}
void convertTemperature( int tempin, char typein)
{
	//华氏温度=摄氏温度*9.0/5.0+32
	const unsigned short ADD_SUBTRACT = 32;
	const double RADIO = 9.0/5.0;
	int tempout;
	char typeout;
	switch( typein )
	{
	case 'c' :
	case 'C' :
		tempout=tempin * RADIO+ADD_SUBTRACT;
		typein = 'C';
		typeout = 'F';
		break;
	case 'F' :
	case 'f' :
		tempout = (tempin - ADD_SUBTRACT)/RADIO;
		typein = 'F';
		typeout = 'C';
		break;
	default:
		typeout='E';
	}
	if (typeout!='E')
	{
		cout<<tempin <<typein <<"="<<tempout<<typeout<<"\n\n";
	}
	else 
	{
		cout<<"输入格式错误!!!"<<"\n";
	}

	cout<<"请输入任意字符结束程序";
	cin.get();
}

课后作业

#include <iostream.h>

double calu(double n1);
double calu(double n1, double n2);
double calu(double n1, double n2, double n3);

int main()
{
	double n1,n2, n3;
	double result1, result2, result3;

	cout << "请输入3个数字,中间以空格隔开";
	cin >> n1 >> n2 >> n3;

	result1 = calu(n1);
	result2 = calu(n1, n2);
	result3 = calu(n1, n2, n3);

	cout << "result1 = " << result1 <<endl
		<< "result2 = " << result2 <<endl
		<< "result3 = " << result3 <<endl;
	return 0;
}

double calu(double n1)
{
	double result;
	result = n1 * n1;
	return result;
}

double calu(double n1, double n2)
{
	double result;
	result = n1 * n2;
	return result;
}

double calu(double n1, double n2, double n3)
{
	double result;
	result = n1 + n2 + n3;
	return result;
}

07视频—复杂的数据类型

题目1数组

#include <iostream>
using namespace std;

const int n = 10;
int main()
{
	int a[10], sum;
	sum = 0;//临时变量,存储在栈中,务必初始化
	cout << "请输入" << n << "个数." << "\n\n";
	for (int i = 0; i < n; i++)
	{
		cout << "第" << i+1 << "个数为:";
		while (! (cin >> a[i]) )//对输入错误进行检验
		{
			cin.clear();//清空输入缓冲区
			cin.ignore(100,'\n');//忽略100个字符,除非遇到'\n'
			cout << "请输入一个合法的值:";
		}
		sum += a[i];
	}
	cout << "Sum is " << sum << " And average is " << (float) sum / 10 << "\n\n";
	system("pause");
	return 0;
}

题目2字符串string


#include <iostream>
using namespace std;
#include <string>

int main()
{
	string str;
	char ch[30];
	cout << "请输入一串字符:";

	//cin >> str;//cin遇到空格、Tab、回车自动结束当前输入

	/*getline(cin, str);//面向行输入,是string的类
	cout << str << endl;*/

	/*cin.getline(ch, 20);//读取第一行,并丢弃第一行换行符
	cout << ch << endl;
	cin.getline(ch, 20);//读取第二行,并丢弃第二行换行符
	cout << ch << endl;*/

	cin.get(ch, 20);//读取第一行,换行符仍保留在输入序列中
	cout << ch << endl;
	cin.get();//读取第一行输入的换行符
	cin.get(ch, 20);//读取第二行,换行符仍保留在输入序列中
	cout << ch << endl;

	return 0;
}

cin、cin.get()、cin.getline()和getline(cin, string str)

(1)cin:使用空格、制表符、换行符来确定字符串的结束位置,【可以和string型用】。

(2)cin.getline()是cin的类成员函数,读取一行输入,使用换行符来确定字符串的结束,并且丢弃换行符,即在输入序列中不存在换行符了【不可以和string型用】。

(3)cin.get()是cin的类成员函数,读取一行输入,使用换行符来确定字符串的结束,但是此时的换行符在输入序列中依然存在【不可以和string型用】。

(4)getline(cin, string str)面向行输入,是string的类

课后思考

PS:提取字符串:substr;搜索字符串find

#include <iostream>
using namespace std;
#include <string>

int main()
{
	string str = "I love you!";
/********************提取字符串********************/
	//函数substr可以提取string字符串中的子字符串,该函数有两个参数,
	//第一个参数为需要提取的子字符串的起始下标,第二个参数是需要提取的子字符串的长度。
	string str1;
	str1 = str.substr(3,6);
	cout << "str = " << str << endl;
	cout << "str1 = " << str1 << endl;//str1 = ove yo,string型和字符数组一样,也是从0开始索引
/********************比较字符串********************/
	//“==”、 “!=”、 “<=”、 “>=”、 “<”和“>”操作符都可以用于进行string类型字符串的比较,
	//这些操作符两边都可以是string字符串,也可以一边是string字符串另一边是字符串数组。
	if( str != str1 )
		cout<< " != " <<endl;
/********************方法1:添加字符串【string实现】********************/
	string str2, str3;
	str2 = "And you?";
	str3 = str + str2;
	cout << str3 << endl;
/********************方法2:添加字符串【数组实现】********************/
	char str4[50];
	strcpy(str4, "I love you!");//输入参数不可以是string型
	strcat(str4, "And you?");//输入参数不可以是string型
	cout << str4 << endl;
/********************搜索字符串********************/
	//详见:https://www.cnblogs.com/sword-/p/8030004.html
	int n;
	if( str.find("you", 0) != string::npos)
	{
		n = str.find("you", 0);
		cout << n << endl;//此时n = 7
	}
	
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值