#include int main() {float sum=0,i,n; int f=1; printf("Enter n: ")

rape a girl 2019-04-21 10:13:05
哪位大佬可以帮我改正一下
...全文
80 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复
《C语言程序设计》 实验指导书 山东水利职业学院 目 录 实验一 C语言程序初步 2 实验二 数据类型、运算符和表达式 3 实验四 循环结构(1) 6 实验五 循环结构(2) 9 实验六 函数(1) 12 实验七 函数(2) 14 实验八 数组(1) 16 实验九 数组(2) 19 实验十 指 针 (1) 22 实验十一 指针(二) 24 实验十二 有关文件的程序设计 27 实验十三 结构体和共用体 30 实验十四 位运算 32 实验十五 综合应用举例 33 实验一 C语言程序初步 [实验目的和要求] 1、 熟悉C语言运行环境。 2、 掌握C语言程序的书写格式和C语言程序的结构。 3、 掌握C语言上机步骤,了解运行一个C程序的方法。 4、 本实验可在学习完教材第一章后进行。 [实验内容和步骤] 1、输入一个简单的C语言程序:输入矩形的两条边,求矩形的面积。 [分析与讨论] 1、记下在调试过程中所发现的错误、系统给出的出错信息和对策。分析讨论对策成功或失败的原因。 2、总结C程序的结构和书写规则。 实验二 数据类型、运算符和表达式 [实验目的] 1.熟练表达式的表示方法 2.了解表达式的运行结果 3.了解C语言中数据类型的意义 [实验内容和步骤] 1. 用printf函数来输入表达式运行结果 2. 程序试图计算由键盘输入的任意两个整数的平均值: [问题讨论] 1.“=”和“==”有什么区别? 2.“&”和“&&”、“|”和“||”有什么区别? 3. 如何正确地选用数据类型? 实验三 选择结构 [实验目的] 1、了解条件与程序流程的关系 2、了解用不同的数据使程序的流程覆盖不同的语句、分支和路径。 [实验内容和步骤] 1、题目 有如下程序段: {…… if (a>1&&b= =0) x=x/a; if(a==2||x>1) x=x+1; } 为了更容易明白程序的逻辑结构,要求用流程图来加以描述。 要求增加一些输入语句和输出语句,以便使上述程序能在不同的a,b和x值下运行,并且能观察程序流程经过(覆盖)了哪些语句、哪些分支及哪些路径。 [分析讨论] (1) 用哪一组数据就可使程序中的每个处理语句都执行一次?为了找出程序中各条处理语句中的错误,应该使用什么样的数据对程序进行测试?请上机验证自己的结论。 (2) 用哪两组数据就可以使程序段中的每个分支都运行一次?这种情形与上面的讨论有何不同?如果为了找出程序中积压分支中的错误,应该使用什么样的数据对程序进行测试?请上机验证自己的结论。 (3)用哪两组数据就可以把判定框中的每个条件运算都进行一次?如果为了测试出判定条件中的错误,应使用哪些数据对程序进行测试?请上机验证自己的结论。 (4)用哪四组数据才可以把各种条件的组合都检测一遍? 如果为了测试各种条件的组合的情形,应该使用什么样的测试数据?请上机验证自己的结论。 (5)用哪四组数据才可以把起始到终止的各条路径都覆盖一次?如果为了测试出程序在不同路径下的错误,应该使用什么样的测试数据?请上机验证自己的结论。 实验四 循环结构(1) [目的和要求] 1、掌握在程序设计条件型循环结构时,如何正确地设定循环条件,以及如何控制循环的次数。 2、了解条件型循环结构的基本测试方法。 [实验内容与步骤] 1、下面是一个计算e的近似值(使误差小于给定的δ)的程序。 main() {double e=1.0,x=1.0,y,detax; int i=1; printf(“\n please input enter a error:”); scanf(“%lf”,&detax); y=1/x; while(y>=detax) { x=x*I; y=1/x; e=e+y; ++i; } printf(“%12.10lf”,e); } [分析讨论] (1)、阅读上面的程序,写出程序所依据的计算公式。 (2)、当输入的detax各是什么值时,能分别使程序按下面的要求运行: .不进入循环; .只循环一次; .只循环两次; .进入死循环(程序将永远循环下去)。 为了能知道程序循环了多少次,应该在程序中增加一条什么样的语句? (3)、原程序中while语句中的y>=detax,分别换成y>detax,y=detax,ymain() { int i ,j; long sum=0; for(i=1,i<=20,i++) for(j=1;j<=3;j++) sum=sum+(i+1)*(i+1)*(j+2) printf(“sum is :%d\n”,sum); } [ 分析讨论] (1) 将上述程序中的和分别做什么样修改,能使程序按如下要求运行: .不循环; .外循环1次,不进入内循环; .外循环,内循环各1次; .外循环1次,内循环2次; .外循环 2次, 内循环1次; .外循环19次,内循环3次; .外循环20次,内循环2次; .外循环21次,内循环3次。 (2) 试修改程序,以便能知道控制表达式 i=1;j<=20;i++ 和 j=1;j<3;j+= 各执行了多少次? (3)内循环与外循环的控制表达式进行对换,观察程序的执行结果有无变化,并且观 察对换后的外循环与内循环控制表达式执行的总次数有无变化? (4)将(3)中程序写为 main() { int i,j,k; long sum=0; for (j=1.j<=3.j++) { k=j+2; for(i=1;i<=20;i++) sum=sum+(i+1)*(i+1)**k; } printf(“sum is :%d\n”,sum); } 观察程序的执行结果有无变化? (5)将(3)中程序改写为 main() { int I,j,m,n; long sum=0; for(j=1;j<=3;j++) { m=j+2; for(I=1;j<=20;I++) { n=I+1; sum=sum+m*m*k; } } printf(“sum is :%d\n”,sum); } 观察程序的执行结果有无变化? (1) 实验六 函数(1) [实验目的] 1.理解和掌握多模块的程序设计与调试的方法。 2.掌握函数的定义和调用的方法。 3.学会使用递归方法进行程序设计。 [实验内容和步骤] 1. 编写一个函数,判断一个数是不是素数。在主函数中输入一个整数,输出是否是素数的信息。 实验步骤与要求: (1) 编写一个函数prime(n),返回给定整数n是否为素数。 (2) 编写一个主函数,输入一个整数,调用(1)中的函数,判断此整数是否为素数,并输出结果。 (3) 对于属于多函数程序,可以采用每个函数分别进行编辑、编译的方法,然后再连接、运行。如果编译有错时,可分别修改,这样便于调试。实验2和实验3也可以使用这样的调试方法。 2. 用梯形法求函数的定积分。 实验步骤与要求: (1) 编制一个函数sab(a,b,n),其功能是求函数f(x)在[a,b]上的定积分,其中n为区间[a,b]的等分数。要求该函数在一个独立的文件中。 (2) 编制一个主函数以及计算被积函数值的函数f(x),在主函数中调用(1)中的函数计算并输出下列积分值。要求主函数与函数f(x)在同一个文件中。 (3) 编制另一个主函数以及计算被积函数值的函数f(x),在主函数中调用(1)中的函数计算并输出下列积分值。要求主函数与函数f(x)在同一个文件中。 说明: 用梯形法求定积分,梯形公式为 s=h[f(a)+f(b)]/2+hf(a+kh)其中,h=(b-a)/n 3. 用递归方法编写程序,求n阶勒让德多项式的值,递归公式为 1 (n=0) pn(x)=x (n=1) ((2n-1)xpn-1(x)-(n-1)pn-2(x))/n (n>1) , [ 分析讨论] 1. 小结函数的定义及调用方法。 2. 小结函数中形参和实参的结合规则。 实验七 函数(2) [目的与要求] 1、 掌握C语言函数定义及调用的规则。 2、 理解参数传递的过程。 [ 实验内容与步骤] 1、 上机调试下面的程序,记录系统给出的出错信息,并指出出错原因。 main() { int,y; printf(“%d\n”,sum(x+y)); int sum(a,b) { int a,b; return(a+b); } } 2、 编写并调试一个求(n为整数)的递归函数,希望能在程序运行过程中动态地显示递归函数被调用的轨迹。 [分析讨论] 1、 针对以上实验内容写出相应的参数传递过程并分析结果。 2、 讨论参数的传递的几种形式。 实验八 数组(1) [实验目的] 掌握有关数组和字符串的程序设计方法。 [实验内容和步骤] 1 已知一组数据如下: 6,3,42,23,35,71,98,67,56,38 编写程序,把它们按从小到大的次序排列起来。 程序如下: /*EX3-1*/ # include<stdio.h> # define N 10 main() { int a[N]={6, 3, 42, 23, 35, 71, 98, 67, 56, 38}; int i, j, t; printf(“The array before sorted:”); for(i=0; iprintf(“%4d”, a[i]); for(i=0; i<9; i++) { for(j=i+1; j<10; j++) { if(a[i]>a[j]) { t=a[i]; a[i]=a[j]; a[j]=t; } } } for(i=0; iprintf(“%4d”, a[i]); return o; } [ 分析讨论] 1. 定义数组时下标的用法和注意事项. 2.什么情况下出现下标越界.  实验九 数组(2) [目的和要求] 1、掌握数组定义的规则。 2、掌握C语言数组的基本用法。 [实验内容与步骤] 1、编写程序,测试下列数组的定义方式是否正确 (1) main() { int n; scanf(“%d”,&n); int a[n]; …… } (2) main() { const int n=10; int a[n]; } (3) #include <stdio.h> #define M 10 main() { int a[M]; } (4) main() { int a[2+2*4]; } (5) #include <stdio.h> #define M 2 #define N 8 main() { int a[M+N]; } 通过这一实验,可以说明什么问题? 2、运行下面的C程序,根据运行结果,可以说明什么? main( ) { int num[5]={1,2,3,4,5}; inti i; for(i=0;i<=5;i++) printf(“%d”,num[i]); } 3、操作符&用以求一个变量的地址,这在函数scanf中已经使用过了。现在要你设计一个程序,返回一个3 * 5 的二维数组各元素的地址,并由此说明二维数组中各元素是按什么顺序存诸的。 4、为一个起泡排序程序设计测试用例,并测试之。 [分析讨论] 通过实验,分析定义与引用数组的区别。 实验十 指 针 (1) [目的和要求] 1. 熟悉指针的正确用法。 [实验内容和步骤] 1、指出下面程序错误的原因。 main( ) { int x=10,y=5,*px,*py; px=py; px=&x; py=&y; printf(“*px=%d,*py=%d,*px,*py); } 2.下面的程序能获得上述运行结果吗? main() { char *S=”COMPUTER”; char c; printf(“which style you want to \n”); printf(“capital (c) or uncapital(u);”); c=getchar(); if(c=’c’) put(s); else { s=”computer”; puts(s); } } 分析出错原因。 3.设计一个C程序,用以测试下列各表达式的运算。 (1) y=*px++ (2) y=*++py (3) y=(*py)++ (4) y=--*ppx++ [分析讨论] 数组与指针的联系。 实验十一 指针(二) [目的和要求] 1、了解指针参数的特殊性。 2、掌握函数、指针、数组的用法。 [实验内容与步骤] 1、想使指针变量pt1指向a 和b 中的大者,pt2指向小者,以下程序能否实现此目的? swap(int *p1,int *p2) { int *p; p=p1;p1=p2;p2=p; } main() { int a,b; scanf(“%d,%d”,&a,&b); pt1=&a;pt2=&b; if(aprintf(“%d,%d\n”,*pt1,*pt2); } 上机调试此程序。如果不能实现题目要求,指出原因,并修改之。 2、 下面的程序。注意其中的复杂声明语句; int (*function[4])(); 和各标记符的含义,并记录执行结果。 main() { int fun1(); int fun2(); int fun3(); int fun4(); int (*function[4])(); int a=10,b=5,i; function[0]=fun1; function[1]=fun2; function[2]=fun3; function[3]=fun4; for(I=0;I<4;i++) printf(“fun no.%d->%d\n”,i+1,execute(a,b,function[i])); } execute(int x,int y,int *fun()) { return(*fun)(x,y);} fun1(int x, int y) { return(x+y);} fun2(int x,int y) { return(x-y);} fun3(int x,int y) { return(x*y);} fun4(int x,int y) { return(x/y);} [分析讨论] 指针、数组、函数的联系。 实验十二 有关文件的程序设计 [实验目的] 1. 掌握文件建立的方法。 2. 掌握包含文件操作的程序设计和调试方法。 [实验内容和步骤 ] 1 建立一个磁盘文件,其内容是0~90°之间每隔5°的正弦值。 程序如下: //EX8-1 #include #include #include #define PI 3.14159 main() { float S[19]; int i, a; ofstream out(“fsin.bny”); if(!out) { cout<<”Cannot open file.”<”的应用。 4、共用体的概念和应用。 [实验内容和步骤] 1、编写程序:有4名学生,每个学生的数据包括学号、姓名、成绩,要求找出成绩最高者的姓名和成绩,上机运行程序。 2、建立一个链表每个结点包括的成员为:职工号、工资。用malloc打印出来。用一个creat 来建立链表,用list来输出数据。5个职工号为101,103,105,107,109。 3、在上题基础上,新增加一个职工的数据,按职工号的顺序插入链表,新插入的职工号为106。写一函数insert来插入新结点。 4、在上面的基础上,写一函数delete,用来删除一个结点。要求删除职工号为103的结点。打印出删除后的链表. 5、你有无办法测试构造链表时,内存空间的动态分配情形? [分析讨论] 怎样引用结构体变量成员? 实验十四 位运算 [目的和要求] 1. 掌握位运算的概念和方法。 2. 掌握位运算(&,|,^,~)的使用方法。 3. 掌握有关位运算的用法。 [实验内容和步骤] 1. 按习题8.2的要求,编一个程序,将一个整数的高字节和低字节分别输出(用位运算方法)。上机运行。 2. 按习题8.6的要求,编一个程序,使一个整数的低4位翻转。上机运行,用十六进制数输入和输出。 3. 按习题8.10的要求,将一个整数i的各位循环左移4位,设i的值为十六进制数fe13。 4. 按习题8.13的要求,设计一个函数,当给定一个整数后,能得到该数的补码(应考虑该整数是正数或负数)。 [分析讨论] 位运算时应注意什么问题? 实验十五 综合应用举例 [目的与要求] 1、熟悉C语言的端口及硬件控制的方法与原理。 2、了解用C语言实现发声程序所要访问的端口。 [实验内容和步骤] 1、调试课本中例10.8程序,注意调出不同的效果。 2、调试并总结程序中所用的知识点,写出乐谱文件播放的是什么乐谱? 乐谱实例: 6000 h5 1 h3 2 h2 0.5 h3 0.5 h1 4 h2 1 m7 2 m6 0.5 h1 0.5 m5 4 m3 2 m5 1.5 m6 0.5 h1 1.5 h2 0.5 m6 0.5 h1 0.5 m5 1 h5 1.5 h1 0.5 h6 0.5 h5 0.5 h3 0.5 h5 0.5 h2 4 h2 1.5 h3 0.5 m7 1 m6 1 m5 1.5 m6 0.5 h1 1 h2 1 m3 1 h1 1 m6 0 m5 0.5 m6 0.5 h1 0.5 m5 4 h3 1.5 h5 0.5 m7 1 h2 1 m6 0.5 h1 0.5 m5 3 m3 0. m5 0.5 m3 0.5 m5 0.5 m5 0.5 m6 0.5 m7 0.5 h2 0.5 m6 3 m5 0.5 m6 0.5 h1 1.5 h2 0.5 h5 1 h3 1 h2 1 h3 0.5 h2 0.5 h1 1 m6 0.5 m5 0.5 m3 2 h1 2 m6 0.5 h1 0.5 m6 0.5 m5 0.5 m3 0.5 m5 0.5 m6 0.5 h1 0.5 m5 3 h3 0.5 h5 0.5 h2 0.5 h3 0.5 h2 0.5 h1 0.5 m7 1 m6 1 h5 4## void play_music(char *filename) /*filename为音乐文件名格式见上*/ { FILE *fp; int rate; char sound_high[3]; float sound_long; register int i=0,j; int sign=0; float str[1000][2]; if ((fp=fopen(filename,"r"))==NULL) { printf("Open file music.doc Errors!\n"); exit(1); } fscanf(fp,"%d",&rate); while(!feof(fp)&&!sign) { fscanf(fp,"%s%f",&sound_high,&sound_long); str[i][1]=rate*sound_long; switch(sound_high[0]) { case 'h': switch(sound_high[1]) { case '1': str[i++][0]=1046.5; break; case '2': str[i++][0]=1174.7; break; case '3': str[i++][0]=1318.5; break; case '4': str[i++][0]=1396.9; break; case '5': str[i++][0]=1568; break; case '6': str[i++][0]=1760; break; case '7': str[i++][0]=1975.5; break; default: printf("\n Error Music.doc\n"); break; } break; case 'm': switch(sound_high[1]) { case '1': str[i++][0]=523.3; break; case '2': str[i++][0]=587.3; break; case '3': str[i++][0]=659.3; break; case '4': str[i++][0]=698.5; break; case '5': str[i++][0]=784.0; break; case '6': str[i++][0]=880; break; case '7': str[i++][0]=987.8; break; default: printf("\n Error music.doc.\n"); break; } break; case 'l': switch(sound_high[1]) { case '1': str[i++][0]=262; break; case '2': str[i++][0]=296; break; case '3': str[i++][0]=329.6; break; case '4': str[i++][0]=349.2; break; case '5': str[i++][0]=392; break; case '6': str[i++][0]=440; break; case '7': str[i++][0]=493.9; break; default: printf("\n Error music.doc.\n"); break; } break; case '*': switch(sound_high[1]) { case '1': str[i++][0]=131; break; case '2': str[i++][0]=147; break; case '3': str[i++][0]=165; break; case '4': str[i++][0]=176; break; case '5': str[i++][0]=196; break; case '6': str[i++][0]=220; break; case '7': str[i++][0]=247; break; default: printf("\n Error music.doc\n"); break; } break; case '#': if (sound_high[1]=='#') sign=1; break; case 'x': if (sound_high[1]=='0') str[i++][0]=20; break; default:printf("\n ERRor music.doc\n"); exit(1); } } for(j=0;j<=i-1;j++) { sound(str[j][0]); delay(str[j][1]); } nosound(); } [分析讨论] 1.c语言怎样对数据库进行管理? 2.分析程序设计过程中怎样对基础知识进行灵活运用。
#include <stdio.h> #include #include #include #define M 3 struct student {long num; char name[20]; char sex; int score[5]; int ave; }stu[M]; void init() { printf("\t\t********************************\n\n"); printf("\t\tStudent score management systerm \n"); printf("\n"); printf("\t\t Make by Maggiehe \n"); printf("\n"); printf("\t\t Student number is 3206***** \n\n"); printf("\t\t********************************\n\n"); } void input() { int i,j; printf("\nInput the student number name sex math English PE maolun majing:\n"); for(i=0;iint i,j; clrscr(); printf("\t**********************student****************************\n"); printf("num name sex math English PE maolun majing:\n"); for(i=0;iprintf("%ld %s %c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); } if((i+1)%10==0) { printf("\npress any key continue.......\n"); getch(); } printf("\t*************************end*********************************\n"); } void average() { int i,j; float sum[M]; for(i=0;isum[i]+=stu[i].score[j]; stu[i].ave=sum[i]/5.0; } void search_num() { long num; int i,j; printf("\nplease enter number which you want to search:"); scanf("%ld",&num); printf("\n"); for(i=0;iprintf("The number is not exist\n"); else { printf("number name sex math English PE maolun majing average\n"); printf("%ld %s %c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); } } void search_nam() { int i,j; char name[20]; printf("\nplease enter name which you want to search:"); scanf("%s",&name); printf("\n"); for(i=0;iprintf("number name sex math English PE maolun majing average:\n"); printf("%ld%s%c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); } else printf("\nThe name is not exist!!!\n"); } void search_ave() { int i,j,a=0; float k; printf("please enter average score which you want to search:"); scanf("%f",&k); printf("\n"); for(i=0;iprintf("The average score is not exist!!!\n"); else for(i=0;iprintf("number name sex math English PE maolun majing average\n"); printf("%ld %s %c",stu[i].num,stu[i].name,stu[i].sex); for(j=0;j<5;j++) printf("%d",stu[i].score[j]); printf("%d",stu[i].ave); a++; } } void sort() { int i,j; struct student temp; for(i=0;iint i; FILE *fp; if((fp=fopen("student.dat","wb"))==NULL) {printf("cannot open file\n"); return; } for(i=0;iprintf("file write error\n"); fclose(fp); } void delect() { int i,t; long num; printf("please enter number which you want to delect:"); scanf("%ld",&num); printf("\n"); for(i=0;iprintf("The number is not exist!!!\n"); if(stu[i].num==num) t=i; } for(i=t;iint i,n,t; struct student s; printf("please input record\n"); printf("number name sex math English PE maolun majing average\n"); scanf("%ld,%s,%c,%d%d%d%d%d%d",&s.num,s.name,&s.sex,&s.score[0],&s.score[1],&s.score[2],&s.score[3],&s.score[4],&s.ave); s.ave=(s.score[0]+s.score[1]+s.score[2]+s.score[3]+s.score[4])/3.0; if((fp=fopen("student.dat","wb"))==NULL) {printf("cannot open file\n"); return; } i=M; for(t=0;stu[i].ave>s.ave&&tprintf("file write error\n"); fclose(fp); } void MainMenu() {clrscr(); printf("\t********************************************************************\n \n); Printf(“\n”); printf("\t choose one of following:\n"); printf("\t 1. search record\n"); printf("\t 2. sort record\n"); printf("\t 3. insert record\n"); printf("\t 4. delect record\n"); printf("\t please enter your choice:"); printf(“\n”); printf("\t********************************************************************\n"); } void menu_select() {clrscr(); printf("\t******************************************************************\n\n"); printf(“\n”); printf("\t choose one of following:\n"); printf("\t 1.1 search by number\n"); printf("\t 1.2 search by name\n"); printf("\t 1.3 search by average score\n"); printf("\t please enter your choice:"); printf(“\n”); printf("\t******************************************************************\n"); } void search() { char ch; clrscr(); menu_select(); ch=getch(); switch(ch) { case '1':search_num();break; case '2':search_nam();break; case '3':search_ave();break; } } main() { char ch; clrscr(); init(); input(); list(); average(); save(); MainMenu(); ch=getch(); switch(ch) { case '1':search();break; case '2':sort();break; case '3':insert();break; case '4':delect();break; } search_num(); search_nam(); search_ave(); sort(); delect(); insert; }
#include<stdio.h> #include #include #include #define ID struct id struct id { char name[20]; // xing ming int num; // xue hao int age; // nian ling char sex[2]; // xing bie char xi[20]; // xi bie char zhuan[20]; // zhuan ye char add[20]; // di zhi ID *next; // }; // FILE *fp; // ID *head; int sum_age=0,max_age=0,min_age=100,pc=0; float ave_age=0; ID *creat() { ID *p1,*p2,*head; int pd; p1=p2=head=NULL; printf("\t\t\t 开始输入记录!!!\n"); printf("please input pd:\n"); scanf("%d",&pd); while(pd!=0) { p1=(ID*)malloc(sizeof(ID)); printf("请输入学生的学号:\n");scanf("%d",&p1->num); printf("请输入学生的姓名:\n");scanf("%s",p1->name); printf("请输入学生的年龄:\n");scanf("%d",&p1->age); sum_age+=p1->age; if(max_ageage) max_age=p1->age; if(min_age>p1->age) min_age=p1->age; printf("请输入学生的性别:\n");scanf("%s",p1->sex); printf("请输入学生的系别:\n");scanf("%s",p1->xi); printf("请输入学生的专业:\n");scanf("%s",p1->zhuan); printf("请输入学生的地址:\n");scanf("%s",&p1->add); if(head==NULL) { head=p1; p2=p1; } else { p2->next=p1; p2=p1; } printf("please input pd:\n"); scanf("%d",&pd); pc++; } p2->next=NULL; return(head); } /*输入/添加记录*/ ID *insert(ID *head) { ID *temp,*p1,*p2; int pd; start: p1=head; printf("插入操作开始!!!\n"); temp=(ID *)malloc(sizeof(ID)); printf("请输入学生的学号:\n");scanf("%d",&temp->num); printf("请输入学生的姓名:\n");scanf("%s",temp->name); printf("请输入学生的年龄:\n");scanf("%d",&temp->age); printf("请输入学生的性别:\n");scanf("%s",temp->sex); printf("请输入学生的系别:\n");scanf("%s",temp->xi); printf("请输入学生的专业:\n");scanf("%s",temp->zhuan); printf("请输入学生的地址:\n");scanf("%s",temp->add); if (head==NULL) { head=temp; temp->next=NULL; } else { while(p1!=NULL) { p2=p1; p1=p1->next; } p2->next=temp; temp->next=NULL; } printf("插入末尾成功"); pc++; printf("是否继续插入 1.Yes 2.No;\n"); scanf("%d",&pd); if(pd==1) goto start; return (head); } /*删除学生记录*/ ID *delet(ID *head) { ID *p1,*p2; int num; printf("请输入要删除的学生的学号:");scanf("%d",&num); p1=head; if (head==NULL) { printf("没有记录\n"); goto end; } while(num!=p1->num && p1!=NULL) { p2=p1;p1=p1->next; } if(num==p1->num) { if (p1==head) head=p1->next; else p2->next=p1->next; printf("删除成功!!!\n"); pc--; //free(p1); //free(p2); } end:return head; } /*查找学生记录*/ ID *search(ID *head) { ID *p1,*p2; int num; printf("请输入要查找的学生的学号:");scanf("%d",&num); p1=head; while(num!=p1->num && p1!=NULL) { p2=p1;p1=p1->next; } if(num==p1->num) { printf("学生的学号:%d\n",p1->num); printf("学生的姓名:%s\n",p1->name); printf("学生的年龄:%d\n",p1->age); printf("学生的性别:%s\n",p1->sex); printf("学生的系别:%s\n",p1->xi); printf("学生的专业:%s\n",p1->zhuan); printf("学生的地址:%s\n",&p1->add); //free(p1); //free(p2); } return head; } /*按学号查询学生记录进行修改*/ ID *modify_num(ID *head) { ID *p; int i,m,n,num; start: printf("请输入要修改的学生的学号:"); scanf("%d",&num); p=head; while(p!=NULL ) { if(num==p->num) { m=1; while(m==1) { m=0; printf("\t\t 1.修改学生的学号\n"); printf("\t\t 2.修改学生的姓名\n"); printf("\t\t 3.修改学生的年龄\n"); printf("\t\t 4.修改学生的性别\n"); printf("\t\t 5.修改学生的系别\n"); printf("\t\t 6.修改学生的专业\n"); printf("\t\t 7.修改学生的地址\n"); printf("请选择(1--7):"); scanf("%d",&i); switch( i ) { case 1 : printf("请输入修改后的学号:"); scanf("%d",&p->num); break; case 2: printf("请输入修改后的姓名:"); scanf("%s",p->name); break; case 3: printf("请输入修改后的年龄:"); scanf("%d",&p->age); break; case 4: printf("请输入修改后的性别:"); scanf("%s",&p->sex); break; case 5: printf("请输入修改后的系别:"); scanf("%s",p->xi); break; case 6: printf("请输入修改后的专业:"); scanf("%s",p->zhuan); break; case 7: printf("请输入修改后的地址:"); scanf("%s",p->add); break; default: printf("输入错误,请重新输入"); } } break; } p=p->next; } printf("是否继续修改其他学生情况(1改 2不改):"); scanf("%d",&n); if(n==1)goto start; else return head; } /*退出程序*/ void ask_age() { if(pc==0) { printf("没有记录\n"); return ; } ave_age=float(sum_age*1.0/pc); printf("%f\n",ave_age); printf("%d\n",max_age); printf("%d\n",min_age); } /*显示结果函数*/ void print(ID *head) { ID *p; p=head; printf("\t\t\t*****************\n"); printf("显示结果是:\n"); if(head!=NULL) do { printf("%10d%10s%10d%10s%10s%10s%10s\n",p->num,p->name,p->age,p->sex,p->xi,p->zhuan,p->add); p=p->next; } while(p!=NULL); } void main() { ID *head; int choise; printf("\t\t\t* * * * 李林C语言课设* * * *\n"); while(1) { printf("\t\t 学生信息管理系统\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("\t\t 1.建立文件\n"); printf("\t\t 2.插入数据\n"); printf("\t\t 3.删除数据\n"); printf("\t\t 4.修改记录\n"); printf("\t\t 5.搜索记录\n"); printf("\t\t 6.求年龄\n"); printf("\t\t 0.退出\n"); printf("\n"); printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"); printf("请选择(0-6):"); scanf("%d",&choise); switch(choise) { case 1: head=creat(); print(head); break; case 2: head=insert(head); print(head); break; case 3: head=delet(head); print(head); break; case 4: head=modify_num(head); print(head); break; case 5:search(head); print(head); break; case 6:ask_age(); break; case 0: exit(0); break; default :printf("输入错误,请重新输入!\n"); } } } ★★★★★★★★★★★★★★★★★★★★★★★★★★★ #include "stdio.h" #include "stdlib.h" #include "string.h" #include "conio.h" #include "stdlib.h" #define null 0 struct record { char name[20]; char phone[20]; char adress[40]; char postcode[10]; char e_mail[30]; }student[500]; struct LinkList { struct record US; struct LinkList *next; }a; struct LinkList *head=null; int num=0; FILE *fp; int menu_select(); int adduser(); int list(); int search(); int display(); int add(); int listbyname(); int dele(); int save(); int exit(); void main() { system("cls"); for(;;) { switch(menu_select()) { case 0:adduser();break; case 1:list();break; case 2:search();break; case 3:display();break; case 4:add();break; case 5:listbyname();break; case 6:dele();break; case 7:save();break; case 8:exit(0); } } } menu_select() { char s[80]; int a; printf("*_* press any key enter menu! *_* \n"); getch(); system("cls"); printf("\t\t********************MENU*********************\n\n"); printf("\t\t 0. 输入记录\n"); printf("\t\t 1. 显示记录 \n"); printf("\t\t 2. 按姓名查找\n"); printf("\t\t 3. 按电话号码查找\n"); printf("\t\t 4. 插入记录 \n"); printf("\t\t 5. 按姓名排序\n"); printf("\t\t 6. 删除记录\n"); printf("\t\t 7. 记录保存文件\n"); printf("\t\t 8. Quit\n"); printf("\t\t***********************************************\n"); do{ printf("\n Enter you choice(0~11):"); scanf("%s",s); a=atoi(s); } while (a<0||a>11); return a; } adduser() { printf("\n\t\t**************** 请输入用户信息 ****************\n"); printf("\n\t\t输入姓名:"); scanf("%s",&student[num].name); printf("\n\t\t输入电话号码:"); scanf("%s",&student[num].phone); printf("\n\t\t输入地址:"); scanf("%s",&student[num].adress); printf("\n\t\t输入邮编:"); scanf("%s",&student[num].postcode); printf("\n\t\t输入e-mail:"); scanf("%s",&student[num].e_mail); num++; printf("\n\t\t是否继续添加?(Y/N):"); if (getch()=='y') adduser(); return(0); } list() { int i; system("cls"); if(num!=0) { printf("\n\t\t*************** 以下为通讯录所有信息************"); for (i=0;iprintf("\n\t\t姓名: %s",student[i].name); printf("\n\t\t电话: %s",student[i].phone); printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\t邮编: %s",student[i].postcode); printf("\n\t\te-mail:%s",student[i].e_mail); printf("\t\t"); if (i+1printf("\n\t\t__________________________"); system("pause"); } } printf("\n\t\t************************************************"); } else printf("\n\t\t通讯录中无任何纪录"); printf("\n\t\t按任意键返回主菜单:"); getch(); return(0); } search() { int mark=0; int i; int a=0; printf("\n\t\t***************** 按姓名查找 *******************"); char name[20]; printf("\n\t\t请输入姓名:"); scanf("%s",name); for(i=a;iprintf("\n\t\t************* 以下是您查找的用户信息 ***********"); printf("\n\t\t姓名: %s",student[i].name); printf("\n\t\t电话: %s",student[i].phone); printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\te-mail:%s",student[i].e_mail); printf("\n\t\t************************************************"); mark++; if ((i+1)printf("\n\t\t是否继续查找相同名字的用户信息:(y/n)"); if (getch()=='y') { a=i; continue; } else return(0); } else { printf("\n\t\t按任意键返回主菜单"); getch(); return(0); } } } if(mark!=0) { printf("\n\t\t没有相同姓名的用户纪录"); printf("\n\t\t按任意键返回主菜单"); getch(); return(0); } else { printf("\n\t\t没有相同姓名的用户纪录"); printf("\n\t\t按任意键返回主菜单"); getch(); return(0); } } display() { int mark=0; int i; int a=0; printf("\n\t\t****************** 按电话查找 ******************"); char phone[10]; printf("\n\t\t请输入电话号码:"); scanf("%s",phone); for(i=0;iprintf("\n\t\t************** 以下是您查找的用户信息 **********"); printf("\n\t\t姓名: %s",student[i].name); printf("\n\t\t电话: %s",student[i].phone); printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\te-mail:%s",student[i].e_mail); printf("\n\t\t************************************************"); printf("\n\t\t按任意键返回主菜单:"); mark++; getch(); return(0); } } if (mark==0) { printf("\n\t\t没有改用户的信息"); printf("\n\t\t按任意键返回主菜单"); getch(); return(0); } return(0); } add() { int i; if ((fp=fopen("student.bin","wb"))==NULL) { printf("\n\t\t文件打开失败"); } for (i=0;iprintf("\n\t\t写入文件错误!\n"); } printf("\n\t\t**************** 请输入用户信息 ****************\n"); printf("\n\t\t输入姓名:"); scanf("%s",&student[num].name); printf("\n\t\t输入电话号码:"); scanf("%s",&student[num].phone); printf("\n\t\t输入地址:"); scanf("%s",&student[num].adress); printf("\n\t\t输入邮编:"); scanf("%s",&student[num].postcode); printf("\n\t\t输入e-mail:"); scanf("%s",&student[num].e_mail); num++; printf("\n\t\t是否继续添加?(Y/N):"); if (getch()=='y') adduser(); return(0); } fclose(fp); printf("\n\t\t通讯录文件已保存"); printf("\n\t\t按任意键退出程序\n\t\t"); exit(0); return(0); } void deletebyphone() { int i,j; int deletemark=0; char phone[20]; printf("\n\t\t请输入要删除用户电话号码:"); scanf("%s",phone); if(num==0) { printf("\n\t\t对不起,文件中无任何纪录"); printf("\n\t\t按任意键返回主菜单"); getch(); return; } for (i=0;iprintf("\n\t\t以下是您要删除的用户纪录:"); printf("\n\t\t姓名: %s",student[i].name); printf("\n\t\t电话: %s",student[i].phone); printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\te-mail:%s",student[i].e_mail); printf("\n\t\t是否删除?(y/n)"); if (getch()=='y') { for (j=i;jprintf("\n\t\t删除成功"); printf("\n\t\t是否继续删除?(y/n)"); if (getch()=='y') deletebyphone(); return; } else return; } continue; } if (deletemark==0) { printf("\n\t\t没有该用户的纪录"); printf("\n\t\t是否继续删除?(y/n)"); if (getch()=='y') deletebyphone(); return; } } void deletebyname() { int a=0; int findmark=0; int j; int deletemark=0; int i; char name[20]; printf("\n\t\t请输入要删除用户姓名:"); scanf("%s",name); for (i=a;iprintf("\n\t\t以下是您要删除的用户纪录:"); findmark++; printf("\n\t\t________________________________"); printf("\n\t\t姓名: %s",student[i].name); printf("\n\t\t电话: %s",student[i].phone); printf("\n\t\t地址: %s",student[i].adress); printf("\n\t\te-mail:%s",student[i].e_mail); printf("\n\t\t________________________________"); printf("\n\t\t是否删除?(y/n)"); if (getch()=='y') { for (j=i;jprintf("\n\t\t删除成功"); if((i+1)printf("\n\t\t是否继续删除相同姓名的用户信息?(y/n)"); if (getch()=='y') { a=i; continue; } } printf("\n\t\t是否继续删除?(y/n)"); if (getch()=='y') deletebyname(); return; } if((i+1)printf("\n\t\t是否继续删除相同姓名的用户信息?(y/n)"); if (getch()=='y') { a=i; continue; } } } else continue; } if ((deletemark==0)&&(findmark==0)) { printf("\n\t\t没有该用户的纪录"); printf("\n\t\t是否继续删除?(y/n)"); if (getch()=='y') deletebyphone(); return; return; } else if (findmark!=0) { printf("\n\t\t没有重名信息"); printf("\n\t\t没有该用户的纪录"); printf("\n\t\t是否继续删除?(y/n)"); if (getch()=='y') deletebyphone(); return; return; } } dele() { char choic; printf("\n\t\t1-按电话号码删除 2-按姓名删除"); printf("\n\t\t请选择:"); choic=getch(); switch (choic) { case '1':deletebyphone();break; case '2':deletebyname();break; } return(0); } listbyname() { int i,j; struct record tmp; for (i=1;i=0)); student[j+1]=tmp; } } printf("\n\t\t排序成功,是否显示?(y/n)"); if (getch()=='y') list(); return(0); } save() { int j; FILE*fp; fp=fopen("student.txt","w"); if (fp==NULL) printf("can't open the file."); if(num!=0) { for(j=0;jprintf("保存成功!"); fclose(fp); return(0); }
主体:(一) 一、C++概述 (一) 发展历史 1980年,Bjarne Stroustrup博士开始着手创建一种模拟语言,能够具有面向对象的程序设计特色。在当时,面向对象编程还是一个比较新的理念,Stroustrup博士并不是从头开始设计新语言,而是在C语言的基础上进行创建。这就是C++语言。 1985年,C++开始在外面慢慢流行。经过多年的发展,C++已经有了多个版本。为次,ANSI和ISO的联合委员会于1989年着手为C++制定标准。1994年2月,该委员会出版了第一份非正式草案,1998年正式推出了C++的国际标准。 (二) C和C++ C++是C的超集,也可以说C是C++的子集,因为C先出现。按常理说,C++编译器能够编译任何C程序,但是C和C++还是有一些小差别。 例如C++增加了C不具有的关键字。这些关键字能作为函数和变量的标识符在C程序中使用,尽管C++包含了所有的C,但显然没有任何C++编译器能编译这样的C程序。 C程序员可以省略函数原型,而C++不可以,一个不带参数的C函数原型必须把void写出来。而C++可以使用空参数列表。 C++中new和delete是对内存分配的运算符,取代了C中的malloc和free。 标准C++中的字符串类取代了C标准C函数库头文件中的字符数组处理函数。 C++中用来做控制态输入输出的iostream类库替代了标准C中的stdio函数库。 C++中的try/catch/throw异常处理机制取代了标准C中的setjmp()和longjmp()函数。 二、关键字和变量 C++相对与C增加了一些关键字,如下: typename bool dynamic_cast mutable namespace static_cast using catch explicit new virtual operator false private template volatile const protected this wchar_t const_cast public throw friend true reinterpret_cast try bitor xor_e and_eq compl or_eq not_eq bitand 在C++中还增加了bool型变量和wchar_t型变量: 布尔型变量是有两种逻辑状态的变量,它包含两个值:真和假。如果在表达式中使用了布尔型变量,那么将根据变量值的真假而赋予整型值1或0。要把一个整型变量转换成布尔型变量,如果整型值为0,则其布尔型值为假;反之如果整型值为非0,则其布尔型值为真。布儿型变量在运行时通常用做标志,比如进行逻辑测试以改变程序流程。 #include iostream.h int main() { bool flag; flag=true; if(flag) cout<include iostream.h int main() { wchar_t wc; wc='b'; wout<int* iptr=(int*) &table; 表达式的前缀(int*)就是传统C风格的强制类型转换说明(typecast),又可称为强制转换说明(cast)。强制转换说明告诉编译器把表达式转换成指定的类型。有些情况下强制转换是禁用的,例如不能把一个结构类型转换成其他任何类型。数字类型和数字类型、指针和指针之间可以相互转换。当然,数字类型和指针类型也可以相互转换,但通常认为这样做是不安全而且也是没必要的。强制类型转换可以避免编译器的警告。 long int el=123; short i=(int) el; float m=34.56; int i=(int) m; 上面两个都是C风格的强制类型转换,C++还增加了一种转换方式,比较一下上面和下面这个书写方式的不同: long int el=123; short i=int (el); float m=34.56; int i=int (m); 使用强制类型转换的最大好处就是:禁止编译器对你故意去做的事发出警告。但是,利用强制类型转换说明使得编译器的类型检查机制失效,这不是明智的选择。通常,是不提倡进行强制类型转换的。除非不可避免,如要调用malloc()函数时要用的void型指针转换成指定类型指针。 四、标准输入输出流 在C语言中,输入输出是使用语句scanf()和printf()来实现的,而C++中是使用类来实现的。 #include iostream.h main() //C++中main()函数默认为int型,而C语言中默认为void型。 { int a; cout<>a; /*输入一个数值*/ cout<include iostream.h int main() { int a; cout<>a; cout<int fun(int x,int y) { return x*2; } 尽管这样的用法是正确的,但大多数C和C++的编译器都会给出一个警告,说参数y在程序中没有被用到。为了避免这样的警告,C++允许声明一个无名形参,以告诉编译器存在该参数,且调用者需要为其传递一个实际参数,但是函数不会用到这个参数。下面给出使用了无名参数的C++函数代码: int fun(int x,int) //注意不同点 { return x*2; } (二) 函数的默认参数 C++函数的原型中可以声明一个或多个带有默认值的参数。如果调用函数时,省略了相应的实际参数,那么编译器就会把默认值作为实际参数。可以这样来声明具有默认参数的C++函数原型: #include iostream.h void show(int=1,float=2.3,long=6); int main() { show(); show(2); show(4,5.6); show(8,12.34,50L); return 0; } void show(int first,float second,long third) { cout<include iostream.h void a(int,int); void a(int); int main() { a(5); a(6,7); return 0; } void a(int i) { cout<int i,int j) { cout<include iostream.h void a(int,int); void a(int,float); int main() { a(5,6); a(6,7.0); return 0; } void a(int i,int j) { cout<int i,float j) { cout<a; for(int i=1;i<=10;i++) //C语言中,不允许在这里定义变量 { static int a=0; //C语言中,同一函数块,不允许有同名变量 a+=i; cout<<::a<< <include iostream.h int main() { int *birthday=new int[3]; birthday[0]=6; birthday[1]=24; birthday[2]=1940; cout<size; int *array=new int[size]; for(int i=0;iint a; int& b=a; 先声明一个名为a的变量,它还有一个别名b。我们可以认为是一个人,有一个真名,一个外号,以后不管是喊他a还是b,都是叫他这个人。同样,作为变量,以后对这两个标识符操作都会产生相同的效果。 #include iostream.h int main() { int a=123; int& b=a; cout<include iostream.h void func1(s p); void func2(s& p); struct s { int n; char text[10]; }; int main() { static s str={123,China}; func1(str); func2(str); return 0; } void func1(s p) { cout<include iostream.h void display(const Date&,const char*); void swapper(Date&,Date&); struct Date { int month,day,year; }; int main() { static Date now={2,23,90}; static Date then={9,10,60}; display(now,Now: ); display(then,Then: ); swapper(now,then); display(now,Now: ); display(then,Then: ); return 0; } void swapper(Date& dt1,Date& dt2) { Date save; save=dt1; dt1=dt2; dt2=save; } void display(const Date& dt,const char *s) { cout<include iostream.h struct Date { int month,day,year; }; Date birthdays[]= { {12,12,60}; {10,25,85}; {5,20,73}; }; const Date& getdate(int n) { return birthdays[n-1]; } int main() { int dt=1; while(dt!=0) { cout<<Enter date # (1-3,0 to quit)<>dt; if(dt>0 && dt<4) { const Date& bd=getdate(dt); cout<include iostream.h class Box { private: int height,width,depth; //3个私有数据成员 public: Box(int,int,int); ~Box(); int volume(); //成员函数 }; Box::Box(int ht,int wd,int dp) { height=ht; width=wd; depth=dp; } Box::~Box() { //nothing } int Box::volume() { return height*width*depth; } int main() { Box thisbox(3,4,5); //声明一个类对象并初始化 cout<int Box::volume() { return height*width*depth; } 还有一种方法就是直接在类声明的内部定义函数体,而不是仅仅给出一个函数原型。我们把上面的函数简化一下: #include iostream.h class Box { private: int height,width,depth; public: Box(int ht,int wd,int dp) { height=ht; width=wd; depth=dp; } ~Box(); int volume() { return height*width*depth; } }; int main() { Box thisbox(3,4,5); //声明一个类对象并初始化 cout<include iostream.h class Box { private: int height,width,depth; public: Box(int ht=2,int wd=3,int dp=4) { height=ht; width=wd; depth=dp; } ~Box(); int volume() { return height*width*depth; } }; int main() { Box thisbox(3,4,5); //初始化 Box defaulbox; //使用默认参数 cout<include iostream.h class Box { private: int height,width,depth; public: Box() { //nothing } Box(int ht=2,int wd=3,int dp=4) { height=ht; width=wd; depth=dp; } ~Box(); int volume() { return height*width*depth; } }; int main() { Box thisbox(3,4,5); //初始化 Box otherbox; otherbox=thisbox; cout<int height,width,depth; public: Box() { height=0;width=0;depth=0; } Box(int ht,int wd,int dp) { height=ht;width=wd;depth=dp; } int volume() { return height*width*depth; } }; 这还不是最好的方法,更好的方法是使用默认参数,根本不需要不带参数的构造函数。 class Box { int height,width,depth; public: Box(int ht=0,int wd=0,int dp=0) { height=ht;width=wd;depth=dp; } int volume() { return height*width*depth; } }; 三、析构函数 当一个类的对象离开作用域时,析构函数将被调用(系统自动调用)。析构函数的名字和类名一样,不过要在前面加上 ~ 。对一个类来说,只能允许一个析构函数,析构函数不能有参数,并且也没有返回值。析构函数的作用是完成一个清理工作,如释放从堆中分配的内存。 我们也可以只给出析构函数的形式,而不给出起具体函数体,其效果是一样的,如上面的例子。但在有些情况下,析构函数又是必需的。如在类中从堆中分配了内存,则必须在析构函数中释放 主体:(三)类的转换 C++的内部数据类型遵循隐式类型转换规则。假设某个表达市中使用了一个短整型变量,而编译器根据上下文认为这儿需要是的长整型,则编译器就会根据类型转换规则自动把它转换成长整型,这种隐式转换出现在赋值、参数传递、返回值、初始化和表达式中。我们也可以为类提供相应的转换规则。 对一个类建立隐式转换规则需要构造一个转换函数,该函数作为类的成员,可以把该类的对象和其他数据类型的对象进行相互转换。声明了转换函数,就告诉了编译器,当根据句法判定需要类型转换时,就调用函数。 有两种转换函数。一种是转换构造函数;另一种是成员转换函数。需要采用哪种转换函数取决于转换的方向。 一、转换构造函数 当一个构造函数仅有一个参数,且该参数是不同于该类的一个数据类型,这样的构造函数就叫转换构造函数。转换构造函数把别的数据类型的对象转换为该类的一个对象。和其他构造函数一样,如果声明类的对象的初始化表同转换构造函数的参数表相匹配,该函数就会被调用。当在需要使用该类的地方使用了别的数据类型,便宜器就会调用转换构造函数进行转换。 #include iostream.h #include time.h #include stdio.h class Date { int mo, da, yr; public: Date(time_t); void display(); }; void Date::display() { char year[5]; if(yr<10) sprintf(year,0%d,yr); else sprintf(year,%d,yr); cout<tm_mon+1; yr=tim->tm_year; if(yr>=100) yr-=100; } int main() { time_t now=time(0); Date dt(now); dt.display(); return 0; } 本程序先调用time()函数来获取当前时间,并把它赋给time_t对象;然后程序通过调用Date类的转换构造函数来创建一个Date对象,该对象由time_t对象转换而来。time_t对象先传递给localtime()函数,然后返回一个指向tm结构(time.h文件中声明)的指针,然后构造函数把结构中的日月年的数值拷贝给Date对象的数据成员,这就完成了从time_t对象到Date对象的转换。 二、成员转换函数 成员转换函数把该类的对象转换为其他数据类型的对象。在成员转换函数的声明中要用到关键字operator。这样声明一个成员转换函数: operator aaa(); 在这个例子中,aaa就是要转换成的数据类型的说明符。这里的类型说明符可以是任何合法的C++类型,包括其他的类。如下来定义成员转换函数; Classname::operator aaa() 类名标识符是声明了该函数的类的类型说明符。上面定义的Date类并不能把该类的对象转换回time_t型变量,但可以把它转换成一个长整型值,计算从2000年1月1日到现在的天数。 #include iostream.h class Date { int mo,da,yr; public: Date(int m,int d,int y) {mo=m; da=d; yr=y;} operator int(); //声明 }; Date::operator int() //定义 { static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; int days=yr-2000; days*=365; days+=(yr-2000)/4; for(int i=0;iint main() { Date now(12,24,2003); int since=now; cout<include iostream.h class CustomDate { public: int da, yr; CustomDate(int d=0,int y=0) {da=d; yr=y;} void display() { cout<int mo, da, yr; public: Date(int m=0,int d=0,int y=0) {mo=m; da=d; yr=y;} Date(const CustomDate&); //转换构造函数 operator CustomDate(); //成员转换函数 void display() { cout<int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31}; Date::Date(const CustomDate& jd) { yr=jd.yr; da=jd.da; for(mo=0;modys[mo]) da-=dys[mo]; else break; mo++; } Date::operator CustomDate() { CustomDate cd(0,yr); for(int i=0;iint main() { Date dt(12,24,3); CustomDate cd; cd = dt; //调用成员转换函数 cd.display(); dt = cd; //调用转换构造函数 dt.display(); return 0; } 这个例子中有两个类CustomDate和Date,CustomDate型日期包含年份和天数。 这个例子没有考虑闰年情况。但是在实际构造一个类时,应该考虑到所有问题的可能性。 在Date里中具有两种转换函数,这样,当需要从Date型变为CustomDate型十,可以调用成员转换函数;反之可以调用转换构造函数。 不能既在Date类中定义成员转换函数,又在CustomDate类里定义转换构造函数。那样编译器在进行转换时就不知道该调用哪一个函数,从而出错。 四、转换函数的调用 C++里调用转换函数有三种形式:第一种是隐式转换,例如编译器需要一个Date对象,而程序提供的是CustomDate对象,编译器会自动调用合适的转换函数。另外两种都是需要在程序代码中明确给出的显式转换。C++强制类型转换是一种,还有一种是显式调用转换构造函数和成员转换函数。下面的程序给出了三中转换形式: #include iostream.h class CustomDate { public: int da, yr; CustomDate(int d=0,int y=0) {da=d; yr=y;} void display() { cout<int mo, da, yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y; } operator CustomDate(); }; Date::operator CustomDate() { static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; CustomDate cd(0,yr); for(int i=0;iint main() { Date dt(11,17,89); CustomDate cd; cd = dt; cd.display(); cd = (CustomDate) dt; cd.display(); cd = CustomDate(dt); cd.display(); return 0; } 五、转换发生的情形 上面的几个例子都是通过不能类型对象之间的相互赋值来调用转换函数,还有几种调用的可能: 参数传递 初始化 返回值 表达式语句 这些情况下,都有可能调用转换函数。 下面的程序不难理解,就不分析了。 #include iostream.h class CustomDate { public: int da, yr; CustomDate() {} CustomDate(int d,int y) { da=d; yr=y;} void display() { cout<int mo, da, yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y; } operator CustomDate(); }; Date::operator CustomDate() { static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; CustomDate cd(0,yr); for (int i=0;iint main() { Date dt(12,24,3); CustomDate cd; cd = dt; cd.display(); dispdate(dt); Tester ts(dt); ts.display(); cd = rtndate(); cd.display(); return 0; } 六、显式构造函数 注意上面Tester类的构造函数前面有一个explicit修饰符。如果不加上这个关键字,那么在需要把CustomDate对象转换成Tester对象时,编译器会把该函数当作转换构造函数来调用。但是有时候,并不想把这种只有一个参数的构造函数用于转换目的,而仅仅希望用它来显式地初始化对象,此时,就需要在构造函数前加explicit。如果在声明了Tester对象以后使用了下面的语句将导致一个错误: ts=jd; //error 这个错误说明,虽然Tester类中有一个以Date型变量为参数的构造函数,编译器却不会把它看作是从Date到Tester的转换构造函数,因为它的声明中包含了explicit修饰符。 七、表达式内部的转换 在表达式内部,如果发现某个类型和需要的不一致,就会发生错误。数字类型的转换是很简单,这里就不举例了。下面的程序是把Date对象转换成长整型值。 #include iostream.h class Date { int mo, da, yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y; } operator long(); }; Date::operator long() { static int dys[]={31,28,31,30,31,30,31,31,30,31,30,31}; long days=yr; days*=365; days+=(yr-1900)/4; //从1900年1月1日开始计算 for(int i=0;iint main() { Date today(12,24,2003); const long ott=123; long sum=ott+today; cout<sum; return 0; } 在表达式中,当需要转换的对象可以转换成某个数字类型,或者表达式调用了作用于某个类的重载运算符时,就会发生隐式转换。运算符重载以后再学习。 主体:(四)私有数据成员和友元 一、私有数据成员的使用 1.取值和赋值成员函数 面向对象的约定就是保证所有数据成员的私有性。一般我们都是通过公有成员函数来作为公共接口来读取私有数据成员的。某些时候,我们称这样的函数为取值和赋值函数。 取值函数的返回值和传递给赋值函数的参数不必一一匹配所有数据成员的类型。 #include iostream.h class Date { int mo, da, yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y; } int getyear() const { return yr; } void setyear(int y) { yr = y; } }; int main() { Date dt(4,1,89); cout<include iostream.h class CustomDate { int da,yr; public: CustomDate() {} CustomDate(int d,int y) { da=d; yr=y; } void display() const {cout<int getday() const { return da; } void setday(int d) { da=d; } }; class Date { int mo,da,yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y; } operator CustomDate() const; }; Date::operator CustomDate() const { static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31}; CustomDate cd(0,yr); int day=da; for(int i=0;iint main() { Date dt(11,17,89); CustomDate cd; cd=dt; cd.display(); return 0; } 注意上面的程序中Date::operator CustomDate()声明为常量型,因为这个函数没有改变调用它对象的数据,尽管它修改了一个临时CustomDate对象并将其作为函数返回值。 二、友元 前面已经说过了,私有数据成员不能被类外的其他函数读取,但是有时候类会允许一些特殊的函数直接读写其私有数据成员。 关键字friend可以让特定的函数或者别的类的所有成员函数对私有数据成员进行读写。这既可以维护数据的私有性,有可以保证让特定的类或函数能够直接访问私有数据。 1.友元类 一个类可以声明另一个类为其友元,这个友元的所有成员函数都可以读写它的私有数据。 #include iostream.h class Date; class CustomDate { int da,yr; public: CustomDate(int d=0,int y=0) { da=d; yr=y; } void display() const {cout<int mo,da,yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y; } operator CustomDate(); }; Date::operator CustomDate() { static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31}; CustomDate cd(0, yr); for (int i=0;iint main() { Date dt(11,17,89); CustomDate cd(dt); cd.display(); return 0; } 在上面的程序中,有这样一句 friend Date; 该语句告诉编译器,Date类的所有成员函数有权访问CustomDate类的私有成员。因为Date类的转换函数需要知道CustomDate类的每个数据成员,所以真个Date类都被声明为CustomDate类的友元。 2.隐式构造函数 上面程序对CustomDate的构造函数的调用私有显示该类需要如下的一个转换构造函数: CustomDate(Date& dt); 但是唯一的一个构造函数是:CustomDate(int d=0;int y=0); 这就出现了问题,编译器要从Date对象构造一个CustomDate对象,但是CustomDate类中并没有定义这样的转换构造函数。不过Date类中定义了一个成员转换函数,它可以把Date对象转换成CustomDate对象。于是编译器开始搜索CustomDate类,看其是否有一个构造函数,能从一个已存在的CustomDate的对象创建新的CustomDate对象。这种构造函数叫拷贝构造函数。拷贝构造函数也只有一个参数,该参数是它所属的类的一个对象,由于CustomDate类中没有拷贝构造函数,于是编译器就会产生一个默认的拷贝构造函数,该函数简单地把已存在的对象的每个成员拷贝给新对象。现在我们已经知道,编译器可以把Date对象转换成CustomDate对象,也可以从已存在的CustomDate对象生成一个新的CustomDate对象。那么上面提出的问题,编译器就是这样做的:它首先调用转换函数,从Date对象创建一个隐藏的、临时的、匿名的CustomDate对象,然后用该临时对象作为参数调用默认拷贝构造函数,这就生成了一个新的CustomDate对象。 3.预引用 上面的例子中还有这样一句 class Date; 这个语句叫做预引用。它告诉编译器,类Date将在后面定义。编译器必须知道这个信号,因为CustomDate类中引用了Date类,而Date里也引用了CustomDate类,必须首先声明其中之一。 使用了预引用后,就可以声明未定义的类的友元、指针和引用。但是不可以使用那些需要知道预引用的类的定义细节的语句,如声明该类的一个实例或者任何对该类成员的引用。 4.显式友元预引用 也可以不使用预引用,这只要在声明友元的时候加上关键自class就行了。 #include iostream.h class CustomDate { int da,yr; public: CustomDate(int d=0,int y=0) { da=d; yr=y; } void display() const {cout<int main() { ... ... } 5.友元函数 通常,除非真的需要,否则并不需要把整个类都设为另一个类的友元,只需挑出需要访问当前类私有数据成员的成员函数,将它们设置为该类的友元即可。这样的函数称为友元函数。 下面的程序限制了CustomDate类数据成员的访问,Date类中只有需要这些数据的成员函数才有权读写它们。 #include iostream.h class CustomDate; class Date { int mo,da,yr; public: Date(const CustomDate&); void display() const {cout<int da,yr; public: CustomDate(int d=0,int y=0) { da=d; yr=y; } friend Date::Date(const CustomDate&); }; Date::Date(const CustomDate& cd) { static int dys[] = {31,28,31,30,31,30,31,31,30,31,30,31}; yr=cd.yr; da=cd.da; for(mo=0;modys[mo]) da-=dys[mo]; else break; mo++; } int main() { Date dt(CustomDate(123, 89)); dt.display(); return 0; } 6.匿名对象 上面main()函数中Date对象调用CustomDate类的构造函数创建了一个匿名CustomDate对象,然后用该对象创建了一个Date对象。这种用法在C++中是经常出现的。 7.非类成员的友元函数 有时候友元函数未必是某个类的成员。这样的函数拥有类对象私有数据成员的读写权,但它并不是任何类的成员函数。这个特性在重载运算符时特别有用。 非类成员的友元函数通常被用来做为类之间的纽带。一个函数如果被两个类同时声明为友元,它就可以访问这两个类的私有成员。下面的程序说明了一个可以访问两个类私有数据成员的友元函数是如何将在两个类之间架起桥梁的。 #include iostream.h class Time; class Date { int mo,da,yr; public: Date(int m,int d,int y) { mo=m; da=d; yr=y;} friend void display(const Date&, const Time&); }; class Time { int hr,min,sec; public: Time(int h,int m,int s) { hr=h; min=m; sec=s;} friend void display(const Date&, const Time&); }; void display(const Date& dt, const Time& tm) { cout << dt.mo << '/' << dt.da << '/' << dt.yr; cout << ' '; cout << tm.hr << ':' << tm.min << ':' << tm.sec; } int main() { Date dt(2,16,97); Time tm(10,55,0); display(dt, tm); return 0; } 主体:(五)析构函数和this指针 一、析构函数 前面的一些例子都没有说明析构函数,这是因为所用到的类在结束时不需要做特别的清理工作。下面的程序给出了一新的Date类,其中包括一个字符串指针,用来表示月份。 #include iostream.h #include string.h class Date { int mo,da,yr; char *month; public: Date(int m=0, int d=0, int y=0); ~Date(); void display() const; }; Date::Date(int m,int d,int y) { static char *mos[] = { January,February,March,April,May,June, July,August,September,October,November,December }; mo=m; da=d; yr=y; if(m!=0) { month=new char[strlen(mos[m-1])+1]; strcpy(month, mos[m-1]); } else month = 0; } Date::~Date() { delete [] month; } void Date::display() const { if(month!=0) cout<int main() { Date birthday(8,11,1979); birthday.display(); return 0; } 在Date对象的构造函数中,首先用new运算符为字符串month动态分配了内存,然后从内部数组中把月份的名字拷贝给字符串指针month。 析构函数在删除month指针时,可能会出现一些问题。当然从这个程序本身来看,没什么麻烦;但是从设计一个类的角度来看,当Date类用于赋值时,就会出现问题。假设上面的main()修改为“ int main() { Date birthday(8,11,1979); Date today; today=birthday; birthday.display(); return 0; } 这会生成一个名为today的空的Date型变量,并且把birthday值赋给它。如果不特别通知编译器,它会简单的认为类的赋值就是成员对成员的拷贝。在上面的程序中,变量birthday有一个字符型指针month,并且在构造函数里用new运算符初始化过了。当birthday离开其作用域时,析构函数会调用delete运算符来释放内存。但同时,当today离开它的作用域时,析构函数同样会对它进行释放操作,而today里的month指针是birthday里的month指针的一个拷贝。析构函数对同一指针进行了两次删除操作,这会带来不可预知的后果。 如果假设today是一个外部变量,而birthday是一个自变量。当birthday离开其作用域时,就已经把对象today里的month指针删除了。显然这也是不正确的。 再假设有两个初始化的Date变量,把其中一个的值赋值给另一个: Date birthday(8,11,1979); Date today(12,29,2003); today=birthday; 问题就更复杂了,当这两个变量离开作用域时,birthday中的month的值已经通过赋值传递给了today。而today中构造函数用new运算符给month的值却因为赋值被覆盖了。这样,birthday中的month被删除了两次,而today中month却没有被删除掉。 二、重载赋值运算符 为了解决上面的问题,我们应该写一个特殊的赋值运算符函数来处理这类问题。当需要为同一个类的两个对象相互赋值时,就可以重载运算符函数。这个方法可以解决类的赋值和指针的释放。 下面的程序中,类中的赋值函数用new运算符从堆中分配了一个不同的指针,该指针获取赋值对象中相应的值,然后拷贝给接受赋值的对象。 在类中重载赋值运算符的格式如下: void operator = (const Date&) 后面我们回加以改进。目前,重载的运算符函数的返回类型为void。它是类总的成员函数,在本程序红,是Date类的成员函数。它的函数名始终是operator =,参数也始终是同一个类的对象的引用。参数表示的是源对象,即赋值数据的提供者。重载函数的运算符作为目标对象的成员函数来使用。 #include iostream.h #include string.h class Date { int mo,da,yr; char *month; public: Date(int m=0, int d=0, int y=0); ~Date(); void operator=(const Date&); void display() const; }; Date::Date(int m, int d, int y) { static char *mos[] = { January,February,March,April,May,June, July,August,September,October,November,December }; mo = m; da = d; yr = y; if (m != 0) { month = new char[strlen(mos[m-1])+1]; strcpy(month, mos[m-1]); } else month = 0; } Date::~Date() { delete [] month; } void Date::display() const { if (month!=0) cout<int main() { Date birthday(8,11,1979); birthday.display(); Date newday(12,29,2003); newday.display(); newday = birthday; newday.display(); return 0; } 除了为Date类加入了一个重载运算符函数,这个程序和上面的一个程序是相同的。赋值运算符函数首先取得所需的数据,然后用delete把原来的month指针所占用的内存返还给堆。接着,如果源对象的month指针已经初始化过,就用new运算符为对象重新分配内存,并把源对象的month字符串拷贝给接受方。 重载的Date类赋值运算符函数的第一个语句比较了源对象的地址和this指针。这个操作取保对象不会自己给自己赋值。 三、this指针 this指针是一个特殊的指针,当类的某个非静态的成员函数在执行时,就会存在this指针。它指向类的一个对象,且这个对象的某个成员函数正在被调用。 this指针的名字始终是this,而且总是作为隐含参数传递给每一个被声明的成员函数,例如: void Date::myFunc(Date* this); 实际编程时函数的声明不需要包含这个参数。 当程序中调用某个对象的成员函数时,编译器会把该对象的地址加入到参数列表中,感觉上就好象函数采用了上面所示的声明,并且是用如下方式来调用的: dt.myFunc(& dt); 静态成员函数不存在this指针。 当调用某个对象的成员函数时,编译器把对象的地址传递给this指针,然后再调用该函数。因此,成员函数你对任何成员的调用实际上都隐式地使用了this指针。 1.以this指针作为返回值 使用this指针可以允许成员函数返回调用对象给调用者。前面的程序中重载赋值运算符没有返回值,因此不能用如下的形式对字符串进行赋值: a=b=c; 为了使重载的类赋值机制也能这样方便,必须让赋值函数返回赋值的结果,在这里就是目标对象。当赋值函数执行时,其返回值也恰好是this指针所指的内容。 下面的程序对前面那个程序进行了修改,让重载赋值运算符返回了一个Date对象的引用。 #include iostream.h #include string.h class Date { int mo,da,yr; char *month; public: Date(int m=0, int d=0, int y=0); ~Date(); void operator=(const Date&); void display() const; }; Date::Date(int m, int d, int y) { static char *mos[] = { January,February,March,April,May,June, July,August,September,October,November,December }; mo = m; da = d; yr = y; if (m != 0) { month = new char[strlen(mos[m-1])+1]; strcpy(month, mos[m-1]); } else month = 0; } Date::~Date() { delete [] month; } void Date::display() const { if (month!=0) cout<int main() { Date birthday(8,11,1979); Date oldday,newday; oldday=newday=birthday; birthday.display(); oldday.display(); newday.display(); return 0; } 2.在链表中使用this指针 在应用程序中,如果数据结构里有指向自身类型的成员,那么使用this指针会提供更多的方便。下面的程序中建立了一个类ListEntry的链表。 #include iostream.h #include string.h class ListEntry { char* listvalue; ListEntry* preventry; public: ListEntry(char*); ~ListEntry() { delete [] listvalue; } ListEntry* PrevEntry() const { return preventry; }; void display() const { cout<int main() { ListEntry* prev = 0; while (1) { cout < name; if (strncmp(name, end, 3) == 0) break; ListEntry* list = new ListEntry(name); if (prev != 0) prev->AddEntry(*list); prev = list; } while (prev != 0) { prev->display(); ListEntry* hold = prev; prev = prev->PrevEntry(); delete hold; } return 0; } 程序运行时,会提示输入一串姓名,当输入完毕后,键入end,然后程序会逆序显示刚才输入的所有姓名。 程序中ListEntry类含有一个字符串和一个指向前一个表项的指针。构造函数从对中获取内存分配给字符串,并把字符串的内容拷贝到内存,然后置链接指针为NULL。析构函数将释放字符串所占用的内存。 成员函数PrevEntry()返回指向链表前一个表项的指针。另一个成员函数显示当前的表项内容。 成员函数AddEntry(),它把this指针拷贝给参数的preventry指针,即把当前表项的地址赋值给下一个表项的链接指针,从而构造了一个链表。它并没有改变调用它的listEntry对象的内容,只是把该对象的地址赋给函数的参数所引用的那个ListEntry对象的preventry指针,尽管该函数不会修改对象的数据,但它并不是常量型。这是因为,它拷贝对象的地址this指针的内容给一个非长常量对象,而编译器回认为这个非常量对象就有可能通过拷贝得到的地址去修改当前对象的数据,因此AddEntry()函数在声明时不需要用const。 主体:(六)类对象数组和静态成员 一、类对象数组 类的对象和C++其他数据类型一样,也可以为其建立数组,数组的表示方法和结构一样。 #include iostream.h class Date { int mo,da,yr; public: Date(int m=0,int d=0, int y=0) { mo=m; da=d; yr=y;} void display() const { cout<int main() { Date dates[2]; Date today(12,31,2003); dates[0]=today; dates[0].display(); dates[1].display(); return 0; } 1.类对象数组和默认构造函数 在前面已经说过,不带参数或者所有参数都有默认值的构造函数叫做默认构造函数。如果类中没有构造函数,编译器会自动提供一个什么都不做的公共默认构造函数 。如果类当中至少有一个构造函数,编译器就不会提供默认构造函数。 如果类当中不含默认构造函数,则无法实例化其对象数组。因为实例花类对象数组的格式不允许用初始化值来匹配某个构造函数的参数表。 上面的程序中,main()函数声明了一个长度为2的Date对象数组,还有一个包含初始化值的单个Date对象。接着把这个初始化的Date对象赋值给数组中第一个对象,然后显示两个数组元素中包含的日期。从输出中可以看到,第一个日期是有效日期,而第二个显示的都是0。 当声明了某个类的对象数组时,编译器会为每个元素都调用默认构造函数。 下面的程序去掉了构造函数的默认参数值,并且增加了一个默认构造函数。 #include class Date { int mo, da, yr; public: Date(); Date(int m,int d,int y) { mo=m; da=d; yr=y;} void display() const { cout <int main() { Date dates[2]; Date today(12,31,2003); dates[0]=today; dates[0].display(); dates[1].display(); return 0; } 运行程序,输出为: Date constructor running Date constructor running 12/31/2003 0/0/0 从输出中可以看出,Date()这个默认构造函数被调用了两次。 2.类对象数组和析构函数 当类对象离开作用域时,编译器会为每个对象数组元素调用析构函数。 #include iostream.h class Date { int mo,da,yr; public: Date(int m=0,int d=0,int y=0) { mo=m; da=d; yr=y;} ~Date() {cout<int main() { Date dates[2]; Date today(12,31,2003); dates[0]=today; dates[0].display(); dates[1].display(); return 0; } 运行程序,输出为: 12/31/2003 0/0/0 Date destructor running Date destructor running Date destructor running 表明析构函数被调用了三次,也就是dates[0],dates[1],today这三个对象离开作用域时调用的。 二、静态成员 可以把类的成员声明为静态的。静态成员只能存在唯一的实例。所有的成员函数都可以访问这个静态成员。即使没有声明类的任何实例,静态成员也已经是存在的。不过类当中声明静态成员时并不能自动定义这个变量,必须在类定义之外来定义该成员。 1.静态数据成员 静态数据成员相当于一个全局变量,类的所有实例都可以使用它。成员函数能访问并且修改这个值。如果这个静态成员是公有的,那么类的作用域之内的所有代码(不论是在类的内部还是外部)都可以访问这个成员。下面的程序通过静态数据成员来记录链表首项和末项的地址。 #include iostream.h #include string.h class ListEntry { public: static ListEntry* firstentry; private: static ListEntry* lastentry; char* listvalue; ListEntry* nextentry; public: ListEntry(char*); ~ListEntry() { delete [] listvalue;} ListEntry* NextEntry() const { return nextentry; }; void display() const { cout<int main() { while (1) { cout<>name

22,209

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧