@abnor
2017-03-22T11:34:17.000000Z
字数 4316
阅读 1898
梁策
- 默认大家都学过C语言
- 局限于考试框架
3.以下代码不是我写的!
对象\操作 | 运算 | 查找 | 排列 |
---|---|---|---|
数字 | |||
字符 | |||
结构 |
数据可来源于主函数,也可来源于文件
请编写函数fun(),它的功能是求Fibonacci数列中小于t的最大的一个
数,结果由函数返回。其中Fibonacci 数列F(n)的定义为 F(0)=0,F(1)=1,
F(n)=F(n-1)+F(n-2) 例如:t=1000 时 ,函数值为987。 注意:部分源程序给
出如下。 请勿改动主函数main 和其他函数中的任何内容,仅在函数fun的花括号
中填入所编写的若干语句。 试题程序:
#include < conio.h >
#include < math.h >
#include < stdio.h >
int fun(int t) //编写函数
{…… }
main()
{
int n;
clrscr();
n=1000;
printf("n=%d, f=%d\n",n, fun(n));
}
/* 解析 */
int fun(int t)
{
int a=1,b=1,c=0,i;
do {
c=a+b;
a=b;
b=c;
}
while (c< t);
c=a;
return c;}
Note:
- 根据题目阅读代码,main函数里的代码很多可以忽略
- 函数实现尽量不用递归
- 类型转换,传值传指
请编写一个函数fun,它的功能是:将一个数字字符串转换为一个整数(不得调用C 语言提供的将字符串转换为整数的函数)。
例如,若输入字符串"-1234",则函数把它转换为整数值 -1234。
注意:部分源程序存在文件prog.c中。
#include < stdio.h >
#include < string.h >
long fun(char *p)
{……}
main() /* 主函数 */
{ char s[6];
long n;
printf("Enter a string:\n") ;
gets(s);
n = fun(s);
printf("%ld\n", n);
}
/*解析*/
long fun(char *p)
{
long n=0;
int flag=0;
while(*p!='\0')
{
if(*p=='-')
flag=1;
else if (*p=='+')
flag=0;
else
n=n*10+(*p-'0');
p++;
}
if (flag==1) n=-n;
return n;
}
字符运算使用指针
给定程序的功能是分别统计字符串中大写字母和小写字母的个数。
例如,给字符串ss输入:AaaaBBb123CCccccd,
则输出结果应为:upper = 5,lower = 9
#include < stdio.h >
void fun ( char *s, int *a, int *b )
{
while ( *s )
{ if ( *s >= 'A' && *s < = 'Z' )
/**********found**********/
___1___ ;
if ( *s >= 'a' && *s < = 'z' )
/**********found**********/
___2___ ;
s++;
}
}
main( )
{
char s[100]; int upper = 0, lower = 0 ;
printf( "\nPlease a string : " ); gets ( s );
fun ( s, & upper, &lower );
/**********found**********/
printf( "\n upper = %d lower = %d\n", ___3___ );
}
Note: 遍历+判断
请编写函数fun,函数的功能是:移动一维数组中的内容;若数组中有n个整数,要求把下标从0到p(含p,p小于等于n-1)的数组元素平移到数组的最后。
例如,一维数组中的原始内容为:1,2,3,4,5,6,7,8,9,10;p的值为3。移动后,一维数组中的内容应为: 5,6,7,8,9,10,1,2,3,4。
#include < stdio.h >#define N 80
void fun(int *w, int p, int n)
{……}
main()
{
int a[N]={1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
int i,p,n=15;
printf("The original data:\n");
for(i=0; i< n; i++)printf("%3d",a[i]);
printf("\n\nEnter p: ");scanf("%d",&p);
fun(a,p,n);
printf("\nThe data after moving:\n");
for(i=0; i< n; i++)printf("%3d",a[i]);
printf("\n\n");
}
/*解析*/
void fun(int *w, int p, int n)
{
int x,j,ch;
for(x=0;x<=p;x++)
{
ch=w[0];
for(j=1;j<n;j++)
{
w[j-1]=w[j];
}
w[n-1]=ch; /*将0到p个数组元素逐一赋给数组w[n-1]*/
}
}
下列给定程序中函数fun()的功能是:在字符串的最前端加入n 个*号,
形成新串,并且覆盖原串。
注意:字符串的长度最长允许79。
请改正函数fun()中的错误,使它能得出正确的结果。
注意:不要改动main 函数,不得增行或删行,也不得更改程序的结构。
试题程序:
#include < stdio.h >
#include < string.h >
#include < conio.h>
/**********************found***********************/
void fun(char s[],int n)
{
char a[80],*p;
int i;
/**********************found***********************/
s=p;
for(i=0;i< n;i++) a[i]= '*';
do
{a[i]=*p;
/**********************found***********************/
i++;
}while(*p);
a[i]=0;
strcpy(s,a);
}
main()
{int n;char s[80];
clrscr();
printf("\nEnter a string: ");gets(s);
printf("\nThe string\%s\n",s);
printf("\nEnter n (number of*):"); scanf("%d",&n);
fun(s,n);
printf("\nThe string after inster:\%s\n",s);
}
给定程序功能是用冒泡法对6个字符串进行排序。
#include < stdio.h >
#define MAXLINE 20
fun ( char *pstr[6])
{
int i, j ;
char *p ;
for (i = 0 ; i < 5 ; i++ )
{
for (j = i + 1; j < 6; j++)
{
/**************found**************/
if(strcmp(*(pstr+i),___1___) >0)
{
p = *(pstr + i) ;
/**************found**************/
pstr[i] = ___2___ ;
/**************found**************/
*(pstr + j) = ___3___ ;
}
}
}
}
main( )
{
int i ;
char *pstr[6], str[6][MAXLINE] ;
for(i = 0; i < 6 ; i++) pstr[i] = str[i] ;
printf( "\nEnter 6 string(1 string at each line): \n" ) ;
for(i = 0 ; i < 6 ; i++) scanf("%s", pstr[i]) ;
fun(pstr) ;
printf("The strings after sorting:\n") ;
for(i = 0 ; i < 6 ; i++) printf("%s\n", pstr[i]) ;
}
/*解析*/
【1】*(pstr+j) 【2】pstr[j] 【3】p
给定程序的功能是调用fun函数建立班级通讯录。通讯录中记录每位学生的编号、姓名和电话号码。
班级的人数和学生的信息从键盘读入,每个人的信息作为一个数据块写到名为myfile5.dat 的二进制文件中。
#include < stdio.h >
#include < stdlib.h >
#define N 5
typedef struct
{
int num;
char name[10];
char tel[10];
}STYPE;
void check();
/**********found**********/
int fun(___【1】___ *std)
{
/**********found**********/
___【2】___ *fp; int i;
if((fp=fopen("myfile5.dat","wb"))==NULL)
return(0);
printf("\nOutput data to file !\n");
for(i=0; i<N; i++)
/**********found**********/
fwrite(&std[i], sizeof(STYPE), 1, ___【3】___);
fclose(fp);
return (1);
}
main()
{
STYPE s[10]={{1,"aaaaa","111111"},
{2,"bbbbb","222222"},
{3,"ccccc","333333"},
{4,"ddddd","444444"},
{5,"eeeee","555555"}};
int k;
k=fun(s);
if (k==1)
{
printf("Succeed!"); check();
}
else
printf("Fail!");
}
void check()
{
FILE *fp; int i;
STYPE s[10];
if((fp=fopen("myfile5.dat","rb"))==NULL)
{printf("Fail !!\n"); exit(0);}
printf("\nRead file and output to screen :\n");
printf("\n num name tel\n");
for(i=0; i<N; i++)
{ fread(&s[i],sizeof(STYPE), 1, fp);
printf("%6d %s %s\n", s[i].num, s[i].name,
s[i].tel);
}
fclose(fp);
}
有代码的地方就有玄学