max=c;
printf("最小值为:
%d\n",min);
}
9.编程计算1-4+9-16+25+……-10000之和,将程序的空白地方补充完整:
#include"stdio.h"
voidmain()
{ints=0,i,p=1;
for(i=1;;i=i+3)
{
s=s+i*p;
;
}
printf("\n1-4+......-1000=%d\n",s);
}
10.编程输出1~100中能2或3或5整除的数,并统计总个数。
将程序的空白地方补充完整:
#include"stdio.h"
voidmain()
{inti,count=0;
printf("\n此程序可统计1...100间能同时被2、3、5整除的整数的个数。
\n");
for(i=1;i<=100;i++)
if()
{printf("%8d",i);//输出符合条件的数值
;
}
printf("\n符合条件的数字个数为:
%d\n",count);
}
11.线性链表的一般形式如下:
以下是用线性链表实现的一个基本系统,主要功能如下:
就此例来讲,可能涉及到的情况有如下几种:
1、添加:
发现此人到了一个新地方,需要记录下来,添加到前一个地名的后面;
2、按顺序输出:
按跟踪的情况输出此人全天的活动范围;
3、查询:
查询此人是否到过某地方;
4、删除:
发现在记录过程中把某个本没有到过的地方给错误地添加进去了,需删除;
5、插入:
发现某个到过的地方没有记录进去,需插入到已记录下来的某个地名后面;
6、修改:
某个地名记录有误,需要重新更正;
7、释放:
此系统已完成任务,不用了,释放对应的链表所占用的内存空间。
具体实现过程如下,请填空:
#include
#include
#include
structpoi_info
{
charname[31];//数据域,存放地名
structpoi_info*next;//指针域,存放后一节点地址
};//要有“;”
//按顺序录入地名,创建单链表
voidinput(structpoi_info*head)
{
intsfjx=1;
structpoi_info*q,*p;
q=head;
while(q->next!
=NULL)
q=q->next;
while(sfjx!
=0)
{
p=
if(p==NULL)
printf("\n空间分配不成功,无法进行记录!
\n");
else
{
printf("\n请输入要记录地地名:
");
scanf("%s",p->name);
q=p;
}
printf("\n是否继续(0-结束 其它-继续):
");
scanf("%d",&sfjx);
}
q->next=NULL;
system("pause");
}
//按从前往后顺序输出所有地名
voidoutput(structpoi_info*head)
{
structpoi_info*p;
p=head->next;
printf("\n以下为输出结果:
\n");
while(p!
=NULL)
{
printf("%s\n",p->name);
}
system("pause");
}
//释放链表空间,使之成为一个空链表
voidrelease(structpoi_info*head)
{
structpoi_info*p,*q;
p=head->next;
while(p!
=NULL)
{
free(p);
}
head->next=NULL;
printf("\n空间已正常释放!
\n");
system("pause");
}
//根据地名判断是此人所到过的第几个地方
voidsearch(structpoi_info*head)
{
intsfjx=1,count;
structpoi_info*p;
chardcdm[31];
while(sfjx!
=0)
{
p=head->next;
count=1;
printf("\n请输入要查询的地名:
");
scanf("%s",dcdm);
while()
{
p=p->next;
count++;
}
if(p==NULL)
printf("\n此人没有到过以下地方:
%s\n",dcdm);
else
printf("\n%s是此人到过的第%d站。
\n",dcdm,count);
printf("\n是否继续(0-结束 其它-继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
//从链表中删除指定的某些地方
voiddel(structpoi_info*head)
{
intsfjx=1;
structpoi_info*p,*q;
chardcdm[31];
while(sfjx!
=0)
{
q=head;
p=head->next;
printf("\n请输入要删除的地名:
");
scanf("%s",dcdm);
while((p!
=NULL)&&(strcmp(dcdm,p->name)!
=0))
{
p=p->next;
}
if()
printf("\n此人没有到过以下地方:
%s\n",dcdm);
else
{
free(p);
printf("\n已成功删除!
\n");
}
printf("\n是否继续(0-结束 其它-继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
//往某个地名前插入一个地名
voidinsert(structpoi_info*head)
{
intsfjx=1;
structpoi_info*p,*q,*x;
chardcdm[31];
while(sfjx!
=0)
{
q=head;
p=head->next;
printf("\n请输入要插入地的地名:
");
scanf("%s",dcdm);
while((p!
=NULL)&&(strcmp(dcdm,p->name)!
=0))
{
q=p;
p=p->next;
}
if(p==NULL)
printf("\n此人没有到过以下地方:
%s,无法确定插入位置!
\n",dcdm);
else
{
x=(structpoi_info*)malloc(sizeof(structpoi_info));
if(x==NULL)
printf("\n空间分配不成功,无法进行记录!
\n");
else
{
printf("\n请输入要插入地的地名:
");
scanf("%s",x->name);
q->next=x;
printf("\n已成功插入!
\n");
}
}
printf("\n是否继续(0-结束 其它-继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
//修改某一指定的地名
voidmodify(structpoi_info*head)
{
intsfjx=1;
structpoi_info*p;
chardcdm[31];
while(sfjx!
=0)
{
p=head->next;
printf("\n请输入要修改的地名:
");
scanf("%s",dcdm);
while((p!
=NULL)&&(strcmp(dcdm,p->name)!
=0))
if(p==NULL)
printf("\n此人没有到过以下地方:
%s\n",dcdm);
else
{
printf("\n原地名为%s,请输入新地名:
:
",dcdm);
scanf("%s",p->name);
printf("\n已成功修改!
\n");
}
printf("\n是否继续(0-结束 其它-继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
voidmain()
{
intxz=1;
structpoi_info*head;
head=(structpoi_info*)malloc(sizeof(structpoi_info));
if(head==NULL)
printf("\n空间分配不成功!
\n");
else
{
head->next=NULL;
while(xz!
=0)
{
printf("\n 欢迎使用民用跟踪记录系统!
\n\n\n");
printf("1-添加 2-输出 3-查询 4-删除 5-修改 6-插入 0-退出\n");
printf("\n请选择:
");
scanf("%d",&xz);
if(xz==1)
input(head);
else
if(xz==2)
output(head);
else
if(xz==3)
else
if(xz==4)
del(head);
else
if(xz==5)
else
if(xz==6)
insert(head);
else
if(xz==0)
{
release(head);
free(head);
}
else
printf("\n无此选项!
\n");
}
}
}
12.以下是一个简单的“班级基本信息管理系统”的实现代码,请补充完整:
#include
#include
#include
#defineN100//最大人数
structstu_info
{
charxh[15];//学号,目前学号为14位
charxm[7];//姓名
charxb[3];//性别
intnl;//年龄
};
structstu_infostu[N+1];//用于存放所有人相关信息,0号元素备用
intCurrentCount=0;//当前实际人数
voidinput()//录入模块
{
charsfjx=1;
while(sfjx!
=0)
{
if(CurrentCount==N)
{
printf("\n人数已达上限,不能添加!
!
!
\n");
sfjx=0;
}
else
{
printf("\n请输入一个人员的相关信息(学号姓名性别年龄):
");
scanf("%s%s%s%d",stu[CurrentCount].xh,stu[CurrentCount].xm,stu[CurrentCount].xb,&stu[CurrentCount].nl);
printf("\n是否继续(0--结束,其它--继续):
");
scanf("%d",&sfjx);
}
}
system("pause");
}
voidsave()//保存模块
{
FILE*fp;
if(fp==NULL)
printf("\n文件打开不成功,信息无法保存!
!
!
\n");
else
{
for(inti=1;i<=CurrentCount;i++)
fprintf(fp,"\n%16s%8s%4s%4d",stu[i].xh,stu[i].xm,stu[i].xb,stu[i].nl);
printf("\n信息已成功保存!
!
!
\n");
}
system("pause");
}
voidread()//读盘模块
{
FILE*fp;
if(fp==NULL)
printf("\n文件打开不成功,信息无法读取!
!
!
\n");
else
{
fscanf(fp,"%d",&CurrentCount);
for(inti=1;i<=CurrentCount;i++)
{
fscanf(fp,"%s%s%s%d",stu[i].xh,stu[i].xm,stu[i].xb,&stu[i].nl);
printf("学号:
%s 姓名:
%s 性别:
%s 年龄:
%d\n",stu[i].xh,stu[i].xm,stu[i].xb,stu[i].nl);
}
fclose(fp);
printf("\n信息已成功读取!
!
!
\n");
}
system("pause");
}
voidsearch()//查询模块
{
chardcxh[15];
intsfjx=1,i;
while(sfjx!
=0)
{
printf("\n请输入一个待查学员的学号:
");
scanf("%s",dcxh);
i=CurrentCount;
while( )
i--;
if(i==0)
printf("查无此人!
!
!
\n");
else
{
printf("\n此人详细信息如下:
\n");
printf("学号:
%s 姓名:
%s 性别:
%s 年龄:
%d\n",stu[i].xh,stu[i].xm,stu[i].xb,stu[i].nl);
}
printf("\n是否继续(0--结束,其它--继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
voiddel()//删除模块
{
chardcxh[15];
intsfjx=1,i,j;
while(sfjx!
=0)
{
printf("\n请输入一个待删学员的学号:
");
scanf("%s",dcxh);
i=CurrentCount;
while(strcmp(stu[i].xh,dcxh)!
=0)
i--;
if(i==0)
printf("查无此人!
!
!
\n");
else
{
printf("\n此人详细信息如下:
\n");
printf("学号:
%s 姓名:
%s 性别:
%s 年龄:
%d\n",stu[i].xh,stu[i].xm,stu[i].xb,stu[i].nl);
printf("\n按任意键开始删除......\n");
system("pause");
for(j=i+1;j<=CurrentCount;j++)
CurrentCount--;
printf("\n已成功删除......\n");
system("pause");
}
printf("\n是否继续(0--结束,其它--继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
voidmodify()//修改模块
{
chardcxh[15];
intsfjx=1,i;
while(sfjx!
=0)
{
printf("\n请输入一个待修改学员的学号:
");
scanf("%s",dcxh);
i=CurrentCount;
while(strcmp(stu[i].xh,dcxh)!
=0)
i--;
if(i==0)
printf("查无此人!
!
!
\n");
else
{
printf("\n此人详细信息如下:
\n");
printf("学号:
%s 姓名:
%s 性别:
%s 年龄:
%d\n",stu[i].xh,stu[i].xm,stu[i].xb,stu[i].nl);
printf("\n请输入新内容......\n");
printf("\n请输入一个人员的相关信息(学号姓名性别年龄):
");
scanf("%s%s%s%d",stu[i].xh,stu[i].xm,stu[i].xb,&stu[i].nl);
printf("\n已成功修改......\n");
system("pause");
}
printf("\n是否继续(0--结束,其它--继续):
");
scanf("%d",&sfjx);
}
system("pause");
}
voidlist()//删除模块
{
inti,j;
for(i=1;ifor(j=CurrentCount;j>i;j--)
if(strcmp(stu[j].xh,stu[j-1].xh)<0)
{
stu[0]=stu[j];
}
printf("\n班级基本信息表\n");
printf("序号 学号 姓名 性别 年龄\n");
for(i=1;i<=CurrentCount;i++)
printf("%4d%s%16s%6s%6d\n",i,stu[i].xh,stu[i].xm,stu[i].xb,stu[i].nl);
system("pause");
}
intcheck()
{
intcount=0,name,pass;
while(count<=2)
{
printf("\n请输入用户名及密码:
");
scanf("%d%d",&name,&pass);
count++;
if((name==1)&&(pass==1))
count=10;
else
if(count>2)
count=5;
}
if(count==10)
return0;
else
return1;
}
voidmain()
{
intxz=1;
printf("\n欢迎使用班级基本信息管理系统\n\n\n");
if(check()!
=0)
{
printf("\n你无权使用本系统......\n\n");
system("pause");
}
else
{
while(xz!
=0)
{
printf("\n请选择相应功能:
\n");
printf("1-录入\n2-查询\n3-修改\n4-删除\n5-保存\n6-读取\n7-按学号列表\n0-结束\n请输入选择:
");
scanf("%d",&xz);
switch(xz)
{
case1:
input();break;
case2:
search();break;
case3:
modify();break;
case4:
del();break;
case5:
case6:
read();break;
case7:
list();break;
case0:
printf("\n\n谢谢使用本系统!
\n\n");system("pause");break;
default:
printf("\n无此功能,请重新选择......\n");
system("pause");
}
}
}
}