简单读写文件 |
c++ |
|
/*
* t4.cpp
*
* Created on: 2012-6-11
* Author: Simoun
*/
#include<iostream.h>
#include<process.h>
int main(){
FILE *fp1;
char str[80];
cout<<"Input a string";
cin.getline(str,80);
//已写的方式打开文件
if((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file"<<endl;
cout<<"Exiting program"<<endl;
exit(1);
}
fputs(str,fp1);
fputs("\n",fp1);
fclose(fp1);
//以读得方式打开文件
if((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file"<<endl;
cout<<"Exiting program"<<endl;
exit(1);
}
/*//循环从“流”文件读取字符,并显示
char ch;
while((ch=fgetc(fp1))!=EOF)
cout<<ch;
cout<<endl;
fclose(fp1);*/
//以下按倒序方式读取文件中的字符,并显示
int i;
char ch;
for(i=-1;;i--)
{
fseek(fp1,i,SEEK_END);//设置文件指针,定格了指针,本来读取一个指针会向下移的,偏移量为I,相对文件尾
if((ch=fgetc(fp1))!=EOF)
cout<<ch;
else
break;
}
//相对于前面的文字为负数,后面的为正数
cout<<endl<<"------------"<<endl;
//以下读取"流"文件中偶数位置上的字符,并打印
long position;
for(i=0;;i=i+2)
{
fseek(fp1,i,SEEK_SET);//相对于文件头
position=ftell(fp1);
if((ch=fgetc(fp1))==EOF)break;//遇到文件尾,则退出
else{
cout<<position<<":"<<ch<<endl;
}
}
cout<<endl;
fclose(fp1);//关闭文件
}
|
引用 |
c++ |
|
#include<iostream.h>
struct student{
char name[10];
float grade;
};
void show(student x);
void swap(student &x,student &y){
student temp;
temp=x;
x=y;
y=temp;
}
student & max(student &x,student &y){
return(x.grade>y.grade?x:y);
}
int main()
{
student a={"Zhanghua",315.5},b={"WangJun",385};
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"-------------------"<<endl;
swap(a,b);
cout<<"a:";
show(a);
show(b);
cout<<"------------"<<endl;
student &t=max(a,b); //如果是student t=max(a,b);将采用的是copy方法,
cout<<"Max:";
show(t);
// char a[10]={'h','e','l','l','o'}; //sizeof(a)为10
t.name[0]='a';
show(a);
show(b);
}
void show(student x){
cout<<x.name<<endl;
}
/*
a:Zhanghua
b:WangJun
-------------------
a:WangJun
Zhanghua
------------
Max:WangJun
aangJun
Zhanghua
*/
|
基本类型强制转换 |
c++ |
|
/*
* emuntest.cpp
*
* Created on: 2012-6-10
* Author: Administrator
*/
#include<iostream.h>
int main()
{
//表达式类型转换
int n=100,m;
double x=3.791,y;
cout<<"n*x="<<n*x<<endl;
//赋值类型转换
m=x;
y=n;
cout<<"m="<<m<<endl;
cout<<"y="<<y<<endl;
//强制类型转换
cout<<"int(x)="<<int(x)<<endl;
cout<<"(int)x="<<(int)x<<endl;
cout<<"int(1.732+x=)"<<int(1.732+x)<<endl; //四舍五入
cout<<"int(1.732)+x="<<int(1.732)+x<<endl;
cout<<"double(100)="<<double(100)<<endl;
}
|
枚举 |
c++ |
|
/*
* emuntest.cpp
*
* Created on: 2012-6-10
* Author: Administrator
*/
#include<iostream.h>
int main()
{
//枚举定义
enum color{
RED=3,
YELLOW=6,
BLUE=9
};
//声明并赋值
enum color a=RED;
color b;
//输出枚举变量
cout<<"RED="<<RED<<endl;
cout<<"YELLOW="<<YELLOW<<endl;
//枚举赋值
b=a;
a=BLUE;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
// a=100; 错误 ,枚举虽然是本质是数字,但不能这样写
//枚举变量的关系运算
b=BLUE;
cout<<"a<b="<<(a<b)<<endl;
}
|
冒泡,二分法 |
c++ |
|
#include<iostream>
using namespace std;
#define size 5
int main(){
int i,j;
float t,a[size];
for(i=0;i<size;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
//对数组大小进行排序
for(i=0;i<size-1;i++){
for(j=i+1;j<size;j++)
{
if(a[i]>a[j])
{
float temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
//显示排序结果
for(i=0;i<size;i++){
cout<<a[i]<<" ";
}
//输入要查找的数据
int value;
int found; //找到为1,否则为0
int low,high,mid;
cout<<endl;
cout<<"input the value where u want found";
cin>>value;
//二分法查找数组a
found=0;
low=0;
high=size-1;
while(low<=high)
{
mid=(high+low)/2;
if(a[mid]==value){
found=1;
break;
}
if(a[mid]<value){
low=mid+1;
} else high=mid-1;
}
if(found)
cout<<"The value found at:a["<<mid<<"]"<<endl;
else
cout<<"is not found"<<endl;
}
|
copy数组 |
c++ |
|
#include "iostream.h"
void strcopy(char *string1,char *string2);
int main()
{
char str1[]={"Tomato Studio"};
char *str2;
int size=sizeof(str1)/sizeof(int);
str2=new char[size] ;
cout <<"STR1的内容是" <<str1 <<endl;
strcopy(str1,str2);
cout <<"String Copied..." <<endl;
cout <<"STR2的内容是" <<str2 <<endl;
;
return 0;
}
void strcopy(char *string1,char *string2)
{
for (char *temp=string1 ;*temp!='\0'; temp++)
{
*string2=*temp;
string2++;
}
*string2='\0';
}
|
常量指针,指针常量区别 |
c++ |
|
#include "iostream.h"
int main()
{
int a=42;
const int b=84;
const int *captr=&a;//常量指针
int * const acptr=&a;//指针常量
int *bptr=&b;//错误,不能把常量的地址给指针变量
const int *cbprt=&b;//把常量的地址给常量指针是允许的
*captr=68;//错误,间接引用常量指针不可修改内存中的数据 - 82 -
*acptr=68;//间接引用指针常量可以修改内存中的数据
captr=&b;//常量指针可以指向其他变量
acptr=&b;//错误,指针常量不能指向别的变量
const int * const ccaptr=&a;//常量指针常量,既不能间接引用修改数据,也不能指向别的变量或常量
*ccaptr=68;//错误,不能间接引用修改数据
ccaptr=&b;//错误,不能指向别的常量或变量
return 0;
}
/* &a 是地址
指针常量: 内容可以修改,地址不能改 int * const
常量指针:内容不能被修改,地址可以更改 const int *
常量指针常量,都不能改 const int * const ccc=&a
|