
C/C++
wenjiashun
这个作者很懒,什么都没留下…
展开
-
类模版
//Stack.h#ifndef _STACK_H_#define _STACK_H_templateclass Stack{private: int size;//栈中元素个数 int top;//栈顶元素位置 T *stackPtr;public: Stack(int = 10); ~Stack() { delete [] stackPtr; } bool原创 2014-09-23 10:08:13 · 384 阅读 · 0 评论 -
函数模版
#include using namespace std;template //等价于template void printArray(const T* tempArray,int count){ for(int i=0;i<count;i++) { cout << " " << tempArray[i]; if((i+1)%15==0) cout << endl; }原创 2014-09-23 10:05:40 · 373 阅读 · 0 评论 -
70_可变数目变元
//_70_可变数目变元//_70_main.cpp//感觉无意义原创 2014-08-15 09:01:40 · 426 阅读 · 0 评论 -
69_伪随机数的生成
//_69_伪随机数的生成//_69_main.cpp//用系统时间通过使用srand()和rand()函数随机的初始化rand()函数//两个函数包含在中#include #include #include int main(){ long time1 = time(NULL);//返回系统当前时间 printf("%ld\n",time1); int time2 =原创 2014-08-15 08:58:49 · 442 阅读 · 0 评论 -
68_排序函数
//_68_排序函数//_68_main.cpp#include #include int num[12]={14,5,9,7,6,0,91,4,1,3,2,8};int comp(const void *,const void *);int main(){ printf("Original array: "); for(int i=0;i<12;i++) printf原创 2014-08-15 08:58:40 · 374 阅读 · 0 评论 -
67_跳转函数
//_67_跳转函数//_67_main.cpp#include #include #include jmp_buf ebuf;//类型在中定义void fun();int main(){ int i; printf("1 "); i = setjmp(ebuf);//返回值0; if(i==0) { fun();//为什么下一句不会被打印呢 printf(原创 2014-08-15 08:58:05 · 407 阅读 · 0 评论 -
66_查找函数
//_66_查找函数//_66_main.cpp#include #include #include char *alpha = "abcdefghijklmnopqrstuvwxyz";int comp(const void *ch,const void *s);int main(){ printf("Enter a character: "); char ch=get原创 2014-08-15 08:57:10 · 321 阅读 · 0 评论 -
65_转换函数
//_65_转换函数//_65_main.cpp/*在头文件中将str指向的串转换成双精度浮点值、整型值、长整型值串中必须含有合法的xxxxxx、xxx、xxxx,一一对应double atof(const char *str);int atoi(const char *str);long int atol(const char *str);*/#include #incl原创 2014-08-15 08:56:25 · 398 阅读 · 0 评论 -
64_常用时间函数
//_64_常用时间函数//_64_main.cpp#include #include #include int main(){ struct tm *local; time_t tm;//定义time_t类型的变量 tm = time(NULL); local = localtime(&tm); printf("Local time and date: %s\n",as原创 2014-08-15 08:55:46 · 402 阅读 · 0 评论 -
62_综合实例
//_62_综合实例//_62_main.cpp//简单的通讯录程序#include #include #define MAX 100struct adress{ char name[20]; char street[40]; char city[10]; char state[4]; unsigned long zip;}addr_list[MAX];void原创 2014-08-15 08:53:32 · 449 阅读 · 0 评论 -
63_动态分配函数
//_63_动态分配函数//_63_main.cpp//void *malloc(size_t size)//向系统申请内存空间//void free(void *ptr)//释放内存空间#include #include #define NUMBER 5int main(){ //定义一个字符型的指针数组 char *str[NUMBER]; int t; //为数组原创 2014-08-15 08:53:02 · 415 阅读 · 0 评论 -
61_错误处理
//_61_错误处理//_61_main.cpp#include #include #define TAB_NUM 8#define IN 0#define OUT 1void error(int e){ if(e==IN) printf("输入错误:\n"); else printf("输出错误:\n"); //exit(1);//跳出程序}//为了使用该原创 2014-08-15 08:52:51 · 471 阅读 · 0 评论 -
60_随机存取
//_60_随机存取//_60_main.cpp//原点origin的宏://SEEK_SET 0//SEEK_CUR 1//SEEK_END 2//int seek(FILE *fp,long int numberbytes,int origin)//以原点为基点,向前移动numberbytes个字节,#include #include int main(int a原创 2014-08-15 08:52:06 · 392 阅读 · 0 评论 -
59_fprintf()和fscanf()
//_59_fprintf()和fscanf()//_59_main.cpp#include #include #include int main(){ FILE *fp; char str[80],str1[80]; int i,j; if((fp=fopen("text.txt","w"))==NULL)//以写入的方式打开 { printf("Can not o原创 2014-08-15 08:51:31 · 338 阅读 · 0 评论 -
58_fread()和fwrite()
//_58_fread()和fwrite()//_58_main.cpp//用于向文件中读写长于一个字节的数据类型#include #include int main(){ FILE *fp; int i = 156; long l = 9701076l; double d = 3.456; if((fp=fopen("test.txt","w"))==NULL) {原创 2014-08-15 08:51:00 · 376 阅读 · 0 评论 -
57_函数rewind()
//_57_函数rewind()//_57_main.cpp//void rewind(FILE *fp)重置文件位置,将其置到函数变元所指定的文件的开头#include #include #include int main(){ char str[80]; FILE *fp;//定义一个文件类型的指针 //以写的方式打开文件file if((fp=fopen("fil原创 2014-08-15 08:50:25 · 524 阅读 · 0 评论 -
54_格式化输入函数
//_54_格式化输入函数//_54_main.cpp#include #include int main(){ int i,j,k; char str[80]; char *p; //输入的数分别以十进制、八进制、和十六进制读入程序 scanf("%d %o %x",&i,&j,&k); printf("%d,%d,%d\n\n",i,j,k);//查看实际输入的数据,原创 2014-08-15 08:48:34 · 378 阅读 · 0 评论 -
56_fputc()和fgetc()
//_56_fputc()和fgetc()//_56_main.cpp#include #include #include #include int main(){ FILE *fp;//文件指针变量 char str[100]; fp=fopen("file.txt","w");//!!!!!!!!!!是w不是r了记得 if(fp==NULL) { printf(原创 2014-08-15 08:48:00 · 345 阅读 · 0 评论 -
52_读写字符串
//_52_读写字符串//_52_main.cpp/*用于读写字符串的函数是gets()和puts(),这两个函数允许程序从控制台 读写字符串*///程序原题:让用户输入一个字,然后在内部数据库中查找它,如果发生匹配//程序便打印出该字的意义,否自,指出该字不在字典中//A simple dictionary#include #include #include #in原创 2014-08-15 08:47:27 · 417 阅读 · 0 评论 -
55_打开和关闭文件
//_55_打开和关闭文件//_55_main.cpp//fopen(),fclose()#include #include int main(){ //定义一个文件指针 FILE *fp; char ch,filename[10]; printf("Please input the name of file:"); scanf("%s",filename);//输入字原创 2014-08-15 08:47:22 · 437 阅读 · 0 评论 -
51_读写字符
//_51_读写字符//_51_main.cpp//getchar()从键盘读入一个字符、//putchar()向显示屏显示一个字符/*getchar()的原始形式中,输入先被缓冲,直到键入回车时才返回*/#include #include #include int main(){ char ch; printf("Please enter some text(in原创 2014-08-15 08:46:47 · 347 阅读 · 0 评论 -
50_枚举类型
//_50_枚举类型//_50_main.cpp//枚举符号本质上是整数值,而不是字符串//实例:从多种不同颜色的铅笔中抽出三根,并输出颜色#include #include int main(){ //默认blue=0,red=1,yellow=2。。。。black=4; enum color{blue,red,yellow,purple,black}; int to原创 2014-08-15 08:46:10 · 328 阅读 · 0 评论 -
53_格式化输出函数
//_53_格式化输出函数//_53_main.cpp#include #include int main(){ unsigned number; double item = 1.23456; for(number=8;number<16;number++) { printf("%3d ",number); printf("%3o ",number);//以八进制格式原创 2014-08-15 08:46:06 · 370 阅读 · 0 评论 -
49_共用体变量
//_49_共用体变量//_49_main.cpp//需要将集中不同类型的变量存放在同一段内存单元中,//它们在内存中所占的字节数并不相同,但是都从同一地址开始存放//这几个变量是相互覆盖的,此时,便需用到共用体//共用体变量所占用的内存长度等于最长的成员的长度#include #include union data{ int a; float b; double c原创 2014-08-12 19:43:57 · 616 阅读 · 0 评论 -
47_结构体指针变量
//_47_结构体指针变量//_47_main.cpp/*先定义结构体再定义指向结构体类型变量的指针变量*/#include #include #include int main(){ struct student//定义结构体类型 { long num; char name[30]; char sex[10]; float score; }; s原创 2014-08-12 19:42:38 · 321 阅读 · 0 评论 -
46_结构体数组
//_46_结构体数组//_46_main.cpp//原题:编写一个函数output,打印出一个学生的成绩数组,该书组中国有三个学生//的数据记录,每个记录包括number、name、score[3],用main函数输入这些//记录,用output输出记录//输出学生成绩并显示#include #include struct student{ char number[6];原创 2014-08-12 19:42:05 · 377 阅读 · 0 评论 -
48_结构体指针数组
//_48_结构体指针数组//_48_main.cpp//定义一个结构体指针数组,其数组名是数组的首地址,定义结构体类型的指针//既可以指向数组的元素,也可以指向数组#include #include //定义一个全局结构体struct student{ long number; char name[20]; char sex; int age;};//声明结构原创 2014-08-12 19:41:20 · 512 阅读 · 0 评论 -
45_结构体变量
//_45_结构体变量//_45_main.cpp//实例:定义一个结构体变量,(年月日),计算该日是本年中的第几天#include #include struct{ int year; int month; int day;}data;//定义一个结构并声明对象为dataint main(){ int days; printf("请输入日期(年、月、日):")原创 2014-08-12 19:41:07 · 350 阅读 · 0 评论 -
44_综合实例
//_44_综合实例//_44_main.cpp//实例:/*一个班级有四个学生,共学习5门功课要求编写程序完成下面的三个功能1、求出5门功课的平均分2、找出两门以上功课不及格的学生,输出她们的学号和全部课程成绩以及平均成绩3、找出平均成绩在90分以上或者课程成绩在85分以上的学生*/#include #include int main(){ char course[原创 2014-08-12 19:40:32 · 403 阅读 · 0 评论 -
42_二维指针
//_42_二维指针//_42_main.cpp//用指向指针的指针变量访问一维和二维数组//区分指向指针的指针和指向某个类型的指针两者之间的区别#include #include int main(){ int a[10],b[3][4]; int *p1,*p2,**p3;//p3是指向指针的指针变量 int i,j; printf("请输入一维数组(10个元素):\原创 2014-08-12 19:38:50 · 383 阅读 · 0 评论 -
41_指针数组
//_41_指针数组//_41_main.cpp//实例:在一个已经排好序的字符串数组中,插入一个键盘输入的字符串,//使其继续保持有序//区分char *point[10]和char (*point)[10]的区别#include #include #include int main(){ //声明子函数 int binary(char *ptr[],char *st原创 2014-08-12 19:37:58 · 429 阅读 · 0 评论 -
43_指针的初始化
//_43_指针的初始化//_43_main.cpp//对于当前没有指向合法的内存位置的指针,为其赋值为零#include #include #include int search(char *p[],char *name);//给字符型的指针数组赋初值char *names[] = { "Herb","Rex","Dennis","John",NULL};//NULL指原创 2014-08-12 19:37:32 · 371 阅读 · 0 评论 -
40_函数指针
//_40_函数指针//_40_main.cpp//实例:比较用户输入的两个串#include #include #include void check(char *a,char *b,int(*cmp)(const char *,const char *));int main(){ char s1[80]; char s2[80]; //p是一个指针变量,所指向的函原创 2014-08-10 17:00:54 · 458 阅读 · 0 评论 -
39_字符串指针
//_39_字符串指针//_39_main.cpp//将字符串a复制到字符串b#include #include #include int main(){ char a[]="I am a student."; char b[20]; char *p1,*p2; p1=a;//将数组a的首地址赋给字符型指针p1 p2=&b[0]; for(;*p1!='\0';p1+原创 2014-08-10 17:00:12 · 420 阅读 · 0 评论 -
38_二维数组指针
//_38_二维数组指针//_38_main.cpp//实例:有一个班级共三名学生各学习四门课,要求计算出这个班级的额总平均分//以及第n个学生的成绩#include #include int main(){ //声明子函数 void average(float *point,int n); void search(float(*point)[4],int); //定原创 2014-08-10 16:57:54 · 410 阅读 · 0 评论 -
37_一维数组指针
//_37_一维数组指针//_37_main.cpp//将数组中的各个整数按照逆序存放#include #include void inv(int *x,int n);void inv2(int *x,int n);int main(){ int testArray[10]={1,3,5,7,9,0,24,5,6,7}; printf("原始数组是:\n"); for(原创 2014-08-10 16:55:23 · 518 阅读 · 0 评论 -
35_综合实例2
//_35_综合实例2//_35_main.cpp//这个例子是一个统计学生成绩的程序,要求输入10个学生5门功课//的成绩,分别用于子函数求出:每个学生的平均分,没门功课的平均分//找出最高分所对应的学生和功课,求出平均分方差#include #include #define M 5#define N 3float score[N][M];//存放成绩float a_st原创 2014-08-10 16:54:39 · 449 阅读 · 0 评论 -
36_变量的指针
//_36_main.cpp//_36_bianlia//输出三个整数并按大小顺序输出#include #include void swap(int *Ptr1,int *Ptr2);void exchange(int *q1,int *q2,int *q3);int main(){ int x,y,z,*p1,*p2,*p3; printf("请输入三个整数:"); s原创 2014-08-10 16:54:25 · 350 阅读 · 0 评论 -
34_综合实例
//_34_综合实例//_34_main.cpp//同函数一样,外部变量的定义和外部函数的说明不同//外部变量的定义只有一词,它的位置必须在所有函数之外//而同一文件中的外部变量的说明可以有很多次,位置实在函数之内//(那个函数要用到就在哪个函数中声明)#include #include //声明三个子函数void head1();void head2();void he原创 2014-08-10 16:53:53 · 309 阅读 · 0 评论 -
33_内部和外部函数
//_33_内部和外部函数//_33_main.cpp//内部函数:static int function(int x,int y);//内部函数只局限在所在文件中,不同文件存在同名的内部函数//互不干扰//外部函数:extern int function(int x,int y);//如果定义时忽略extern,则隐含为外部函数原创 2014-08-10 16:50:05 · 420 阅读 · 0 评论