【程序设计】
题目:函数fun的功能是:将s所指字符串中除下标为偶数同时ASCII码值也为偶数的字符
外,其余的全部删除;字符串中剩余字符所形成的新串放在t所指的数组中。
例如:若s所指字符串中的内容为"ABCDEFG123456",其中字符A的ASCII码值为奇数,因此应
当删除;字符B的ASCII码值为偶数,但在数组中的下标为奇数,因此也应当删除;字
符2的ASCII码值为偶数,在数组中的下标也为偶数,因此不应当删除,其他依此类推。
最后t所指的数组中的内容应是"246"。
注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号中填入
你编写的若干语句。
#include <stdio.h>
#include <string.h>
void wwjt ( );
void fun(char *s, char t[])
{
/**********Program**********/
/********** End **********/
}
main()
{
char s[100], t[100];
printf("\nPlease enter string S:");
scanf("%s", s);
fun(s, t);
printf("\nThe result is: %s\n", t);
wwjt();
}
void wwjt ( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
char s[100], t[100] ;
FILE *in, *out ;
int i ;
in=fopen("in.dat","r");
if(in==NULL)
{
printf("Please Verify The Currernt Dir..It May Be Changed");
}
out=fopen("out.dat","w");
if(out==NULL)
{
printf("Please Verify The Current Dir.. It May Be Changed");
}
for(i = 0 ; i < 10 ; i++)
{
fscanf(in, "%s", s) ;
fun(s, t) ;
fprintf(out, "%s\n", t) ;
}
fclose(in) ;
fclose(out) ;
}
【程序设计】
---------------------------------------------------------
题目:编写函数fun,其功能是:利用下面的简单迭代方法求方程cos(x)-x=0的一个实根。
迭代步骤如下:
(1)取x1初值为0.0;
(2)x0=x1,将x1的值赋给x0;
(3)x1=cos(x0),求出一个新的x1;
(4)若x0-x1的绝对值小于0.000001,执行步聚(5),否则执行步聚(2);
(5)所求x1就是方程cos(x)-x=0的一个实根,作为函数值返回。
程序将输出结果Root=0.739086。
注意:请勿改动主函数main和其它函数中的任何内容,仅在函数fun的花括号填入
你编写的若干语句。
#include <math.h>
#include <stdio.h>
void wwjt ( );
double fun()
{
/**********Program**********/
/********** End **********/
}
main()
{
printf("Root =%f\n", fun());
wwjt();
}
void wwjt ( )
{/* 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 */
FILE *out ;
out = fopen("out.dat","w") ;
fprintf(out, "%f\n", fun()) ;
fclose(out) ;
}
【程序设计】
---------------------------------------------------------
题目:请编写函数fun,该函数的功能是:删除一维数组中所有相同的数,使之只剩一个。
数组中的数已按由小到大的顺序排列,函数返回删除后数组中数据的个数。
例如:若一维数组中的数据是:2 2 2 3 4 4 5 6 6 6 6 7 7 8 9 9 10 10 10删除后,数
组中的内容应该是: 2 3 4 5 6 7 8 9 10。
注意:请勿改动main函数和其他函数中的任何内容,仅在函数fun的花括号中填入
所编写的若干语句。
#include <stdio.h>
#define N 80
void wwjt( );
int fun(int a[], int n)
{
/**********Program**********/
/********** End **********/
}
void main()
{
int a[N]={ 2,2,2,3,4,4,5,6,6,6,6,7,7,8,9,9,10,10,10,10}, i, n=20;
printf("The original data :\n");
for(i=0; i<n; i++)
printf("%3d",a[i]);
n=fun(a,n);
printf("\n\nThe data after deleted :\n");
for(i=0; i<n; i++)
printf("%3d",a[i]);
printf("\n\n");
wwjt();
}
void wwjt()
{
FILE *in,*out;
int a[N],i,n=20;
in=fopen("in.dat","r");
if(in==NULL)
{
printf("Please Verify The Currernt Dir..It May Be Changed");
}
out=fopen("out.dat","w");
if(out==NULL)
{
printf("Please Verify The Current Dir.. It May Be Changed");
}
for(i=0;i<20;i++)
{
fscanf(in,"%d",&a[i]);
}
n=fun(a,n);
for(i=0; i<n; i++)
fprintf(out,"%3d",a[i]);
fclose(in);
fclose(out);
}
【程序填空】
---------------------------------------------------------
题目:下列给定程序中,函数fun的功能是:从形参ss所指字符串数组中,删除所有串长
超过k的字符串,函数返回剩余字符串的个数。ss所指字符串数组中共有N个字符串,
且串长小于M。
#include <stdio.h>
#include <string.h>
#define N 5
#define M 10
int fun(char (*ss)[M], int k)
{
int i,j=0,len;
/***********SPACE***********/
for(i=0; i< k ; i++)
{
len=strlen(ss[i]);
/***********SPACE***********/
if(len<=strlen(ss[j]))
/***********SPACE***********/
strcpy(ss[j++],j++);
}
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<N;i++)puts(x[i]); printf("\n");
f=fun(x,7);
printf("The string witch length is less than or equal to 7 :\n");
for(i=0; i<f; i++) puts(x[i]);printf("\n");
}
【程序填空】
---------------------------------------------------------
题目:下列给定程序中,函数fun的功能是:在形参ss所指字符串数组中查找与形参t所指
字符串相同的串,找到后返回该串在字符串数组中的位置(即下标值),若未找到则
返回-1。ss所指字符串数组中共有N个内容不同的字符串,且串长小于M。
#include <stdio.h>
#include <string.h>
#define N 5
#define M 8
int fun(char (*ss)[M],char *t)
{
int i;
/***********SPACE***********/
for(i=0; i< 【?】 ; i++)
if(strcmp(ss[i],t)==0 )
/***********SPACE***********/
return 【?】 ;
return -1;
}
main()
{
char ch[N][M]={"if","while","switch","int","for"},t[M];
int n,i;
printf("\nThe original string\n\n");
for(i=0;i<N;i++)puts(ch[i]);
printf("\n");
printf("\nEnter a string for search: ");
gets(t);
n=fun(ch,t);
/***********SPACE***********/
if(n== 【?】)
printf("\nDon't found!\n");
else
printf("\nThe position is %d .\n",n);
}
【程序填空】
---------------------------------------------------------
题目:下列给定程序中,函数fun的功能是:找出100~999之间(含100和999)所有整数中各
位上数字之和为x(x为正整数)的整数,并输出;符合条件的整数个数作为函数值返回。
例如:当x值为5时,100~999之间各位上数字之和为5的整数有:104、113、122、131、
140、203、212、221、230、302、311、320、401、410、500,共有15个。
当x值为27时,各位数字之和为27的整数是:999,只有1个。
#include <stdio.h>
int fun(int x)
{
int n, s1, s2, s3, t;
n=0;
t=100;
/***********SPACE***********/
while(t<=999)
{
s1=t%10;
/***********SPACE***********/
s2=(t/10)%10;
s3=t/100;
/***********SPACE***********/
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));
}
【程序填空】
---------------------------------------------------------
题目:给定程序中,函数fun的功能是将形参给定的字符串、整数、浮点数写到文本文件
中,再用字符方式从此文本文件中逐个读入并显示在终端屏幕上。
#include <stdio.h>
void fun(char *s, int a, double f)
{
/***********SPACE***********/
【?】 fp;
char str;
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");
str = fgetc(fp);
/***********SPACE***********/
while (!feof(【?】))
{
/***********SPACE***********/
putchar(【?】);
str = fgetc(fp);
}
putchar('\n');
fclose(fp);
}
main()
{
char a[10]="Hello!";
int b=12345;
double c= 98.76;
fun(a,b,c);
}
【程序填空】
---------------------------------------------------------
题目:给定程序中,函数fun的功能是将参数给定的字符串、整数、浮点数写到文本文件中,
再用字符串方式从此文本文件中逐个读入,并调用库函数atoi和atof将字符串转换
成相应的整数、浮点数,然后将其显示在屏幕上。
#include <stdio.h>
#include <stdlib.h>
void fun(char *s, int a, double f)
{
/***********SPACE***********/
【?】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);
/***********SPACE***********/
【?】 ;
fp = fopen("file1.txt", "r");
/***********SPACE***********/
fscanf(【?】,"%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);
}
【程序填空】
---------------------------------------------------------
题目:下列给定程序中,函数fun的功能是:统计带头结点的单向链表中结点的个数,并存放
在形参n所指的存储单元中。
#include <stdio.h>
#include <stdlib.h>
#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;
/***********SPACE***********/
【?】=0;
p=h->next;
while(p)
{
(*n)++;
/***********SPACE***********/
p=p->【?】;
}
}
main()
{
SLIST *head;
int a[N]={12,87,45,32,91,16,20,48}, num;
head=creatlist(a);
outlist(head);
/***********SPACE***********/
fun(【?】, &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; 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("The list is NULL!\n");
else
{
printf("\nHead ");
do
{
printf("->%d",p->data);
p=p->next;
}
while(p!=NULL);
printf("->End\n");
}
}
【程序填空】
---------------------------------------------------------
题目:程序通过定义学生结构体变量,存储了学生的学号、姓名和三门课的成绩。所有学
生数据均以二进制方式输出到文件中。函数fun的功能是重写形参filename所指文件
中最后一个学生的数据,即用新的学生数据覆盖该学生原来的数据,其他学生的数
据不变。
#include <stdio.h>
#define N 5
typedef struct student
{
long sno;
char name[10];
float score[3];
} STU;
void fun(char *filename, STU n)
{
FILE *fp;
/***********SPACE***********/
fp = fopen(【?】, "rb+");
/***********SPACE***********/
fseek(【?】, -(long)sizeof(STU), SEEK_END);
/***********SPACE***********/
fwrite(&n, sizeof(STU), 1, 【?】);
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<N; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++)
printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
fun("student.dat", n);
printf("\nThe data after modifing :\n\n");
fp = fopen("student.dat", "rb");
fread(ss, sizeof(STU), N, fp);
fclose(fp);
for (j=0; j<N; j++)
{
printf("\nNo: %ld Name: %-8s Scores: ",ss[j].sno, ss[j].name);
for (i=0; i<3; i++)
printf("%6.2f ", ss[j].score[i]);
printf("\n");
}
}
【程序填空】
---------------------------------------------------------
题目:给定程序中,函数fun的功能是:找出N×N矩阵中每列元素中的最大值,并按顺序
依次存放于形参b所指的一维数组中。
#include <stdio.h>
#define N 4
void fun(int (*a)[N], int *b)
{
int i,j;
for(i=0; i<N; i++)
{
/***********SPACE***********/
b[i]=【?】;
for(j=1; j<N; j++)
/***********SPACE***********/
if(b[i]【?】 a[j][i])
b[i]=a[j][i];
}
}
main()
{
int x[N][N]={ {12,5,8,7},{6,1,9,3},{1,2,3,4},{2,8,4,3} },y[N],i,j;
printf("\nThe matrix :\n");
for(i=0;i<N; i++)
{
for(j=0;j<N; j++)
printf("%4d",x[i][j]);
printf("\n");
}
/***********SPACE***********/
fun(【?】);
printf("\nThe result is:");
for(i=0; i<N; i++)
printf("%3d",y[i]);
printf("\n");
}
【程序填空】
---------------------------------------------------------
题目:给定程序中,函数fun的功能是将带头结点的单向链表结点数据域中的数据从小到
大排序。即若原链表结点数据域从头至尾的数据为:10、4、2、8、6,排序后链
表结点数据域从头至尾的数据为:2、4、6、8、10。
#include <stdio.h>
#include <stdlib.h>
#define N 6
typedef struct node
{
int data;
struct node *next;
} NODE;
void fun(NODE *h)
{
NODE *p, *q;
int t;
/***********SPACE***********/
p = 【?】;
while (p)
{
/***********SPACE***********/
q = 【?】;
while (q)
{
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; i<N; i++)
{
q=(NODE *)malloc(sizeof(NODE));
q->data=a[i];
q->next = NULL;
/***********SPACE***********/
if (h->next == 【?】)
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);
}
【程序填空】
---------------------------------------------------------
题目:给定程序中,函数fun的功能是:把形参s所指字符串中最右边的n个字符复制到形
参t所指字符数组中,形成一个新串。若s所指字符串的长度小于n,则将整个字符
串复制到形参t所指字符数组中。
例如:形参s所指的字符串为:abcdefgh,n的值为5,程序执行后t所指字符数组中的字符
串应为:defgh。
#include <stdio.h>
#include <string.h>
#define N 80
void fun(char *s, int n, char *t)
{
int len,i,j=0;
len=strlen(s);
if(n>=len)
/***********SPACE***********/
strcpy(【?】);
else
{
for(i=len-n; i<=len-1; i++)
/***********SPACE***********/
t[j++]= 【?】 ;
/***********SPACE***********/
t[j]= 【?】 ;
}
}
main()
{
char s[N],t[N];
int n;
printf("Enter a string: ");
gets(s);
printf( "Enter n:");
scanf("%d",&n);
fun(s,n,t);
printf("The string t : ");
puts(t);
}