建筑工地.docx
《建筑工地.docx》由会员分享,可在线阅读,更多相关《建筑工地.docx(48页珍藏版)》请在冰豆网上搜索。
建筑工地
一题目要求
建立如下四个文件:
(1)销售商—配件—建筑工地(建筑商号码,配件号,工地号,数量)
(2)建筑工地(工地号,名字,城市)
(3)配件(配件号,配件名,颜色,重量,产地)
(4)销售商(号码,名字,状态,城市)
功能:
(1)首先建立文件,然后插入数据,并进行删除,更新操作,最后进行查询操作。
(2)查询与供应商所在城市相同的零件—供应商,项目—供应商记录对。
二需求分析
根据题目要求,由于销售商—配件—建筑工地,建筑工地,配件,销售商的各种信息要求以文本文件的方式存放在四个文件中,所以应提供文件的输入,输出等操作;在程序中要浏览销售商—配件—建筑工地,建筑工地,配件,销售商的各种信息,应提供显示,查找,增加,更新等操作;另外还应提供键盘式选择菜单实现功能选择。
三总体设计
我们来分析一下整个系统,根据上面的需求分析,可以将这个系统的设计分为如下五大模块:
检查,更新,增加,删除,退出。
其中检查,更新,增加,删除是四个主要功能。
四详细设计
(1)定义结构体
#include"stdio.h"
#include"string.h"
#defineSPBstructseller_fit_buildingC语言
#defineBUDstructbuilding
#definePEJstructfit
#defineXSSstructseller
intm;
SPB/*定义结构体spb代表销售商—配件—建筑工地*/
{
intseller_num;
intfit_num;
intbuilding_num;
intcount;
}spb[100];
BUD/*定义结构体bud代表建筑工地*/
{intnum;
charname[20];
charcity[20];
}bud[100];
PEJ/*定义结构体pej代表配件*/
{
intnum;
charname[20];
charcoulor[20];
intweight;
charmakeplace[20];
}pej[100];
XSS/*定义结构体xss代表销售商*/
{
intnum;
charname[20];
charstate[20];
charcity[20];
}xss[100];
voidTextOut(){/*主功能菜单*/
printf("\t\twannacheck,press-------------0.\n\n");
printf("\t\twannaADD,press---------------1.\n\n");
printf("\t\twannaupdate,press------------2.\n\n");
printf("\t\twannadelete,press------------3.\n\n");
printf("\t\twannaexit,press---------------4.\n\n");
}
voidinput_spb(inti)/*输入结构体结点spb的信息*/
{
printf("seller'snumber:
\n");
scanf("%d",&spb[i].seller_num);
printf("fit'snumber:
\n");
scanf("%d",&spb[i].fit_num);
printf("building'snumber:
\n");
scanf("%d",&spb[i].building_num);
printf("count'snumber:
\n");
scanf("%d",&spb[i].count);
}
voidinput_bud(inti)/*输入结构体结点bud的信息*/
{
printf("building'snumber:
\n");
scanf("%d",&bud[i].num);
printf("name:
\n");
scanf("%s",&bud[i].name);
printf("city:
\n");
scanf("%s",&bud[i].city);
}
voidinput_pej(inti)/*输入结构体结点pej的信息*/
{
printf("fit'snumber:
\n");
scanf("%d",&pej[i].num);
printf("name:
\n");
scanf("%s",&pej[i].name);
printf("coulor:
\n");
scanf("%s",&pej[i].coulor);
printf("weight:
\n");
scanf("%d",&pej[i].weight);
printf("makeplace:
\n");
scanf("%s",&pej[i].makeplace);
}
voidinput_xss(inti)/*输入结构体结点xss的信息*/
{
printf("number:
\n");
scanf("%d",&xss[i].num);
printf("name:
\n");
scanf("%s",&xss[i].name);
printf("state:
\n");
scanf("%s",&xss[i].state);
printf("city:
\n");
scanf("%s",&xss[i].city);
}
voidenter()/*输入文件信息*/
{
inti,c;
do
{
puts("\nenterby=>\n\n1)spb2)bud3)pej4)xss5)exit");
printf("whichyouneeded?
:
\n");
scanf("%d",&c);
if(c<1||c>5)
puts("\nChioseerror!
Pleaseagain!
");
}
while(c<1||c>5);
printf("Howmangdates?
:
\n");
scanf("%d",&m);
for(i=0;i{
printf("Input%dthdate\n",i+1);
switch(c)
{
case1:
input_spb(i);break;
case2:
input_bud(i);break;
case3:
input_pej(i);break;
case4:
input_xss(i);break;
}
}
switch(c)
{
case1:
if(i!
=0)save_spb(m,spb);break;
case2:
if(i!
=0)save_bud(m,bud);break;
case3:
if(i!
=0)save_pej(m,pej);break;
case4:
if(i!
=0)save_xss(m,xss);break;
}
}
2,主函数
主函数一般设计得比较简单,只提供输入,功能处理和输出部分的函数调用.其中各功能模块用菜单方式选择.
[程序]
main(){
intch;
TextOut();
scanf("%d",&ch);
while(ch!
=4){/*对选择的数字做判断*/
if(ch==0){/*检查模块*/
IndexFile(Choise());
printf("Youhavesuccessfullyindexthereader!
\n");
TextOut();
scanf("%d",&ch);
continue;
}
if(ch==1){/*增加模块*/
enter();
printf("Youhavesuccessfullyaddareader!
\n");
TextOut();
scanf("%d",&ch);
continue;
}
if(ch==2){/*更新模块*/
UpdateFile(Choise());
printf("Youhavesuccessfullyupdatethelist!
\n");
TextOut();
scanf("%d",&ch);
continue;
}
if(ch==3){/*删除模块*/
DeleteFile(Choise());
printf("Youhavesuccessfullyoutputthelist!
\n");
TextOut();
scanf("%d",&ch);
continue;
}
elsereturn;
}
}
3各功能模块设计
(1)检查模块
intChoise(){/*选择要操作的文件*/
intj;
printf("ifyouwanttomanipulatefilespb.txt.pleasepress1.\n");
printf("ifyouwanttomanipulatefilebud.txt.pleasepress2.\n");
printf("ifyouwanttomanipulatefilepej.txt.pleasepress3.\n");
printf("ifyouwanttomanipulatefilexss.txt.pleasepress4.\n");
scanf("%d",&j);
returnj;
}
voidsave_spb(intm,SPB*curr){/*将结构体spb的结点保存在文件spb.txt中*/
FILE*fp;
inti;
if((fp=fopen("spb.txt","at"))==NULL)
printf("\nCannotopenfile\n");
for(i=0;ifprintf(fp,"%10d%10d%10d%10d\n"
curr[i].seller_num,curr[i].fit_num,curr[i].building_num,curr[i].count);
fclose(fp);
}
voidsave_bud(intm,BUD*curr){/*将结构体bud的结点保存在文件bud.txt中*/
FILE*fp;
inti;
if((fp=fopen("bud.txt","at"))==NULL)
printf("\nCannotopenfile\n");
for(i=0;ifprintf(fp,"%10d%20s%20s\n"
curr[i].num,curr[i].name,curr[i].city);
fclose(fp);
}
voidsave_pej(intm,PEJ*curr){/*将结构体pej的结点保存在文件pej.txt中*/
FILE*fp;
inti;
if((fp=fopen("pej.txt","at"))==NULL)
printf("\nCannotopenfile\n");
for(i=0;ifprintf(fp,"%10d%20s%20s%10d%20s\n"
curr[i].num,curr[i].name,curr[i].coulor,curr[i].weight,curr[i].makeplace);
fclose(fp);
}
voidsave_xss(intm,XSS*curr){/*将结构体xss的结点保存在文件xss.txt中*/
FILE*fp;
inti;
if((fp=fopen("xss.txt","at"))==NULL)
printf("\nCannotopenfile\n");
for(i=0;ifprintf(fp,"%10d%20s%20s%20s\n"
curr[i].num,curr[i].name,curr[i].state,curr[i].city);
fclose(fp);
}
(2)更新模块
voidUpdateFile(intm){/*更新文件*/
SPBcurrnode1,movenode1;
BUDcurrnode2,movenode2;
PEJcurrnode3,movenode3;
XSScurrnode4,movenode4;
inti=0,j=0;
intnum;
charname[20];
FILE*fp;
if(m==1){/*更新文件spb.txt*/
printf("pleaseinputtheseller_numofspb:
\n");
scanf("%d",&num);
printf("pleaseinputthenewmessageofspb:
\n");
printf("seller'snumber:
\n");
scanf("%d",&movenode1.seller_num);
printf("fit'snumber:
\n");
scanf("%d",&movenode1.fit_num);
printf("building'snumber:
\n");
scanf("%d",&movenode1.building_num);
printf("count'snumber:
\n");
scanf("%d",&movenode1.count);
if((fp=fopen("spb.txt","rt+"))==NULL){
printf("%s\n","CannotopenthefileorCannotfindthefile.pleasecheckout.");
}
i=ftell(fp);
fscanf(fp,"%d%d%d%d"
&currnode1.seller_num,&currnode1.fit_num,&currnode1.building_num
&currnode1.count);
j=ftell(fp);
j=j-i;
rewind(fp);
while(!
feof(fp)){
fscanf(fp,"%d%d%d%d"
&currnode1.seller_num,&currnode1.fit_num,&currnode1.building_num,&currnode1.count);
if(feof(fp))
break;
if(num==currnode1.seller_num){
fseek(fp,-j,1);
fprintf(fp,"%10d%10d%10d%10d\n"
movenode1.seller_num,movenode1.fit_num,movenode1.building_num,movenode1.count);
fseek(fp,2L,1);
}
}
}
if(m==2){/*更新文件bud.txt*/
printf("pleaseinputthenumofbud:
\n");
scanf("%d",&num);
printf("pleaseinputthenewmessageofbud:
\n");
printf("building'snumber:
\n");
scanf("%d",&movenode2.num);
printf("name:
\n");
scanf("%s",movenode2.name);
printf("city:
\n");
scanf("%s",movenode2.city);
if((fp=fopen("bud.txt","rt+"))==NULL){
printf("%s\n","CannotopenthefileorCannotfindthefile.pleasecheckout.");
}
i=ftell(fp);
fscanf(fp,"%d%s%s"
&currnode2.num,currnode2.name,currnode2.city);
j=ftell(fp);
j=j-i;
rewind(fp);
while(!
feof(fp)){
fscanf(fp,"%d%s%s"
&currnode2.num,currnode2.name,currnode2.city);
if(feof(fp))
break;
if(num==currnode2.num){
fseek(fp,-j,1);
fprintf(fp,"%10d%20s%20s\n"
movenode2.num,movenode2.name,movenode2.city);
fseek(fp,2L,1);
}
}
}
if(m==3){/*更新文件pej.txt*/
printf("pleaseinputthenumofpej:
\n");
scanf("%d",&num);
printf("fit'snumber:
\n");
scanf("%d",&movenode3.num);
printf("name:
\n");
scanf("%s",movenode3.name);
printf("coulor:
\n");
scanf("%s",movenode3.coulor);
printf("weight:
\n");
scanf("%d",&movenode3.weight);
printf("makeplace:
\n");
scanf("%s",movenode3.makeplace);
if((fp=fopen("pej.txt","rt+"))==NULL){
printf("%s\n","CannotopenthefileorCannotfindthefile.pleasecheckout.");
}
i=ftell(fp);
fscanf(fp,"%d%s%s%d%s"
&currnode3.num,currnode3.name,currnode3.coulor
&currnode3.weight,currnode3.makeplace);
j=ftell(fp);
j=j-i;
rewind(fp);
while(!
feof(fp)){
fscanf(fp,"%d%s%s%d%s"
&currnode3.num,currnode3.name,currnode3.coulor
&currnode3.weight,currnode3.makeplace);
if(feof(fp))
break;
if(num==currnode3.num){
fseek(fp,-j,1);
fprintf(fp,"%10d%20s%20s%10d%20s\n"
movenode3.num,movenode3.name,movenode3.coulor,movenode3.weight,movenode3.makeplace);
fseek(fp,2L,1);
}
}
}
if(m==4){/*更新文件xss.txt*/
printf("pleaseinputthenumofxss:
\n");
scanf("%d",&num);
printf("number:
\n");
scanf("%d",&movenode4.num);
printf("name:
\n");
scanf("%s",movenode4.name);
printf("state:
\n");
scanf("%s",movenode4.state);
printf("city:
\n");
scanf("%s",movenode4.city);
if((fp=fopen("xss.txt","rt+"))==NULL){
printf("%s\n","CannotopenthefileorCannotfindthefile.pleasecheckout.");
}
i=ftell(fp);
fscanf(fp,"%d%d%s%s%s"
&currnode4.num,currnode4.name,currnode4.state,currnode4.city);
j=ftell(fp);
j=j-i;
rewind(fp);
while(!
feof(fp)){
fscanf(fp,"%d%d%s%s%s"
&currnode4.num,currnode4.name,currnode4.state,currnode4.city);
if(feof(fp))
break;
if(num==currnode4.num){
fseek(fp,-j,1);
fprintf(fp,"%10d%20s%20s%20s\n"
movenode4.num,movenode4.name,movenode4.state,movenode4.city);
fseek(fp,2L,1);
}
}
}
}
(3)删除模块
voidDeleteFile(intm){/*删除文件信息*/
SPBcurrnode1;
BUDcurrnode2;
PEJcurrnode3;
XSScurrnode4;
inti=0,j=0;
intnum;
charname[20];
FILE*fp;
if(m==1){/*删除文件spb.txt信息*/
printf("pleaseinputtheseller_numofspb:
\n");
scanf("%d",&num);
if((fp=fopen("spb.txt","rt+"))==NULL){
printf("%s\n","CannotopenthefileorCannotfindthefile.pleasecheckout.");
}
i=ftell(fp);
fscanf(fp,"%d%