二级C语言
请编写函数fun,其功能是:计算并输出:s=1+(1+根号2)+(1+根号2+根号3)+....+(1+根号12+根号3+...+根号n)(要求n的值大于1但不大于100)
例如,在主函数中从键盘给n输入20后,输出为:s=534.188884
请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
#include <math.h>
#include <stdio.h>
double fun(int n)
{
}
main ()
{
int n; double s;
printf("\n\nlnput n: ");
scanf("%d",&n);
s=fun(n);
printf("\n\ns=%f\n\n",s);
NONO();
)
NONO ()
{/*请在此函数内打开文件,输入测试数据, 调用fun函数,输出数据,关闭文件.*/
FILE *rf, *wf; int n, i; double s;
rf = fopen("in.dat", "r");
wf - fopen("out.dat",“w”);
for(i = 0 ; i < 10 ; i++)
{
fscanf(rf, "%d", &n);
s = fun(n);
fprintf(wf, ”%lf\n", s);
fclose(rf);
fclose(wf);
}
}给定程序中已建立一个带有头结点的单向链表,在main函数中将多次调用fun函数,每调用一次fun函数,输出链表尾部结点中的数据,并释放该结点,使链表缩短。
请在程序的下划线处填入正确的内容,使程序得出正确的结果。
不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#include <stdlib.h>
#define N 8
typedef struct list
{
int data;
struct list *next;
} SLIST;
void fun(SLIST *p)
{
SLIST *t, *s;
t=p->next; s=p;
while(t->next != NULL)
{
s=t;
/**********found**********/
t=t->__(1)__;
}
/**********found**********/
printf(" %d ",__(2)__);
s->next=NULL;
/**********found**********/
free(__(3)__);
}
SLIST *creatlist(int *a)
{
SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; i<N; i++)
{
q=(SLIST *)malloc(sizeof(SLIST));
q->data=a[i];
p->next=q;
p=q;
}
p->next=0;
return h;
}
void outlist(SLIST *h)
{
SLIST *p;
p=h->next;
if (p==NULL)
{
printf("\nThe list is NULL!\n");
}
else
{
printf("\nHead");
do
{
printf("->%d" ,p->data);
p=p->next;
}
while(p!=NULL);
printf("->End\n");
}
}
main()
{
SLIST *head;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("\nOutput from head:\n");
outlist(head);
printf("\nOutput from tail:\n");
while (head->next != NULL)
{
fun(head);
printf("\n\n");
printf("\nOutput from head again :\n");
outlist(head);
}
system("pause");
}函数fun的功能是:将字符串中的字符按逆序输出,但不改变字符串中的内容。
例如,若字符串为abcd,则应输出:dcba,请改正程序中的错误,使它能得出正确的结果。
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#include <stdlib.h>
/**********found**********/
fun (char a)
{
if (*a)
{
fun(a+1);
/**********found**********/
printf("%c" *a);
}
}
main()
{
char s[10]="abcd";
printf("处理前字符串:%s\n处理后字符串:", s);
fun(s);
printf("\n");
system("pause");
}请编写函数fun,其功能是:将所有大于1小于整数m的非素数存入xx所指数组中,非素数的个数通过k传回。
例如,若输入:17,则应输出:46891012141516
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
#include <stdio.h>
#include <stdlib.h>
void fun(int m, int *k, int xx[])
{
int i,j, n=0;
for(i=2;i<m;i++)/*找出大于1小于整数m的非素数*/
{
for(j=2;j<i;j++) }
}
main()
{
int m, n, zz[100];
void NONO ();
printf("\nPlease enter an integer number between 10 and 100:");
scanf("%d", &n);
fun(n, &m, zz);
printf("\n\nThere are %d non-prime numbers less than %d:", m, n);
for(n=0; n<m; n++)
{
printf("\n %4d", zz[n]);
}
NONO();
system("pause");
}
void NONO()
{/* 请在此函数内打开文件,输入测试数据,
调用函数,输出数据,关闭文件。 */
int m, n, zz[100];
FILE *rf, *wf;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
fscanf(rf, "%d", &n);
fun(n, &m, zz);
fprintf(wf, "%d\n%d\n", m, n);
for(n=0; n<m; n++)
{
fprintf(wf, "%d\n", zz[n]);
}
fclose(rf);
fclose(wf);
}请补充函数proc,其功能是:计算下面公式S的值:

例如,当N=20时,SN=29.031674
注意:请勿改动main函数和其他函数中的任何内容,仅在函数proc的横线上填入所编写的若干表达式或语句。
#include <stdio.h>
#include <stdlib.h>
double proc(int n)
{
double s=1.0, sl=0.0;
int k;
/**********found**********/
for(__(1)__; k<=n; k++)
{
sl=s;
/**********found**********/
__(2)__;
}
/**********found**********/
return __(3)__;
}
void main()
{
int k=0;
double sum;
system("CLS");
printf("\nPlease input N=");
scanf("%d", &k);
sum=proc(k);
printf("\nSN=%lf", sum);
system("pause");
}函数proc的功能是:根据整型形参n,计算如下公式的值:

例如,n中的值为10,则应输出0.817962,请改正程序中的错误,使它能得出正确的结果。
不要改动main函数,不得增行或删行,也不得更改程序的结构!
#include <stdio.h>
#include <stdlib.h>
double proc(int n)
{
double y=1.0;
/**********found**********/
int j=1;
int i;
for(i=2; i<=n; i++)
{
j=-1*j;
/**********found**********/
y+=1/(i*i);
}
return(y);
}
void main()
{
int n=10;
system("CLS");
printf("\nThe result is %lf\n", proc(n));
system("pause");
}编写一个函数proc,从传入的M个字符中找出最长的一个字符串,并通过形参指针max传回该串地址(用***作为结束输入的标志)。
请勿改动main0函数和其他函数中的任何内容,仅在函数proc的花括号中填入所编写的若干语句。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void proc(char(*a)[81],int num,char **max)
{
int i;
*max=a[0];
for(①)
{
if(②)
{
③
}
}
}
main()
{
void NONO();
char ss[10][81],*ps;
int n,i=0;
printf("输入若干个字符串:");
gets(ss[i]);
puts(ss[i]);
while(!strcmp(ss[i],"****")==0)
{
i++;
gets(ss[i]);
puts(ss[i]);
}
n=i;
proc(ss,n,&ps);
printf("\nmax=%s\n",ps);
NONO();
system("pause");
}
void NONO()
{/* 请在此函数内打开文件,输入测试数据,
调用 函数,输出数据,关闭文件。 */
char ss[20][81],*max;
int n,i=0;
FILE *rf, *wf;
rf=fopen("in.dat","r");
wf=fopen("out.dat","w");
fgets(ss[i], 81, rf);
while(!strncmp(ss[i],"****",4)==0)
{
i++;
fgets(ss[i], 81, rf);
}
n=i;
proc(ss, n, &max);
fprintf(wf, "%s",max);
fclose(rf);
fclose(wf);
}给定程序BLANK1.C中,函数fun的功能是在数组中找出两科成绩之和最高的学生并返回其在数组中的下标。对所给函数int fun(STU*d,int n),主函数传给形参d的是学生数组名,而传给形参n的是该数组中学生的个数。
例如,若学生数组数据为:
2016500301李清水83 92
2016500336 刘世才85 94
2016500371王子晨88 88
则调用该函数后,程序输出为:2016500336刘世才85 94
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:不得增行或删行,也不得更改程序的结构!
#include<stdio.h>
typedef struct stu
{
char ID[30];
char name[20];
int score[2];
} STU;
int fun(STU *d,int n)
{
int i,m;
/******found******/
______(1)______;
for(i=1;i<n;i++)
/******found******/
if(d[i].score[0]+d[i].score[1]>________(2)________)
m=i;
/******found******/
______(3)______;
}
void main()
{
STU a[10]={ "2016500301","李清水",83,92,"2016500336","刘世才",85,94,"2016500371","王子晨",88,88};
int i,n=3;
i=fun(a,n);
printf("%30s%20s%4d%4d",a[i].ID,a[i].name,a[i].score[0],a[i].score[1]);
printf("\n");
}给定程序MODI1.C中,函数void list(MYDATA *h)的功能是:列出带头结点单链表中所有没有删除标记的数据。调用这个函数时,传给形参h的是指向单链表头结点的指针。
例如,当10个结点的数据为1,2,3,4,5,6,7,8,9,10时,输出将是:3 4 6 7 8 9 10
其中,各个数据所对应的删除标记是由随机数产生的。
请改正函数list中指定部位的错误,使它能得出正确的结果。
注意:不要改动main函数和creat函数,不得增行或删行
#include<stdio.h>
#include<stdlib.h>
typedef struct dat
{
char deleted; //是否删除:0-未删除,1-删除
int data;
struct dat* next;
} MYDATA;
void list(MYDATA *h)
{
/******found******/
MYDATA p;
p=h->next;
while(p!=NULL)
{
/******found******/
if(p->data==0)
{
printf("%d ",p->data);
}
/******found******/
p=next;
}
}
void creat(MYDATA *h,int *d,int n)
{
MYDATA *p, *q;
int i=0,del;
q=h;
while(n>0)
{
p=( MYDATA *)malloc(sizeof(MYDATA));
del=rand()%2;
p->data=d[i];
p->deleted=del;
p->next=q->next;
q->next=p;
q=p;
n--;i++;
}
}
void main()
{
MYDATA *head;
int n=10,dd[]={1,2,3,4,5,6,7,8,9,10};
head=(MYDATA *)malloc(sizeof(MYDATA));
head->next=NULL;
creat(head,dd,n);
list(head);
}请编写函数void fun(int *dp,int n,int upordown),其功能是,找出dp所指数组中的最小或最大值,并与其第1个元素交换。形参n为元素的个数,形参upordown为查找标志:值为0时找最小值;值为1时找最大值。
注意:部分源程序在文件PROG1.C中。请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
#include <stdio.h>
#include <stdlib.h>
#define N 10
#pragma warning (disable:4996)
void NONO(FILE *fp, int pd[], int n);
void fun (int *dp,int n,int upordown)
{
int temp=0;
int i=0;
temp=*dp;
if(upordown==0)
{
for(i=1;i<n;i++)
{
if(dp[i]<temp)
{
dp[0]=dp[i];
dp[i]=temp;
temp=dp[0];
}
}
}
else if(/******found******/)
{
for(/******found******/)
{
if(/******found******/)
{
/******found******/
/******found******/
/******found******/
}
}
}
*dp=temp;
}
void display(int pd[],int n)
{
int i;
for(i=0;i<n;i++)
printf("%4d",pd[i]);
printf("\n");
}
void main()
{
int data[N],i,n=N;
FILE *out ;
out = fopen("out.dat","w") ;
for(i=0;i<N;i++)
data[i]=rand()%90+10;
for(i=0;i<N-1;i++)
fun(data+i,n-i,0);
display(data,n);
NONO(out, data, n);
for(i=0;i<N;i++)
data[i]=rand()%90+10;
for(i=0;i<N-1;i++)
fun(data+i,n-i,1);
display(data,n);
NONO(out, data, n);
fclose(out);
}
void NONO(FILE *fp, int pd[], int n)
{
int i;
for(i=0;i<n;i++)
fprintf(fp, "%4d", pd[i]);
fprintf(fp, "\n");
}给定程序中,函数fun的功能是:在形参s所指字符串中寻找与参数c相同的字符,并在其后插入一个与之相同的字符,若找不到相同的字符则函数不做任何处理。
例如,s所指字符串为:baacda,c中的字符为:a,执行后s所指字符串为:baaaacdaa。
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK1.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include<stdio.h>
void fun(char *s, char c)
{
int i, j, n;
for(i=0; s[i]!=___1___ ; i++)
if(s[i]==c)
{
n=___2___ ;
while(s[i+1+n]!='\0') n++;
for(j=i+n+1; j>i; j--) s[j+1]=s[j];
s[j+1]=___3___ ;
i=i+1;
}
}
main()
{
char s[80]="baacda", c;
printf("\nThe string: %s\n",s);
printf("\nInput a character: ");
scanf("%c",&c);
fun(s,c);
printf("\nThe result is: %s\n",s);
}在主函数中从键盘输入若干个数放入数组中,用0结束输入并放在最后一个元素中。给定程序MODI1.C中函数fun的功能是:计算数组元素中值为正数的平均值(不包括0)。
例如:数组中元素中的值依次为:39,-47,21,2,-8,15,0,则程序的运行结果为:19.250000。
请改正程序中的错误,使它能得出正确的结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构!
给定源程序:
#include<stdio.h>
double fun ( int x[])
{
____1____
int c=0, i=0;
while (x[i] != 0)
{
if (x[i] > 0)
{
sum += x[i]; c++;
}
i++;
}
____2____
return sum;
}
main()
{
int x[1000];
int i=0;
printf( "\nPlease enter some data (end with 0): " );
do
{
scanf("%d", &x[i]);
}
while (x[i++] != 0);
printf("%f\n", fun ( x ));
}编写函数fun,函数的功能是:根据以下公式计算s,计算结果作为函数值返回;n通过形参传入。
1 1 1
S=1 + ── + ─── + … + ──────
1+2 1+2+3 1+2+3+…+n
例如:若n的值为11时,函数的值为:1.833333
注意:部分源程序在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include<stdio.h>
float fun(int n)
{
____1____
____2____
for(____3____)
{
____4____
for(____5____)
t+=j;
____6____
}
return s;
}
main()
{
int n;
float s;
printf("\nPlease enter N:");
scanf("%d", &n);
s = fun(n);
printf("the result is: %f\n", s);
NONO();
}给定程序中,函数fun的功能是:将N╳N矩阵主对角线元素中的值与反向对角线对应位置上元素中的值进行交换。例如,若N=3,有下列矩阵:
1 2 3
4 5 6
7 8 9
交换后为:
3 2 1
4 5 6
9 8 7
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdio.h>
#define N 4
/**********found**********/
void fun(int ___1___ , int n)
{int i,s;
/**********found**********/
for(___2___; i++)
{s=t[i][i];
t[i][i]=t[i][n-i-1];
/**********found**********/
t[i][n-1-i]=___3___;
}
}
main()
{int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42, 21,33,10}, i, j;
printf("\nThe original array:\n");
for(i=0; i<N; i++)
{for(j=0; j<N; j++) printf("%d ",t[i][j]);
printf("\n");
}
fun(t,N);
printf("\nThe result is:\n");
for(i=0; i<N; i++)
{for(j=0; j<N; j++) printf("%d ",t[i][j]);
printf("\n");
}
}由N个有序整数组成的数列已放在一堆数组中,给定程序MODI1.C中函数fun的功能是:利用折半查找算法查找整数m在数组中的位置。若找到,返回其下标值;反之,返回-1.
折半查找的基本算法是:每次查找前先确定数组中待查的范围:low和high(low
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
给定源程序:
#include <stdio.h>
#define N 10
/************found************/
___________1___________
{int low=0,high=N-1,mid;
while(low<=high)
{mid=(low+high)/2;
if(m<a[mid])
high=mid-1;
/************found************/
________2_______
low=mid+1;
else return(mid);
}
return(-1);
}
main()
{int i,a[N]={-3,4,7,9,13,45,67,89,100,180 },k,m;
printf("a数组中的数据如下:");
for(i=0;i<N;i++) printf("%d ", a[i]);
printf("Enter m: "); scanf("%d",&m);
k=fun(a,m);
if(k>=0) printf("m=%d,index=%d\n",m,k);
else printf("Not be found!\n");
}假定输入的字符串中只包含字母和*号。请编写函数fun,它的功能是:除了尾部的*号之外,将字符串中其它*号全部删除。形参p已指向字符串中最后的一个字母。在编写函数时,不得使用C语言提供的字符串函数。
例如,字符串中的内容为:****A*BC*DEF*G*******,删除后,字符串中的内容应当是:ABCDEFG*******。
注意:部分源程序在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include <stdio.h>
void fun(char *a, char *p)
{
____1____
____2____
while(____3____){
if(*q !='*')____4____;
____5____
}
while(*p)____6____;
a[j]='\0';
}
main()
{char s[81],*t;
void NONO ();
printf("Enter a string:\n");gets(s);
t=s;
while(*t)t++;
t--;
while(*t=='*')t--;
fun(s , t);
printf("The string after deleted:\n");puts(s);
NONO();
}
void NONO()
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *in, *out ;
int i ; char s[81],*t ;
in = fopen("in.dat","r");
out = fopen("out.dat","w");
for(i = 0 ; i < 10 ; i++) {
fscanf(in, "%s", s);
t=s;
while(*t)t++;
t--;
while(*t=='*')t--;
fun(s,t);
fprintf(out, "%s\n", s) ;
}
fclose(in);
fclose(out);
}给定程序中,函数fun的功能是将不带头结点的单向链表逆置。即若原链表中从头至尾结点数据域依次为:2、4、6、8、10,逆置后,从头至尾结点数据域依次为:10、8、6、4、2.
请在程序的下划线处填入正确的内容并把下划线删除,使程序得出正确的结果。
注意:源程序存放在考生文件夹下的BLANK.C中。
不得增行或删行,也不得更改程序的结构!
给定源程序:
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node {
int data;
struct node *next;
} NODE;
/**********found**********/
__1__ fun(NODE *h)
{NODE *p, *q, *r;
p = h;
if (p == NULL)
return NULL;
q = p->next;
p->next = NULL;
while (q)
{
/**********found**********/
r = q->__2__;
q->next = p;
p = q;
/**********found**********/
q = __3__ ;
}
return p;
}
NODE *creatlist(int a[])
{NODE *h,*p,*q; int i;
h=NULL;
for(i=0; i<N; i++)
{q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
if (h == NULL) h = p = q;
else {p->next = q; p = q;}
}
return h;
}
void outlist(NODE *h)
{NODE *p;
p=h;
if (p==NULL) printf("The list is NULL!\n");
else
{printf("\nHead ");
do
{printf("->%d", p->data); p=p->next;}
while(p!=NULL);
printf("->End\n");
}
}
main()
{NODE *head;
int a[N]={2,4,6,8,10};
head=creatlist(a);
printf("\nThe original list:\n");
outlist(head);
head=fun(head);
printf("\nThe list after inverting :\n");
outlist(head);
}给定程序MODI1.C中函数fun的功能是:将s所指字符串中位于奇数位置的字符或ASCII码为偶数的字符放入t所指数组中(规定第一个字符放在第0位中)。
例如,字符串中的数据为:AABBCCDDEEFF,则输出应当是ABBCDDEFF。
请改正程序中的错误,使它能得出正确结果。
注意:不要改动main函数,不得增行或删行,也不得更改程序的结构。
给定源程序:
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *s, char t[])
{int i, j=0;
for(i=0; i<(int)strlen(s); i++)
/***********found**********/
if(__1__)
t[j++]=s[i];
/***********found**********/
__2__;
}
main()
{char s[N], t[N];
printf("\nPlease enter string s : "); gets(s);
fun(s, t);
printf("\nThe result is : %s\n",t);
}请编写函数fun,函数的功能是:将M行N列的二维数组中的数据,按列的顺序依次放到一维数组中。
例如,二维数组中的数据为:
33 33 33 33
44 44 44 44
55 55 55 55
则一维数组中的内容应该是:
33 44 55 33 44 55 33 44 55 33 44 55。
注意:部分源程序在文件PROG1.C中。
请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入你编写的若干语句。
给定源程序:
#include <stdio.h>
void fun(int s[][10], int b[], int *n, int mm, int nn)
{
__1__;
for(j=0;j<nn;j++)
for(__2__)
{
b[*n]=__3__;
__4__;
}
}
main()
{int w[10][10]={{33,33,33,33},{44,44,44,44},{55,55, 55,55}},i,j;
int a[100]={0}, n=0;void NONO ();
printf("The matrix:\n");
for(i=0; i<3; i++)
{for(j=0;j<4; j++)printf("%3d",w[i][j]);
printf("\n");
}
fun(w,a,&n,3,4);
printf("The A array:\n");
for(i=0;i<n;i++)printf("%3d",a[i]);printf("\n\n");
NONO();
}
void NONO ()
{/* 请在此函数内打开文件,输入测试数据,调用 fun 函数,输出数据,关闭文件。 */
FILE *rf, *wf ; int i, j, k ;
int w[10][10], a[100], n = 0, mm, nn ;
rf = fopen("in.dat","r");
wf = fopen("out.dat","w");
for(k = 0 ; k < 5 ; k++) {
fscanf(rf, "%d %d", &mm, &nn);
for(i = 0 ; i < mm ; i++)
for(j = 0 ; j < nn ; j++) fscanf(rf, "%d", &w[i][j]);
fun(w, a, &n, mm, nn);
for(i = 0 ; i < n ; i++) fprintf(wf, "%3d", a[i]); fprintf(wf, "\n");
}
fclose(rf); fclose(wf);
}一个栈的初始状态为空。一方面将元素5,4,3,2,1依次入栈,然后退栈一次,再将元素 A,B,C,D依次入栈,之后将所有元素所有退栈,则所有元素退栈(涉及中间退栈的元素)的顺序为____