‘壹’ c语言 5道练习题 求大神编程QWQ 调试可运行
#include typedef struct stu{ char num[10]; char name[20]; float score[2]; float sum;}STU;int main(){ STU stus[3]; int i,max=0; printf("输入3名学生的信息:\n"); for(i=0;i stus[max].sum) max = i; } for(i=0;i<3;i++) { printf("\n第%d名学生两门课平均成绩:%.2f\n", i+1, stus[i].sum / 2); } printf("\n最高分学生的信息:\n"); printf("学号:%s\n", stus[max].num); printf("姓名:%s\n", stus[max].name); printf("第1门课成绩:%.2f\n", stus[max].score[0]); printf("第2门课成绩:%.2f\n", stus[max].score[1]); printf("平均成绩:%.2f\n", stus[max].sum/2); return 0;}
‘贰’ C语言编程练习
#include<stdio.h>
structStudent
{
intID;
floatgrade;
};
intmain()
{
Studentstu[50];
intID,i=0,j;
boolESC=false;
while(!ESC)
{
printf("1.AddStudentInformation ");
printf("2.DisplayStudentInformation ");
printf("3.DeleteStudentInformation ");
printf("0.Exit ");
printf("Pleaseinputmenuitem:");
fflush(stdin);
switch(getchar())
{
case'1':
printf("AddStudentInformation ");
printf("PleaseinputstudentID:");
scanf("%d",&stu[i].ID);
printf("Pleaseinputgrade:");
scanf("%f",&stu[i].grade);
++i;
printf("Succeed! ");
break;
case'2':
printf("StudentID Grade ");
for(j=0;j<i;++j)
if(stu[j].ID!=0)
printf("%d %.1f ",stu[j].ID,stu[j].grade);
break;
case'3':
printf("PleaseinputstudentID:");
scanf("%d",&ID);
for(j=0;j<i;++j)
if(stu[j].ID==ID)
stu[j].ID=0;
printf("Succeed! ");
break;
case'0':
ESC=true;
}
}
return0;
}
‘叁’ c语言编程练习题
第一个是输入半径,输出圆的直径、周长、面积
const double p = 3.14159;
void main()
{
double radius = 0;
printf("Radius of Circle:");
scanf("%lf", &radius);
printf("Diameter:%lf, Circumference:%lf, Area:%f ", 2*radius, 2*p*radius, p*radius*radius);
}
第二个是根据给定的余额和利息,计算一年以后的余额
void main()
{
double initBalance = 6000;
double interestRate = 0.0425;
printf("Interestcalculation program.\nStarting balance:%.lf Annual\ninterest rate percentage:%.4lf\n", initBalance,interestRate);
printf("One year balance:%.lf", initBalance*(1+interestRate));
}
‘肆’ c语言编程练习题1
#include
"stdio.h"
main()
{
double
a=0,b;
int
i;
for(i=1;;i++)
{
printf("Enter
a
number:");
scanf("%lf",&b);
if(b>a)
a=b;
if(b<=0)
break;
}
if(a>=0)
printf("%lf\n",a);
}
经验证,float数据不够精确,如输入100.62,输出的却是如100.620003。所以用了更精确的double数据。希望能帮到你。
‘伍’ C语言编程练习
#includeint main(){ double s; int a[10000]; int i,n,d; while(~scanf("%d",&n)) { d=0; s=0; for(i=0; i=60) d++; printf("average = %.1lf\ncount = %d\n",s,d); } return 0;}
‘陆’ 大二C语言编程题
下面代码大部分与你那个相似,仅作参考!
/*为了调用FILE等函数 */
#include "stdio.h"
#include "conio.h"
/* 为了调用atof()等函数 */
#include "stdlib.h"
#define TRUE 1
/* 结构体声明 */
struct gong
{
int month;
float id;
float jbgz;
float jt;
float gt;
float bt;
float ft;
float jtbt;
float yfs;
float fz;
float cx;
float hf;
float grsds;
float yks;
float sfs;
};
/* 建立50个元素的结构体数组 */
struct gong zg[50];
/* 建立结构体变量 */
struct gong zgone;
/* 职员列表中的数量 */
int n=0;
/* 获得字符串 */
char numstr[50];
main()
{
char ch;
n=0;
/*rfile(); 使用后启动程序时系统会自动加载纪录*/
while(TRUE)
{
printf("\n");
printf("===========================================\n");
printf("Welcome!Please choice(1-6) : +\n");
printf("1.Add one infomation +\n");
printf("2.Delete one infomation +\n");
printf("3.Modify one infomation +\n");
printf("4.Print out by id +\n");
printf("5.Print out all +\n");
printf("6.Save date +\n");
printf("7.Load date +\n");
printf("8.Exit +\n");
printf("===========================================\n");
ch=getche();
switch(ch)
{
case '1':addone();break;
case '2':delone();break;
case '3':modone();break;
case '4':idone();break;
case '5':listall();break;
case '6':wfile();break;
case '7':rfile();break;
case '8':return;
default:printf("Sorry,please choice again!\n");
}
}
}
/*计算个人所得税*/
float tgrsds(float m)
{
float sui1,sui2,sui3;
float tmp;
sui1=0;sui2=0;sui3=0;
if(m>=5000)
{
sui1=(m-5000)*0.2;
sui2=(5000-1000)*0.1;
sui3=(1000-800)*0.05;
}
else if(m>=1000 && m<5000)
{
sui2=(m-1000)*0.1;
sui3=(1000-800)*0.05;
}
else if(m>=800 && m<1000)
{
sui3=(m-800)*0.05;
}
tmp=sui1+sui2+sui3;
return(tmp);
}
/* 添加职工的工资信息 */
addone(void)
{
printf("\n");
printf("Record NO. %d \n",n+1);
printf("Please put in month: \n");
gets(numstr);
zg[n].month=atoi(numstr);
printf("Please put in gongbianhao: \n");
gets(numstr);
zg[n].id=atof(numstr);
printf("Please put in jibengongzi: \n");
gets(numstr);
zg[n].jbgz=atof(numstr);
printf("Please put in jintie: \n");
gets(numstr);
zg[n].jt=atof(numstr);
printf("Please put in gangtie: \n");
gets(numstr);
zg[n].gt=atof(numstr);
printf("Please put in butie: \n");
gets(numstr);
zg[n].bt=atof(numstr);
printf("Please put in fangtie: \n");
gets(numstr);
zg[n].ft=atof(numstr);
printf("Please put in jiaotongbutie: \n");
gets(numstr);
zg[n].jtbt=atof(numstr);
zg[n].yfs=(zg[n].jbgz)+(zg[n].jt)+(zg[n].gt)+(zg[n].bt)+(zg[n].ft)+(zg[n].jtbt);
printf("Please put in fangzu: \n");
gets(numstr);
zg[n].fz=atof(numstr);
printf("Please put in chuxu: \n");
gets(numstr);
zg[n].cx=atof(numstr);
printf("Please put in huifei: \n");
gets(numstr);
zg[n].hf=atof(numstr);
zg[n].grsds=tgrsds(zg[n].yfs);
zg[n].yks=(zg[n].fz)+(zg[n].cx)+(zg[n].hf)+(zg[n].grsds);
zg[n].sfs=(zg[n].yfs)-(zg[n].yks);
n++;
}
/*删除个人纪录*/
delone(void)
{
struct gong tmp[50];
float gongid;
int j,k;
int flagfind;
flagfind=0;
k=0;
printf("\n Please put in gongbianhao: \n");
gets(numstr);
gongid=atof(numstr);
for(j=0;j<=n;j++)
{
if(zg[j].id==gongid)
{
flagfind=1;
}
else
{
tmp[k]=zg[j];
k++;
}
}
if(flagfind==1)
{
for(j=0;j<=(n-1);j++)
{
zg[j]=tmp[j];
}
printf("\n Record deleted! \n");
n=n-1;
}
else
{
printf("\n Record not found! \n");
}
return;
}
/*修改个人纪录*/
modone(void)
{
float gongid;
int j;
int flagfind;
flagfind=0;
printf("\n Please put in gongbianhao: \n");
gets(numstr);
gongid=atof(numstr);
for(j=0;j<=n;j++)
{
if(zg[j].id==gongid)
{
printf("NO.:%d\n",j);
printf("month:%d\n",zg[j].month);
printf("gongbianhao%10.0f\n",zg[j].id);
printf("jibengongzi:%10.2f\n",zg[j].jbgz);
printf("Please put in jibengongzi: \n");
gets(numstr);
zg[j].jbgz=atof(numstr);
printf("jintie:%10.2f\n",zg[j].jt);
printf("Please put in jintie: \n");
gets(numstr);
zg[j].jt=atof(numstr);
printf("gangtie:%10.2f\n",zg[j].gt);
printf("Please put in gangtie: \n");
gets(numstr);
zg[j].gt=atof(numstr);
printf("butie:%10.2f\n",zg[j].bt);
printf("Please put in butie: \n");
gets(numstr);
zg[j].bt=atof(numstr);
printf("fangtie:%10.2f\n",zg[j].ft);
printf("Please put in fangtie: \n");
gets(numstr);
zg[j].ft=atof(numstr);
printf("jiaotongbutie:%10.2f\n",zg[j].jtbt);
printf("Please put in jiaotongbutie: \n");
gets(numstr);
zg[j].jtbt=atof(numstr);
zg[j].yfs=(zg[j].jbgz)+(zg[j].jt)+(zg[j].gt)+(zg[j].bt)+(zg[j].ft)+(zg[j].jtbt);
printf("fangzu:%10.2f\n",zg[j].fz);
printf("Please put in fangzu: \n");
gets(numstr);
zg[j].fz=atof(numstr);
printf("chuxu:%10.2f\n",zg[j].cx);
printf("Please put in chuxu: \n");
gets(numstr);
zg[j].cx=atof(numstr);
printf("huifei:%10.2f\n",zg[j].hf);
printf("Please put in huifei: \n");
gets(numstr);
zg[j].hf=atof(numstr);
zg[j].grsds=tgrsds(zg[j].yfs);
zg[j].yks=(zg[j].fz)+(zg[j].cx)+(zg[j].hf)+(zg[j].grsds);
zg[j].sfs=(zg[j].yfs)-(zg[j].yks);
flagfind=1;
}
else
{
flagfind=0;
}
}
if(flagfind==0)printf("\n Can not find record! \n");
return;
}
/*随机从内存中查找纪录根据id*/
idone(void)
{
float gongid;
int j;
int flagfind;
flagfind=0;
printf("\n Please put in gongbianhao: \n");
gets(numstr);
gongid=atof(numstr);
for(j=0;j<=n;j++)
{
if(zg[j].id==gongid)
{
printf("\n %d record found! \n |",j+1);
printf("month:%d |",zg[j].month);
printf("gongbianhao%10.0f |",zg[j].id);
printf("jibengongzi:%10.2f |",zg[j].jbgz);
printf("jintie:%10.2f |",zg[j].jt);
printf("gangtie:%10.2f |",zg[j].gt);
printf("butie:%10.2f |",zg[j].bt);
printf("fangtie:%10.2f |",zg[j].ft);
printf("jiaotongbutie:%10.2f |",zg[j].jtbt);
printf("yingfashu:%10.2f |",zg[j].yfs);
printf("fangzu:%10.2f |",zg[j].fz);
printf("chuxu:%10.2f |",zg[j].cx);
printf("huifei:%10.2f |",zg[j].hf);
printf("gerensuodeshui:%10.2f |",zg[j].grsds);
printf("yingkoushu:%10.2f |",zg[j].yks);
printf("shifashu:%10.2f\n",zg[j].sfs);
flagfind=1;
}
else
{
flagfind=0;
}
}
if(flagfind==0)printf("\n Can not find record! \n");
return;
}
/* 输出当月全体职工的工资信息 */
listall(void)
{
int j;
if(n<1)
{
printf("\n No record! \n");
}
else
{
for(j=0;j<n;j++)
{
printf("\n NO:%d |",j+1);
printf("month:%d |",zg[j].month);
printf("gongbianhao%10.0f |",zg[j].id);
printf("jibengongzi:%10.2f |",zg[j].jbgz);
printf("jintie:%10.2f |",zg[j].jt);
printf("gangtie:%10.2f |",zg[j].gt);
printf("butie:%10.2f |",zg[j].bt);
printf("fangtie:%10.2f |",zg[j].ft);
printf("jiaotongbutie:%10.2f |",zg[j].jtbt);
printf("yingfashu:%10.2f |",zg[j].yfs);
printf("fangzu:%10.2f |",zg[j].fz);
printf("chuxu:%10.2f |",zg[j].cx);
printf("huifei:%10.2f |",zg[j].hf);
printf("gerensuodeshui:%10.2f |",zg[j].grsds);
printf("yingkoushu:%10.2f |",zg[j].yks);
printf("shifashu:%10.2f\n",zg[j].sfs);
}
}
}
/* 把所有纪录从内存中写到磁盘gongzi.rec中 */
wfile(void)
{
FILE *fptr;
printf("\n Saving date... \n");
if(n<1)
{
printf("\n No record! \n");
return;
}
if((fptr=fopen("gongzi.rec","wb"))==NULL)
printf("Can not open file gongzi.rec! \n");
else
{
fwrite(zg,sizeof(zg[0]),n,fptr);
fclose(fptr);
printf("File of %d records written! \n",n );
}
}
/* 把所有纪录从磁盘gongzi.rec读到数组gz中 */
rfile(void)
{
FILE *fptr;
n=0;
printf("\n Loading date... \n");
if((fptr=fopen("gongzi.rec","rb"))==NULL)
printf("Can not open file gongzi.rec! \n");
else
{
while(fread(&zg[n],sizeof(zg[n]),1,fptr)==1)
n++;
fclose(fptr);
printf(" Total %d records read! \n",n);
}
}
‘柒’ c语言编程:练习题。
#include<iostream.h>
int main()
{
int i,j,t;
int a[10],b[10];
cout<<"请输入第一个数组:"<<endl;
for(i=0;i<10;i++)
cin>>a[i];
for(i=1;i<10;i++)
for(j=0;j<9;j++)
if(a[i]<a[j])
{t=a[i];a[i]=a[j];a[j]=t;}
cout<<"排序后的第一个数组为:"<<endl;
for (i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
cout<<"请输入第二个数组:"<<endl;
for(j=0;j<10;j++)
cin>>b[j];
for(i=1;i<10;i++)
for(j=0;j<9;j++)
if(b[i]<b[j])
{t=b[i];b[i]=b[j];b[j]=t;}
cout<<"排序后的第二个数组为:"<<endl;
for (i=0;i<10;i++)
cout<<b[i]<<" ";
cout<<endl;
for(i=0;i<10;i++)
a[i]=a[i]+b[i];
cout<<"两数组的和为:"<<endl;
for (i=0;i<10;i++)
cout<<a[i]<<" ";
cout<<endl;
return 0;
}
‘捌’ 大一C语言编程题
车票根据题意两两站点连线生成,金额和数量分别用随机数。
数据存储:站点,车票,用户分别为3个结构体,用于对应数据库。
购买流程:检查输入数值有效性->检查车票库存->购买成功同步修改用户购买流水及车票库存。(这里题目需求不明确,没有指明用户购买方式),我的代码允许一个用户同时购买多种票,每种票可分批购买多张。购买流水记录自动增加和更新。
退票流程:检查输入和用户购买记录,同步更新用户及车票信息。
删除流程:选择删除没有被用户购买的票(函数检查所有用户,虽然本题只要一个用户,实际传参用户数组首地址)。
PS: 这个题目涉及到增删改,用链表来写更好,但考虑你学习范围,没有使用链表而是动态数组,动态数组删除,考虑内存占用,不仅要需循环移位还要释放多余地址。
另外整体程序,异常的处理我只是单纯返回0或者1或者-1区分基本的成功与失败。你如想对个别异常进行特殊处理,自行修改返回值,接收判断。
下面是代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <windows.h>
#include <conio.h>
#define MS 5//最大站点数
typedef struct station
{
char sid[10];
char sName[20];
}STN;
typedef struct ticket
{
int tid;//票编号
STN *ssP;//起始站
STN *tsP;//终点站
int value;//票价
int number;//数量
}TKT;
typedef struct userInfo//用户
{
char uid[20];//身份证号
int cnt;//购买的票种类数量
int *btids;//购买的所有票id数组
int *btNum;//购买的所有票数量数组
}UIFO;
int init(STN stns[MS],TKT **tkts);//初始化车站、票数据
int disAllTickets(TKT *tkts);
int buyTicket(UIFO *uifo,TKT *tkts);//异常返回0
int reTicket(UIFO *uifo,TKT *tkts);//异常返回0
int showMenu(UIFO *uifo,TKT *tkts);
int delIntByIndex(int *nums,int len,int index);//通过下标index删除动态整型数组nums对应元素,并释放多余地址,返回删除后的数组长度,异常返回-1
int delTkts(UIFO *uifos,int len,TKT *tkts);//删除指定票(检查用户组,只要有一个用户购买,无法删除)
int cnt;//票种类数量
int main()
{
STN stns[MS];
TKT *tkts=NULL;
UIFO uifo={"321002199902050614",0,NULL,NULL};
srand(time(NULL));
cnt=init(stns,&tkts);
printf("共生成%d组票(每组往返两种票,共%d种票) ",cnt/2,cnt);
printf("按任意键继续。。。。 "),getch();
system("cls");
showMenu(&uifo,tkts);
return 0;
}
int showMenu(UIFO *uifo,TKT *tkts)
{
int n=5;
printf("(1) Buy ticket ");
printf("(2) Refund ticket ");
printf("(3) Remove ticket ");
printf("(4) Display all tickets ");
printf("(0) Exit ");
while(n<0 || n>4)
scanf("%d",&n);
switch(n)
{
case 0: return 0;
case 1: if(!buyTicket(uifo,tkts)) return 0;break;
case 2: if(!reTicket(uifo,tkts)) return 0;break;
case 3: if(!delTkts(uifo,1,tkts)) return 0;break;
//这里删除功能,用户多名,要传数组首地址,由于演示只有一个用户(len=对应用户数),所以只传该用户地址(len=1)
case 4: disAllTickets(tkts);break;
}
showMenu(uifo,tkts);
return 1;
}
int disAllTickets(TKT *tkts)
{
int i;
for(i=0;i<cnt;i++)
printf("编号:%2d 路程:%s-%s 票价:%3d 剩余票数:%d ",tkts[i].tid,tkts[i].ssP->sName,tkts[i].tsP->sName,tkts[i].value,tkts[i].number);
printf("按任意键继续。。。。 "),getch();
system("cls");
return 1;
}
int delIntByIndex(int *nums,int len,int index)
{
int i,*temp=NULL;
if(index>len-1) return -1;
for(i=index;i<len-1;i++)
nums[i]=nums[i+1];
len--;
if(len)
{
temp=(int *)realloc(nums,sizeof(int)*len);
if(!temp) return -1;
nums=temp;
}
else
free(nums);
return len;
}
int reTicket(UIFO *uifo,TKT *tkts)
{
int i,j,tid,n,index,len,flag=0;
printf("当前用户购买记录: ");
for(i=0;i<uifo->cnt;i++)
{
for(j=0;j<cnt;j++)
if(tkts[j].tid==uifo->btids[i]) index=j;
printf("--车票编号:%d,起末站:%s-%s,购买票数:%d ",uifo->btids[i],tkts[index].ssP->sName,tkts[index].tsP->sName,uifo->btNum[i]);
}
printf("请输入要退票的车票id及票数:"),scanf("%d%d",&tid,&n);
for(i=0;i<uifo->cnt;i++)
if(uifo->btids[i]==tid && uifo->btNum[i]>=n)
{
for(j=0;j<cnt;j++)
if(tkts[j].tid==uifo->btids[i]) tkts[j].number+=n;//同步修改对应车票剩余票数
uifo->btNum[i]-=n;//用户购买记录修改
if(uifo->btNum[i]==0)//某种车票全部退完,删除该条记录
{
len=delIntByIndex(uifo->btNum,uifo->cnt,i);
if(len==-1) return 0;
len=delIntByIndex(uifo->btids,uifo->cnt,i);
if(len==-1) return 0;
uifo->cnt=len;
}
flag=1;
break;
}
if(flag)
printf("退票成功! ");
else
printf("无此购买记录或输入数量不符合 ");
printf("按任意键继续。。。。 "),getch();
system("cls");
return 1;
}
int buyTicket(UIFO *uifo,TKT *tkts)
{
char spn[20],tpn[20];
int i,n=-1,index=-1,*temp=NULL;
for(i=0;i<cnt;i++)
printf("编号:%2d 路程:%s-%s 票价:%3d 剩余票数:%d ",tkts[i].tid,tkts[i].ssP->sName,tkts[i].tsP->sName,tkts[i].value,tkts[i].number);
printf("请输入始发站站名和终点站站名:"),scanf("%s%s",spn,tpn);
for(i=0;i<cnt;i++)
if(strcmp(tkts[i].ssP->sName,spn)==0 && strcmp(tkts[i].tsP->sName,tpn)==0)
{
printf("车票%s-%s 票价:%3d 剩余票数:%d ",tkts[i].ssP->sName,tkts[i].tsP->sName,tkts[i].value,tkts[i].number);
index=i;
break;
}
if(index==-1)
printf("购买失败!无此路程的车票 ");
else
{
while(n<=0)
{
printf("请输入要购买的数量(大于0):"),scanf("%d",&n);
if(tkts[index].number<n)
printf("错误:车票数量不足或者输入数值超出范围!请重新输入! "),n=-1;
}
for(i=0;i<uifo->cnt;i++)//检查用户购买记录,重复票累加购买数量,不同票新增记录
{
if(uifo->btids[i]==tkts[index].tid)
{
printf("该票已购买了%d张,现再次购买%d张,累计购买了%d张 ",uifo->btNum[i],n,uifo->btNum[i]+n);
uifo->btNum[i]+=n;
tkts[index].number-=n;
break;
}
}
if(i==uifo->cnt)//未检查到重复记录,新增
{
if(!uifo->btids)
{
uifo->btids=(int *)malloc(sizeof(int));
if(!uifo->btids) return 0;
uifo->btNum=(int *)malloc(sizeof(int));
if(!uifo->btNum) return 0;
}
else
{
temp=(int *)realloc(uifo->btids,sizeof(int)*(uifo->cnt+1));
if(!temp) return 0;
uifo->btids=temp;
temp=(int *)realloc(uifo->btNum,sizeof(int)*(uifo->cnt+1));
if(!temp) return 0;
uifo->btNum=temp;
}
uifo->btids[uifo->cnt]=tkts[index].tid;
printf("该票购买了%d张 ",(uifo->btNum[uifo->cnt]=n));
tkts[index].number-=n;
uifo->cnt++;
printf("用户购买记录: ");
for(i=0;i<uifo->cnt;i++)
printf("--车票编号:%d,购买票数:%d ",uifo->btids[i],uifo->btNum[i]);
}
printf("本次成功购买了%s-%s的票%d张! ",tkts[index].ssP->sName,tkts[index].tsP->sName,n);
}
printf("按任意键继续。。。。 "),getch();
system("cls");
return 1;
}
int delTkts(UIFO *uifos,int len,TKT *tkts)//len:所有购买的用户数量
{
int i,j,k,tid,flag=0,index;
for(i=0;i<cnt;i++)
printf("编号:%2d 路程:%s-%s 票价:%3d 剩余票数:%d ",tkts[i].tid,tkts[i].ssP->sName,tkts[i].tsP->sName,tkts[i].value,tkts[i].number);
printf("请输入要删除的车票编号:"),scanf("%d",&tid);
for(i=0;i<cnt;i++)
if(tid==tkts[i].tid)
{
index=i;
flag=1;
for(j=0;j<len;j++)//检查所有用户购买记录
{
for(k=0;k<uifos[j].cnt;k++)
if(uifos[j].btids[k]==tid)
{
flag=0;
printf("该票已被用户购买,无法删除,需先完成退票! ");
break;
}
if(!flag)
break;
}
break;
}
if(!flag) printf(" 删除失败,输入数值超出范围或不可删除! ");
else
{
cnt--;
if(cnt==0)
{
free(tkts[index].ssP);
free(tkts[index].tsP);
free(&tkts[index]);
}
else
{
free(tkts[index].ssP);
free(tkts[index].tsP);
for(i=index;i<cnt;i++)
tkts[i]=tkts[i+1];
tkts[cnt].ssP=NULL;
tkts[cnt].tsP=NULL;
free(&tkts[cnt]);
}
printf(" 删除成功! ");
}
printf("按任意键继续。。。。 "),getch();
system("cls");
return 1;
}
int init(STN stns[MS],TKT **tkts)
{
TKT *tTemp=NULL;
int i,j,n,v,cnt=2;
static int id=1;
printf("生成站点: ");
for(i=0;i<MS;i++,id++)
{
sprintf(stns[i].sid,"车站%03d",id);
sprintf(stns[i].sName,"SN%03d",id);
printf("----站点名:%s。站点ID:%s ",stns[i].sid,stns[i].sName);
}
printf("计算所有站点连线,生成车票(票是往返,所以一次生成往返两组票): ");
for(i=0;i<MS;i++)
for(j=i+1;j<MS;j++)
{
if(!(*tkts))
{
*tkts=(TKT *)malloc(sizeof(TKT)*2);
if(!(*tkts)) return -1;
}
else
{
tTemp=(TKT *)realloc((*tkts),sizeof(TKT)*cnt);
if(!tTemp) return -1;
*tkts=tTemp;
}
n=rand()%4+2;//每种票随机2~5张(保证总数大于20)
v=rand()%201+50;//随机生成票价50~250
(*tkts)[cnt-1].tid=cnt;
(*tkts)[cnt-1].ssP=&stns[i];
(*tkts)[cnt-1].tsP=&stns[j];
(*tkts)[cnt-1].value=v;
(*tkts)[cnt-1].number=n;
(*tkts)[cnt-2].tid=cnt-1;
(*tkts)[cnt-2].ssP=&stns[j];
(*tkts)[cnt-2].tsP=&stns[i];
(*tkts)[cnt-2].value=v;
(*tkts)[cnt-2].number=n;
printf("----%s和%s的之间往返票各生成%d张,票价为%d(随机) ",stns[i].sName,stns[j].sName,n,v);
cnt+=2;
}
return cnt-2;
}
//答题不易,如采纳的,请不要无故删除问题。
‘玖’ c编程练习题
这个问题很简单。
首先你要知道函数的参数传递的只是副本。
调用函数swap(int *p1,int *p2) ,只是对函数实参数的地址副本进行地址交换,就着这个函数来说,在交换前,p1地址是pointer_1,p2的地址是pointer_2;交换后p1地址是pointer_2,p2的地址是pointer_1,但pointer_1和pointer_2的地址还是没变还是原来的地址,通俗说,a,b形参分别拿着A、B两实参的钥匙的复印件,a、b交换了钥匙,但A、B并没有交换钥匙,这里的钥匙就是指的是地址!
如果你想让pointer_1和pointer_2的值改变,就改写swap(int *p1,int *p2)。
swap(int *p1,int *p2)
{int *p;
*p=*p1;
*p1=*p2;
*p2=*p;
}
这个函数通俗说,a,b形参分别拿着A、B两实参的钥匙的复印件打开A、B的房门将A、B俩房间里的东西对调,虽然A、B并没有交换钥匙但却交换了房间里的东西!
‘拾’ c语言编程题
#include<stdio.h>
void main()
{
int max,min,sum,i,score[10];
printf("输入数据:");
for(i=0;i<10;i++)
scanf("%d",&score[i]);
sum = max = min = score[0];
for(i=1; i<10; i++)
{
if(max<score[i])max = score[i];
if(min>score[i])min = score[i];
sum =sum+score[i];
}
printf("%.2f\n", (sum-min-max)/8.0);
}