C语言编程题

1、定义一个结构体变量(包括年、月、日),计算该日在本年中为第几天?(注意考虑闰年问题),要求写一个函数days,实现上面的计算。由主函数将年月日传递给days函数,计算后将日子传递回主函数输出。
#include<stdio.h>
#include<conio.h>

struct ymd
{
int day;
int month;
int year;
};

int dayof[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

int days(struct ymd *p)
{
int i,d;
if(p->year%4==0&&p->year%100!=0||p->year%400==0)
dayof[2]=29;
d=p->day;
for(i=1;i<p->month;i++)
d=d+dayof[i];
return (d);
}

void main()
{
struct ymd date;
int d;

clrscr();
for (;;)
{
printf("\n-----------------------------------\n\n");
printf("date(yyyy/mm/dd)=? (yyyy=0--Exit)\n\n");
scanf("%d/%d/%d",&date.year,&date.month,&date.day);
if(date.year==0)
break;
d=days(&date);
printf("\nThe day of the year is %d !\n\n",d);
}
}

2、建立一个链表,每个结点包括:学号、姓名、性别、年龄,输入一个学号,如果链表中的结点包括该学号,则输出该结点内容后,并将其结点删去。

#define LEN sizeof(struct stud_node)
#include<conio.h>
#include<stdio.h>
#include<string.h>

struct stud_record
{
char StudNo[6];
char StudName[10];
char StudSex; /*M---Male F---Female*/
int StudAge;
};

struct stud_node
{
struct stud_record stud_mem;
struct stud_node *next;
};

/*Create Student Linear table*/
struct stud_node *create()
{
struct stud_node *head,*p,*q;
char vno[6],vname[10],vsex;
int vage;
head=NULL;
while(1)
{
printf("\nPlease input a student record\n\nNo\tName\tSex\tAge\n\n");
scanf("%s",vno);
getchar();
if(strcmp(vno,"0")==0) //when vno=="0" to exit
break;
scanf("%s",vname);
getchar();
scanf("%c%d",&vsex,&vage);
getchar();
p=(struct stud_node *)malloc(LEN); //allocate space to node p
strcpy(p->stud_mem.StudNo,vno);
strcpy(p->stud_mem.StudName,vname);
p->stud_mem.StudSex=vsex;
p->stud_mem.StudAge=vage;
if(head==NULL)
head=p;
else
q->next=p; //q is the previous node of p
q=p;
}
if(head!=NULL)
q->next=NULL; //the last node has no child
return head;
}

/*Find a student and If Found then Delete the node*/
struct stud_node *delete(struct stud_node *head,char no[6])
{
struct stud_node *p,*q;
p=head;
q=p;
while(p)
{
if(strcmp(p->stud_mem.StudNo,no)==0) /*Delete the node*/
{
if(p==head) //delete the first node
head=p->next;
else
{
if(p->next!=NULL) //delete the middle node
q->next=p->next;
else //delete the last node
q->next=NULL;
}
printf("\n\t\t%s\t%s\t%c\t%d\n",p->stud_mem.StudNo,p->stud_mem.StudName,
p->stud_mem.StudSex,p->stud_mem.StudAge);
free(p);
break;
}
q=p;
p=p->next;
}
return head;
}

/*Disp linear table content*/
void prn(struct stud_node *head)
{
struct stud_node *p;
int i=1;
p=head;
printf("\nRecord\tNo\tName\tSex\tAge\n");
while(p)
{
printf("%3d\t%s\t%s\t%c\t%d\n",i,p->stud_mem.StudNo,p->stud_mem.StudName,
p->stud_mem.StudSex,p->stud_mem.StudAge);
p=p->next;
i++;
}
}

/*main program here*/
void main()
{
struct stud_node *head;
char no[6];

clrscr();
head=create();
prn(head);
getch();
printf("\nPlease input a studno to Find:");
gets(no);
head=delete(head,no);
prn(head);
getch();
}


3、给定一个日期,求出该日为星期几(已知2002-3-28为星期四)。
#include<stdio.h>
#include<conio.h>

struct ymd
{
int year;
int month;
int day;
};

/*if a year is a leap one*/
int yn_rn(int year)
{
if(year%4==0&&year%100!=0||year%400==0)
return 1;
else
return 0;
}

/*return which day in the year*/
int d_of_day(struct ymd dayof)
{
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int i,d=0;
if(yn_rn(dayof.year))
days[2]=29;
for(i=1;i<dayof.month;i++)
d=d+days[i];
return(d+dayof.day);
}

/*return the positive days if day1>day2,or negative days if day1<day2*/
int day_diff(struct ymd day1,struct ymd day2)
{
int d1,d2,i,diff=0;
d1=d_of_day(day1);
d2=d_of_day(day2);
if(day1.year>day2.year)
{
for(i=day2.year;i<day1.year;i++)
if(yn_rn(i))
diff=diff+366;
else
diff=diff+365;
}
else
{
for(i=day1.year;i<day2.year;i++)
if(yn_rn(i))
diff=diff-366;
else
diff=diff-365;
}
return diff+d1-d2;
}

void main()
{
struct ymd oldday,day;
int oldweek,week,diff;
char *rq[7]={"Sun","Mon","Tue","Wen","Thu","Fri","Sat"};

clrscr();

/*2003-4-3: Thursday*/
oldday.year=2003;
oldday.month=4;
oldday.day=3;
oldweek=4;

printf("\nPlease input day(YYYY-MM-DD):");
scanf("%d-%d-%d",&day.year,&day.month,&day.day);
diff=day_diff(day,oldday);

week=(diff%7+oldweek)%7;
printf("\n%d*%d-%d: %s\n",day.year,day.month,day.day,rq[week]);
getch();
}

4、编写一个程序,输入两个包含5个元素的数组,先将两个数组升序排列,然后将这两个数组合并成一个升序数组。
#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,k,a[6]={0},b[6]={0},c[11]={0},sub1();
clrscr();
printf("\nplease input 5 int numbers to array1: ");
for(i=1;i<=5;i++) //a[0] for no use
scanf("%d",&a[i]);
getchar();
sub1(a,5);
printf("\nplease input 5 int numbers to array2: ");
for(i=1;i<=5;i++) //b[0] for no use
scanf("%d",&b[i]);
getchar();
sub1(b,5);
printf("\nthe sorted array a is:\n\n");
for(i=1;i<=5;i++)
printf("a[%d]=%d ",i,a[i]);
printf("\n");
printf("\nthe sorted array b is:\n\n");
for(i=1;i<=5;i++)
printf("b[%d]=%d ",i,b[i]);
k=i=j=1;
while(i<=5&&j<=5)
if(a[i]<b[j])
c[k++]=a[i++]; //c[0] for no use
else
c[k++]=b[j++];
if(i<j) //appending the rest ones in array a
for(;i<=5;i++)
c[k++]=a[i];
else //appending the rest ones in array b
for(;j<=5;j++)
c[k++]=b[j];
printf("\n\n");
printf("\nthe merged array c is:\n\n");
for(k=1;k<=10;k++)
{
if(k==6)
printf("\n");
printf("c[%d]=%d ",k,c[k]);
}
while(!kbhit());
}
int sub1(int b[],int n)
{
int t,i,j,post;
for(i=1;i<n;i++)
{
post=i;
for(j=i+1;j<=n;j++)
if(b[post]>b[j])
post=j;
if(post!=i)
{
t=b[i];
b[i]=b[post];
b[post]=t;
}
}
return 0;
}

5、耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号。
/*
// approach one
#define N 13
#include<stdio.h>
#include<conio.h>
struct person
{
int number; //its order in the original circle
int nextp; //record its next person
};
struct person link[N+1]; //link[0] for no use
void main()
{
int i,count,next; //count for 12 persons,and
//next for the person not out of circle yet
clrscr();
for (i=1;i<=N;i++)
{
link[i].number=i; //numbering each person
if(i==N)
link[i].nextp=1;
else
link[i].nextp=i+1; //numbering each next person
}
printf("\nThe sequence out of the circle is:\n");
for(next=1,count=1;count<N;count++) //count until 12 persons
{
i=1;
while (i!=3) //i counts 1,2,3
{
do //skip the ones whose numbers are zero
next=link[next].nextp;
while(link[next].number==0); //end of do
i++;
}
printf("%3d ",link[next].number);
link[next].number=0; //indicate out of circle already
do //start from the ones whose numbers are not zero next time
next=link[next].nextp;
while(link[next].number==0);
}
printf("\n\nThe betrayer of them is:");
for(i=1;i<=N;i++)
if(link[i].number)
printf("%3d\n",link[i].number);
getch();
}
*/
// approach two using cyclic list
#define N 13
#define LEN sizeof(struct person)
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
#include<stdlib.h>
// struct person //permit struct placed here//
// {
// int number;
// struct person *next;
// };
void main()
{
int i,count;
struct person //or permit struct placed here also//
{
int number;
struct person *next;
};
struct person *head,*p1,*p2;
clrscr();
head=p2=NULL;
for(i=1;i<=N;i++)
{
p1=(struct person *)malloc(LEN);
p1->number=i;
if(head==NULL)
head=p1;
else
p2->next=p1;
p2=p1;
}
p2->next=head;
printf("\nthe sequence out of the circle is:\n");
for (count=1;count<N;count++)
{
i=1;
while(i!=3)
{
p1=head;
head=head->next;
i++;
}
p2=head;
printf("%3d ",p2->number);
p1->next=head=p2->next;
free(p2);
}
printf("\nThe betrayer of them is:\n%3d",head->number);
getch();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值