假设我们要更改的源代码如下:
头文件基本格式:
#ifndef _xx_h_
#define _xx_h_
//do something
#endif
第一步:首先将每个头文件写好,如下:
注意在源文件陪培使用头文件时要用“”而不能用<>,即:
#include "ff.h" 而不是 #include <ff.h>
第二步,将源代码写好后的运行结果:
这就可以了。程序能运行出结核乱历果了。
需要注意的一点:文件要在同一路径。如下图:
必须这样做!否则编译器将找不到头文件,会报错,会出现:
//我引用了一个未定义的头文件 ErrorTest.h:
即“No such file or directory” 就是没有文件或者路径。说明你的路径写错了,在编程时务必要注改搜意一下。
输入文件名叫 abc.txt 你可以改名。输出文件名叫 aaX.txt X=0,1,2,3....
单个文孝吵件大小 one_size, 你可以 更改大小。
#include<stdio.h>
main(){
FILE *fp, *fout
char nameout[80]
int i,j,k,n
long size, one_size=1024
char *buff
// one_size=one_size * 0.1 //你可以 更改大小
fp = fopen ("abc.txt","rb")
if (fp==NULL) { printf("Error opening file")return 0}
fseek (fp, 0, SEEK_END) // non-portable
size=ftell (fp)
printf ("Size of abc.txt: %d bytes.\n",size) //得到大小
rewind(fp)
n=size/巧答侍one_size
buff = (char*)malloc(sizeof(char)*one_size)
for (i=0i<ni++)
{
sprintf(nameout,"aa%d.txt",i)
fout = fopen(nameout,"wb")
k = fread (buff,1,one_size,fp)
//举闷if (k != one_size) {printf("read err\n")return (0)}
fwrite(buff,1,one_size,fout)
fclose(fout)
printf("output in %s\n",nameout)
} // for i
j = size % one_size
if (j!=0) {
sprintf(nameout,"aa%d.txt",n)
fout = fopen(nameout,"wb")
k = fread (buff,1,j,fp)
// if (k != j) {printf("read err\n")return (0)}
fwrite(buff,1,k,fout)
fclose(fout)
printf("output in %s\n",nameout)
}
fclose (fp)
return 0
}
把一个源程序分成三个文件,一般情况包含《文件名》.h头文件用于实现函数的袜指声明,函数文件<文件名>.cpp,主函数文件头袭前文件和函数文件用#include“文件名.h(.cpp)开头声明!下边例子头文件head.h是类定义头文件,head.cpp是类实现文拍好清件4-4.cpp是主函数文件
//head.h
class Point{
public:
Point(int xx=0,int yy=0){
x=xx
y=yy
}
Point(Point &p)
int getX(){return x}
int getY(){return y}
private:
int x,y
}class Line{
public:
Line(Point xp1,Point xp2)
Line(Line &l)
double getLen(){return len}
private:
Point p1,p2
double len
}
//head.app
#include"head.h"
#include<iostream>
#include<cmath>
using namespace std
Point::Point(Point &p){
x=p.x
y=p.y
cout<<"Calling the copy constructor of Point"<<endl
}
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){
cout<<"Calling consturctor of Line"<<endl
double x=static_cast<double>(p1.getX()-p2.getX())
double y=static_cast<double>(p1.getY()-p2.getY())
len=sqrt(x*x+y*y)
}
Line::Line (Line &l):p1(l.p1),p2(l.p2){
cout<<"Calling the copy constructor of Line"<<endl
len=l.len
}
//4_4.app
#include "head.h"
#include <iostream>
using namespace std
int main(){
Point myp1(1,1),myp2(4,5)
Line line(myp1,myp2)
Line line2(line)
cout<<"The length of the line2 is:"
cout<<line.getLen()<<endl
cout<<"The length of the line2 is:"
cout<<line2.getLen()<<endl
return 0
}
综合到一块如下:
#include <iostream>
using namespace std
class Point{
public:
Point(int xx=0,int yy=0){
x=xx
y=yy
}
Point(Point &p)
int getX(){return x}
int getY(){return y}
private:
int x,y
}class Line{
public:
Line(Point xp1,Point xp2)
Line(Line &l)
double getLen(){return len}
private:
Point p1,p2
double len
}using namespace std
Point::Point(Point &p){
x=p.x
y=p.y
cout<<"Calling the copy constructor of Point"<<endl
}
Line::Line(Point xp1,Point xp2):p1(xp1),p2(xp2){
cout<<"Calling consturctor of Line"<<endl
double x=static_cast<double>(p1.getX()-p2.getX())
double y=static_cast<double>(p1.getY()-p2.getY())
len=sqrt(x*x+y*y)
}
Line::Line (Line &l):p1(l.p1),p2(l.p2){
cout<<"Calling the copy constructor of Line"<<endl
len=l.len
}
int main(){
Point myp1(1,1),myp2(4,5)
Line line(myp1,myp2)
Line line2(line)
cout<<"The length of the line2 is:"
cout<<line.getLen()<<endl
cout<<"The length of the line2 is:"
cout<<line2.getLen()<<endl
return 0
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)