C程序代码大全

发布时间:2020-07-16 14:12:57   来源:文档文库   
字号:

<

cout<<------------------<

cout<

#include <> 1 c<

int a[10]; double xy[10];

cout<

else if (score>=80) cout<=70) cout<.<=60) cout<

#include <> main() {

int n;

cout<<

=; cin>>n;

if (n>=0 && n<=100 &&n%2==0) cout<<

=<

cout< main() {

int a,b,Max; .10

for(int i=1;i<=10;i++) cout<

for(int j=10;j>=1;j--) cout<

for(int k=1;k<=10;k=k+2) cout<

.

for(float x=0;x<=;x=x+ cout<

for(float x1=0;x1<=+2;x1=x1+ cout< cout<

for(int n=1;n<=100;n++) s=s+n;

cout<

#include<> main() {

.+100

int s=0,n=1; while(n<=100) { s=s+n; n++; }

cout<

int s=0,n=0; do { n++; s+=n; }while(n<100);

cout<

cin>>st[i].name; cin>>st[i].maths; cin>>st[i].physics; cin>>st[i].chemistry; } otal=st[i].maths+st[i].physics+st[i].chemistry; um<<'\t';

cout<

#include<> main() {

ame=<<(*p).name<

cout<<(*p).sex=<<(*p).sex<name=<name<

cout<sex=<sex<age=<age<

cout<>(*p).sex; cout<>(*p).age; ame=<<(*p).name<

cout<<(*p).sex=<<(*p).sex<

cout<

cout<<----------------<

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<< ;

cout< cout<

#include<> otal

swap_Rec(&a[i],&a[j]); <<\ <

#include<>

void disp(void); <

#include<> <

cout< exit(1); <

cout<

cout<

cout<

cout< exit(1); <

cout<

cout<

cout<

cout<>st[i].name; cin>>st[i].grade;

fprintf(fp1,%d %s %f\n,st[i].num,st[i].name,st[i].grade);

} fclose(fp1); <<

cout<

cout<

cout<

cout<<

cout<

cout<

cout<

cout<<

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 ); } }

#include<> #include<> <

for (i=1; i<=MAX; i++) { cin>>x; (x); } <for (i=1; i<=MAX; i++) { cin>>x; (x); } <

~stack(void) << endl; << endl; }

<< endl; isp_count(); delete p; (); }

#include<> << endl; }

~ex_class() { cout << The Object destroyed. <

void set_value(int n);

void show_val(char *name); } ; <

#include<> um :; cout<

cout<<<

um=<

public: um=;

cout<<<

virtual void aFn2(void) { cout<

<

<

<

void aFn2(void) {

cout<

<

void aFn2(void){

cout<erived class.<

cout<

cout<

#include<> #include <> ;

int n; 1.7320534f << endl; }

~stack(void) {

cout << Stack Destroyed. << endl; }

void push(T); T pop(void); }; << endl;

return; }

stck[tos++] = i; }

template T stack::pop(void) {

if(tos==0) {

cout << Stack underflow. << endl; return 0; }

return stck[--tos]; } irst;

cout<<,<<(*theIterator).second<< ; }

cout<

cout<<,<<(*theIterator).second<< ; }

cout<

cout<<,<<(*it).second<< ; } cout<

econd << ;

else cout << [err] ; }

cout << endl; } }

#include #include #include using namespace std; econd; cout<<\ <<(*theIterator).first< cout<<\ <<(*i).first< <<(*it).first< <<(*i).first<

#include #include #include <> using namespace std; #define ARRAY_SIZE 3 .入栈 for (i=0;i<10;i=i+2) (i); if (!()) { cout << ()=20; << endl;

()=20; } //弹出栈中所有的数据并显示 cout<

while (!()) { cout<<()<< ; (); } cout<

#include #include

#include using namespace std;

//创建一个list容器的实例LISTINT,其存放int型数据 typedef list LISTINT;

void main(void) {

//LISTINT创建一个名为listOnelist对象 LISTINT listOne;

//指定i为迭代器变量 LISTINT::iterator i;

LISTINT::reverse_iterator ir; //从前面向listOne容器中添加数据 (2); (1); //从后面向listOne容器中添加数据 (3); (4); //从前向后显示listOne中的数据 for (i = (); i != (); ++i) cout << *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 std; //利用类模板生成类实例 typedef vector < int > Int; 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}; 潣瑵?硜嵛尺※

for (int i=0;i<5;i++) cout<

cout<; cout<

cout<

// accumulate算法对于vector容器的计算 //--------------------------------------------- //声明intvector容器和迭代器ii Int intvector; Int::iterator ii; //intvector容器中插入元素 for (i=1; i<=5; i++) { (i); }; //显示intvector容器中的元素值和累加结果 cout << intvector: <ii) cout<<(*ii)<< ; cout<

cout<

// accumulate算法对于list容器的计算 //--------------------------------------------- //声明list容器对象和迭代器 LISTINT::iterator iL; LISTINT list1; //list1容器对象中插入元素并显示 (1); (3); (5); (2); (6); //显示list1容器的元素值和累加结果 cout << list1: <

cout<umulate(),(),0)=; cout<算法对于set容器的计算 //--------------------------------------------- //声明set容器对象和迭代器 SET_INT set1;

SET_INT::iterator si;

//set1容器中插入元素 (5); (20); (10); (15); (25); //显示set1容器的元素值和累加结果 cout <

cout<ccumulate(),(),100)=; cout<

#include #include #include #include #include #define size 10

using namespace std; //产生指定范围的整数随机数 int getrand(int min,int max) { int m;

m=(max-min);

m=min+double(rand())/RAND_MAX*m ; return m; } //利用类模板生成实例 typedef vector < int > Int; typedef list LISTINT; typedef set SET_INT; //main()函数中测试accumulate算法 void main () {

//-------------------------------------------- // count算法对于普通数组的计算 //--------------------------------------------- int x[size]; 潣瑵?硜嵛尺※

for (int i=0;i

cout<

cout<dl; cout<算法对于vector容器的计算 //---------------------------------------------

//声明intvector容器和迭代器ii Int intvector; Int::iterator ii; //intvector容器中插入元素 for (i=1; i

//显示intvector容器中的元素值和统计结果 cout << intvector: ; for (ii=();ii !=();++ii) cout<<(*ii)<< ; cout<

cout<count(),(),4)<

//-------------------------------------------- // count算法对于list容器的计算 //--------------------------------------------- //声明list容器对象和迭代器 LISTINT::iterator iL; LISTINT list1; //list1容器对象中插入元素并显示 for (i=1; i显示list1容器的元素值和统计结果 cout << list1: ;

for (iL=();iL !=();++iL) cout<<(*iL)<< ; cout<

cout<

//-------------------------------------------- // count算法对于set容器的计算 //--------------------------------------------- //声明set容器对象和迭代器 SET_INT set1;

SET_INT::iterator si; //set1容器中插入元素 for (i=1; i显示set1容器的元素值和统计结果 cout <

for (si=();si !=();++si) cout<<(*si)<< ; cout<

cout<

#include #include #include #include using namespace std;

//如果字符串以'S'开头,则返回true int MatchFirstChar( const string& str) {

string s(S) ; return s == (0,1) ; } //测试count_if算法 void main() {

const int VECTOR_SIZE = 8 ; //生成成员类型为stringsvector容器类 typedef vector StringVector ; //定义迭代器类型 typedef StringVector::iterator StringVectorIt ; //声明vector容器的对象 StringVector NamesVect(VECTOR_SIZE) ; //声明迭代器 StringVectorIt start, end, it ; int result = 0 ; // 存放统计数据 //初始化vector容器NamesVect NamesVect[0] = She ; NamesVect[1] = Sells ; NamesVect[2] = Sea ; NamesVect[3] = Shells ; NamesVect[4] = y ; NamesVect[5] = he ; NamesVect[6] = Sea ; NamesVect[7] = Shore ; //设置容器的起始位置和终止位置 start = () ; end = () ; //显示NamesVect容器的元素 cout << NamesVect: ; for(it = start; it != end; it++) cout << *it << ; cout <统计并显示NamesVect容器的所有元素中以'S'字符开头的字符串 result = count_if(start, end, MatchFirstChar) ;

cout << Number of elements that start with letter \S\ = << result << endl ; //显示NamesVect容器[1,6]之间的元素 cout <

for(it =&NamesVect[1]; it != &NamesVect[7]; it++) cout << *it << ; cout <<endl ; //统计并显示NamesVect容器的所有元素中以'S'字符开头的字符串 result = count_if(&NamesVect[1], &NamesVect[7], MatchFirstChar) ;

cout << Number of elements that start with letter \S\ = << result << endl ; }

#include #include #include using namespace std; //利用类模板生成实例 typedef vector < int > Int; //显示数组 void put_array(int x[],int size) { for(int i=0;i

//显示vector容器中的元素 void put_vector(Int v) {

IntArray::iterator theIterator; for (theIterator=();theIterator!=();++theIterator){ cout<<(*theIterator)<< ; }

cout<main()函数中测试fillfill_n算法 void main () {

//-------------------------------------------- // fillfill_n算法对普通数组的计 //--------------------------------------------- int x[]={1,3,5,7,9}; 潣瑵??硜嵛?? put_array(x,5); //填数处理 fill(x+1,x+3,2);

cout <<

ill(x+1,x+3,2): <

cout <<

ill_n(x,3,8): <

//--------------------------------------------

// fillfill_n算法对于vector容器的计算 //--------------------------------------------- //声明intvector容器和迭代器ii Int intvector; //intvector容器中插入元素 for (int i=1; i<=10; i++) { (i); };

//显示intvector容器中的元素值和统计结果 cout << intvector: <填数处理 fill(),()+3,2);

cout <<

ill(),()+3,2): <

put_vector(intvector); fill_n(&intvector[5],3,8);

cout <<

ill_n(&intvector[5],3,8): <

#include #include #include

#define ARRAY_SIZE 10 using namespace std; //利用类模板生成实例 typedef vector < int > Int; //显示数组 void put_array(int x[],int size) { for(int i=0;i显示vector容器中的元素 void put_vector(Int v) {

Int::iterator theIterator; for (theIterator=();theIterator!=();++theIterator){ cout<<(*theIterator)<< ; }

cout<main()函数中测试find()算法 void main () { int i,value,*p;

//-------------------------------------------- // find()算法对于普通数组的处理 //---------------------------------------------

int x[ARRAY_SIZE]={1,3,5,7,9,2,4,6,8,10}; 潣瑵??硜嵛??

put_array(x,ARRAY_SIZE); //find()算法查找,并显示查找结果 for(i=0;i<=2;i++) { cout<>value;

p=find(x,x+ARRAY_SIZE,value); if (p != x + ARRAY_SIZE) { //查到 cout << First element that matches << value; cout<< is at location << p - x<< endl; }

else { //未查到 cout << The sequence does not contain any elements; cout<< with value << value << endl ; } }

//--------------------------------------------

// find()算法对于vector容器的处理 //--------------------------------------------- //声明intvector容器对象 Int intvector; //intvector容器中插入元素 for (i=1; i<=10; i++) { (i); }; //显示intvector容器中的元素值 cout << intvector: ; put_vector(intvector); //find()算法查找,并显示查找结果 Int::iterator pos; for (i=0;i<=2;i++) { cout<>value;

pos=find(),(),value); if (pos != ()) { //查到 cout << First element that matches << value; cout<< is at location <

else { //未查到 cout << The sequence does not contain any elements; cout<< with value << value << endl ; } } }

#include #include #include

#define ARRAY_SIZE 10 using namespace std; //利用类模板生成实例 typedef vector < int > Int; //显示数组 void put_array(int x[],int size) { for(int i=0;i显示vector容器中的元素 void put_vector(Int v) {

Int::iterator theIterator; for (theIterator=();theIterator!=();++theIterator){ cout<<(*theIterator)<< ; } } //main()函数中测试find()_end()算法 void main () {

//--------------------------------------------

// find_end()算法对普通数组的处理 //---------------------------------------------

int x[ARRAY_SIZE]={1,3,5,7,9,2,4,6,8,10}; 潣瑵??硜嵛??

put_array(x,ARRAY_SIZE); cout<算法查找,并显示查找结果 int *p=find_end(x,x+ARRAY_SIZE,&y[0],&y[2]); if (p != x + ARRAY_SIZE) { //查到 cout << The first element that matches : ; put_array(y,3);

cout<< is at location in x << p - x<< endl; }

else { //未查到 cout << The sequence does not contain any elements; cout<< with value ; put_array(&x[3],3); } //-------------------------------------------- // find_end()算法对vector容器的处理 //--------------------------------------------- //声明intvector容器对象 Int intvector; //intvector容器中插入元素 for (int i=1; i<=10; i++) { (i); }; //显示intvector容器中的元素值 cout << intvector: ; put_vector(intvector); cout<

cout << emp: ; put_vector(temp); cout<find_end()算法查找,并显示查找结果 Int::iterator pos; pos=find_end(),(),(),()); if (pos != ()) { //查到 cout << The first element that matches ; put_vector(temp);

cout<< is at location in intvector <

else { //未查到 cout << The sequence does not contain any elements; cout<< with value ;

put_vector(temp); cout<< endl ; } }

#include #include #include hm> using namespace std; //返回一个Fibonacci数,其由generate_n()算法调用 int Fibonacci1(void) {

static int r;

static int f1 = 0; static int f2 = 1; r = f1 + f2 ; f1 = f2 ; f2 = r ; return f1 ; }

//返回一个Fibonacci数,其由generate()算法调用 int Fibonacci2(void) {

static int r;

static int f1 = 0; static int f2 = 1; r = f1 + f2 ; f1 = f2 ; f2 = r ; return f1 ; }

//定义整型数的vector容器类 typedef vector IntVector ; //显示vector容器中的元素 void put_vector(IntVector v,char *name) {

IntVector::iterator theIterator; cout<

for (theIterator=();theIterator!=();++theIterator){ cout<<(*theIterator)<< ; }

cout<测试generate()generate_n()算法 void main() {

const int VECTOR_SIZE = 15 ; //定义迭代器类 typedef IntVector::iterator IntVectorIt ; //声明vector容器对象 IntVector Numbers1(VECTOR_SIZE),Numbers2(VECTOR_SIZE); int i ; //初始化vector容器对象 for(i = 0; i < VECTOR_SIZE; i++)

Numbers1[i] = i ; //显示vector容器对象的元素 cout << Before calling generate_n: << endl ; put_vector(Numbers1,Numbers1); //利用generate_n算法用Fibonacci 数填充vector容器 generate_n(), VECTOR_SIZE, Fibonacci1) ; //显示vector容器对象的元素 cout << After calling generate_n: << endl ; put_vector(Numbers1,Numbers1); //利用generate算法用Fibonacci 数填充vector容器 generate(),(), Fibonacci2) ; //显示vector容器对象的元素 cout << After calling generate: << endl ; put_vector(Numbers2,Numbers2); }

#include #include #include using namespace std; //利用类模板生成实例 typedef vector < int > Int; //显示数组 void put_array(int x[],int size) { for(int i=0;i显示vector容器中的元素 void put_vector(Int v) {

Int::iterator theIterator; for (theIterator=();theIterator!=();++theIterator){ cout<<(*theIterator)<< ; } cout<main()函数中测试reverse()reverse_copy()算法 void main () {

//--------------------------------------------

// reverse()reverse_copy()算法对普通数组处理 //--------------------------------------------- int x[]={1,3,5,7,9}; 潣瑵?硜嵛尺※ put_array(x,5); //reverse()反转x数组并显示 reverse(x,x+5); 潣瑵?硜嵛尺※

put_array(x,5); int y[]={2,4,6,8,10}; cout<反转y数组的部分元素并拷贝到x数组第2个元素位置 reverse_copy(y+1,y+3,x+1); 潣瑵?硜嵛尺※ put_array(x,5); cout<

//--------------------------------------------

// reverse()reverse_copy()算法对vector容器的处理 //--------------------------------------------- //声明intvector容器和迭代器ii Int intvector; //intvector容器中插入元素 for (int i=1; i<=10; i++) { (i); }; //显示intvector容器中的元素值 cout << intvector: <对于vector容器的处理 reverse(),());

cout << intvector: <对于vector容器的处理 Int temp(5);

reverse_copy()+2,()+7,()); cout << emp: <

#include #include #include #include <>

#define ARRAY_SIZE 15 using namespace std; //定义整型数的vector容器类 typedef vector IntVector ; //显示数组 void put_array(int x[],int size) { for(int i=0;i显示vector容器中的元素 void put_vector(IntVector v,char *name) {

IntVector::iterator theIterator; cout<

for (theIterator=();theIterator!=();++theIterator){ cout<<(*theIterator)<< ; }

cout<产生指定范围的整数随机数 int getrand(int min,int max) { int m;

m=(max-min);

m=min+double(rand())/RAND_MAX*m ; return m; } //main()函数中测试sort()partial_sort()算法 void main () {

int i;

//--------------------------------------------

// sort()partial_sort()算法对普通数组处理 //--------------------------------------------- //sort()算法处理数组,并显示 int x[ARRAY_SIZE];

for (i=0;i1,20); }

潣瑵?硜嵛尺※

put_array(x,ARRAY_SIZE); sort(x,x+ARRAY_SIZE);

cout<算法对于数组进行处理 int y[ARRAY_SIZE];

for (i=0;i30) ; }

cout<

put_array(y,ARRAY_SIZE);

partial_sort(y+2,y+7,y+ARRAY_SIZE);

cout<

// sort()partial_sort()算法对vector容器的处理 //--------------------------------------------- IntVector Numbers1,Numbers2; for(i=0;i<15;i++) { (getrand(1,30)); (getrand(1,30)); } put_vector(Numbers1,Numbers1); put_vector(Numbers2,Numbers2); //sort()算法处理并显示 sort(),()); cout<算法处理并显示 partial_sort()+2,()+7,());

cout<

#include #include #include <> #include <> #define ARRAY_SIZE 15 using namespace std; //显示数组 void put_array(int x[],int size) { for(int i=0;i产生指定范围的整数随机数 int getrand(int min,int max) { int m; m=(max-min);

m=min+double(rand())/RAND_MAX*m ; return m; }

//main()函数中测试max_element() min_element()算法 void main () {

//声明变量和数组 int i;

int x[ARRAY_SIZE]; //1100的随机数初始化数组,并显示 srand( (unsigned)time( NULL ) ); for (i=0;i

潣瑵?硜嵛尺※

put_array(x,ARRAY_SIZE); //对数组x使用max_element()算法,并显示 int *pMax=max_element(x,x+ARRAY_SIZE); cout< cout<对数组x使用min_element()算法,并显示 int *pMin=min_element(x,x+ARRAY_SIZE); cout<}

本文来源:https://www.2haoxitong.net/k/doc/c5a0ebfdd8ef5ef7ba0d4a7302768e9951e76e90.html

《C程序代码大全.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式