C语言用字符串sex储存,《C语言》上机实验题及参考答案2

13、编写一个程序,输入两个包含5个元素的数组,先将两个数组升序排列,然后将这两个数组合并成一个升序数组。

#include

#include

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]

c[k++]=a[i++]; //c[0] for no use

else

c[k++]=b[j++];

if(i

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

{

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;

}

14、耶稣有13个门徒,其中有一个就是出卖耶稣的叛徒,请用排除法找出这位叛徒:13人围坐一圈,从第一个开始报号:1,2,3,1,2,3……,凡是报到“3”就退出圈子,最后留在圈内的人就是出卖耶稣的叛徒,请找出它原来的序号。

/*

// approach one

#define N 13

#include

#include

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

{

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

#include

#include

#include

// 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

{

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();

}

15、编写一个程序,根据用户输入的不同边长,输出其菱形。例如,边长为3的菱形为:

20071210152615558.GIF

16、按如下图形打印杨辉三角形的前10行。其特点是两个腰上的数都为1,其它位置上的每一个数是它上一行相邻两个整数之和。

20071210152615205.GIF

#include

#include

#define N 10

void main()

{

int i,j,k,a[N][N];

clrscr();

for(i=0;i

{

a[i][0]=1;

a[i][i]=1;

}

for(i=2;i

for(j=1;j

a[i][j]=a[i-1][j-1]+a[i-1][j];

for(i=0;i

{

for(k=0;k<=3*(N-i);k++)

printf(" ");

for(j=0;j<=i;j++)

printf("%6d",a[i][j]);

printf("\n\n");

}

getch();

}

17、某班有5个学生,三门课。分别编写3个函数实现以下要求:

(1) 求各门课的平均分;

(2) 找出有两门以上不及格的学生,并输出其学号和不及格课程的成绩;

(3) 找出三门课平均成绩在85-90分的学生,并输出其学号和姓名

主程序输入5个学生的成绩,然后调用上述函数输出结果。

#define SNUM 5 /*student number*/

#define CNUM 3 /*course number*/

#include

#include

/*disp student info*/

void DispScore(char num[][6],char name[][20],float score[][CNUM])

{

int i,j;

printf("\n\nStudent Info and Score:\n");

for(i=0;i

{

printf("%s ",num[i]);

printf("%s ",name[i]);

for(j=0;j

printf("%8.2f",score[i][j]);

printf("\n\n");

}

}

/*calculate all student average score*/

void CalAver(float score[][CNUM])

{

float sum,aver;

int i,j;

for(i=0;i

{

sum=0;

for(j=0;j

sum=sum+score[j][i];

aver=sum/SNUM;

printf("Average score of course %d is %8.2f\n",i+1,aver);

}

}

/*Find student: two courses no pass*/

void FindNoPass(char num[][6],float score[][CNUM])

{

int i,j,n;

printf("\nTwo Course No Pass Students:\n");

for(i=0;i

{

n=0;

for(j=0;j

if(score[i][j]<60)

n++;

if(n>=2)

{

printf("%s ",num[i]);

for(j=0;j

if(score[i][j]<60)

printf("%8.2f",score[i][j]);

printf("\n");

}

}

}

/*Find student: three courses 85-90*/

void FindGoodStud(char num[][6],char name[][20],float score[][CNUM])

{

int i,j,n;

printf("\nScore of three courses between 85 and 90:\n");

for(i=0;i

{

n=0;

for(j=0;j

if(score[i][j]>=85&&score[i][j]<=90)

n++;

if(n==3)

printf("%s %s\n",num[i],name[i]);

}

}

/*input student info*/

void main()

{

char num[SNUM][6],name[SNUM][20]; //array num refers to student number

float score[SNUM][CNUM]; //and its length is 6

int i,j;

clrscr();

printf("\nPlease input student num and score:\n");

for(i=0;i

{

printf("\n\nStudent%d number: ",i+1);

scanf("%s",num[i]);

printf("\nStudent%d name: ",i+1);

scanf("%s",name[i]);

printf("\nStudent%d three scores: ",i+1);

for(j=0;j

scanf("%f",&score[i][j]);

}

DispScore(num,name,score);

CalAver(score);

FindNoPass(num,score);

FindGoodStud(num,name,score);

getch();

}

18、编写一人个求X的Y次幂的递归函数,X为double型,y为int型,要求从主函数输入x,y的值,调用函数求其幂。

#include

#include

double fact(double x,int y)

{

if(y==1)

return x;

else

return x*fact(x,y-1);

}

void main()

{

double x;

int y;

clrscr();

printf("\nPlease x,y:");

scanf("%lf%d",&x,&y);

printf("\nx^y=%.2lf",fact(x,y));

getch();

}

19、打印魔方阵。

所谓魔方阵是指这样的的方阵:

它的每一行、每一列和对角线之和均相等。

输入n,要求打印由自然数1到n2的自然数构成的魔方阵(n为奇数)。

例如,当n=3时,魔方阵为:

8 1 6

3 5 7

4 9 2

魔方阵中各数排列规律为:

① 将“1”放在第一行的中间一列;

② 从“2”开始直到n×n为止的各数依次按下列规则存放:每一个数存放的行比前一个数的行数减1,列数同样加1;

③ 如果上一数的行数为1,则下一个数的行数为n(最下一行),如在3×3 方阵中,1在第1行,则2应放在第3行第3列。

④ 当上一个数的列数为n时,下一个数的列数应为1,行数减1。如2在第3行第3列,3应在第2行第1列。

⑤如果按上面规则确定的位置上已有数,或上一个数是第1行第n列时,则把下一个数放在上一个数的下面。如按上面的规定,4应放在第1行第2列,但该位置已被1占据,所以4就放在3的下面。由于6是第1行第3列(即最后一列),故7放在6下面。

#include

#include

#define Max 15

void main()

{

int i,row,col,odd;

int m[Max][Max];

clrscr();

printf("\nPlease input an odd:");

scanf("%d",&odd);

if(odd<=0||odd%2==0)

{

printf("\nInput Error!\n");

getch();

return 0;

}

printf("\nodd=%d\n\n",odd);

row=0;

col=odd/2; //1 placed in the middle of the first row

for(i=1;i<=odd*odd;i++)

{

m[row][col]=i;

if(i%odd==0) //to the last col

if(row==odd-1) //to the last row

row=0;

else

row++;

else //outmost else

{

if(row==0)

row=odd-1;

else

row--;

if(col==odd-1)

col=0;

else

col++;

} //end of outmost else

} //end of for

for(row=0;row

{

for(col=0;col

printf("%4d",m[row][col]);

printf("\n\n");

}

getch();

return 0;

}

20、找出一个二维数组中的“鞍点”,即该位置上的元素在该行中最大,在该列中最小(也可能没有“鞍点”),打印出有关信息。

#define N 20

#define M 20

#include

#include

void main( )

{

int a[N][M]; //int a[][]; not allowed here

int i,j,k,row,col,n,m,find=0;

clrscr();

printf("\nEnter n & m:\n\n");

scanf("%d%d",&n,&m);

printf("\nEnter a[0][0]--a[%d][%d]\n\n",n-1,m-1);

for(i=0;i

for(j=0;j

scanf("%d",&a[i][j]);

printf("\n\nThe array you have just entered is:\n");

for(i=0;i

{

for(j=0;j

printf("%5d",a[i][j]);

printf("\n\n");

}

//find the point

for(i=0;i

{

for(col=0,j=1;j

if(a[i][col]

col=j;

for(row=0,k=1;k

if(a[row][col]>a[k][col]) //find row,select sort according to row

row=k;

if(i==row)

{

find=1;

printf("The point is a[%d][%d].\n",row,col);

}

}

if(!find)

printf("\nNo solution.\n");

getch();

}

21、马克思在《数学手稿》中提出如下问题:有30个人(包括男人、女人和小孩)在一家饭店吃饭共花50先令,其中每个男人花3先令,每个女人花2先令,

每个小孩花1先令,问男人、女人、小孩各有多少人?

#include

#include

void main()

{

int man,woman,child,money=50,count=30;

int i,j,k;

clrscr();

printf("They consist of:\n\n");

for(i=0;i<=count;i++)

for(j=0;j<=count;j++)

for(k=0;k<=count;k++)

if(i+j+k==count&&3*i+2*j+k==money)

{

printf("man=%2d woman=%2d child=%2d\n",i,j,k);

printf("\n");

}

getch();

}

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

#include

#include

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;imonth;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);

}

}

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

#define LEN sizeof(struct stud_node)

#include

#include

#include

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();

}

24、给定一个日期,求出该日为星期几(已知2002-3-28为星期四)。

#include

#include

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

d=d+days[i];

return(d+dayof.day);

}

/*return the positive days if day1>day2,or negative days if day1

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

if(yn_rn(i))

diff=diff+366;

else

diff=diff+365;

}

else

{

for(i=day1.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();

}

25、用递归法将一个整数n转换成字符串(例如输入4679,应输出字符串“4679”),n为不确定数,可以是位数不超过5,且数值在-32768~32767之间和任意整数。

#include

#include

void convert(int n)

{

int i;

if((i=n/10)!=0)

convert(i);

putchar(n%10+'0'); //the Ascii of char '0' is 48

}

void main()

{

int number;

clrscr();

printf("\ninput an integer:");

scanf("%d",&number);

printf("\n\nOutput string is : ");

if(number<0)

{

putchar('-');

number=-number;

}

convert(number);

getch();

}

26、有一个字符串,包括n个字符。写一个函数,将此字符串从第m个字符开始的全部字符复制成另一个字符串。要求在主函数输入字符串及m值并输出复制结果。

#include

#include

main()

{ int m;

char *str1[80],*str2[80];

printf("Input a string(length<80:");

scanf("%s",str1);

printf("\nWhich character starting from?");

scanf("%d",&m);

if(strlen(str1)

printf("\nError input !");

else

{ copystr(str1,str2,m);

printf("\nResult is : %s",str2);

}

}

copystr(p1,p2,m)

char *p1,*p2;

int m;

{

int n;

n=0;

while(n<=m-1)

{ n++;

p1++;

}

while(*p1!='\0')

{ *p2=*p1;

p1++;

p2++;

}

*p2='\0';

}

27、在主函数中输入6个字符串,用另一个函数对他们按从小到大的顺序,然后在主函数中输出这6个已经排好序的字符串。要求使用指针数组进行处理。

#define MAXLINE 20

main()

{ int i;

char *pstr[6],str[6][MAXLINE];

for(i=0;i<6;i++)

pstr[i]=str[i];

printf("Input 6 string (1 string at each line):\n");

for(i=0;i<6;i++)

scanf("%s",pstr[i]);

sort(pstr);

printf("The string after sorting :\n");

for(i=0;i<6;i++)

printf("%s\n",pstr[i]);

}

sort(pstr)

char *pstr[6];

{

int i,j;

char *p;

for(i=0;i<6;i++)

{ for(j=i+1;j<6;j++)

{ if(strcmp(*(pstr+i),*(pstr+j))>0)

{ p=*(pstr+i);

*(pstr+i)=*(pstr+j);

*(pstr+j)=p;

}

}

}

}

28、编写一个函数实现对两个字符串的比较。不用使用C语言提供的标准函数strcmp。要求在主函数中输入两个字符串,并输出比较的结果(相等的结果为0,不等时结果为第一个不相等字符的ASCII差值)。

strcmp(p1,p2)

char *p1,*p2;

{ int i;

i=0;

while(*(p1+i)==*(p2+i))

if(*(p1+i++)=='\0') return(0);

return(*(p1+i)-*(p2+i));

}

main()

{ int m;

char str1[20],str2[20],*p1,*p2;

printf("Input two strings(1 string at each line):\n");

scanf("%s",str1);

scanf("%s",str2);

p1=&str1[0];

p2=&str2[0];

m=strcmp(p1,p2);

printf("The result of comparison :%d\n",m);

}

29、有一个unsigned long型整数,先要分别将其前2个字节和后2个字节用为两个unsigned int型整数输出(设一个int型数据占2个字节),试编写一函数partition实现上述要求。要求在主函数输入该long型整数,在函数partition中输出结果。

void partition(unsigned long num)

{ union a

{

unsigned int part[2];

unsigned long w;

} n,*p;

p=&n;n.w=num;

printf("long integer=%lx\n",num);

printf("\nlong integer=%0x,high-part number=%0x\n",p->part[0],p->part[1]);

}

void main()

{ unsigned long x;

printf("Input a long number:");

scanf("%lx",&x);

partition(x);

}

30、编一程序,能把从终端读入的一个字符中的小写字母全部转换成大写字母,然后输出到一个磁盘文件“test”中保存(用字符!表示输入字符串的结束)。

#include

void main()

{ FILE *fp;

char str[100];

int i=0;

if((fp=fopen("test","w"))==NULL)

{ printf("Can't open this file.\n");

exit(0);

}

printf("Input a string : \n");

gets(str);

while(str[i]!='!')

{ if(str[i]>='a'&&str[i]<='z')

str[i]=str[i]-32;

fputc(str[i],fp);

i++;

}

fclose(fp);

if((fp=fopen("test","r"))==NULL)

{ printf("can't open test r\n");

exit(0);

}

fgets(str,strlen(str)+1,fp);

printf("Output is : %s",str);

fclose(fp);

}

31、有五个人坐在一起,问第5个人多少岁?他说比第4个人大2岁。问第4个人多少岁?他说比第3个人大2岁。问第3个人多少岁?他说比第3个人大2岁。问第2个人多少岁?他说比第1个人大2岁。最后问第1个人多少岁?他说是10岁。请问第5个人多大?(这是一个递归问题)

#include

int age(int n)

{ if(n==1) return(10);

else return age(n-1)+2;

}

void main()

{ int n;

n=5;

printf("The fifth age is %d.\n",age(n));

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值