求大神发一个c语言写的代码 公交路线的查询 具体要求【用户能够方便查询某条公交路线的详细信息。】
c语言公交查询系统设计绝对给力嘚,原创!
这是我写的程序 希望能给你参考
#include
#include
#include
#include
#define NULL 0
int n=0;
int aa[20]={0};
struct road
{long num;
char stage[20][20];
struct road *next;
};
struct road *create()
{struct road *head,*p1,*p2;
int m,i;
char yes_no;
FILE *fp,*fp1;
fp=fopen("d:\
izaoran.txt","w");
if(fp==NULL)
{printf("file cant be open
");
exit(1);
}
fp1=fopen("d:\ecord.txt","w");
if(fp1==NULL)
{printf("file can't be open
");
exit(1);
}
head=NULL;
do
{p1=(struct road*)malloc(LEN);
printf("please input %d num:",n+1);
scanf("%ld",&p1->num);
fprintf(fp,"%ld",p1->num);
printf("how many stages?");
scanf("%d",&aa[n]);
fprintf(fp1,"%d
",aa[n]);
for(i=0;i<aa[n];i++)
{getchar();
printf("please input the %d station:",i+1);
gets(p1->stage[i]);
fprintf(fp," %s",&p1->stage[i]);
}
fputc('
',fp);
p1->next=NULL;
n++;
if(n==1) head=p1;
else p2->next=p1;
p2=p1;
printf("do you want to continue (Y/N)?");
scanf("%c",&yes_no);
}while(yes_no=='y'||yes_no=='Y');
fclose(fp);
free (p1);
return head;
}
void add()
{
struct road temp;
int i=0;
FILE *fp;
fp=fopen("d:\
izaoran.txt","a");
printf("input the route you want to add:");
scanf("%ld",&temp.num);
fprintf(fp,"%ld",temp.num);
printf("please input how many stages:");
scanf("%d",&aa[n]);
while(i<aa[n])
{getchar();
printf("please input the %d stage
",i+1);
gets(temp.stage[i]);
fprintf(fp," %s",temp.stage[i]);
i++;
}
fprintf('
',fp);
n++;
fclose(fp);
}
void display()
{FILE *fp;
char ch;
if((fp=fopen("d:\
izaoran.txt","r"))==NULL)
{printf("cant open file
");
exit(1);
}
while(!feof(fp))
{ch=fgetc(fp);
printf("%c",ch);
}
fclose(fp);
}
void modify()
{ FILE *fp;
struct road temp[20];
char a[6],b[20],ch;
int i=0,j=0,k=0,m=0,t=0;
long num;
if((fp=fopen("d:\
izaoran.txt","r"))==NULL)
{printf("cant open file
");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("which do you want to modify(num/stage)?");
getchar();
gets(a);
if(strcmp("num",a)==0)
{printf("which num:");
scanf("%ld",&num);
if((fp=fopen("d:\
izaoran.txt","w"))==NULL)
{printf("cant open file
");
exit(1);
}
for(j=0;j<n;j++)
if(temp[j].num==num)
{printf("please input the right num:");
scanf("%ld",&temp[j].num);
break;
}
if(j==n) printf("no find
");
}
else if(strcmp("stage",a)==0)
{if((fp=fopen("d:\
izaoran.txt","w"))==NULL)
{printf("cant open file
");
exit(1);
}
printf("the num and the stage:");
scanf("%ld%s",&num,b);
for(j=0;j<n;j++)
{if(temp[j].num==num)
{ for(i=0;i<aa[j];i++)
if(strcmp(temp[j].stage[i],b)==0)
{printf("please input the right stage
");
getchar();
gets(temp[j].stage[i]);
break;
}
if(i==aa[j])
printf("no fould");
break;
}
}
if(j==n)
printf("no found
");
}
for(i=0;i<n;i++)
{fprintf(fp,"%ld",temp[i].num);
for(j=0;j<aa[i];j++)
fprintf(fp," %s",temp[i].stage[j]);
fputc('
',fp);
}
fclose(fp);
}
void delete()
{FILE *fp;
long num;
struct road temp[20];
int i=0,j=0;
if((fp=fopen("d:\
izaoran.txt","r"))==NULL)
{printf("cant open file
");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("input the num you want to delete
");
scanf("%ld",&num);
for(i=0;i<n;i++)
{
if(temp[i].num==num)
break;
}
if(i==n)
{printf("no found
");
return;
}
else
{
temp[i].num=0;
}
fp=fopen("d:\
izaoran.txt","w");
for(i=0;i<n;i++)
{
if(temp[i].num!=0)
{
fprintf(fp,"%ld",temp[i].num);
printf("%ld",temp[i].num);
for(j=0;j<aa[i];j++)
{fprintf(fp," %s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fputc('
',fp);
putchar('
');
}
}
fclose(fp);
n--;
printf("
%d
",n);
}
void search_num()
{
FILE *fp;
struct road temp[20];
int i,j,i1=0,j1=0;
long num;
if((fp=fopen("d:\
izaoran.txt","r"))==NULL)
{printf("cant open file
");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("please input the number you want to search
");
scanf("%ld",&num);
for(i1=0;i1<n;i1++)
{if(temp[i1].num==num)
break;
}
if(i1<n)
{ printf("%ld",temp[i1].num);
for(j1=0;j1<aa[i1];j1++)
{putchar(' ');
puts(temp[i1].stage[j1]);
}
putchar('
');
}
else printf("no found
");
}
void search_stage()
{
FILE *fp;
struct road temp[20];
int i,j,t,flag=0;
char a[20];
if((fp=fopen("d:\
izaoran.txt","r"))==NULL)
{printf("cant open file
");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("please input the stage you want to search
");
getchar();
gets(a);
for(i=0;i<n;i++)
for(j=0;j<aa[i];j++)
if(strcmp(temp[i].stage[j],a)==0)
{printf("%ld",temp[i].num);
for(t=0;t<aa[i];t++)
{putchar(" ");
puts(temp[i].stage[t]);
}
putchar('
');
flag=1;
}
if(flag==0) printf("no found
");
}
void search_route()
{
FILE *fp;
struct road temp[20];
int i,j,first=0,second=0,fir=0,sec=0;
char a[20],b[20];
if((fp=fopen("d:\
izaoran.txt","r"))==NULL)
{printf("cant open file
");
exit(1);
}
for(i=0;i<n;i++)
{fscanf(fp,"%ld",&temp[i].num);
printf("%ld ",temp[i].num);
for(j=0;j<aa[i];j++)
{fscanf(fp,"%s",temp[i].stage[j]);
putchar(' ');
puts(temp[i].stage[j]);
}
fgetc(fp);
}
fclose(fp);
printf("please input the first stage
");
getchar();
gets(a);
printf("please input the second stage
");
getchar();
gets(b);
for(i=0;i<n;i++)
for(j=0;j<aa[i];j++)
{if(strcmp(temp[i].stage[j],a)==0)
{first=i;
fir=j;
}
if(strcmp(temp[i].stage[j],a)==0)
{second=i;
sec=j;
}
}
if(first==second)
{if(fir>sec)
{t=fir;
fir=sec;
sec=t;
}
printf("%ld",temp[first].num);
for(t=fir;t<=sec;t++)
{ putchar(" ");
puts(temp[fisrt].stage[t]);
}
}
else printf("not the same routine
");
}
void search()
{int i;
printf("which way do you want(1,2,3):
");
scanf("%d",&i);
switch(i)
{case 1 : search_num();break;
case 2 : search_stage();break;
case 3 : search_route();break;
}
}
void fileout()
{FILE fp1;
int i;
if((fp1=fopen("d:\ecoord.txt","r"))==NULL);
{n=0;
for(i=0;i<20;i++)
aa[i]=0;
}
else
{fscanf(fp1,"%d",&n);
for(i=0;i<20;i++)
fscanf(fp1," %d",&aa[i]);
}
fclose(fp1);
}
void filein()
{FILE fp1;
int i,count=0;
if((fp1=fopen("d:\ecoord.txt","r"))==NULL)
{printf("file cant be opened
");
exit(1);
}
else {fprintf(fp1,"%d",n);
for(i=0;i<20;i++)
if(aa[i]>0)
{fprintf(fp1," %d",aa[i]);
count++;
}
if(count<20)
for(i=count;i<20;i++)
fprintf(fp1,"%d",0);
}
fclose(fp1);
}
main()
{int choose;
char yes_no;
fileoout();
do
{clrscr();
printf(" ******************************
");
printf(" please input your choose(0~6):
");
printf(" ******************************
");
printf(" ********1-create route********
");
printf(" ********2-display route*******
");
printf(" ********3-search route********
");
printf(" ********4-modify route********
");
printf(" ********5-add route***********
");
printf(" ********6-delete route********
");
printf(" ********0-exit****************
");
printf(" input your choose:");
scanf("%d",&choose);
printf("
");
switch(choose)
{case 1 : create();break;
case 2 : display();break;
case 3 : search();break;
case 4 : modify();break;
case 5 : add();break;
case 6 : delete();break;
case 0 : exit(0);
default: printf("error");
}
getchar();
printf("
do you want to continue to choose menu (y/n)?
");
do
{ yes_no=getchar();
}while(yes_no!='Y'&&yes_no!='y'&&yes_no!='N'&&yes_no!='n');
}while(yes_no=='Y'||yes_no=='y');
filiin();
}
#define CHK_BY_LINE_ID 0
#define CHK_BY_BUS_ID 1
#define CHK_ERROR (-1)
#define MAX_LINE_ID 50
#define MAX_BUS_NUM 10
typedef struct
{
char bus_id[16];//公交车车牌号
unsigned char running_state;//运营状态,是收车了,还是在运营
unsigned short stop_id;//车站ID,表示公交车已经运行到哪个站了
unsigned char is_forward;//1表示从起点往终点开,0表示从终点往起点开
//其他你需要定义的内容,自己想吧
} BUS_INFO;//该结构体表示公交的信息
//公交车信息初始化
void init_bus_info(BUS_INFO **buses,int lines, int bus_numbers);
//打印公交车信息
void show_bus_info(BUS_INFO *buses, int lines);
//通过公交车线路编号,在buses中查询该线路的所有车辆信息
void search_bus_info_by_line_id(BUS_INFO **buses, unsignedshort line_id);
//通过公交车车牌号,在buses中查询该车的信息
void search_bus_info_by_bus_id(BUS_INFO **buses, char *bus_id_ptr);
//检查查询条件,是公交车线路编号,还是公交车车牌号,或无效的查询条件
int check_by_what(const char *key);
//循环函数
void loops(BUS_INFO **bus_info);
int main(int argc, char **argv)
{
//MAX_LINE_ID个线路,每个线路MAX_BUS_NUM辆车
BUS_INFO buses[MAX_LINE_ID][ MAX_BUS_NUM];
init_bus_info(buses, MAX_LINE_ID, MAX_BUS_NUM);//公交车信息初始化
loops(buses);//进程在此出于工作状态
return0; // 程序退出
}
void init_bus_info(BUS_INFO **buses,int line_numbers, int bus_numbers)
{
//初始化函数可以用手动输入信息的形式,也可以用自动的形式,我这里就用自动的方式了
//当然,手动形式初始化,函数的入参应采用另外的形式
int lines, bus;
for (lines=0; lines < line_numbers; ++ lines)
for (bus = 0; bus< bus_numbers; ++bus)
{
sprint(buses[lines][bus]. bus_id, “PRC-%d-%d”, lines, bus );//车牌号
buses[lines][bus]. running_state = 1;//运营中
buses[lines][bus]. stop_id = ((lines <8)& 0x0000FF00)| (bus & 0x000000FF);//位操作,lines为高字节号,bus为低字节号,两者共同构成车站号
buses[lines][bus]. is_forward = 1;
}
return;
}
int check_by_what(const char *key)
{
int i = 0;
int is_num = 1;
while (key[i] != ‘\0’)//未走到字符串尾部
{
if (key[i] >= ‘0’ && key[i] <= ‘9’)
i++;
else
{
is_num = 0;
break;
}
}
if (is_num)//is_num未被置0,说明全是数字
return CHK_BY_LINE_ID;
//车牌号有效性检查
if(0 == strncmp(key, “PRC-”, 4))
return CHK_BY_BUS_ID;
else
returnCHK_ERROR;
}
void show_bus_info(BUS_INFO *buses, int lines)
{
printf(“LINE: %d BUS_ID:%s STATE:%d STOP_ID:%d IS_FORWARD:%d\n”,lines, buses->bus_id, buses->running_state,buses->stop_id, buses->is_forward);
return;
}
void search_bus_info_by_line_id(BUS_INFO **buses, unsignedshort line_id)
{
int index;
if(line_id<= MAX_LINE_ID)//判断,防止数组越界
for (index=0; index< MAX_BUS_NUM; index++)
show_bus_info(&buses[line_id- 1][index], line_id);
else
printf(“Invalid bus line!!!\n”);
return;
}
void search_bus_info_by_bus_id(BUS_INFO **buses, char *bus_id_ptr)
{
int index1,index2;
int is_find = 0;
for (index1=0; index1< MAX_LINE_ID; ++index1 )
{
if(is_find )
break;//找到了,跳出外层循环
for (index2=0;index2 < MAX_BUS_NUM; ++index2 )
if (0 ==strcmp(bus_id_ptr, buses[index1][index2].bus_id ))
{
show_bus_info(&buses[index1][index2], index1);
is_find = 1;//is_find置为1,表示找到了,就不用再找了
// 因为车牌号是唯一的
break;//跳出内层循环
}
}
return;
}
void loops(BUS_INFO **bus_info)
{
char inputs[17];//为什么比车牌号缓冲区要多一个字符,自己想
int check_cond;
while (1)//死循环,学校教学的时候,告诉我们不能用这样的死循环
//今天在我看来,是错误的
//实际应用中,大多数后台进程都是死循环一直在运行
//关键是要在循环过程中,如何避免无谓的耗费cpu资源,如加阻塞函数
{
memset(inputs,0, sizeof(inputs));
printf(“Please input condition to search:”);
fflush(stdout);//上句没有“\n”,用此函数刷新标准输出
fgets(inputs, sizeof(inputs)-2, stdin);//从标准输入读取,是个阻塞函数
//没有输入时,一直停留在这一步,不耗费cpu资源;
inputs[strlen(inputs)-1] = 0;//把末尾换行符(不是我们想要的)替换为字符串的结束符
check_cond = check_by_what(inputs);
if (check_cond == CHK_BY_LINE_ID)
search_bus_info_by_line_id(bus_info,atoi(inputs));// atoi把字符串转化为数字
else if (check_cond == CHK_BY_BUS_ID)
search_bus_info_by_bus_id(bus_info, inputs);
else
printf(“Invalid condition!!!\n”);
}
return;//执行不到这一步,但还是写上,养成良好编程风格
}
先写到这里吧。不知道你是不是学生,为了完成作业,若是,这样可不好哦!
另外,该程序只有简单的查询功能,若想拓展新的功能,如:可实时更新公交车信息,可采用多线程编程,或其他方式。若你是学生的话,慢慢学吧,嘿嘿哟!
哎,刚直看了你的问题,没看到问题下面的要求。
公交车信息的录入,可以在loops函数里增加,要通过判断你从键盘输入了什么内容,比如格式化的命令,如:refresh info bus-id <id> running_state <value>。只是举个例子而已。
另外,也可以采用我上面说的多线程编程,这样更好,但是要主要互斥、加锁的问题。
由于本人是个上班的爷们,要是完成你所要的所有功能,非要写到半夜不可,所以就不给你多弄了,你自己学者弄吧。
求大神写一段C语言程序
if(target == a[i]){ for(int j = i + 1;j < MAX;j ++)a[j - 1] = a[j];return;} } } void insert(int *a,int target){ a[MAX -1] = target;} 带了点c++的东西 vc6.0测试通过
求大神帮忙用C语言写一个程序。 要求:定义两个线性表(内容由用户输入...
\/\/ 将两个线性表合并并对元素进行排序 void merge_list(List *dest, List *li1, List *li2){ list_init(dest, li1->top+li2->top+1);\/\/ 把线性表li1,li2的所有元素添加到线性表dest中 int i = 0;while(i < li1->top){ add_list(dest, li1->data[i]);i++;} i = 0;...
求大神帮忙,用c语言写一个程序。
include <stdio.h>int main(){ float r; float c,s; scanf("%f",&r); c=2*3.14*r; s=3.14*r*r; printf("%f %f\\n", c,s); return 0;}
求一个c语言代码,要就是长
int s, int t, int pos, int key) {if (s == t && s == pos) { a[x] = key; b[x] = key; return; }int mid = (s + t) >> 1;if (pos <= mid) change(x << 1, s, mid, pos,
求C语言大神帮忙编写一个程序,按要求写出来。
void rotate( int (*arrA)[MAX], int (*arrB)[MAX], int m, int n);int main(void){ int arrA[MAX][MAX], arrB[MAX][MAX], i, j, m, n;printf("请输入 m n: ");scanf("%d %d", &m, &n);printf("请输入 %d 行 %d 列矩阵:\\n", m, n);for (i=0; i<m; i...
有没有C语言大神啊,帮写个程序呗。输入一个五位数的正整数,按顺序输出...
1.从个位开始输出 include <stdio.h> int main(int argc, char *argv[]){ int a,n;printf("输入一个五位数正整数:");scanf("%d",&a);while(a>0){n=a%10;printf("%d\\n",n);a=a\/10;} return 0;} 2.从最高(万)位开始输出 include <stdio.h> int main(int argc, char *...
跪求编程大神~用c语言编个程序
题目描述输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。输入第一行输入二叉树的先序遍历序列;第二行输入二叉树的中序遍历序列。输出输出该二叉树的后序遍历序列。示例输入ABDCEFBDAECF示例输出DBEFCA include <iostream>#include <cstring>#define MAX 50+3using namespace std;...
求大神帮忙C语言编程:做一个简单的问题回答小系统。要求程序运行时,显 ...
问题库(QAS)用外部的配置文件保存,每次运行前先载入库中已经建立的问题。然后再进入操作选择。完整代码如下:\/ 1. 暂时不考虑汉字;2. 可以用链表优化;3. 将每一种操作封装成一个函数;\/ include <stdio.h> include <stdlib.h> include <string.h> include \/\/\/ \/\/ 字符串最大长度 define M...
求大神可以帮忙编译一个C语言程序。输入一个8位数。输出前四位的和除 ...
include<stdio.h>#include<stdlib.h>void c(void){ char ch; while((ch=getchar())!='\\n'&&ch!=EOF);}int main(){ int num; while(1) { while(1) { printf("input a number of 8 bit:"); scanf("%d",&num); c(); if(num<10000000||num...
求大神帮忙,编写一个C语言程序,从键盘输入两个整数,输出这两个数之和...
include<iostream> intmain(){ usingnamespacestd;inta,b;cout<<"Pleaseenterthetwonumber:\\n";cin>>a>>b;doublesum=((double)(a+b)\/2*(b-a+1));cout<<sum<<endl;return0;}
牢易益谱: #include<stdio.h> #include<malloc.h>void move(int *p, int n, int m) {for(int i = 0; i < n - m; i++){int pianliang = n - m;int temp = *(p + m + i);*(p + m + i) = *(p + i);*(p + i) = temp;} }int main(void) {int n = 0;printf("请输入您需要输入的数字...
西双版纳傣族自治州15753161990: 求大神帮忙写一个C语言程序 - ?
牢易益谱: #include <stdio.h>#include <conio.h>#include <string.h>#define ST_SIZE 64 char * s_gets(char *, int); int main(int argc, char * argv[]){ char input[ST_SIZE]; printf("请输入\"完美\":"); s_gets(input, ST_SIZE); if(strcmp(input, "完美") == ...
西双版纳傣族自治州15753161990: 求C语言大神编写条程序 - ?
牢易益谱: #include int num[5];int cmp(const void* a, const void* b){ return *((int*)a) - *((int*)b);}int main(){ int i; for(i=0;...
西双版纳傣族自治州15753161990: 求大神发一个c语言30行以上的编程,小弟在线等! - ?
牢易益谱: /* 巴斯卡三角形 */#include#define N 12 long combi(int n,int r) { int i; long p = 1; for( i = 1;i{ p = p*(n-i+1)/i; } return p; } int main() { int n,r,t; for(n = 0;n{ for(r = 0;r{ int i; if(r == 0) { for(i = 0;i printf(" "); } else { printf( " "); } printf("%3d",combi(n,r)); } printf("\n"); } return 0; }
西双版纳傣族自治州15753161990: 跪求大神帮忙,要求C语言实现代码? - ?
牢易益谱: #include using namespace std;int main(){int a,b,c;icn<<>"a+b">>a+b>>endl;return0;}...
西双版纳傣族自治州15753161990: 请大神给我一段代码 用c语言且用*打出字母H - ?
牢易益谱: #include int main() { char *strH[7] = {"* *","* *","* *","*******","* *","* *","* *"}; int i; for(i = 0; iputs(strH[i]); return 0; }
西双版纳傣族自治州15753161990: 哪位大神能把C语言步骤写下来啊,最好截图把建立工程什么的都写出来. - ?
牢易益谱: (一)建立工程 File->new,如图,然后点OK 一路点finish OK就行(二)新建文件 File->new 如图,文件名要以.c结尾(三)写代码 编译->执行...
西双版纳傣族自治州15753161990: 求大神帮我写一段C语言程序?
牢易益谱: #include <stdio.h> int main() { FILE *a,*b; char buf[BUFSIZ]; int ret; a=fopen("a.bin","rb+"); b=fopen("b.bin","rb"); if(a==NULL||b==NULL) { perror("Open file failed."); return 1; } while((ret=fread(buf,1,BUFSIZ,b))>0) fwrite(buf,1,ret,a); fcloseall(); return 0; }
西双版纳傣族自治州15753161990: C语言编程求大神帮忙写下代码,还有每个的注解.谢谢 - ?
牢易益谱: intfenge(inta){intsum=0,k;while(a>=1){k=a%100;sum+=k;a=a/100;}if(sum%11==0)return0;elsereturn1;}intmain(){intnum;cout>num;if(fenge(num)==0)cout
西双版纳傣族自治州15753161990: 求一个C语言代码,大神帮帮忙... - ?
牢易益谱: #include int main() { char str[100]; printf("please input the name of the store:"); while(1) { scanf("%s",str); if(strcmp(str, "Callaghan")!=0 && strcmp(str,"Lambton")!=0) { printf("Such store does not exist, please enter angin:"); continue; } ...