#include <iostream.h>
const float PI=3.1416;
float fCir_L(float);
float fCir_S(float);
main()
{
float r,l,s;
cout<<"r=";
cin>>r;
l=fCir_L(r);
s=fCir_S(r);
cout<<"l="<<l;
cout<<"\ns="<<s;
}
float fCir_L(float x)
{
float z=-1.0;
if (x>=0.0)
z=2*PI*x;
return(z);
}
float fCir_S(float x)
{
float z=-1.0;
if (x>=0.0)
z=PI*x*x;
return(z);
}
#include <iostream.h>
void main(void)
{
double s1,s2,s3;
s1=1.5;
cout<<"s1="<<s1<<endl;
s2=2.5;
cout<<"s2="<<s2<<endl;
s3= 3.5;
cout<<"s3="<<s3<<endl;
cout<<"s1+s2+s3="<<s1+s2+s3<<endl;
}
#include <iostream.h>
main()
{
double r=1.0;
cout<<"r="<<r<<endl;
double l;
l=2*3.1416*r;
cout<<"l="<<l<<endl;
double s=3.1416*r*r;
cout<<"s="<<s<<endl;
cout<<"r=";
cin>>r;
l=2*3.1416*r;
cout<<"l="<<l<<endl;
s=3.1416*r*r;
cout<<"s="<<s<<endl;
}
#include <iostream.h>
void main()
{
char c1='A';
cout<<'W';
cout<<c1<<endl;
cout<<"This is a test."<<endl;
cout<<"------------------"<<endl;
int n=100;
cout<<10;
cout<<n;
cout<<2*n<<endl;
cout<<"------------------"<<endl;
double pi=3.1415926,r=10.0,s=pi*r*r;
cout<<pi<<endl;
cout<<r;
cout<<s;
cout<<2*r*pi<<endl;
cout<<"------------------"<<endl;
cout<<'W'<<" "<<c1<<endl;
cout<<"This is a test."<<endl;
cout<<"pi="<<pi<<" r="<<r<<" s="<<s<<endl;
}
#include <iostream.h>
main()
{
char c;
cin>>c;
cout<<"c="<<c<<endl;
int n;
cin>>n;
cout<<"n="<<n<<endl;
double x;
cin>>x;
cout<<"x="<<x<<endl;
cout<<"n=";
cin>>n;
cout<<"n="<<n<<endl;
cout<<"c n x"<<endl;
cin>>c>>n>>x;
cout<<"c="<<c<<" n="<<n<<" x="<<x<<endl;
}
#include <iostream.h>
main()
{
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
cout<<a<<"+"<<b<<"="<<a+b<<endl;
cout<<a<<"-"<<b<<"="<<a-b<<endl;
cout<<a<<"*"<<b<<"="<<a*b<<endl;
cout<<a<<"/"<<b<<"="<<a/b<<endl;
cout<<a<<"%"<<b<<"="<<a%b<<endl;
short n=32767,m;
cout<<"n="<<n<<endl;
m=n+1;
cout<<"n+1="<<m<<endl;
}
#include <iostream.h>
main()
{
int a=010,b=10,c=0X10;
cout<<"DEC:";
cout<<" a="<<a;
cout<<" b="<<b;
cout<<" c="<<c<<endl;
cout<<"OCT:";
cout<<oct;
cout<<" a="<<a;
cout<<" b="<<b;
cout<<" c="<<c<<endl;
cout<<"HEX:";
cout<<hex;
cout<<" a="<<a;
cout<<" b="<<b;
cout<<" c="<<c<<endl;
cout<<"a+b+c=";
cout<<dec;
cout<<a+b+c<<endl;
cout<<"DEC:a="; cin>>a;
cout<<"OCT:b="; cin>>b;
cout<<"HEX:a="; cin>>c;
cout<<"DEC:"<<dec<<endl;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
}
#include <iostream.h>
#include<iomanip.h>
main()
{
float fx,fy;
cout<<"fx=";
cin>>fx;
cout<<"fy=";
cin>>fy;
cout<<fx<<"+"<<fy<<"="<<fx+fy<<endl;
cout<<fx<<"-"<<fy<<"="<<fx-fy<<endl;
cout<<fx<<"*"<<fy<<"="<<fx*fy<<endl;
cout<<fx<<"/"<<fy<<"="<<fx/fy<<endl<<endl;
float dx,dy;
cout<<"dx=";
cin>>dx;
cout<<"dy=";
cin>>dy;
cout<<dx<<"+"<<dy<<"="<<dx+dy<<endl;
cout<<dx<<"-"<<dy<<"="<<dx-dy<<endl;
cout<<dx<<"*"<<dy<<"="<<dx*dy<<endl;
cout<<dx<<"/"<<dy<<"="<<dx/dy<<endl<<endl;
fx=10.0;fy=6.0;
float fz=fx/fy;
dx=10.0;dy=6.0;
double dz=dx/dy;
cout<<"fz=";
cout<<setprecision(20)<<fx<<"/"<<fy<<"="<<fz<<endl;
cout<<"dz=";
cout<<setprecision(20)<<dx<<"/"<<dy<<"="<<dz<<endl<<endl;;
float x=3.5e14;
cout<<"x="<<x<<endl;
cout<<"x*x="<<x*x<<endl;
cout<<"x*x*x="<<x*x*x<<endl;
}
#include <iostream.h>
main()
{
char c1='A';
char c2;
c2=c1+32;
cout<<"c1="<<c1<<endl;
cout<<"c2="<<c2<<endl;
cout<<c1<<" : "<<int(c1)<<endl;
cout<<c2<<" : "<<int(c2)<<endl;
cout<<'$'<<" : "<<int('$')<<endl;
cout<<"c1 c2"<<endl;
cin>>c1>>c2;
cout<<"c1="<<c1<<" c2="<<c2<<endl;
}
#include <iostream.h>
main()
{
char c1='\a',TAB='\t';
cout<<c1<<endl;
cout<<1<<TAB<<2<<TAB<<3<<TAB<<4<<endl;
cout<<"He said \"Thank you\"."<<endl;
cout<<"abc\n"<<"def"<<'\n';
}
#include <iostream.h>
main()
{
bool flag1=false,flag2=true;
cout<<"false:"<<false<<endl;
cout<<"true: "<<true<<endl;
cout<<"flag1="<<flag1<<endl;
cout<<"flag2="<<flag2<<endl;
int x=1;
flag1=x>0;
cout<<"flag1="<<flag1<<endl;
flag2=flag1;
cout<<"flag2="<<flag2<<endl;
flag1=100;
cout<<"flag1="<<flag1<<endl;
flag2=-100;
cout<<"flag2="<<flag2<<endl;
}
#include <iostream.h>
const double PI=3.1416;
main()
{
double r,l,s;
cout<<"r=";
cin>>r;
l=2*PI*r;
cout<<"l="<<l<<endl;
s=PI*r*r;
cout<<"s="<<s<<endl;
}
#include<iostream.h>
main()
{
enum color {
RED=3,
YELLOW=6,
BLUE=9
};
enum color a=RED;
color b;
cout<<"RED="<<RED<<endl;
cout<<"YELLOW="<<YELLOW<<endl;
cout<<"BLUE="<<BLUE<<endl;
b=a;
a=BLUE;
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
b=BLUE;
cout<<"a<b="<<(a<b)<<endl;
}
#include <iostream.h>
const double PI=3.1416;
main()
{
double r=3,l,s;
l=2*PI*r;
cout<<"l="<<l<<endl;
s=PI*r*r;
cout<<"s="<<s<<endl;
int il,is;
il=l;
is=s;
cout<<"il="<<il<<endl;
cout<<"is="<<is<<endl;
}
#include <iostream.h>
main()
{
char c;
double x,y;
cout<<"++E and E++ :"<<endl;
c='B';
cout<<"c="<<++c<<endl;
c='B';
cout<<"c="<<c++<<endl;
x=1.5;
y=5+ ++x;
cout<<"y="<<y<<endl;
x=1.5;
y=5+x++;
cout<<"y="<<y<<endl;
cout<<"--------------------"<<endl;
cout<<"--E and E-- :"<<endl;
c='B';
cout<<"c="<<--c<<endl;
c='B';
cout<<"c="<<c--<<endl;
x=1.5;
y=5+--x;
cout<<"y="<<y<<endl;
x=1.5;
y=5+x--;
cout<<"y="<<y<<endl;
}
#include <iostream.h>
main()
{
int a=3, b=2;
cout<<a<b<<endl;
cout<<(a<b)<<(a>b)<<(a>=b)<<(a==b)<<(a!=b)<<endl;
bool flag=2*a<b+10;
cout<<"flag="<<flag;
}
#include <iostream.h>
main()
{
float a=3.5,b=2.1,c=0;
cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
cout<<"a&&b="<<(a&&b)<<endl;
cout<<"a&&c="<<(a&&c)<<endl;
cout<<"a||b="<<(a||b)<<endl;
cout<<"a||c="<<(a||c)<<endl;
cout<<"!a="<<!a<<endl<<"!c="<<!c<<endl;
bool flag=a>=0 && a<=5;
cout<<"a=>0 && a<=5="<<flag<<endl;
cout<<"a+5>2*b+2||a<b+3="<<(a+5>2*b+2||a<b+3)<<endl;
}
#include <iostream.h>
main()
{
cout<<"24&12="<<(24&12)<<endl;
cout<<"24^12="<<(24^12)<<endl;
cout<<"24|12="<<(24|12)<<endl;
cout<<"~24="<<(~24)<<endl;
cout<<"5<<3="<<(5<<3)<<endl;
cout<<"-5<<3="<<(-5<<3)<<endl;
cout<<"5>>3="<<(5>>3)<<endl;
cout<<"-5>>3="<<(-5>>3)<<endl;
}
#include <iostream.h>
main()
{
int a=1,b=1,c=3;
cout<<"a="<<a<<" b="<<b<<" c="<<c<<endl;
b+=a+2*c%5;
cout<<"(1) b="<<b<<endl;
a=1,b=1,c=3;
a<<=c-2*b;
cout<<"(2) a="<<a<<endl;
a=1,b=1,c=3;
a*=b=c=3;
cout<<"(3) a="<<a<<" b="<<b<<" c="<<c<<endl;
a=1,b=1,c=3;
a+=b+=c;
cout<<"(4) a="<<a<<" b="<<b<<" c="<<c<<endl;
a=1,b=1,c=3;
a-=b=++c+2;
cout<<"(5) a="<<a<<" b="<<b<<" c="<<c<<endl;
}
#include <iostream.h>
main()
{
cout<<"sizeof('$')="<<sizeof('$')<<endl;
cout<<"sizeof(1)="<<sizeof(1)<<endl;
cout<<"sizeof(1.5)="<<sizeof(1.5)<<endl;
cout<<"sizeof(\"Good!\")="<<sizeof("Good!")<<endl;
int i=100;
char c='A';
float x=3.1416;
double p=0.1;
cout<<"sizeof(i)="<<sizeof(i)<<endl;
cout<<"sizeof(c)="<<sizeof(c)<<endl;
cout<<"sizeof(x)="<<sizeof(x)<<endl;
cout<<"sizeof(p)="<<sizeof(p)<<endl;
cout<<"sizeof(x+1.732)="<<sizeof(x+1.732)<<endl;
cout<<"sizeof(char)="<<sizeof(char)<<endl;
cout<<"sizeof(int)="<<sizeof(int)<<endl;
cout<<"sizeof(float)="<<sizeof(float)<<endl;
cout<<"sizeof(double)="<<sizeof(double)<<endl;
char str[]="This is a test.";
int a[10];
double xy[10];
cout<<"sizeof(str)="<<sizeof(str)<<endl;
cout<<"sizeof(a)="<<sizeof(a)<<endl;
cout<<"sizeof(xy)="<<sizeof(xy)<<endl;
struct st {
short num;
float math_grade;
float Chinese_grade;
float sum_grade;
};
st student1;
cout<<"sizeof(st)="<<sizeof(st)<<endl;
cout<<"sizeof(student1)="<<sizeof(student1)<<endl;
}
#include <iostream.h>
main()
{
int x, y;
x=50;
y=(x=x-5, x/5);
cout<<"x="<<x<<endl;
cout<<"y="<<y<<endl;
}
#include <iostream.h>
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.723+x<<endl;
cout<<"double(100)="<<double(100)<<endl;
}
#include <iostream.h>
main()
{
float a,b,s;
cout<<"a b"<<endl;
cin>>a>>b;
s=a;
if (a<b) {
s=b;
}
s=s*s;
cout<<"s="<<s;
}
#include <iostream.h>
main()
{
int x,y;
cout<<"x=";
cin>>x;
if (x<=0) {
y=2*x;
cout<<"y="<<y;
}
else {
y=x*x;
cout<<"y="<<y;
}
}
#include <iostream.h>
main()
{
int a,b,c;
int smallest;
cout<<"a b c"<<endl;
cin>>a>>b>>c;
if (a<=b)
{
if (a<=c)
smallest=a;
else
smallest=c;
}
else
{
if (b<=c)
smallest=b;
else
smallest=c;
}
cout<<"Smallest="<<smallest<<endl;
}
#include <iostream.h>
main()
{
int score;
cout<<"score=";
cin>>score;
if (score<0 || score>100)
{
cout<<"The score is out of range!"<<endl;
}
else if (score>=90)
cout<<"Your grade is a A."<<endl;
else if (score>=80)
cout<<"Your grade is a B."<<endl;
else if (score>=70)
cout<<"Your grade is a C."<<endl;
else if (score>=60)
cout<<"Your grade is a D."<<endl;
else
cout<<"Your grade is a E."<<endl;
}
#include <iostream.h>
main()
{
int n;
cout<<"n=";
cin>>n;
if (n>=0 && n<=100 &&n%2==0)
cout<<"n="<<n<<endl;
else
cout<<"The "<<n<<" is out of range!"<<endl;
}
#include <iostream.h>
main()
{
int a,b,Max;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
Max=a>b?a:b;
cout<<"Max="<<Max<<endl;
}
#include <iostream.h>
main()
{
int a,b;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
if (b!=0 && a%b==0) {
cout<<b<<" divides "<<a<<endl;
cout<<"a/b="<<a/b<<endl;
}
else
cout<<b<<" does not divide "<<a<<endl;
}
#include <iostream.h>
main()
{
int x,y,z;
char c1;
cin>>x>>c1>>y;
switch(c1) {
case '+':cout<<x<<"+"<<y<<"="<<x+y<<endl;
break;
case '-':cout<<x<<"-"<<y<<"="<<x-y<<endl;
break;
case '*':cout<<x<<"*"<<y<<"="<<x*y<<endl;
break;
case '/':cout<<x<<"/"<<y<<"="<<x/y<<endl;
break;
case '%':cout<<x<<"%"<<y<<"="<<x%y<<endl;
break;
default :cout<<"Wrong !"<<endl;
}
}
#include<iostream.h>
float x=365.5;
main() {
int x=1,y=2;
double w=x+y;
{
double x=1.414,y=1.732,z=3.14;
cout<<"inner:x="<<x<<endl;
cout<<"inner:y="<<y<<endl;
cout<<"inner:z="<<z<<endl;
cout<<"outer:w="<<w<<endl;
cout<<"::x="<<::x<<endl;
}
cout<<"outer:x="<<x<<endl;
cout<<"outer:y="<<y<<endl;
cout<<"outer:w="<<w<<endl;
cout<<"::x="<<::x<<endl;
}
#include<iostream.h>
main() {
for(int i=1;i<=10;i++)
cout<<i<<" ";
cout<<endl;
for(int j=10;j>=1;j--)
cout<<j<<" ";
cout<<endl;
for(int k=1;k<=10;k=k+2)
cout<<k<<" ";
cout<<endl;
for(char c='A';c<='Z';c++)
cout<<c;
cout<<endl;
for(float x=0;x<=1.0;x=x+0.1)
cout<<x<<" ";
cout<<endl;
for(float x1=0;x1<=1.0+0.1/2;x1=x1+0.1)
cout<<x1<<" ";
cout<<endl;
int s=0;
for(int n=1;n<=100;n++)
s=s+n;
cout<<"s="<<s<<endl;
}
#include<iostream.h>
main()
{
int s=0,n=1;
while(n<=100) {
s=s+n;
n++;
}
cout<<"s="<<s<<endl;
double x,sum=0.0;
cout<<"x=";
cin>>x;
while(x!=0) {
sum+=x;
cout<<"x=";
cin>>x;
}
cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
int s=0,n=0;
do {
n++;
s+=n;
}while(n<100);
cout<<"s="<<s<<endl;
double x,sum=0.0;
do {
cout<<"x=";
cin>>x;
sum+=x;
} while(x!=0);
cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
for (int i=1;i<=9;i++) {
cout<<i;
for (int j=1;j<=9;j++)
cout<<'\t'<<i<<"*"<<j<<"="<<i*j;
cout<<endl;
}
}
#include<iostream.h>
main()
{
int x,sum=0;
L1: cout<<"x=";
cin>>x;
if (x==-1)
goto L2;
else
sum+=x;
goto L1;
L2: cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
double x,sum=0.0;
while(1) {
cout<<"x=";
cin>>x;
if (x<=0) break;
sum+=x;
}
cout<<"sum="<<sum<<endl;
}
#include<iostream.h>
main()
{
int i;
for (i=1;i<=20;i++)
{
if (i%3==0)
continue;
cout<<i<<" ";
}
cout<<endl;
}
#include<iostream.h>
main()
{
int a[5],i,sum;
double avg;
for (i=0;i<5;i++) {
cout<<"a["<<i<<"]=";
cin>>a[i];
}
cout<<a[0]<<a[1]<<a[2]<<a[3]<<a[4]<<endl;
for (i=0;i<5;i++)
cout<<a[i]<<" ";
cout<<endl;
sum=a[0]+a[1]+a[2]+a[3]+a[4];
cout<<"sum="<<sum<<endl;
for (sum=0,i=0;i<5;i++)
sum+=a[i];
cout<<"sum="<<sum<<endl;
avg=sum/5.0;
cout<<"avg="<<avg<<endl;
}
#include<iostream.h>
main()
{
int i,max,index,a[5];
for (i=0;i<=4;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
max=a[0];
for (i=0;i<=4;i++)
{
if (max<a[i])
{
max=a[i];
index=i;
}
}
cout<<"\nMax="<<max<<" index="<<index;
}
#include<iostream.h>
#define size 5
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])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for (i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
int value;
int found;
int low,high,mid;
for (i=1;i<=3;i++) {
cout<<"value=";
cin>>value;
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 valu found at:a["<<mid<<"]="<<a[mid]<<endl;
else
cout<<"The "<<value<<" is not found!"<<endl;
}
}
#include<iostream.h>
main()
{
int i,j;
float t,a[5];
for (i=0;i<=4;i++)
{
cout<<"a["<<i<<"]=";
cin>>a[i];
}
for (i=0;i<=3;i++)
for (j=i+1;j<=4;j++)
if (a[i]<=a[j])
{
t=a[i];
a[i]=a[j];
a[j]=t;
}
for (i=0;i<=4;i++)
cout<<a[i]<<" ";
}
#include<iostream.h>
main()
{
int a[2][3],i,j;
for (i=0;i<2;i++)
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
int h,l,Max=a[0][0];
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
if (Max<a[i][j]) {
Max=a[i][j];
h=i;
l=j;
}
}
}
cout<<"Max:"<<"a["<<h<<"]["<<l<<"]="<<a[h][l]<<endl;
}
#include<iostream.h>
main()
{
char str[6];
int i;
cout<<"str=";
cin>>str;
cout<<str<<endl;
cout<<str<<endl;
for (i=0;i<6;i++)
cout<<str[i];
cout<<endl;
for (i=5;i>=0;i--)
cout<<str[i];
cout<<endl;
for (i=0;i<=5;i++)
str[i]-=32;
cout<<str<<endl;
}
#include<iostream.h>
main()
{
int a,b,c,*ip;
a=100;
ip=&a;
cout<<"a="<<a<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
ip=&b;
b=200;
cout<<"b="<<b<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
ip=&c;
*ip=a+b;
cout<<"c="<<c<<endl;
cout<<"*ip="<<*ip<<endl;
cout<<"ip="<<ip<<endl;
}
#include<iostream.h>
main()
{
int a[2][3],i,j;
int* ip;
for (i=0;i<2;i++)
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
cout<<endl;
}
ip=&a[0][0];
for (i=0;i<2;i++) {
for (j=0;j<3;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cout<<ip<<" ";
cout<<*ip<<endl;
ip++;
}
}
}
#include<iostream.h>
main()
{
int a[]={1,2,3,4,5,6};
int *ip1,*ip2;
ip1=a;
ip2=ip1;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl;
ip1++;
ip2+=4;
cout<<"*ip1="<<(*ip1)<<endl;
cout<<"*ip2="<<(*ip2)<<endl;
int n=ip2>ip1;
cout<<"ip2>ip1="<<n<<endl;
cout<<"ip2!=NULL="<<(ip2!=NULL)<<endl;
n=ip2-ip1;
cout<<"ip2-ip1="<<n<<endl;
}
#include<iostream.h>
main()
{
char str[10];
char *strip=str;
cout<<"str=";
cin>>str;
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
cout<<"strip=";
cin>>strip;
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
*(strip+2)='l';
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
strip=new char(100);
cout<<"strip=";
cin>>strip;
cout<<"str="<<str<<endl;
cout<<"strip="<<strip<<endl;
}
#include<iostream.h>
main()
{
int h[]={1001,1002,1003,1004};
float x[]={12.3,13.1,11.9,12.1};
char *p[]={"Wang hua","Zhang jian","Li wei","Hua ming"};
int i,j,it;
float ft;
char *pt;
for (i=0;i<=3;i++)
for (j=i+1;j<=3;j++)
if (x[i]>=x[j]) {
ft=x[i],x[i]=x[j],x[j]=ft;
it=h[i],h[i]=h[j],h[j]=it;
pt=p[i],p[i]=p[j],p[j]=pt;
}
for (i=0;i<=3;i++)
cout<<h[i]<<" ,"<<p[i]<<" ,"<<x[i]<<endl;
}
#include<iostream.h>
main()
{
char *colors[]={"Red","Blue","Yellow","Green"};
char **pt;
pt=colors;
for (int i=0;i<=3;i++) {
cout<<"pt="<<pt<<endl;
cout<<"*pt="<<*pt<<endl;
cout<<"**pt="<<**pt<<endl;
pt++;
}
}
#include<iostream.h>
main()
{
struct books
{
char title[20];
char author[15];
int pages;
float price;
} ;
struct books Zbk={"VC++ ","Zhang",295,35.5};
books Wbk;
cout<<"Zbk:"<<endl;
cout<<Zbk.title <<endl;
cout<<Zbk.author<<endl;
cout<<Zbk.pages<<endl;
cout<<Zbk.price<<endl;
cout<<"--------------------"<<endl;
Zbk.pages+=10;
Zbk.price+=0.5;
cout<<"Zbk.pages="<<Zbk.pages<<endl;
cout<<"Zbk.price="<<Zbk.price<<endl;
cout<<"--------------------"<<endl;
cout<<"Wbk.title =";
cin>>Wbk.title;
cout<<"Wbk.author=";
cin>>Wbk.author;
cout<<"Wbk.pages=";
cin>>Wbk.pages;
cout<<"Wbk.price=";
cin>>Wbk.price;
cout<<"Wbk:"<<endl;
cout<<Wbk.title <<endl;
cout<<Wbk.author<<endl;
cout<<Wbk.pages<<endl;
cout<<Wbk.price<<endl;
cout<<"--------------------"<<endl;
books temp;
temp=Wbk;
cout<<"temp:"<<endl;
cout<<temp.title<<endl;
cout<<temp.author<<endl;
cout<<temp.pages<<endl;
cout<<temp.price<<endl;
}
#include<iostream.h>
main()
{
int i;
struct student {
int num;
char name[10];
float maths;
float physics;
float chemistry;
double total;
};
student st[3];
cout<<" num name maths physics chemistry "<<endl;
for (i=0;i<3;i++)
{
cout<<i+1<<" ";
cin>>st[i].num;
cin>>st[i].name;
cin>>st[i].maths;
cin>>st[i].physics;
cin>>st[i].chemistry;
}
for (i=0;i<3;i++)
st[i].total=st[i].maths+st[i].physics+st[i].chemistry;
for (i=0;i<3;i++)
{
cout<<"st["<<i<<"]: ";
cout<<st[i].num<<'\t';
cout<<st[i].name<<'\t';
cout<<st[i].maths<<'\t';
cout<<st[i].physics<<'\t';
cout<<st[i].chemistry<<'\t';
cout<<st[i].total<<endl;
}
}
#include<iostream.h>
main()
{
struct human {
char name[10];
int sex;
int age;
};
struct human x={"WangPing",1,30},*p=NULL;
p=&x;
cout<<"x.name="<<x.name<<endl;
cout<<"x.sex="<<x.sex<<endl;
cout<<"x.age="<<x.age<<endl;
cout<<"(*p).name="<<(*p).name<<endl;
cout<<"(*p).sex="<<(*p).sex<<endl;
cout<<"(*p).age="<<(*p).age<<endl;
cout<<"p->name="<<p->name<<endl;
cout<<"p->sex="<<p->sex<<endl;
cout<<"p->age="<<p->age<<endl;
cout<<"name:";
cin>>(*p).name;
cout<<"sex:";
cin>>(*p).sex;
cout<<"age:";
cin>>(*p).age;
cout<<"x.name="<<x.name<<endl;
cout<<"x.sex="<<x.sex<<endl;
cout<<"x.age="<<x.age<<endl;
}
include<iostream.h>
main()
{
struct human {
char name[10];
int sex;
int age;
};
struct human x={"WangPing",1,30},*p=&x;
cout<<"(*p).name="<<(*p).name<<endl;
cout<<"(*p).sex="<<(*p).sex<<endl;
cout<<"(*p).age="<<(*p).age<<endl;
cout<<"-------------------------"<<endl;
p=new human;
cout<<"p->name=";
cin>>p->name;
cout<<"p->sex=";
cin>>p->sex;
cout<<"p->age=";
cin>>p->age;
cout<<"-------------------------"<<endl;
cout<<"p->name="<<p->name<<endl;
cout<<"p->sex="<<p->sex<<endl;
cout<<"p->age="<<p->age<<endl;
cout<<"-------------------------"<<endl;
cout<<"x.name="<<x.name<<endl;
cout<<"x.sex="<<x.sex<<endl;
cout<<"x.age="<<x.age<<endl;
delete p;
}
#include<iostream.h>
main()
{
struct human {
char name[10];
int sex;
int age;
};
human x[]={{"WeiPing",1,30},{"LiHua",1,25},{"LiuMin",0,23}},*p=NULL;
for (int i=0;i<3;i++)
{
cout<<x[i].name<<'\t';
cout<<x[i].sex<<'\t';
cout<<x[i].age<<endl;
}
cout<<"----------------"<<endl;
for (p=x;p<=&x[2];p++)
{
cout<<p->name<<'\t';
cout<<p->sex<<'\t';
cout<<p->age<<endl;
}
}
#include<iostream.h>
main()
{
struct test {
char *str;
int *ip;
} x;
x.ip=new int;
*(x.ip)=100;
cout<<"x.ip:"<<x.ip<<'\t'<<*(x.ip)<<endl;
cout<<"---------------"<<endl;
delete x.ip;
x.ip=new int[5];
for(int i=0;i<5;i++)
*(x.ip+i)=100+i;
cout<<"x.ip:"<<endl;
for(i=0;i<5;i++)
cout<<x.ip+i<<'\t'<<(*(x.ip+i))<<endl;
delete x.ip;
cout<<"---------------"<<endl;
x.str=new char('A');
cout<<"x.str:"<<(*x.str)<<endl;
cout<<"---------------"<<endl;
delete x.str;
x.str=new char[5];
*x.str='G';
*(x.str+1)='o';
*(x.str+2)='o';
*(x.str+3)='d';
*(x.str+4)='\0';
cout<<"x.str:"<<x.str<<endl;
delete x.str;
cout<<"---------------"<<endl;
test y={"Very Good!",NULL};
cout<<"y.str:"<<y.str<<endl;
cout<<"y.ip:"<<y.ip<<endl;
}
#include<iostream.h>
main()
{
struct date
{
int year;
int month;
int day;
};
struct baby {
int num;
float weight;
date birthday;
};
baby b1={10001,10,{2002,12,25}};
cout<<"b1.num="<<b1.num<<endl;
cout<<"b1.weight="<<b1.weight<<endl;
cout<<"b1.birthday.year="<<b1.birthday.year<<endl;
cout<<"b1.birthday.month="<<b1.birthday.month<<endl;
cout<<"b1.birthday.day="<<b1.birthday.day<<endl;
cout<<"--------------------------"<<endl;
baby temp;
temp=b1;
cout<<"temp.num="<<temp.num<<endl;
cout<<"temp.weight="<<temp.weight<<endl;
cout<<"temp.birthday.year="<<temp.birthday.year<<endl;
cout<<"temp.birthday.month="<<temp.birthday.month<<endl;
cout<<"temp.birthday.day="<<temp.birthday.day<<endl;
}
#include<iostream.h>
main()
{
struct list {
char name[10];
int sex;
int age;
list *next;
};
list L1={"WeiPing",1,35.5,NULL};
cout<<"L1:"<<endl;
cout<<"name\t"<<L1.name<<endl;
cout<<"sex\t"<<L1.sex<<endl;
cout<<"age\t"<<L1.age<<endl;
cout<<"next\t"<<L1.next<<endl;
}
#include<iostream.h>
main()
{
int i;
struct student {
char name[10];
int math;
int computer;
float sum;
student *next;
};
struct student *head,*tail,*temp;
temp=new struct student;
head=temp;
tail=head;
cout<<"\tname Math Computer"<<endl;
for (i=1;;i++) {
cout<<i<<"\t";
cin>>temp->name;
if (temp->name[0]!='*')
{
cin>>temp->math>>temp->computer;
temp->sum=temp->math+temp->computer;
temp->next=NULL;
tail=temp;
}
else
{
delete temp;
tail->next=NULL;
break;
}
temp->next=new struct student;
temp=temp->next;
}
cout<<"--------------------"<<endl;
temp=head;
while (temp!=NULL) {
cout<<temp->name<<","<<temp->math<<",";
cout<<temp->computer<<","<<temp->sum<<endl;
temp=temp->next;
}
}
#include<iostream.h>
main()
{
int i;
struct student {
char name[10];
int math;
int computer;
float sum;
student *forw;
student *next;
};
struct student *head,*tail,*temp;
temp=new struct student;
head=temp;
tail=head;
head->forw=NULL;
cout<<"\tname Math Computer"<<endl;
for (i=1;;i++) {
cout<<i<<"\t";
cin>>temp->name;
if (temp->name[0]!='*')
{
cin>>temp->math>>temp->computer;
temp->sum=temp->math+temp->computer;
temp->next=NULL;
tail=temp;
}
else
{
delete temp;
tail->next=NULL;
break;
}
temp->next=new struct student;
temp->next->forw=temp;
temp=temp->next;
}
cout<<"head------>tail:"<<endl;
temp=head;
while (temp!=NULL) {
cout<<temp->name<<","<<temp->math<<",";
cout<<temp->computer<<","<<temp->sum<<endl;
temp=temp->next;
}
cout<<"tail------>head:"<<endl;
temp=tail;
while (temp!=NULL) {
cout<<temp->name<<","<<temp->math<<",";
cout<<temp->computer<<","<<temp->sum<<endl;
temp=temp->forw;
}
}
#include<iostream.h>
main()
{
int i;
union utag {
char c;
int k;
float x;
};
union utag u;
u.c='*';
cout<<"u.c="<<u.c<<endl;
u.k=1000;
cout<<"u.k="<<u.k<<endl;
u.x=3.1416;
cout<<"u.x="<<u.x<<endl;
utag u1={'A'};
cout<<"u1.c="<<u1.c<<endl;
cout<<"u1.k="<<u1.k<<endl;
cout<<"u1.x="<<u1.x<<endl;
}
#include<iostream.h>
main()
{
struct s_tag {
short i;
float x;
} sx={100,3.1416};
union u_tag {
short i;
float x;
} ux={1000};
cout<<"sizeof(struct s_tag)="<<sizeof(struct s_tag)<<endl;
cout<<"sx.i="<<sx.i<<endl;
cout<<"sx.x="<<sx.x<<endl;
cout<<"sizeof(sx)="<<sizeof(sx)<<endl;
cout<<"------------------------------"<<endl;
cout<<"sizeof(union u_tag)="<<sizeof(union u_tag)<<endl;
ux.i=200;
cout<<"ux.i="<<ux.i<<endl;
ux.x=123.456;
cout<<"ux.x="<<ux.x<<endl;
cout<<"sizeof(ux)="<<sizeof(ux)<<endl;
}
#include<iostream.h>
main()
{
typedef int ARRAY_INT[50];
int i;
ARRAY_INT a;
for (i=0;i<50;i++) {
if (i%10==0)
cout<<endl;
a[i]=i;
cout<<a[i]<<"\t";
}
cout<<endl;
}
#include<iostream.h>
struct student
{
int num;
char name[20];
float grade;
};
void main(void)
{
int i,size;
char str[]="This is a string.";
int int_values[] = {51, 23, 2, 44, 45,0,11};
float float_values[] = {15.1, 13.3, 22.2, 10.4, 1.5};
student st_arr[]={101,"WangLin",92,102,"LiPing",85,103,"ZhaoMin",88};
size=sizeof(str) / sizeof(char);
cout<<"Number of elements in str: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<str[i];
}
cout<<endl;
size=sizeof(int_values) / sizeof(int);
cout<<"Number of elements in int_values: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<int_values[i]<<" ";
}
cout<<endl;
size=sizeof(float_values) / sizeof(float);
cout<<"Number of elements in float_values: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<float_values[i]<<" ";
}
cout<<endl;
size=sizeof(st_arr) / sizeof(student);
cout<<"Number of elements in st_arr: ";
cout<<size<<endl;
for(i=0;i<size;i++) {
cout<<st_arr[i].num<<" ";
cout<<st_arr[i].name<<" ";
cout<<st_arr[i].grade<<endl;
}
}
#include<iostream.h>
double add(double x,double y)
{
double z;
z=x+y;
cout<<x<<"+"<<y<<"="<<z<<endl;
return(z);
}
main()
{
double a=0.5,b=1.0;
cout<<"add(1.5,2.5)="<<add(1.5,2.5)<<endl;
cout<<"add(a,b)="<<add(a,b)<<endl;
cout<<"add(2*a,a+b)="<<add(2*a,a+b)<<endl;
cout<<"----------------------"<<endl;
double c=2*add(a,b);
cout<<"c="<<c<<endl;
cout<<"----------------------"<<endl;
add(2*a,b);
cout<<"----------------------"<<endl;
int n=1,m=2;
cout<<"add("<<n<<","<<m<<")="<<add(n,m)<<endl;
}
#include<iostream.h>
int sgn(double x)
{
if (x>0) return(1);
if (x<0) return(-1);
return(0);
}
main()
{
double x;
int i;
for (i=0;i<=2;i++) {
cout<<"x=";
cin>>x;
cout<<"sgn("<<x<<")="<<sgn(x)<<endl;
}
}
#include<iostream.h>
main()
{
float max(float,float);
float a,b,Max;
cout<<"a=";
cin>>a;
cout<<"b=";
cin>>b;
Max=max(a,b);
cout<<"max("<<a<<","<<b<<")="<<Max<<endl;
}
float max(float x,float y)
{
float z;
z=(x>y)?x:y;
return(z);
}
#include<iostream.h>
f(int x,int y)
{
++x;
--y;
cout<<"x="<<x<<",y="<<y<<endl;
}
main() {
int a,b;
a=b=10;
f(a,b);
cout<<"a="<<a<<",b="<<b<<endl;
f(2*a,a+b);
}
#include<iostream.h>
struct student {
int num;
char name[10];
float maths;
float physics;
float chemistry;
double total;
};
input_Rec(struct student *p)
{
cin>>p->num;
cin>>p->name;
cin>>p->maths;
cin>>p->physics;
cin>>p->chemistry;
}
swap_Rec(struct student *p1,struct student *p2)
{
struct student x;
x=*p1;
*p1=*p2;
*p2=x;
}
put_Rec(struct student *p)
{
cout<<p->num<<'\t';
cout<<p->name<<'\t';
cout<<p->maths<<'\t';
cout<<p->physics<<'\t';
cout<<p->chemistry<<'\t';
cout<<p->total<<endl;
}
main()
{
int i,j;
struct student *p1,a[3];
cout<<"num\tname\tmaths\tphysics\tchemistry"<<endl;
for (p1=a;p1<=a+2;p1++) {
input_Rec(p1);
p1->total=p1->maths+p1->physics+p1->chemistry;
}
for (i=0;i<=2;i++)
for (j=i+1;j<=2;j++)
if (a[i].total<a[j].total)
swap_Rec(&a[i],&a[j]);
cout<<"-------------------"<<endl;
cout<<"num\tname\tmaths\tphysics\tchemistry\ttotal"<<endl;
for (p1=a;p1<=a+2;p1++)
put_Rec(p1);
}
#include<iostream.h>
struct student {
char name[10];
float grade;
};
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);
}
void show(student &x)
{
cout<<x.name<<" "<<x.grade<<endl;
}
void main()
{
student a={"ZhangHua",351.5},b={"WangJun",385};
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl;
swap(a,b);
cout<<"a:";
show(a);
cout<<"b:";
show(b);
cout<<"------------------"<<endl;
student t=max(a,b);
cout<<"Max:";
show(t);
}
#include <iostream.h>
disp(int x=1,int y=1,int z=1)
{
cout<<"参数1: "<<x<<endl;
cout<<"参数2: "<<y<<endl;
cout<<"参数3: "<<z<<endl;
cout<<"------------------"<<endl;
}
void main()
{
disp();
disp(10);
disp(10,20);
disp(10,20,30);
int a=1,b=2,c=3;
disp(a,b,c);
}
#include <iostream.h>
int str_len(const char *string)
{
int i=0;
while (*(string+i)!=NULL)
i++;
return i;
}
void main()
{
char a[]="ABCDE";
cout<<a<<"\t"<<str_len(a)<<endl;
char *str="Hello!";
cout<<str<<"\t"<<str_len(str)<<endl;
cout<<"This is a test."<<"\t"<<str_len("This is a test.")<<endl;
}
#include<iostream.h>
void disp(void);
void main(void)
{
disp();
}
void disp(void) {
cout<<" You are welcome."<<endl;
}
#include<iostream.h>
int abs(int x);
long abs(long x);
float abs(float x);
void main(void)
{
int i1=32767,i2=-32767;
long l1=456789,l2=-456789;
float x1=1.1234,x2=-1.1234;
cout<<abs(i1)<<","<<abs(i2)<<endl;
cout<<abs(l1)<<","<<abs(l2)<<endl;
cout<<abs(x1)<<","<<abs(x2)<<endl;
}
int abs(int x) {
if (x<0)
return(-x);
else
return(x);
}
long abs(long x) {
if (x<0)
return(-x);
else
return(x);
}
float abs(float x) {
if (x<0.0)
return(-x);
else
return(x);
}
#include<iostream.h>
inline int max(int x,int y)
{
return x>y?x:y;
}
main()
{
int a=3,b=5,c;
c=max(a,b);
cout<<"max("<<a<<","<<b<<")="<<c<<endl;
cout<<"max("<<15<<","<<11<<")="<<max(15,11)<<endl;
}
#include<iostream.h>
main()
{
int fact(int x);
int n,sn;
for (int i=1;i<=3;i++)
{
cout<<i<<" n=";
cin>>n;
sn=fact(n);
cout<<n<<"!="<<sn<<endl;
}
}
int fact(int x)
{
if (x==0) return(1);
return(x*fact(x-1));
}
#include<iostream.h>
int main(int argc,char *argv[])
{
int i;
for(i=0;i<argc;i++)
cout<<i<<":"<<argv[i]<<endl;
return 0;
}
#include<iostream.h>
void show_array1(int*,int);
void show_array2(int a[],int);
void sort(int*,int);
main()
{
int a[]={2,4,6,1,3,5};
int b[3][3]={{2,4,6},{1,3,5},{0,1,2}};
cout<<"show_array1(int*,int):"<<endl;
show_array1(a,6);
show_array1(&b[0][0],3*3);
cout<<"sort(int*,int) and show_array1(int*,int): "<<endl;
sort(a,6);
show_array1(a,6);
sort(&b[0][0],3*3);
show_array1(&b[0][0],9);
cout<<"show_array2(int a[],int):"<<endl;
show_array2(a,6);
show_array2(&b[0][0],3*3);
}
void show_array1(int *p,int size) {
for(int i=0;i<size;i++)
cout<<*(p+i)<<" ";
cout<<endl;
}
void show_array2(int a[],int size) {
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
void sort(int *p,int size) {
int t;
for (int i=0;i<size-1;i++)
for (int j=i+1;j<size;j++)
if (*(p+i)<=*(p+j))
{
t=*(p+i);
*(p+i)=*(p+j);
*(p+j)=t;
}
}
#include<iostream.h>
struct student {
char name[10];
float grade;
};
void change(student &x,float grade)
{
x.grade=grade;
}
void change1(student *p,float grade)
{
p->grade=grade;
}
void change2(student x,float grade)
{
x.grade=grade;
}
void show(student &x)
{
cout<<x.name<<" "<<x.grade<<endl;
}
void main()
{
student a={"ZhangHua",351.5};
show(a);
cout<<"change(student &x,float grade):"<<endl;
change(a,360);
show(a);
cout<<"change1(student *p,float grade):"<<endl;
change1(&a,375);
show(a);
cout<<"change2(student x,float grade):"<<endl;
change2(a,380.5);
show(a);
}
#include<iostream.h>
void calculate(int a[],int size,int& sum,float& average)
{
sum=0;
for (int i=0;i<size;i++) {
sum+=a[i];
}
average=sum/size;
}
void put_arr(int a[],int size)
{
for(int i=0;i<size;i++)
cout<<a[i]<<" ";
cout<<endl;
}
main()
{
int asize,bsize;
int a[]={2,4,6,1,3,5};
int b[]={1,3,5,7,9,11,13,15};
asize=sizeof(a)/sizeof(int);
cout<<"put_arr(a,asize):"<<endl;
put_arr(a,asize);
bsize=sizeof(b)/sizeof(int);
cout<<"put_arr(b,bsize):"<<endl;
put_arr(b,bsize);
float a_ave,b_ave;
int a_sum,b_sum;
cout<<"calculate(a,asize,a_sum,a_ave):"<<endl;
calculate(a,asize,a_sum,a_ave);
cout<<"a_sum="<<a_sum;
cout<<" a_ave="<<a_ave<<endl;
cout<<"calculate(b,bsize,b_sum,b_ave):"<<endl;
calculate(b,bsize,b_sum,b_ave);
cout<<"b_sum="<<b_sum;
cout<<" b_ave="<<b_ave<<endl;
}
#include<iostream.h>
int get_result(int a, int b, int (*sub)(int,int))
{
int r;
r=sub(a,b);
return r;
}
int max(int a, int b)
{
cout<<"In max"<<endl;
return((a > b) ? a: b);
}
int min(int a, int b)
{
cout<<"In min"<<endl;
return((a < b) ? a: b);
}
int sum(int a, int b)
{
cout<<"In sum"<<endl;
return(a+b);
}
void main(void)
{
int a,b,result;
for (int i=1;i<=3;i++) {
cout<<"Input a and b :";
cin>>a>>b;
cout<<i<<"\tget_result("<<a<<","<<b<<", &max):"<<endl;
result =get_result(a, b, &max);
cout<<"Max of "<<a<<" and "<<b<<" is "<<result<<endl;
result = get_result(a, b, &min);
cout<<"Min of "<<a<<" and "<<b<<" is "<<result<<endl;
result = get_result(a, b, &sum);
cout<<"Sum of "<<a<<" and "<<b<<" is "<<result<<endl;
}
}
#include<iostream.h>
#include<stdio.h>
#define size 3
struct book
{
char title[20];
char author[15];
int pages;
float price;
};
input_book(book& bk,char *name)
{
cout<<name<<":"<<endl;
cout<<"title:";
cin>>bk.title;
cout<<"author:";
cin>>bk.author;
cout<<"pages:";
cin>>bk.pages;
cout<<"price:";
cin>>bk.price;
}
output_book(book& bk,char *name)
{
cout<<name<<": ";
cout<<bk.title<<" ";
cout<<bk.author<<" ";
cout<<bk.pages<<" ";
cout<<bk.price<<endl;
}
void main(void)
{
int i;
char str[20];
book bk[size];
for(i=0;i<size;i++) {
sprintf(str,"bk[%d]",i+1);
input_book(bk[i],str);
}
for(i=0;i<size;i++) {
sprintf(str,"bk[%d]",i+1);
output_book(bk[i],str);
}
}
#include<iostream.h>
extern int a[]={1,2,3};
extern float p=3.14;
show() {
int i;
cout<<"In show():"<<endl;
cout<<"p="<<p<<endl;
cout<<"a[]: ";
for (i=0;i<=2;i++)
cout<<a[i]<<" ";
cout<<endl;
}
int y=5678;
main()
{
int i,p=100;
cout<<"In main():"<<endl;
cout<<"p="<<p<<endl;
cout<<"::p="<<::p<<endl;
cout<<"a[]: ";
for (i=0;i<=2;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"y="<<y<<endl;
show();
}
#include <iostream.h>
count1()
{
static int i=0;
return(++i);
}
count2()
{
int i=0;
return(++i);
}
main()
{
int i;
cout<<"count1():"<<endl;
for (i=1;i<=12;i++)
cout<<count1()<<" ";
cout<<endl;
cout<<"count2():"<<endl;
for (i=1;i<=12;i++)
cout<<count2()<<" ";
cout<<endl;
}
#include<iostream.h>
main()
{
int i,s=0;
extern int fact(int x);
for (i=2;i<=6;i=i+2)
s+=fact(i);
cout<<"s="<<s<<endl;
}
extern int fact(int x)
{
int i,t=1;
if(x==0) return(1);
for(i=1;i<=x;i++)
t*=i;
return(t);
}
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
main() {
FILE *fp1;
char str[80];
cout<<"Inupt 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);
}
#include<iostream.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
void main(void) {
char ch;
FILE *fp1;
if ((fp1=fopen("d.dat","w"))==NULL) {
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
cout<<"char:"<<endl;
cin>>ch;
while (ch!='*') {
fputc(ch,fp1);
cin>>ch;
}
fclose(fp1);
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
while ((ch=fgetc(fp1))!=EOF)
cout<<ch<<" ";
cout<<endl;
fclose(fp1);
}
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
main() {
int i=0;
char p[100];
FILE *fp1;
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
for (i=1;;i++) {
cout<<i<<" string:";
cin>>p;
if (stricmp(p,"end")) {
fputs(p,fp1);
fputs("\n",fp1);
}
else
break;
}
fclose(fp1);
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
while (fgets(p,100,fp1)!=NULL)
cout<<p;
fclose(fp1);
}
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#define MAX 10
main() {
int i,n;
FILE *fp1;
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
for (i=1;i<=MAX;i++) {
n=rand();
putw(n,fp1);
cout<<n<<" ";
}
cout<<endl<<"--------------------"<<endl;
fclose(fp1);
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
while ((n=getw(fp1))!=EOF)
cout<<n<<" ";
fclose(fp1);
}
#include<iostream.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
#include<stdlib.h>
#define MAX 3
main() {
struct student {
int num;
char name[10];
float grade;
};
student st[3];
int i;
FILE *fp1;
if ((fp1=fopen("d.dat","w"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
cout<<" num name grade"<<endl;
for (i=0;i<MAX;i++) {
cout<<i+1<<" ";
cin>>st[i].num;
cin>>st[i].name;
cin>>st[i].grade;
fprintf(fp1,"%d %s %f\n",st[i].num,st[i].name,st[i].grade);
}
fclose(fp1);
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
student t;
while ((fscanf(fp1, "%d %s %f",&t.num,t.name,&t.grade))!=EOF) {
cout<<t.num<<" ";
cout<<t.name<<" ";
cout<<t.grade<<endl;
}
fclose(fp1);
}
#include<iostream.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
int main(void)
{
FILE *fpd,*fpw;
unsigned char dw;
int i=0;
if((fpd=fopen("C:\WINDOWS\Calc.exe", "rb"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
if((fpw=fopen("test.exe", "wb+"))==NULL)
{
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
while(!feof(fpd)) {
fread(&dw, 1, 1, fpd);
fwrite(&dw, 1, 1, fpw);
}
fclose(fpd);
fclose(fpw);
cout<<"1 Run C:\WINDOWS\Calc.exe"<<endl;
system("C:\WINDOWS\Calc.exe");
cout<<"-------------------"<<endl;
cout<<"2 Run test.exe!"<<endl;
system("test.exe");
}
#include<iostream.h>
#include <process.h>
#include<stdio.h>
#include<conio.h>
void main(void) {
int i;
char ch;
FILE *fp1;
if ((fp1=fopen("d.dat","w"))==NULL) {
cout<<"\nCould not open the file."<<endl;
cout<<"Exiting program."<<endl;
exit(1);
}
cout<<"char:";
cin>>ch;
while (ch!='*') {
fputc(ch,fp1);
cin>>ch;
}
cout<<"--------------------"<<endl;
fclose(fp1);
if ((fp1=fopen("d.dat","r"))==NULL)
{
cout<<"\nCould n