330 likes | 443 Vues
设计一个程序,显示命令行参数. #include<iostream.h> //ECH0.CPP main(int argc,char *argv[]) { for(int i=0;i<argc;i++) cout<<argv[i]<<'t'; cout<<'<br>'; }. 设计一个程序,输出环境变量. #include<iostream.h> void main(int argc,char *argv[],char *eve[]) { int i; for(i=0;eve[i];i++)
E N D
设计一个程序,显示命令行参数 #include<iostream.h> //ECH0.CPP main(int argc,char *argv[]) { for(int i=0;i<argc;i++) cout<<argv[i]<<'\t'; cout<<'\n'; }
设计一个程序,输出环境变量 #include<iostream.h> void main(int argc,char *argv[],char *eve[]) { int i; for(i=0;eve[i];i++) cout<<"eve["<<i<<"]="<<eve[i]<<'\n'; }
设计一个程序,完成两个操作数的加、减、乘、除四则运算设计一个程序,完成两个操作数的加、减、乘、除四则运算 #include <iostream.h> #include <stdlib.h> float add(float x,float y) { cout<<x<<"+"<<y<<"="; return x+y; } float sub (float x,float y) { cout<<x<<"-"<<y<<"="; return x-y; }
float mul(float x,float y) { cout<<"*"<<y<<"="; return x*y; } float dev(float x,float y) { cout<<"/"<<y<<"="; return x/y; }
void main(void) { float a,b; char c; float(*p)(float,float); cout<<"输入数据格式:操作数1运算符操作数2\n"; cin>>a>>c>>b; switch(c){ case'+':p=add;break; case'-':p=sub;break; case'*':p=mul;break; case'/':p=dev;break; default:cout<<"输入数据的格式不对!\n";exit(1); } cout<<p(a,b)<<'\n'; }
求已知数组各元素和、最大元素、下标为奇数元素之和及各元素平均值求已知数组各元素和、最大元素、下标为奇数元素之和及各元素平均值 #include<iostream.h> #include<stdlib.h> float sum(float*p,int n) { float sum=0; for(int i=0;i<n;i++)sum+=*p++; return sum; } float max(float *p,int n) { float m=* p++; for(int i=1;i<n;i++){ if(* p>m) m=* p; p++; } return m; }
float oddsum(float*p,int n) { float sum=0; for(int i=1;i<n;i+=2){ sum+=*p++; p++; } return sum; } float ave(float *p,int n) { return sum(p,n)/n;} void process(float *p,int n,float(*fp)(float *,int )) { cout<<fp(p,n)<<'\n';}
void main(void) { float x[]={2.3,4.5,7.8,345.6,56.9,77,3.34,7.87,200}; int n=sizeof(x)/sizeof(float); cout<<n<<"个元素之和是:"; process(x,n,max); cout<<"奇数下标元素之和:"; process(x,n,oddsum); cout<<n<<"个元素的平均值是:"; process(x,n,ave); }
用梯形法求定积分的近似值 #include<iostream.h> #include<math.h> float f1(float x) {return (x+x*x)/(1+sin(x)+x*x);} float f2(float x) {return (1+x*x);} float f3(float x) {return x/(1+x*x);}
float jifen(float(*f)(float),float a,float b,int n) { float y,h; int i; y=(f(a)+f(b))/2; h=(b-a)/n; for(i=1;i<n;i++)y+=f(a+i*h); return(y*h); } main() { cout<<"第一个积分值:"<<jifen(f1,1,3,1000)<<'\n'; cout<<"第二个积分值:"<<jifen(f2,2,4,1000)<<'\n'; cout<<"第三个积分值:"<<jifen(f3,1,2.5,1000)<<'\n'; }
设计一个简单的计算器程序 #include<iostream.h> #include<stdlib.h> int add(int x,int y) { cout<<x<<"+"<<y<<"="; return x+y; } int sub(int x,int y) { cout<<x<<"-"<<y<<"="; return x-y; }
int mul(int x,int y) { cout<<x<<"*"<<y<<"="; return x*y; } int divi(int x,int y) { cout<<x<<"/"<<y<<"="; return x/y; } void OutResult(int a,int b,int(*f)(int ,int)) {cout<<(*f)(a,b)<<endl;}
void main(int argc,char *argv[]) { int(*fun[4])(int,int)={add,sub,mul,divi}; int i; if(argc!=4){ cout<<"Usage:command op1 op op2<CR>example:\n"; cout<<"EX 34*25<CR>\n"; exit(1); } char op=argv[2][0]; switch(op){ case'+':i=0;break; case'-':i=1;break; case'*':i=2;break; case'/':i=3;break; default: cout<<"Operater is error!\n"; exit(1); } OutResult(atoi(argv[1]),atoi(argv[3]),fun[i]); }
设计一程序,实现动态内存空间的分配 #include<iostream.h> void main(void) { int *p1; float *fp1,(*p)[10]; char *cp1; p1=new int; fp1=new float(2.5); p=(float(*)[10])new float[10]; cp1=new char; *cp1='A'; for(int i=0;i<10;i++)(*p)[i]=(float)i; for(i=0;i<10;i++){ cout<<"(*p)["<<i<<"]="<<(*p)[i]<<'\t'; if((i+1)%5==0)cout<<'\n'; }
*p1=25; cout<<"*p1="<<*p1<<'\n'; cout<<"*fp1="<<*fp1<<'\n'; cout<<"*cp="<<*cp1<<'\n'; delete p1; delete fp1; delete cp1; delete[10]p; }
引用类型变量的使用 #include<iostream.h> void main(void) { int i,&refi=i; i=100;refi+=100; cout<<"refi="<<refi<<'\n'; refi*=2; cout<<"i="<<i<<'\n'; }
设计一个程序,输入两个整数,使之交换后输出设计一个程序,输入两个整数,使之交换后输出 #include<iostream.h> void swap1(int *p1,int *p2) { int t; t=*p1;*p1=*p2;*p2=t; } void swap2(int &p1,int &p2) { int t; t=p1;p1=p2;p2=t; }
void main(void) { int x,y; int a,b; cout<<"Input values of x and y:"; cin>>x>>y; swap1(&x,&y); cout<<"Input values of a and b:"; cin>>a>>b; swap2(a,b); cout<<"x="<<x<<"y="<<y<<'\n'; cout<<"a="<<a<<"b="<<b<<'\n'; }
函数返回值为引用类型 #include<iostream.h> int &f1(void) { static int count; return ++count; } int index; int &f2(void) { return index;}
void main(void) { f1()=100; for(int i=0;i<5;i++)cout<<f1()<<" "; cout<<'\n'; int n; f2()=100; n=f2(); cout<<"n="<<n<<'\n'; f2()=200; cout<<"index="<<index<<'\n'; }
#include <iostream.h> int ComInt(void*a,void*b) { return*(int *)a-*(int*)b;} int ComFloat(void*a,void*b) { return *(float*)a-*(float *)b>0?1:-1;}
void Sort(void*v,int n,int size,int(*Com)(void*,void*)){ int i,j,k; char*p,*q,t; for(i=0;i<n-1;i++){ p=(char*)v+i*size; for(j=i+1;j<n;j++){ q=(char*)v+j*size; if(Com(p,q)>0) for(k=0;k<size;k++){ t=p[k];p[k]=q[k];q[k]=t; } } } }
void main(void) { int vi[]={23,44,32,66,15,25}; float vf[]={15.4,34.789,55.4,5,6,18,3,99.8,67.34}; cout<<"排序前的整数为:\n"; for(int i=0;i<sizeof(vi)/sizeof(int);i++)cout<<vi[i]<<'\t'; cout<<'\n'; cout<<"排序后的整数为:\n"; for(i=0;i<sizeof(vi)/sizeof(float);i++) cout<<vi[i]<<'\t'; cout<<'\n'; cout<<"排序前的实数为:\n"; for( i=0;i<sizeof(vf)/sizeof(float);i++) cout<<vf[i]<<'\t'; cout<<'\n'; Sort(vf,sizeof(vf)/sizeof(float),sizeof(float),ComFloat); cout<<"排序后的实数为:\n"; for(i=0;i<sizeof(vf)/sizeof(float);i++)cout<<vf[i]<<'\t'; cout<<'\n'; }