1、C经典程序代码大全endl; cout-endl; endl; coutpi=pi r=r s=sendl;#include 1 cendl; ; int a10; double xy10; coutsizeof(str)=sizeof(str)endl; coutsizeof(a)=sizeof(a)endl; coutsizeof(xy)=sizeof(xy)endl; =80) coutYour grade is a B.=70) coutYour grade is a C.=60) coutYour grade is a D.endl; else coutYour grade is a
2、 E.endl;#include main() int n; coutn; if (n=0 & n=100 &n%2=0) coutn=nendl; else coutThe n is out of range!endl;#include main() int a,b,Max; .10 for(int i=1;i=10;i+) couti ; cout=1;j-) coutj ; coutendl; .9 for(int k=1;k=10;k=k+2) coutk ; coutendl; .Z for(char c=A;c=Z;c+) coutc; coutendl; . for(float
3、x=0;x=;x=x+ coutx ; coutendl; . for(float x1=0;x1=+2;x1=x1+ coutx1 ; coutendl; .+100 int s=0; for(int n=1;n=100;n+) s=s+n; couts=sendl;#includemain() .+100 int s=0,n=1; while(n=100) s=s+n; n+; couts=sendl; .+100 int s=0,n=0; do n+; s+=n; while(n100); couts=ssti.name; cinsti.maths; cinsti.physics; ci
4、nsti.chemistry; otal=sti.maths+sti.physics+sti.chemistry; umt; coutsti.namet; coutsti.mathst; coutsti.physicst; coutsti.chemistryt; coutsti.totalendl; #includemain() ame=(*p).nameendl; cout(*p).sex=(*p).sexendl; cout(*p).age=(*p).ageendl; coutname=nameendl; coutsex=sexendl; coutage=ageendl; ame; cou
5、t(*p).sex; cout(*p).age; ame=(*p).nameendl; cout(*p).sex=(*p).sexendl; cout(*p).age=(*p).ageendl; cout-endl; amet; coutxi.sext; coutxi.ageendl; cout-endl; ; int int_values = 51, 23, 2, 44, 45,0,11; float float_values = , , , , ; student st_arr=101,WangLin,92,102,LiPing,85,103,ZhaoMin,88; um ; coutst
6、_arri.name ; coutst_arri.gradeendl; #includeotalaj.total) swap_Rec(&ai,&aj); tstr_len(This is a test.)endl;#includevoid disp(void); endl;#includeendl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl;
7、exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.sti.name; cinsti.grade; fprintf(fp1,%d %s %fn,sti.num,sti.name,sti.grade); fclose(fp1); endl; coutExi
8、ting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.endl; exit(1); endl; coutExiting program.
9、endl; exit(1); endl; coutExiting program.endl; exit(1); */ putc( A, stdin ); if( ferror( stdin ) ) perror( Write error ); clearerr( stdin ); /* See if read causes an error. */ printf( Will input cause an error? ); c = getc( stdin ); if( ferror( stdin ) ) perror( Read error ); clearerr( stdin ); #inc
10、lude#include endl; for (i=1; ix; (x); endl; void push(float x) endl; for (i=1; ix; (x); endl; void push(float x) endl; stack(void) endl; endl; endl; isp_count(); delete p; ();#include endl; ex_class() cout The Object destroyed. endl; void set_value(int n); void show_val(char *name); ;endl;#includeum
11、 :; coutnumendl; ;um :; coutendl; um=numendl; public: um=; coutendl;how_value(*p)obj2); al=valendl; DispFirst(); ;endl; virtual void aFn2(void) coutaFn2 is in Base class.endl; endl; ;endl; endl; endl; endl; endl; void aFn2(void) coutaFn2 is in First derived class.endl; ;endl; void aFn2(void) coutaFn
12、2 is in Second derived class.endl; ; couts1: s1endl; couts2: s2endl; 10.2f10.5f; coutString: stringendl; coutp : pendl;#include#include ; int n; 1.7320534f endl; stack(void) cout Stack Destroyed. endl; void push(T); T pop(void); endl; return; stcktos+ = i;template T stack:pop(void) if(tos=0) cout St
13、ack underflow. endl; return 0; return stck-tos;irst; cout,(*theIterator).second ; coutendl; irst; cout,(*theIterator).second ; coutendl; irst; cout,(*it).second ; coutendl; econd ; else cout err ; cout endl; #include #include #include using namespace std;econd; coutt(*theIterator).firstendl; econd;
14、coutt(*i).firstendl; econd; coutt(*it).firstendl; econd; coutt(*i).firstendl; #include #include #include using namespace std;#define ARRAY_SIZE 3 .入栈 for (i=0;i10;i=i+2) (i); if (!() cout ()=20; endl; ()=20; /弹出栈中所有的数据并显示 coutstack1: ; while (!() cout() ; (); coutendl;#include #include #include usin
15、g namespace std;/创建一个list容器的实例LISTINT,其存放int型数据typedef list LISTINT;void main(void) /用LISTINT创建一个名为listOne的list对象 LISTINT listOne; /指定i为迭代器变量 LISTINT:iterator i; LISTINT:reverse_iterator ir; /从前面向listOne容器中添加数据 (2); (1); /从后面向listOne容器中添加数据 (3); (4); /从前向后显示listOne中的数据 for (i = (); i != (); +i) cout
16、 *i ; cout endl; /从后向后显示listOne中的数据 for (ir =();ir!=(); +ir) cout *ir ; cout endl; /从键盘上输入数据 for (i = (); i != (); +i) cout(*i); /从前向后显示listOne中的数据 for (i = (); i != (); +i) cout *i ; cout endl; /bidirectional迭代器不允许加减运算 / i=()+1;#include #include #include #include #include #include using namespace s
17、td;/利用类模板生成类实例typedef vector IntArray;typedef list LISTINT;typedef set SET_INT;int add(int a, int b) return a+b;/在main()函数中测试accumulate算法void main ()/-/ accumulate算法对于普通数组的计算/- int x=1,3,5,7,9; coutx:; for (int i=0;i5;i+) coutxi ; coutendl; coutaccumulate(x,x+5,0)=; coutaccumulate(x,x+5,0)endl; int
18、val=100; coutval=valendl; coutaccumulate(x,x+5,val)=; coutaccumulate(x,x+5,val)endl;/-/ accumulate算法对于vector容器的计算/- /声明intvector容器和迭代器ii IntArray intvector; IntArray:iterator ii; /向intvector容器中插入元素 for (i=1; i=5; i+) (i); ; /显示intvector容器中的元素值和累加结果 cout intvector: endl; for (ii=();ii !=();+ii) cout(
19、*ii) ; coutendl; coutaccumulate(),(),0)=; coutaccumulate(),(),0)endl;/-/ accumulate算法对于list容器的计算/- /声明list容器对象和迭代器 LISTINT:iterator iL; LISTINT list1; /向list1容器对象中插入元素并显示 (1); (3); (5); (2); (6); /显示list1容器的元素值和累加结果 cout list1: endl; for (iL=();iL !=();+iL) cout(*iL) ; coutendl; coutaccumulate(),(),
20、0)=; coutaccumulate(),(),0)endl;/-/ accumulate算法对于set容器的计算/- /声明set容器对象和迭代器 SET_INT set1; SET_INT:iterator si; /向set1容器中插入元素 (5); (20); (10); (15); (25); /显示set1容器的元素值和累加结果 cout set1: endl; for (si=();si !=();+si) cout(*si) ; coutendl; coutaccumulate(),(),0)=; coutaccumulate(),(),0)endl; coutaccumul
21、ate(),(),100)=; coutaccumulate(),(),100)endl;#include #include #include #include #include #define size 10using namespace std;/产生指定范围的整数随机数int getrand(int min,int max) int m; m=(max-min); m=min+double(rand()/RAND_MAX*m ; return m;/利用类模板生成实例typedef vector IntArray;typedef list LISTINT;typedef set SET_INT;/在main()函数中测试accumulate算法void main ()/-/ count算法对于普通数组的计算/- int xsize; coutx:; for (int i=0;isize;i+) xi=getrand(1,3); coutxi ; coutendl; coutcount(x,x+size,2)=; coutcount(x,x+size,2)endl; coutcount(x+2,x+8,2)=; coutcount(x+2,x+8,2)endl;/-/ count算法对于vector容器的计算/- /声
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1