国二计算机之填空国二计算机之填空
1.下列给定程序,函数fun的功能是:计算如下公式S=3/2*2-5/4*4+7/6*6...
#include
double fun(double e)
{ int i, k; double s, t, x;
s=0; k=1; i=2;
/**********found**********/
x=3.0/4;
/**********found**********/
while(x > e)
{ s=s+k*x;
k=k* (-1);
t=2*i;
/**********foun...
国二计算机之填空
1.下列给定程序,函数fun的功能是:计算如下公式S=3/2*2-5/4*4+7/6*6...
#include
double fun(double e)
{ int i, k; double s, t, x;
s=0; k=1; i=2;
/**********found**********/
x=3.0/4;
/**********found**********/
while(x > e)
{ s=s+k*x;
k=k* (-1);
t=2*i;
/**********found**********/
x=(t+1)/(t*t);
i++;
}
return s;
}
main()
{ double e=1e-3;
printf("\nThe result is: %f\n",fun(e));
}
2.下列给定程序中,函数fun的功能是:计算如下公式前n项的和,并作为函数值返回S=1*3/2*2+3*5/4*4+5*7/6*6... #include
double fun(int n)
{ int i; double s, t; /**********found**********/
s=0;
/**********found**********/
for(i=1; i<=n; i++)
{ t=2.0*i;
/**********found**********/
s=s+(2.0*i-1)*(2.0*i+1)/(t*t);
}
return s;
}
main()
{ int n=-1;
while(n<0)
{ printf("Please input(n>0): "); scanf("%d",&n); }
printf("\nThe result is: %f\n",fun(n));
}3.统计形参s所指的字符串中数字字符出现的次数,并存放在形参t所指的变量中,最后在
主函数中输出。
#include
void fun(char *s, int *t)
{ int i, n;
n=0;
/**********found**********/
for(i=0; s[i] !=0; i++)
/**********found**********/
if(s[i]>='0'&&s[i]<= '9' ) n++; /**********found**********/
*t=n ;
}
main()
{ char s[80]="abcdef35adgh3kjsdf7";
int t;
printf("\nThe original string is : %s\n",s);
fun(s,&t);
printf("\nThe result is : %d\n",t); }
4.把形参a所指的数组中的奇数按原顺序依次存放到a[0]、a[1]、a[2]…中,把偶数从数组中删除,奇数个数通过函数值返回。
#include
#define N 9
int fun(int a[], int n)
{ int i,j;
j = 0;
for (i=0; i
unsigned long fun(unsigned long n) { unsigned long x=0; int t;
while(n)
{ t=n%10;
/**********found**********/
if(t%2==0)
/**********found**********/
x=10*x+t;
/**********found**********/
n=n/10;
}
return x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0
#define N 9
void fun(int a[], int n)
{ int i,j, max, min, px, pn, t;
for (i=0; ia[j])
{ min = a[j]; pn = j; }
}
if (pn != i)
{ t = a[i]; a[i] = min; a[pn] = t;
if (px == i) px =pn;
}
if (px != i+1)
{ t = a[i+1]; a[i+1] = max; a[px] = t; }
}
}
main()
{ int b[N]={9,1,4,2,3,6,5,8,7}, i;
printf("\nThe original data :\n");
for (i=0; i
/**********found**********/
char fun(char ch)
{
/**********found**********/
if (ch>='0' && ch<='9')
/**********found**********/
return '9'- (ch-'0');
return ch ;
}
main()
{ char c1, c2;
printf("\nThe result :\n");
c1='2'; c2 = fun(c1);
printf("c1=%c c2=%c\n", c1, c2);
c1='8'; c2 = fun(c1);
printf("c1=%c c2=%c\n", c1, c2);
c1='a'; c2 = fun(c1);
printf("c1=%c c2=%c\n", c1, c2); }
8.求ss所指字符串数组中长度最短的字符串所在的行下标,作为函数值返回,并把其串长放在形参n所指的变量中。ss所指字符串数组中共有M个字符串,且串长小于N
#include
#include
#define M 5
#define N 20
int fun(char (*ss)[N], int *n) { int i, k=0, len= N;
/**********found**********/
for(i=0; i
void fun(char *s)
{ int i, j=0, k=0; char t1[80], t2[80];
for(i=0; s[i]!='\0'; i++)
if(s[i]>='0' && s[i]<='9')
{
/**********found**********/
t2[j]=s[i]; j++;
}
else t1[k++]=s[i];
t2[j]=0; t1[k]=0;
/**********found**********/
for(i=0; i
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *h, int x)
{ SLIST *p, *q, *s;
s=(SLIST *)malloc(sizeof(SLIST)); /**********found**********/
s->data=x;
q=h;
p=h->next;
while(p!=NULL && x>p->data) { /**********found**********/
q=p;
p=p->next;
}
s->next=p;
/**********found**********/
q->next=s;
}
SLIST *creatlist(int *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; idata=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 x;
int a[N]={11,12,15,18,19,22,25,29};
head=creatlist(a);
printf("\nThe list before inserting:\n"); outlist(head);
printf("\nEnter a number : "); scanf("%d",&x);
fun(head,x);
printf("\nThe list after inserting:\n"); outlist(head);
}
11.将形参a所指数组中的前半部分元素中的值与后半部分元素中的值对换、形参n中存放数组中数据的个数,若n为奇数,则中间的元素不动。
#include
#define N 9
void fun(int a[], int n)
{ int i, t, p;
/**********found**********/
p = (n%2==0)?n/2:n/2+1;
for (i=0; i
#include
#define N 5
#define M 10
int fun(char (*ss)[M], int k)
{ int i,j=0,len;
/**********found**********/
for(i=0; i< N ; i++)
{ len=strlen(ss[i]);
/**********found**********/
if(len<= k)
/**********found**********/
strcpy(ss[j++],ss[i]);
}
return j;
}
main()
{ char x[N][M]={"Beijing","Shanghai","Tianjing","Nanjing","Wuhan"};
int i,f;
printf("\nThe original string\n\n");
for(i=0;i=1; i=i-2) s[i+2]=s[i]; /**********found**********/
s[1]=c ;
}
main()
{ char s[80]="abcdefgh";
printf("\nThe original string is : %s\n",s);
fun(s);
printf("\nThe result is : %s\n",s); }
14.在形参ss所指字符串数组中查找与形参t所指字符串相同的串,找到后返回该串在字符串数组中的位置 即下标 ,若未找到则返回-1。ss所指字符串数组中共有N个不同的字符串,且串长小于M。
#include
#include
#define N 5
#define M 8
int fun(char (*ss)[M],char *t) { int i;
/**********found**********/
for(i=0; i
#include #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->next;
}
/**********found**********/
printf(" %d ",t->data);
s->next=NULL;
/**********found**********/
free(t);
}
SLIST *creatlist(int *a) { SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; idata=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);
}
}
16.逆置数组元素中的值 如123 321 形参n给出数组中数据的个数
#include
void fun(int a[], int n)
{ int i,t;
/**********found**********/
for (i=0; i
#include
int fun(char *source, char *target) { FILE *fs,*ft; char ch; /**********found**********/
if((fs=fopen(source, "r"))==NULL)
return 0;
if((ft=fopen(target, "w"))==NULL)
return 0;
printf("\nThe data in file :\n");
ch=fgetc(fs);
/**********found**********/
while(!feof(fs))
{ putchar( ch );
/**********found**********/
fputc(ch,ft);
ch=fgetc(fs);
}
fclose(fs); fclose(ft);
printf("\n\n");
return 1;
}
main()
{ char sfname[20] ="myfile1",tfname[20]="myfile2";
FILE *myf; int i; char c;
myf=fopen(sfname,"w");
printf("\nThe original data :\n");
for(i=1; i<30; i++){ c='A'+rand()%25;fprintf(myf,"%c",c); printf("%c",c); }
fclose(myf);printf("\n\n");
if (fun(sfname, tfname)) printf("Succeed!");
else printf("Fail!");
}
18.有N×N矩阵,根据给定的m(m<=N)值,将每行元素中的值均向右移动m个位置,左位置为0.
#include
#define N 4
void fun(int (*t)[N], int m) { int i, j;
/**********found**********/
for(i=0; i=0; j--) /**********found**********/
t[i][j+m]=t[i][j]; /**********found**********/
for(j=0; j
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a) { struct student b; int i; /**********found**********/
b = a;
b.sno = 10002;
/**********found**********/
strcpy(b.name, "LiSi");
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",b.sno, b.name);
/**********found**********/
for (i=0; i<3; i++) printf("%6.2f ", b.score[i]);
printf("\n");
}
main()
{ struct student s={10001,"ZhangSan", 95, 80, 88};
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
fun(s);
}
20.从键盘输入若干字符串(每行不超过80个字符),写入文件myfile4.txt中,用-1作字符串输入结束 的标志,然后将文件的内容显示在屏幕上,文件的读写分别由函数readtext和writetext实现。
#include
#include
#include
void WriteText(FILE *);
void ReadText(FILE *);
main()
{ FILE *fp;
if((fp=fopen("myfile4.txt","w"))==NULL)
{ printf(" open fail!!\n"); exit(0); }
WriteText(fp);
fclose(fp);
if((fp=fopen("myfile4.txt","r"))==NULL)
{ printf(" open fail!!\n"); exit(0); }
ReadText(fp);
fclose(fp);
}
/**********found**********/
void WriteText(FILE *fw)
{ char str[81];
printf("\nEnter string with -1 to end :\n");
gets(str);
while(strcmp(str,"-1")!=0) {
/**********found**********/
fputs(str,fw,fw); fputs("\n",fw);
gets(str);
}
}
void ReadText(FILE *fr)
{ char str[81];
printf("\nRead file and output to screen :\n");
fgets(str,81,fr);
while( !feof(fr) ) {
/**********found**********/
printf("%s",str);
fgets(str,81,fr);
}
}
21.有N×N矩阵,将矩阵的外围元素做顺时针旋转。操作顺序是:首先将第一行元素的值存入临时数组r,然后使第一列成为第一行,最后一行成为第一列,最后一列成为最后一行,再使临时数组中的元素成为最后一列。
#include
#define N 4
void fun(int (*t)[N])
{ int j ,r[N];
for(j=0; j=0;j--)
t[N-1][N-1-j]=t[j][N-1];
for(j=N-1; j>=0; j--)
/**********found**********/
t[j][N-1]=r[j];
}
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
#include
#define N 5
#define M 15
void fun(char (*ss)[M], char *substr) { int i,find=0;
/**********found**********/
for(i=0; i< N ; i++)
/**********found**********/
if( strstr(ss[i],substr) != NULL )
{ find=1; puts(ss[i]); printf("\n"); } /**********found**********/
if (find==0) printf("\nDon't found!\n"); }
main()
{ char x[N][M]={"BASIC","C langwage","Java","QBASIC","Access"},str[M];
int i;
printf("\nThe original string\n\n");
for(i=0;i
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *);
void outlist(SLIST *);
int fun( SLIST *h)
{ SLIST *p; int s=0;
p=h->next;
while(p)
{
/**********found**********/
s+= p->data;
/**********found**********/
p=p->next;
}
return s;
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48};
head=creatlist(a); outlist(head); /**********found**********/
printf("\nsum=%d\n", fun(head)); }
SLIST *creatlist(int a[])
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; idata=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("The list is NULL!\n");
else
{ printf("\nHead ");
do
{ printf("->%d", p->data); p=p->next; }
while(p!=NULL);
printf("->End\n");
}
}
24.程序通过定义学生结构体变量,存储学生的学号、姓名和3门课程的成绩。函数fun的功能是:将形参a所指结构体变量s中的数据进行修改,并把a的地址作为函数值返回主函
数,从主函数中输出修改的数据。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
struct student * fun(struct student *a) { int i;
a->sno = 10002;
strcpy(a->name, "LiSi");
/**********found**********/
for (i=0; i<3; i++) a->score[i] += 1; /**********found**********/
return a ;
}
main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, *t;
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
t = fun(&s);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t->sno, t->name);
for (i=0; i<3; i++) printf("%6.2f ", t->score[i]);
printf("\n");
}
25.将N×N矩阵主对角线院校的值与反向对角线对应位置上的元素的值进行交换。
#include
#define N 4
/**********found**********/
void fun(int t[][N], int n)
{ int i,s;
/**********found**********/
for(i=0;i
int fun(int x)
{ int n, s1, s2, s3, t;
n=0;
t=100;
/**********found**********/
while(t<=999){
/**********found**********/
s1=t%10; s2=(t/10)%10; s3=t/100;
/**********found**********/
if(s1+s2+s3==x)
{ printf("%d ",t);
n++;
}
t++;
}
return n;
}
main()
{ int x=-1;
while(x<0)
{ printf("Please input(x>0): "); scanf("%d",&x); }
printf("\nThe result is: %d\n",fun(x)); }
27.程序通过定义学生结构体变量,存储学生的学号、姓名和三门课程的成绩。函数fun的功能是:对形参b所指结构体变量中的数据进行修改,并在主函数中输出修改后的数据。 #include
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun( struct student *b)
{
/**********found**********/
b->sno = 10004;
/**********found**********/
strcpy(b->name, "LiJie");
}
main()
{ struct student t={10002,"ZhangQi", 93, 85, 87};
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);
printf("\n");
/**********found**********/
fun(&t);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);
printf("\n");
}
28.下列给定程序中已建立了一个带头结点的单向链表,联邦中的各结点数据域递增有序链接。函数fun的功能是:删除链表中数据域值相同的结点,使之只保留一个。
#include
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
void fun( SLIST *h)
{ SLIST *p, *q;
p=h->next;
if (p!=NULL)
{ q=p->next;
while(q!=NULL)
{ if (p->data==q->data)
{ p->next=q->next; /**********found**********/
free(q);
/**********found**********/
q=p->next;
}
else
{ p=q;
/**********found**********/
q=q->next;
}
}
}
}
SLIST *creatlist(int *a) { SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; idata=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]={1,2,2,3,4,4,4,5};
head=creatlist(a);
printf("\nThe list before deleting :\n"); outlist(head);
fun(head);
printf("\nThe list after deleting :\n"); outlist(head);
}
29.有N×N矩阵,以主对角线为对称线,对称元素相加并将结果存放在左下三角元素中,右上三角元素置为0.
#include
#define N 4
/**********found**********/
void fun(int (*t)[N] )
{ int i, j;
for(i=1; i
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, long sno) { FILE *fp;
STU n; int i;
fp = fopen(filename,"rb+");
/**********found**********/
while (!feof(fp))
{ fread(&n, sizeof(STU), 1, fp); /**********found**********/
if (n.sno==sno) break;
}
if (!feof(fp))
{ for (i=0; i<3; i++) n.score[i] += 3; /**********found**********/
fseek(fp, -(long)sizeof(STU), SEEK_CUR);
fwrite(&n, sizeof(STU), 1, fp);
}
fclose(fp);
}
main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j
double fun(int n)
{
int i,j;
double sum=0.0,t;
for(i=1;i<=n;i++)
{
t=0.0;
for(j=1;j<=i;j++)
t+=j;
sum+=(double)1/t;
}
return sum;
}
void main()
{
int n;
double s;
printf("\nInput n: ");
scanf("%d",&n);
s=fun(n);
printf("\n\ns=%f\n\n",s); }
32.函数fun的功能是计算下式:S=1/2*2+3/4*4+5/6*6+...+(2×n-1)/(2×n)*(2×n) 。
例如:形参e的值为le-3,函数的返回值为2.985678.
#include
double fun(double e)
{ int i; double s, x;
/**********found**********/
s=0; i=0;
x=1.0;
while(x>e){
/**********found**********/
i++;
/**********found**********/
x=(2.0*i-1)/((2.0*i)*(2.0*i));
s=s+x;
}
return s;
}
main()
{ double e=1e-3;
printf("\nThe result is: %f\n",fun(e));
}
33.在形参所指的字符串中的每个数字字符之后插入一个*号。
#include
void fun(char *s)
{ int i, j, n;
for(i=0; s[i]!='\0'; i++) /**********found**********/
if(s[i]>='0' && s[i]<='9')
{ n=0;
/**********found**********/
while(s[i+1+n]!= 0) n++;
for(j=i+n+1; j>i; j--) /**********found**********/
s[j+1]= s[j];
s[j+1]='*';
i=i+1;
}
}
main()
{ char s[80]="ba3a54cd23a";
printf("\nThe original string is : %s\n",s);
fun(s);
printf("\nThe result is : %s\n",s);
}
34.给定程序中,函数fun的功能是将不带头结点的单向链表逆置,即若原来链表中从头至尾结点数据域依次为2,4,6,8,10,逆置后从头至尾结点数据域依次为10,8,6,4,2.
#include
#include
#define N 5
typedef struct node {
int data;
struct node *next;
} NODE;
/**********found**********/ NODE * 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->next;
q->next = p;
p = q;
/**********found**********/
q = r;
}
return p;
}
NODE *creatlist(int a[]) { NODE *h,*p,*q; int i;
h=NULL;
for(i=0; idata=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);
}
35.程序通过定义学生结构体变量,存储学生的学号、姓名和三门课的成绩。函数fun的功能是:将形参a中的数据进行修改,把修改后的数据作为函数值返回主函数进行输出。
#include
#include
struct student {
long sno;
char name[10];
float score[3];
};
/**********found**********/
struct student fun(struct student a) { int i;
a.sno = 10002;
/**********found**********/
strcpy(a.name, "LiSi");
/**********found**********/
for (i=0; i<3; i++) a.score[i]+= 1;
return a;
}
main()
{ struct student s={10001,"ZhangSan", 95, 80, 88}, t;
int i;
printf("\n\nThe original data :\n");
printf("\nNo: %ld Name: %s\nScores: ",s.sno, s.name);
for (i=0; i<3; i++) printf("%6.2f ", s.score[i]);
printf("\n");
t = fun(s);
printf("\nThe data after modified :\n");
printf("\nNo: %ld Name: %s\nScores: ",t.sno, t.name);
for (i=0; i<3; i++) printf("%6.2f ", t.score[i]);
printf("\n");
}
36.给定程序中,函数fun的功能是将带头结点的单向链表逆置,即若原链表中从头至尾结点数据域依次为2,4,6,8,10,逆置后,从头至尾结点数据域依次为10,8,6,4,2.
#include
#include
#define N 5
typedef struct node {
int data;
struct node *next;
} NODE;
void fun(NODE *h)
{ NODE *p, *q, *r;
/**********found**********/
p = h->next;
/**********found**********/
if (p==NULL) return;
q = p->next;
p->next = NULL;
while (q)
{ r = q->next; q->next = p; /**********found**********/
p = q;
q = r;
}
h->next = p;
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h = (NODE *)malloc(sizeof(NODE));
h->next = NULL;
for(i=0; idata=a[i];
q->next = NULL;
if (h->next == NULL) h->next = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p = h->next;
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);
fun(head);
printf("\nThe list after inverting :\n");
outlist(head);
}
37.将形参s所指字符串中下标为奇数的字符取出,并按ASC2码大小递增排序,将排序后的字符存入形参p所指的字符数组中,形参一个新串。
#include
void fun(char *s, char *p) { int i, j, n, x, t;
n=0;
for(i=0; s[i]!='\0'; i++) n++;
for(i=1; is[j]) t=j;
if(t!=i)
{ x=s[i]; s[i]=s[t]; s[t]=x; }
}
for(i=1,j=0; i
#include
#define N 6
typedef struct node {
int data;
struct node *next;
} NODE;
void fun(NODE *h)
{ NODE *p, *q; int t;
p = h;
while (p) {
/**********found**********/
q = p->next ;
/**********found**********/
while (q)
{ if (p->data > q->data)
{ t = p->data; p->data = q->data; q->data = t; }
q = q->next;
}
/**********found**********/
p = p->next ;
}
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h=NULL;
for(i=0; idata=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]= {0, 10, 4, 2, 8, 6 };
head=creatlist(a);
printf("\nThe original list:\n");
outlist(head);
fun(head);
printf("\nThe list after inverting :\n");
outlist(head);
}
39.给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。
#include
void fun(char *s, int a, double f) {
/**********found**********/
FILE* fp;
char ch;
fp = fopen("file1.txt", "w");
fprintf(fp, "%s %d %f\n", s, a, f);
fclose(fp);
fp = fopen("file1.txt", "r");
printf("\nThe result :\n\n");
ch = fgetc(fp);
/**********found**********/
while (!feof(fp))
{
putchar(ch);
ch = fgetc(fp);
}
putchar('\n');
fclose(fp);
}
main()
{ char a[10]="Hello!"; int b=12345;
double c= 98.76;
fun(a,b,c);
}
40.在形参s所指字符串中寻找与参数c相同的字符,并在其后插入一个与之相同的字符,若找不到相同的字符则不做任何处理。
#include
void fun(char *s, char c)
{ int i, j, n;
/**********found**********/
for(i=0; s[i]!=0 ; i++)
if(s[i]==c)
{
/**********found**********/
n=0 ;
while(s[i+1+n]!='\0') n++;
for(j=i+n+1; j>i; j--) s[j+1]=s[j];
/**********found**********/
s[j+1]=c;
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); }
41.将形参s所指字符串中的所以字母字符顺序前移,其他字符顺序后移,处理后将新字符串的首地址作为函数值返回。
#include
#include
#include
char *fun(char *s)
{ int i, j, k, n; char *p, *t;
n=strlen(s)+1;
t=(char*)malloc(n*sizeof(char));
p=(char*)malloc(n*sizeof(char));
j=0; k=0;
for(i=0; i='a')&&(s[i]<='z'))||((s[i]>='A')&&(s[i]<='Z'))) {
/**********found**********/
t[j]=s[i]; j++;}
else
{ p[k]=s[i]; k++; }
}
/**********found**********/
for(i=0; i
#include
void fun(char *s, int a, double f) {
/**********found**********/
FILE * fp;
char str[100], str1[100], str2[100];
int a1; double f1;
fp = fopen("file1.txt", "w");
fprintf(fp, "%s %d %f\n", s, a, f); /**********found**********/
fclose(fp);
fp = fopen("file1.txt", "r"); /**********found**********/
fscanf(fp,"%s%s%s", str, str1, str2);
fclose(fp);
a1 = atoi(str1);
f1 = atof(str2);
printf("\nThe result :\n\n%s %d %f\n", str, a1, f1);
}
main()
{ char a[10]="Hello!"; int b=12345;
double c= 98.76;
fun(a,b,c);
}
43.计算N×N矩阵的主对角线元素和反向对角线元素之和,并作为函数值返回。要求先累加主对角线元素中的值,在累加反向对角线元素中的值。
#include
#define N 4
fun(int t[][N], int n)
{ int i, sum;
/**********found**********/
sum=0;
for(i=0; i
int fun(int x)
{ int n, s1, s2, s3, t;
/**********found**********/
n=0;
t=100;
/**********found**********/
while(t<=x)
{ s1=t%10; s2=(t/10)%10; s3=t/100;
if(s1+s2+s3==15)
{ printf("%d ",t);
n++;
}
/**********found**********/
t++;
}
return n;
}
main()
{ int x=-1;
while(x>999||x<0)
{ printf("Please input(0
#include
#define N 5
#define M 10
/**********found**********/
void fun(char (*ss) [M], int k) { int i=0 ;
/**********found**********/
while(i< N) {
/**********found**********/
ss[i][k]=0; i++; }
}
main()
{ char x[N][M]={"Create","Modify","Sort","skip","Delete"};
int i;
printf("\nThe original string\n\n");
for(i=0;i
#include
int fun(char *fname )
{ FILE *fp; int i,n; float x;
if((fp=fopen(fname, "w"))==NULL) return 0;
for(i=1;i<=10;i++)
/**********found**********/
fprintf(fp,"%d %f\n",i,sqrt((double)i));
printf("\nSucceed!~\n");
/**********found**********/
fclose(fp);
printf("\nThe data in file :\n"); /**********found**********/
if((fp=fopen(fname,"r"))==NULL)
return 0;
fscanf(fp,"%d%f",&n,&x);
while(!feof(fp))
{ printf("%d %f\n",n,x); fscanf(fp,"%d%f",&n,&x); }
fclose(fp);
return 1;
}
main()
{ char fname[]="myfile3.txt";
fun(fname);
}
47.将形参n所指变量中,各位上为偶数的数去掉,剩余的数按原来从高位到低位的顺序组成一个新数,并通过形参指针n传回所指变量。
#include
void fun(unsigned long *n)
{ unsigned long x=0, i; int t;
i=1;
while(*n)
/**********found**********/
{ t=*n %10;
/**********found**********/
if(t%2!=0)
{ x=x+t*i; i=i*10; }
*n =*n /10;
}
/**********found**********/
*n=x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0
#define N 4
void fun(int (*t)[N])
{ int i, j, x;
/**********found**********/
for(i=0; i=1; j--)
t[i][j]=t[i][j-1];
/**********found**********/
t[i][0]=x;
}
}
main()
{ int t[][N]={21,12,13,24,25,16,47,38,29,11,32,54,42,21,33,10}, i, j;
printf("The original array:\n");
for(i=0; i
#define N 9
/**********found**********/
void fun(int a[], int n)
{ int i, j, max, min, px, pn, t; /**********found**********/
for (i=0; i a[j])
{ min = a[j]; pn = j; }
}
if (px != i)
{ t = a[i]; a[i] = max; a[px] = t;
if (pn == i) pn= px;
}
if (pn != i+1)
{ t = a[i+1]; a[i+1] = min; a[pn] = t; }
}
}
main()
{ int b[N]={1,4,2,3,9,6,5,8,7}, i;
printf("\nThe original data :\n");
for (i=0; i
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(char *);
void outlist(SLIST *);
int fun( SLIST *h, char ch)
{ SLIST *p; int n=0;
p=h->next;
/**********found**********/
while(p!=NULL)
{ n++;
/**********found**********/
if (p->data==ch) return n;
else p=p->next;
}
return 0;
}
main()
{ SLIST *head; int k; char ch;
char a[N]={'m','p','g','a','w','x','r','d'};
head=creatlist(a);
outlist(head);
printf("Enter a letter:");
scanf("%c",&ch);
/**********found**********/
k=fun(head,ch);
if (k==0) printf("\nNot found!\n");
else printf("The sequence number is : %d\n",k);
}
SLIST *creatlist(char *a)
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; idata=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("->%c",p->data); p=p->next; }
while(p!=NULL);
printf("->End\n");
}
}
51.将形参n中,各位上为偶数的数取出,并按原来从高位到低位的顺序组成一个新数,作
为函数值返回。
#include
unsigned long fun(unsigned long n)
{ unsigned long x=0, s, i; int t;
s=n;
/**********found**********/
i=1;
/**********found**********/
while(s)
{ t=s%10;
if(t%2==0){
/**********found**********/
x=x+t*i; i=i*10;
}
s=s/10;
}
return x;
}
main()
{ unsigned long n=-1;
while(n>99999999||n<0)
{ printf("Please input(0 #define N 9
int fun(int a[], int n) { int i,j;
j = 0;
for (i=0; i
#include
#define N 8
typedef struct list
{ int data;
struct list *next;
} SLIST;
SLIST *creatlist(int *a);
void outlist(SLIST *);
void fun( SLIST *h, int *n) { SLIST *p;
/**********found**********/
*n=0;
p=h->next;
while(p)
{ (*n)++;
/**********found**********/
p=p->next;
}
}
main()
{ SLIST *head;
int a[N]={12,87,45,32,91,16,20,48}, num;
head=creatlist(a); outlist(head); /**********found**********/
fun(head, &num);
printf("\nnumber=%d\n",num); }
SLIST *creatlist(int a[])
{ SLIST *h,*p,*q; int i;
h=p=(SLIST *)malloc(sizeof(SLIST));
for(i=0; idata=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("The list is NULL!\n");
else
{ printf("\nHead ");
do
{ printf("->%d",p->data); p=p->next; }
while(p!=NULL);
printf("->End\n");
}
}
54.下列给定程序的功能是调用fun函数建立班级通讯录。通讯录中每位学生的编号、姓名和电话号码。班级人数和学生信息从键盘读入,每个人的信息作为一个数据块写到名为myfile5.dat的二进制文件中。
#include
#include
#define N 5
typedef struct
{ int num;
char name[10];
char tel[10];
}STYPE;
void check();
/**********found**********/
int fun(STYPE *std)
{
/**********found**********/
FILE *fp; int i;
if((fp=fopen("myfile5.dat","wb"))==NULL)
return(0);
printf("\nOutput data to file !\n");
for(i=0; i
#include
struct student {
long sno;
char name[10];
float score[3];
};
void fun(struct student a[], int n) {
/**********found**********/
struct student t;
int i, j;
/**********found**********/
for (i=0; i 0)
{ t = a[i]; a[i] = a[j]; a[j] = t; } }
main()
{ struct student s[4]={{10001,"ZhangSan", 95, 80, 88},{10002,"LiSi", 85, 70, 78},
{10003,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87}};
int i, j;
printf("\n\nThe original data :\n\n");
for (j=0; j<4; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("\n");
}
fun(s, 4);
printf("\n\nThe data after sorting :\n\n");
for (j=0; j<4; j++)
{ printf("\nNo: %ld Name: %-8s Scores: ",s[j].sno, s[j].name);
for (i=0; i<3; i++) printf("%6.2f ", s[j].score[i]);
printf("\n");
}56.人员的记录由编号和出生年、月、日组成,N名人员的数据已在主函数中存入结构体数组std中,且编号唯一。函数fun的功能是:找出指定编号人员的数据,作为函数值返回,由主函数返回,若指定编号不存在,返回数据中的编号为空串。
#include
#include
#define N 8
typedef struct
{ char num[10];
int year,month,day ;
}STU;
/**********found**********/
STU fun(STU *std, char *num)
{ int i; STU a={"",9999,99,99};
for (i=0; i
#include
#include
void main()
{
int n,m;
long sum;
sum=0;
system("CLS");
printf("\nInput n,m\n");
scanf("%d,%d",&n,&m);
while(n<=m)
{
sum+=n;
n++;
}
printf("sum=%d\n",sum);
}
58.程序的功能是计算S=累加k~。
#include
long fun(int n)
{
int i;
long s;
s=1;
for(i=1;i<=n;i++)
s=s*i;
return s;
}
main( )
{
long s;
int k,n;
scanf ("%d",&n);
s=0;
for (k=0;k<=n;k++)
s=s+fun(k);
printf("%ld,n",s);
}
59.将形参所指结构体数组中的三个元素按num成员进行升序。
#include
typedef struct
{ int num;
char name[10];
}PERSON;
/**********found**********/ void fun(PERSON std[])
{
/**********found**********/
PERSON temp;
if(std[0].num>std[1].num)
{ temp=std[0]; std[0]=std[1]; std[1]=temp; }
if(std[0].num>std[2].num)
{ temp=std[0]; std[0]=std[2]; std[2]=temp; }
if(std[1].num>std[2].num)
{ temp=std[1]; std[1]=std[2]; std[2]=temp; }
}
main()
{ PERSON std[ ]={ 5,"Zhanghu",2,"WangLi",6,"LinMin" };
int i;
/**********found**********/
fun(std);
printf("\nThe result is :\n");
for(i=0; i<3; i++)
printf("%d,%s\n",std[i].num,std[i].name);
}
60.将形参std所指结构体数组中年龄最多者的数据作为函数值返回,并在main函数中输出。
#include
typedef struct
{ char name[10];
int age;
}STD;
STD fun(STD std[], int n)
{ STD max; int i;
/**********found**********/
max=std[0];
for(i=1; i
typedef struct
{ int num;
char name[9];
char sex;
struct { int year,month,day ;} birthday;
float score[3];
}STU;
/**********found**********/
void show(STU tt)
{ int i;
printf("\n%d %s %c %d-%d-%d", tt.num, tt.name, tt.sex,
tt.birthday.year, tt.birthday.month, tt.birthday.day);
for(i=0; i<3; i++)
/**********found**********/
printf("%5.1f",tt.score[i]);
printf("\n");
}
main( )
{ STU std={ 1,"Zhanghua",'M',1961,10,8,76.5,78.0,82.0 };
printf("\nA student data:\n"); /**********found**********/
show(std);
}
62.对形参ss所指字符串数组中的M个字符按长度由短到长进行排序。ss所指字符串数组中共有M个字符串,且串长小于N。
#include
#include
#define M 5
#define N 20
void fun(char (*ss)[N])
{ int i, j, k, n[M]; char t[N];
for(i=0; in[j]) k=j;
if(k!=i)
{ strcpy(t,ss[i]);
strcpy(ss[i],ss[k]);
/**********found**********/
strcpy(ss[k], t);
n[k]=n[i];
}
}
}
main()
{ char ss[M][N]={"shanghai","guangzhou","beijing","tianjing","cchongqing"};
int i;
printf("\nThe original strings are :\n");
for(i=0; i
#include
#define M 5
#define N 20
void fun(char (*ss)[N])
{ int i, j, k=0, n, m, len;
for(i=0; in) {
/**********found**********/
n=len; k=i;
}
}
for(i=0; i=0; j--)
ss[i][m--]=ss[i][j];
for(j=0; j
#include
#define M 5
#define N 20
void fun(char (*ss)[N]) { int i, j, n, len=0;
for(i=0; in)n=len;
}
for(i=0; i
double f1(double x)
{ return x*x; }
double f2(double x, double y)
{ return x*y; }
/**********found**********/
double fun(int i, double x, double y) { if (i==1)
/**********found**********/
return f1(x);
else
/**********found**********/
return f2(x, y);
}
main()
{ double x1=5, x2=3, r;
r = fun(1, x1, x2);
r += fun(2, x1, x2);
printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n\n",x1, x2, r);
}
66.若形参ch中是小写英文字母,则转换成对应的大写英文字母;若ch中是大写英文字母,
则转换成对应的小写英文字母;若是其他字符则保持不变;并转换后的结果作为函数值返回。
#include
#include
char fun(char ch)
{
/**********found**********/
if ((ch>='a')&&(ch<='z'))
return ch -'a' + 'A';
if ( isupper(ch) )
/**********found**********/
return ch +'a'-'A'; /**********found**********/
return ch;
}
main()
{ char c1, c2;
printf("\nThe result :\n");
c1='w'; c2 = fun(c1);
printf("c1=%c c2=%c\n", c1, c2);
c1='W'; c2 = fun(c1);
printf("c1=%c c2=%c\n", c1, c2);
c1='8'; c2 = fun(c1);
printf("c1=%c c2=%c\n", c1, c2);
}
67.函数fun的功能是:计算f(x)=1+x-x*x/2!+x立方/3~-x四次方/4~.... 若x=2.5,n=15时,
函数值为1.917914
#include
#include
double fun(double x, int n) { double f, t; int i; /**********found**********/
f = 1.0 ;
t = -1;
for (i=1; i
#include
double fun(double x)
{ double f, t; int n;
f = 1.0 + x;
t=-x;
n = 1;
do {
n++;
t*=(-1.0)*x/n;
f += t;
}
while(abs(t)>=1e-6);
return f;
}
main()
{ double x, y;
x=2.5;
y = fun(x);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f\n", x, y);
}
69.函数fun的功能是:计算f(x)=1+x+x*x/2!+...+x的n次方/n~的前n项。若x=2.5,函
数值为12.182340.
#include
double fun(double x, int n) { double f, t; int i;
f = 1.0;
/**********found**********/
t=1;
/**********found**********/
for(i=1;i
#include
double fun(double x)
{ double f, t; int n; /**********found**********/
f = 1.0+x;
t = x;
n = 1;
do {
n++;
/**********found**********/
t *= x/n;
/**********found**********/
f += t;
} while (fabs(t) >= 1e-6);
return f;
}
main()
{ double x, y;
x=2.5;
y = fun(x);
printf("\nThe result is :\n");
printf("x=%-12.6f y=%-12.6f \n", x, y);
}
71.统计所有小于等于n(n>2)的素数的个数
#include
int fun(int n)
{ int i,j, count=0;
printf("\nThe prime number between 3 to %d\n", n);
for (i=3; i<=n; i++) { /**********found**********/
for (j=2; j=i)
{ count++; printf( count%15? "%5d":"\n%5d",i); }
}
return count;
}
main()
{ int n=20, r;
r = fun(n);
printf("\nThe number of prime is : %d\n", r);
}
72.统计长整数n的各位出现数字1,2,3的次数,并用全局变量c1,c2,c3返回
#include
int c1,c2,c3;
void fun(long n)
{
c1=c2=c3=0;
while(n)
{
switch(n%10)
{
case 1:
c1++;break;
case 2:
c2++;break;
case 3:
c3++;
}
n/=10;
}
}
main()
{
long n=123114350L;
fun(n);
printf("\nThe result: \n");
printf("n=%ld c1=%d c2=%d c3=%d\n",n,c1,c2,c3);
}
73。用刷选法可得到2~10000之间所有的素数,是:首先从素数2开始,将所有2的倍数从数表
中删除。接着从数表中找下一个非0数,以此类推。
#include
int fun(int n)
{ int a[10000], i,j, count=0;
for (i=2; i<=n; i++) a[i] = i;
i = 2;
while (i
/**********found**********/
#define OK(i, t, n) ((i%t==0) && (i/t
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename) { FILE *fp; int i, j;
STU s[N], t;
/**********found**********/
fp = fopen(filename, "r");
fread(s, sizeof(STU), N, fp);
fclose(fp);
for (i=0; i s[j].sno)
{ t = s[i]; s[i] = s[j]; s[j] = t; }
fp = fopen(filename, "wb"); /**********found**********/
fwrite(s, sizeof(STU), N, fp);
fclose(fp);
}
main()
{ STU t[N]={ {10005,"ZhangSan", 95, 80, 88}, {10003,"LiSi", 85, 70, 78},
{10002,"CaoKai", 75, 60, 88}, {10004,"FangFang", 90, 82, 87},
{10001,"MaChao", 91, 92, 77}}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), 5, fp);
fclose(fp);
printf("\n\nThe original data :\n\n");
for (j=0; j
#define N 5
typedef struct student {
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{ FILE *fp;
/**********found**********/
fp = fopen(filename, "rb+");
/**********found**********/
fseek(fp, -(long)sizeof(STU), SEEK_END); /**********found**********/
fwrite(&n, sizeof(STU), 1, fp);
fclose(fp);
}
main()
{ STU t[N]={ {10001,"MaChao", 91, 92, 77}, {10002,"CaoKai", 75, 60, 88},
{10003,"LiSi", 85, 70, 78}, {10004,"FangFang", 90, 82, 87},
{10005,"ZhangSan", 95, 80, 88}};
STU n={10006,"ZhaoSi", 55, 70, 68}, ss[N];
int i,j; FILE *fp;
fp = fopen("student.dat", "wb");
fwrite(t, sizeof(STU), N, fp);
fclose(fp);
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
printf("\nThe original data :\n\n");
for (j=0; j
#include
#define N 10
double fun(double x[],double *y)
{ int i,j; double av;
/**********found**********/
av=0.0;
/**********found**********/
for(i=0; iav) y[j++]= x[i];
y[j]=-1;
return av;
}
main()
{ int i; double x[N],y[N];
for(i=0; i=0; i++) printf("%5.1f ",y[i]);
printf("\n");
}
78。计算X所指数组中N个的平均值。
#include
#include
#define N 10
double fun(double x[],double *av) { int i,j; double d,s;
s=0;
for(i=0; i
#include
#define N 10
double fun(double *x)
{ int i, j; double s, av, y[N];
s=0;
for(i=0; iav ){
/**********found**********/
y[j++]=x[i];
x[i]=-1;}
for(i=0; i
#include
#define N 10
double fun(double *x)
{ int i, j; double av, y[N];
av=0;
for(i=0; i
#include
#include
#define N 9
long ctod( char *s )
{ long d=0;
while(*s)
if(isdigit( *s)) { /**********found**********/
d=d*10+*s-'0';
/**********found**********/
s++;
}
return d;
}
long fun( char *a, char *b ) {
/**********found**********/
return ctod(a)+ctod(b); }
main()
{ char s1[N],s2[N];
do
{ printf("Input string s1 : "); gets(s1); }
while( strlen(s1)>N );
do
{ printf("Input string s2 : "); gets(s2); }
while( strlen(s2)>N );
printf("The result is: %ld\n", fun(s1,s2) );
}
82。调用随机函数产生的20个互不相同的整数放在形参a所指数组中。
#include
#include
#define N 20
void fun( int *a)
{ int i, x, n=0;
x=rand()%20;
/**********found**********/
while (n<20)
{ for(i=0; i
#define N 7
/**********found**********/ void fun(int (*a)[N])
{ int i,j,k,m;
if(N%2==0) m=N/2 ;
else m=N/2+1;
for(i=0; i
#define N 7
/**********found**********/ void fun(int (*a)[N])
{ int i,j,k,m;
if(N%2==0) m=N/2 ;
else m=N/2+1;
for(i=0; i
#define N 3
int fun(int (*a)[N])
{ int i,j,m1,m2,row,colum;
m1=m2=0;
for(i=0; i
double f1(double x)
{ return x*x; }
double f2(double x, double y) { return x*y; }
double fun(double a, double b) {
/**********found**********/
double (*f)();
double r1, r2;
/**********found**********/
f = f1 ;
r1 = f(a);
/**********found**********/
f = f2 ;
r2 = (*f)(a, b);
return r1 + r2;
}
main()
{ double x1=5, x2=3, r;
r = fun(x1, x2);
printf("\nx1=%f, x2=%f, x1*x1+x1*x2=%f\n",x1, x2, r);
}87。将带头结点的单向链表节点域中的数据从小到大排序。
3#include
#include
#define N 6
typedef struct node {
int data;
struct node *next;
} NODE;
void fun(NODE *h)
{ NODE *p, *q; int t; /**********found**********/
p = h ;
while (p) {
/**********found**********/
q = p->next ;
while (q) {
/**********found**********/
if (p->data> q->data)
{ t = p->data; p->data = q->data; q->data = t; }
q = q->next;
}
p = p->next;
}
}
NODE *creatlist(int a[])
{ NODE *h,*p,*q; int i;
h = (NODE *)malloc(sizeof(NODE));
h->next = NULL;
for(i=0; idata=a[i];
q->next = NULL;
if (h->next == NULL) h->next = p = q;
else { p->next = q; p = q; }
}
return h;
}
void outlist(NODE *h)
{ NODE *p;
p = h->next;
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]= {0, 10, 4, 2, 8, 6 };
head=creatlist(a);
printf("\nThe original list:\n");
outlist(head);
fun(head);
printf("\nThe list after sorting :\n");
outlist(head);
}
88。把数组aa中的偶数元素按原来的先后顺序放在原数组后面。
#include
#include
#define N 10
void fun(int aa[])
{
int i, j=0,k=0;
int bb[N];
for (i=0;i
#include
#define N 80
void main()
{
char str[N], ch;
int i;
system("CLS");
printf("\n Input a string:\n");
gets(str);
printf("\n*** original string ***\n");
puts(str);
ch=str[0];
for(i=0;str[i+1]!='\0';i++)
str[i]=str[i+1];
str[i]=ch;
printf("\n *** new string ***\n");
puts(str);
}
90。判断字符ch是否与串str中的某个字符相同,若相同什么也不做,不同插在字符串末尾。
#include
#include
void fun (char *str,char ch) {
while (*str &&*str !=ch) str++;
if(*str!=ch)
{ str[0]=ch;
*(str+1)=0;
}
}
void main()
{char s[81],c;
system("CLS");
printf("\nPlease enter a string : "); gets(s);
printf("\n Please enter the character to search : ");
c=getchar();
fun(s,c);
printf("\nThe result is %s\n",s); }
91。从一个字符串中截取前面若干个给定长度的子字符串。
#include
#include
#include
#define LEN 80
void main()
{
char str1[LEN],str2[LEN];
int n,i;
system("CLS");
printf("Enter the string:\n");
gets(str1);
printf("Enter the position of the string deleted:");
scanf("%d",&n);
for(i=0;i
int fun(int a,int b,int c) { int t;
t=(a>b)?(b>c?b:(a>c?c:a))
:((a>c)?a
:((b>c)?c:b));
return t;
}
main()
{
int a1=3,a2=5,a3=4,r;
r=fun(a1,a2,a3);
printf("\nThe middle number is: %d\n ",r);
}
93。从键盘输入一个字符串以及一个指定字符然后把这个字符后面的所有字符全部删除。
#include
#include
#define N 80
void main()
{
int i=0;
char str[N];
char ch;
system("CLS");
printf("\n Input a string:\n");
gets(str);
printf("\n Input a charator;\n");
scanf("%c", &ch);
while(str[i]!='\0')
{
if(str[i]==ch)
break;
i++;
}
str[i]='\0';
printf("\n*** display string ***\n");
puts(str);
}
94。寻找两个整数间的所有素数。
#include #include #include #define N 1000
int fun(int n,int m,int bb[N])
{
int i,j,k=0,flag;
for(j=n;j<=m;j++)
{
flag=1;
for(i=2;i #include
#include
#define N 20
int bb[N];
int fun(char *str,char ch) {
int i=0, n=0;
char t=ch;
char *p=str;
while(*p)
{
if (*p==t)
bb[n++]=i;
p++;
i++;
}
return n;
}
void main()
{
char str[N];
char ch;
int i,n;
system("CLS");
printf("*******Input the original string ********\n");
gets(str);
printf("*******The Original ********\n");
puts(str);
printf("******* Input character *******\n");
scanf("%c",&ch);
n=fun(str,ch);
printf("\nThe numbr of character is:%d\n",n);
printf("********The suffix of character *******\n");
for(i=0;i
#include
#include
#define N 20
int bb[N];
int fun(char *str,char ch) {
int i=0, n=0;
char t=ch;
char *p=str;
while(*p)
{
if (*p==t)
bb[n++]=i;
p++;
i++;
}
return n;
}
void main()
{
char str[N];
char ch;
int i,n;
system("CLS");
printf("*******Input the original string ********\n");
gets(str);
printf("*******The Original ********\n");
puts(str);
printf("******* Input character *******\n");
scanf("%c",&ch);
n=fun(str,ch);
printf("\nThe numbr of character is:%d\n",n);
printf("********The suffix of character *******\n");
for(i=0;i #include #include int fun(int *s,int t,int *k)
{
int i,max;
max=s[0];
for(i=0;imax)
{
max=s[i];
*k=i;
}
}
main()
{
int a[10]={ 876,675,896,101,301,401,980,431,451,777},k;
system("CLS");
fun(a, 10, &k);
printf("%d, %d\n ", k, a[k]);
}
98。求100以为能被2或5整除,但不能被2和5同时整除的自然数。
#include #include #define N 100
int fun(int bb[])
{
int i, j;
for(i=1,j=0; i<100; i++)
if((i%2!=0&&i%5==0)||(i%2==0&&i%5!=0))
bb[j++]=i;
return j;
}
void main()
{
int i, n;
int bb[N];
system("CLS");
n=fun(bb);
for(i=0; i #include
#include
#define LEN 80
void main()
{
char str1[LEN],str2[LEN];
char *p1=str1,*p2=str2;
int i=0,j=0;
system("CLS");
printf("Enter the string:\n");
scanf("%s",str1);
printf("***the origial string***\n");
while(*(p1+j))
{
printf("%c",*(p1+j));
j++;
}
for(i=0;i
#include
#include
void main()
{
float a,b,c, disc,x1,x2;
system("CLS");
do
{
printf("Input a,b,c:");
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc<0)
printf("disc=%f\n Input again!\n",disc);
}while(disc<0);
printf("*******the result*******\n");
x1=(-b+sqrt(disc))/(2*a);
x2=(-b-sqrt(disc))/(2*a);
printf("\nx1=%6.2f\nx2=%6.2f\n",x1,x2);
}
本文档为【国二计算机之填空】,请使用软件OFFICE或WPS软件打开。作品中的文字与图均可以修改和编辑,
图片更改请在作品中右键图片并更换,文字修改请直接点击文字进行修改,也可以新增和删除文档中的内容。
[版权声明] 本站所有资料为用户分享产生,若发现您的权利被侵害,请联系客服邮件isharekefu@iask.cn,我们尽快处理。
本作品所展示的图片、画像、字体、音乐的版权可能需版权方额外授权,请谨慎使用。
网站提供的党政主题相关内容(国旗、国徽、党徽..)目的在于配合国家政策宣传,仅限个人学习分享使用,禁止用于任何广告和商用目的。