if(a[t]>a[j])t=j;
temp=a[i];
a[i]=a[t];
a[t]=temp;
voidBubbleSort(inta[],intn)inti,j,temp;
for(i=0;ia[j+1])temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
voidprint(inta[],intn)inti;
printf("输出排序后的整数:
\n");
for(i=0;i<10;i++)printf("%d",a[i]);
printf("\n");
3.已知head指向一个带头结点的单向链表,链表中每个结点包含数
据域(data)和指针域(next)。
请编写函数实现如图所示链表逆置。
要求:
不开辟任何额外的链表结点空间,实现链表的就地逆置考察:
对链表结构的基本操作
#include#include#include
#include#defineLENsizeof(structsth)structsthchara[20];
structsth*next;
};
structsth*creat(void);
structsth*exchange(structsth*creat);
voidprint(structsth*head);
voidfree1(structsth*head);
voidmain(void)structsth*head;
printf("请输入链表:
\n");
head=creat();
printf("请输出链表:
\n");
print(head);
exchange(head);
printf("输出逆置的链表:
\n");
print(head);
free1(head);
structsth*creat(void)structsth*head=NULL;
structsth*p1,*p2;
charinput[10]={'\0'};
//p1=p2=(structsth*)malloc(LEN);
if((p1=(structsth*)malloc(LEN))==NULL)printf("内存不足,请稍后再试!
\n");
return0;
elsehead=p1;
p2=head;
printf("请输入相关信息:
\n");
while(gets(input)!
=0&&input[0]!
=0)if((p1=(structsth*)malloc(LEN))==NULL)
printf("内存不足,请稍后再试!
\n");
return0;
elsep2->next=p1;
p2=p1;
p1->next=NULL;
strcpy(p1->a,input);
printf("请输入数据(空行则退出链表的输入):
\n");
return(head);
structsth*exchange(structsth*head)structsth*p1,*p2,*p3;
p1=head->next;;
p2=p1->next;
p3=p2->next;
p2->next=p1;
p1->next=NULL;
p1=p2;
p2=p3;
while(p2!
=NULL)p3=p2->next;
p2->next=p1;
p1=p2;
p2=p3;
//p2->next=p1;
head->next=p1;
returnhead;
voidprint(structsth*head)
structsth*p1;
p1=head->next;
if(head!
=NULL)
while(p1!
=NULL)
printf("%s\n",p1->a);
p1=p1->next;
voidfree1(structsth*head)structsth*p1,*p2;
if(head!
=NULL)p1=head->next;
if(p1!
=NULL)p2=p1->next;
free(p1);
p1=p2;
功能描述:
4.有两个链表a和b设结点中包含学号和姓名。
从a链表中删去与b链表中相同学号的结点。
#include#include
#include#defineLENsizeof(structstudent)structstudentcharnum[10];
charname[10];
structstudent*next;
};
structstudent*creat(void);
voidprint(structstudent*heada);
voidfree1(structstudent*heada);
structstudent*found(structstudent*heada,structstudent*headb);
intmain(void)structstudent*head1,*head2;
printf("请输入链表a:
\n");
head1=creat();
print(head1);
printf("请输入链表b:
\n");
head2=creat();
print(head2);
printf("输出删除后的链表a:
\n");
found(head1,head2);
free1(head1);
free1(head2);
return(0);
structstudent*creat(void)intn=0,i;
charnum[2]={'\0'};
structstudent*head;
structstudent*p1,*p2;
head=NULL;
p1=p2=(structstudent*)malloc(LEN);
printf(”请分别输入学号和姓名:
例:
1204王岩\n");
scanf("%s%s",p1->num,p1->name);
while(p1->num!
=NULL&&strcmp("q",p1->num))fflush(stdin);
n=n+1;
if(n==1)
head=p1;
elsep2->next=p1;
p2=p1;
p1=(structstudent*)malloc(LEN);
printf("请分别输入学号和姓名:
\n");
scanf("%s%s",p1->num,p1->name);
p2->next=NULL;
return(head);
structstudent*found(structstudent*heada,structstudent*headbstructstudent*p1,*p2;
structstudent*p3,*p4;
intflag;
if(heada==NULL)
printf("end");
p1=p2=heada;
p3=p4=headb;
while(p1!
=NULL)flag=0;
while(p3!
=NULL)if(strcmp(p1->num,p3->num)==0)
flag=1;
if(p1==heada)heada=p1->next;
free(p1);
p1=heada;
elsep2->next=p1->next;
free(p1);
p1=p2->next;
break;
elsep3=p3->next;
if(flag==0)p1=p1->next;
p3=headb;
print(heada);
return(0);
voidprint(structstudent*heada)
p1=heada;
while(p1!
=NULL)
printf("%s,%s\n",p1->num,p1->name);
p1=p1->next;
voidfree1(structstudent*heada)structstudent*p1,*p2;
p2=p1=heada;
while(p1!
=NULL)p2=p1->next;
free(p1);
p1=p2;
题目:
5.请编写这样一个程序,要求反复输入您的年龄,
然后显示该年龄已经过了多少秒?
当数据溢出时或者输入非法时发出报警声,输入q则退出程序
注意对常数的定义,年按每年365天计算)。
#include
#include#defineNUM_YEAR365intmain()intsecond,s,i,a;
printf("输入年龄:
\n");
scanf("%d",&a);
while
(1)if((a>=1)&&(a<=68))s=NUM_YEAR*24*60*60;
second=s*a;
printf("该年龄过的秒数:
\n");
printf("%d\n",second);
printf("继续输入年龄:
\n");
scanf("%d",&a);
elseif(a>68)printf("\a");
elseif(a='q')printf("out");
break;
//return;
将一个5*5的矩阵中最大的元素放在中心,四个角分别放四个最小的元素
顺序从左到右,从上到下顺序依次从小到大存放),写一个函数实现之。
用main函数调用。
#includevoidarray(inta[5][5]);
intmain(void)
inta[5][5],i,j;
printf("输入25个数:
\n");
scanf("%d",&a[i][j]);
printf("以矩阵的形式输出25个数:
\n");
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if((j+1)%5==0)
printf("%d",a[i][j]);
printf("\n");
break;
printf("%d",a[i][j]);
array(a);
printf("输出交换后的矩阵:
\n");
for(j=0;j<5;j++)
if((j+1)%5==0)
printf("%d",a[i][j]);
printf("\n");
break;
printf("%d",a[i][j]);
voidarray(inta[5][5])
inti,j,min1,min2,min3,min4,max,temp,mini=0,minj=0;
intmin_1=0,min_2=0,min_3=0,min_4=1,min_5=0,min_6=1,min_7=0,min_8=1;
max=a[0][0];
if(min1>a[i][j])
min1=a[i][j];
min_1=i;
min_2=j;
temp=a[0][0];
a[0][0]=a[min_1][min_2];
a[min_1][min_2]=temp;
min2=a[0][1];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(min2>a[i][j]&&(i!
=0||j!
=0))
min2=a[i][j];
min_4=j;
temp=a[0][4];
a[0][4]=a[min_3][min_4];
a[min_3][min_4]=temp;
min3=a[0][1];
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if((min3>a[i][j])&&(i!
=0||j!
=0)&&(i!
=0||j!
=4))
min3=a[i][j];
min_5=i;
min_6=j;
temp=a[4][0];
a[4][0]=a[min_5][min_6];
a[min_5][min_6]=temp;
for(j=0;j<5;j++)
if((min4>a[i][j])&&(i!
=0||j!
=0)&&(i!
=0||j!
=4)&&(i!
=4||j!
=0))
min4=a[i][j];
min_7=i;
min_8=j;
temp=a[4][4];
a[4][4]=a[min_7][min_8];
a[min_7][min_8]=temp;
for(i=0;i<5;i++)
for(j=0;j<5;j++)
if(maxmax=a[i][j];
mini=i;
minj=j;
temp=a[2][2];
a[2][2]=a[mini][minj];
a[mini][minj]=temp;
功能描述9.
(1)编写一段程序,实现统计一个或多个文件的行数、字数和字符数,程序中包括错误检查,以确定参数数目是否正确和是否能打开文件。
如果不能打开文件,程序要报告这一情况然后继续处理下一个文件。
要求说明:
a)一个行由一个换行符限定,一个字由空格分隔(包括空白符、制表符和换行符),
b)字符是指文件中的所有字符。
要求程序设置3个任选的参数,让用
户指定他所要的统计或操作。
它们是:
1统计文件行数
w统计文件字数c统计文件字符数
若用户未指定任选的参数,则默认是lwc都有。
c)运行本程序时的参数按以下格式给出:
-l-w-c文件1文件2...文件n
其中,参数l、w、c的出现与否和出现顺序任意,或任意组合在一起出现,如:
-Iwc,-cwl,-Iw,-wl,-Ic,-cl,—cw等。
程序依次打开每
个文件,然后报告每个文件中相应的统计信息。
文件名和统计信息一起报告。
考察:
(1)程序的命令行参数提供
2)文件的基本操作
3)错误检测和程序运行控制。
2)在
(1)的基础上可以添加每个文件中统计特定字符(串)和替
换功能。
a)程序参数运行如下:
f查找文件中的某个字符(串)
r替换文件中的某个字符(串)为其他
b)运行本程序时的参数按以下格式给出:
-f字符(串)文件1文件2…文件n
程序依次打开每个文件,然后报告每个文件中该字符(串)出现的次数。
文件名和字符(串)本身也与计数值一起报告。
-r字符(串)1字符(串)2文件1文件2…文件n
程序依次打开每个文件,然后依次将每个文件中字符(串)1全部替换为字符(串)2#include#includeintfile1(char*p);
intfile2(char*p);
intfile3(char*p);
intmain(intargc,char*argv[])intcount,i,lflag=0,wflag=0,cflag=0;
char*t;
printf("Thecommandlinehas%darguments:
\n",argc);
if(argc<2)printf("请重新输入:
\n");
return0;
elseif(argc>=2)if(*argv[1]=='-')for(count=1;countfor(t=argv[count];*t!
='\0';t++)if(*t=='l')lflag=1;
if(*t=='w')wflag=1;
if(*t=='c')cflag=1;
for(count=2;count='-')if(lflag==1)
file1(argv[count]);
if(wflag==1)file2(argv[count]);
if(cflag==1)file3(argv[count]);
elseprintf("错误\n");
elseif(*argv[1]!
='-')for(count=2;countif(*argv[count]=='-')printf("参数输入有误\n");
return0;
for(count=1;countfile2(argv[count]);
file3(argv[count]);
return0;
intfile1(char*p)
FILE*fp;
charch;
intflag=0,count=0;
if((fp=fopen(p,"r"))==NULL)
printf("cannotopenfile\n");
return0;
ch=fgetc(fp);
while(ch!
=EOF)if(ch=='\n')flag=0;
elseif(flag==0)count++;
flag=1;
ch=fgetc(fp);
printf("输出文件中的行数:
\n");
printf("%d\n",count);
intfile2(char*p)
FILE*fp;
charch;
intflag=0,count=0;
if((fp=fopen(p,"r"))==NULL)printf("cannotopenfile\n");
return0;
ch=fgetc(fp);
while(ch!
=EOF)if(ch==''||ch=='\n'||ch=='\t')flag=0;
elseif(flag==0)count++;
flag=1;
ch=fgetc(fp);
printf("输出文件中的字数:
\n");
printf("%d\n",count);
intfile3(char*p)
FILE*fp;
charch;
intflag=0,count=0;
if((fp=fopen(p,"r"))==NULL)printf("cannotopenfile\n");
return0;
ch=fgetc(fp);
while(ch!
=EOF)count++;
ch=fgetc(fp);
printf("输出文件中的字符数:
\n");
printf("%d\n",count);
编写一个函数。
该函数接受下列参数:
一个int数组的名称,数组大小和一个代表选取次数的值。
然后函数从数组中随机选择指定数量的元素并打印他们。
每个元素最多选择一次(模拟抽奖或挑选陪审成员)。
另外,请使用time()或其它类似的函数生成种子。
以便srand()函数初始化随机数生成器rand()。
编写一个简单的程序测试该函数。
#include#include#includevoidarray(intb[],intn,intm);
intmain(void)inta[10],i,j,k;
printf("输入10个整数:
\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("输出10个整数:
\n");
for(i=0;i<10;i++)
printf("%d",a[i]);
printf("\n");
printf("输入10以内的整数:
\n");
scanf("%d",&j);
for(k=0;k<20;k++)
if(j>=10)
printf("请重新输入,请输入10以内的整数:
\n");
scanf("%d",&j);
array(a,10,j);
return0;
voidarray(intb[],intn,intm)
intj,i,l,flag;
intc[m];
srand(time(0));/*播种子返回的是一个值*/for(j=0;jflag=0;
i=(int)(10.0*rand()/(RAND_MAX+1.0));/*产生10以内的随机整数
*/
c[j]=i;
for(l=0;lif(c[j]==c[l])
flag=1;
break;
if(flag==0)
printf("输出下标:
%d输出元
素:
%d\n",c[j],b[c[j]]);
j++;