1 程序改错#
1.1 下面程序段的功能是交换两个字符数组的内容(每个字符串字符数均不超过100) (8分) 【 见2012年笔试题1.1】#
1 2 3 4 5 6 | void StrSwap( char *pa, char *pb)
{ char *temp;
temp = pa;
pa = pb;
pb = temp;
}
|
分析:该程序中pa,pb两个字符指针变量只是形式参数,刚开始pa指向数组pa[100]的的首元素,指向pb[100]首元素;交换后pa指向数组pb[100]的的首元素,指向pa[100]首元素;而且该程序段运行结束后pa,pb两个变量由内存回收,因此最后两个字符数组的内容没有改变。
改正:
方法一:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | void copy( char *pa, char *pb)
{
while (*pb!=0)
{
*pa=*pb;
pa++;
pb++;
}
*pa=0;
}
void StrSwap( char *pa, char *pb)
{
char temp[100];
copy(temp,pa);
copy(pa,pb);
copy(pb,temp);
}
|
方法二:#
1 2 3 4 5 6 7 8 9 10 11 12 | void swap ( char *pa , char *pb )
{
if ( strlen (pa)!= strlen (pb)) exit (-1);
char tmp;
int i=-1;
while (pa[++i])
{
tmp=pa[i];
pa[i]=pb[i];
pb[i]=tmp;
}
}
|
方法三:#
1 2 3 4 5 6 7 8 | void swap ( char *pa , char *pb )
{
if ( strlen (pa)!= strlen (pb)) exit (-1);
char tmp[100];
strcpy (tmp,pa);
strcpy (pa,pb);
strcpy (pb,tmp);
}
|
1.2 程序段如下 【 见2012年笔试题1.2】#
1 2 3 4 5 6 | char a[] = “House”;
char *b = “House”;
b[2] = ‘r’;
a[2] = ‘r’;
b = a;
b[2] = ‘r’;
|
分析:
b[2] = ‘r’; //不能对字符常量赋值
a = b; a是一个地址常量,不能把地址赋给一个常量
2 简答题#
2.1下面程序中,arr[]、numb、item分别是整型数组、数组元素个数、某一整数,程序功能是遍历数组arr,查找与item相等的元素,并输出该元素的下标。但是此程序不严谨,请问它可能导致什么样的异常结果?为什么?(5分) 【 见2012年笔试题2.1】#
1 2 | for (numb = 0;arr[numb] != item;numb --);
printf (“%d”,numb);
|
问题:只能遍历首元素,之后数组下标会越界,而且最多只能找到一个。
修改:
1 2 3 | for ( ; numb>0 ; numb--)
if ( arr[numb-1] == item)
printf ( "%d " ,numb-1);
|
2.2 下面程序段中各个常量、变量分别存储在内存中的什么位置、各按什么样的顺序存储、各占多少个字节?(提示:整型变量占2个字节,字符占1个字节,指针占4个字节)(10分)#
【见2015年笔试题】
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int k;
void main()
{
char *p = “hello”;
char q[]= ”hello”;
char ch;
int k;
func(k);
}
void func( int m)
{
int n;
……
}
|

2.3在调用函数时,如果形参和实参分别是下列情况,则相应的调用方式是什么?(5分) 【见2012年笔试题2.3】#
(1) 实参和形参都是数组元素 //传值调用(值传递)
(2) 形参是指针 //传地址调用(地址传递)
(3) 实参和实参都是数组 //传地址调用(地址传递)
3 编程题(共60分)#
3.1 编写一个函数,使之能完成以下功能:把一个字符串逆序排列。(10分)#
方法一:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | #include "stdio.h"
#include "string.h"
void reverse( char str[])
{
char ch;
int i,l= strlen (str);
for (i=0; i<l/2; i++)
{
ch=str[i];
str[i]=str[l-i-1];
str[l-i-1]=ch;
}
}
int main()
{
char ss[]= "abcdefg" ;
reverse(ss);
printf ( "%s" ,ss);
return 0;
}
|
方法二:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | # include <stdio.h>
# include <string.h>
int change( char *str)
{
int i,len=0;
char c;
for ( ; str[len]!= '\0' ; len++);
for (i=0; i<len/2; i++)
{
c=str[i];
str[i]=str[len-i-1];
str[len-i-1]=c;
}
return len;
}
void main()
{
int i,len;
char str[20];
printf ( "请输入一个字符串:" );
gets (str);
len=change(str);
printf ( "将字符串中的字符互换输出为:" );
for (i=0; i<len; i++)
{
printf ( "%c" ,str[i]);
}
printf ( "\n" );
}
|
3.2 编写一个函数,使之能完成以下功能:利用递归方法找出一个数组中的最大值和最小值,要求递归调用函数的格式如下:MinMaxValue(arr,n,&max,&min),其中arr是给定的数组,n是数组的个数,max、min分别是最大值和最小值。(15分) 【见2015年笔试题【保】】 #
方法一:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include "stdio.h"
#define N 10
void MinMaxValue( int arr[], int n, int *max, int *min)
{
if (n>=0)
{
if (*max<arr[n])
*max=arr[n];
if (*min>arr[n])
*min=arr[n];
MinMaxValue(arr,n-1,max,min);
}
}
int main()
{
int max=-32768,min=32767;
int a[]= {1,54,23,65,87,12,54,87,98,233};
MinMaxValue(a,N-1,&max,&min);
printf ( "Max=%d,Min=%d\n" ,max,min);
return 0;
}
|

方法二:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include "stdio.h"
#define N 10
void MinMaxValue( int arr[], int n, int *max, int *min)
{
if (n>0)
{
if (*max<arr[n])
*max=arr[n];
if (*min>arr[n])
*min=arr[n];
MinMaxValue(arr,n-1,max,min);
}
}
int main()
{
int a[]= {1,-8,23,65,87,12,54,887,98,233};
int max=a[0],min=a[0];
MinMaxValue(a,N-1,&max,&min);
printf ( "Max=%d,Min=%d\n" ,max,min);
return 0;
}
|
方法三:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #include<stdio.h>
void MinMaxValue( int arr[], int n, int *max, int *min)
{
if (n==0) return ;
if (arr[n-1]>*max)
*max=arr[n-1];
if (arr[n-1]<*min)
*min=arr[n-1];
MinMaxValue(arr,n-1,max,min);
}
void main()
{
int arr[]= {1,3,4,2,56,5};
int max=arr[0],min=arr[0];
MinMaxValue(arr, sizeof (arr)/ sizeof ( int ),&max,&min);
printf ( "%d-%d\n" ,max,min);
}
|
3.3 【文件】编写一个函数,使之能完成以下功能:把file1.doc的内容全部复制到file2.doc中,file1.doc中全部是字符(含空格),要求复制时,在file2.doc中的每一行都要加上行号,例如:行号*(其中“*”表示具体的数字)。最后该函数返回file1.doc中的字符个数(不包括空格)。(10分)#
方法一:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | #include <stdio.h>
#include <stdlib.h>
int exam()
{
char ch;
int count=0,row=0;
FILE *fp1,*fp2;
fp1= fopen ( "D:\file1.doc" , "r+" );
while (! feof (fp1))
{
ch= getc (fp1);
fp2= fopen ( "D:\file2.doc " , "a+" );
if (ch!= ' ' )
count++;
if (ch== '\n' )
{
row++;
fprintf (fp2, "行号:%d" ,row);
fprintf (fp2, "\n" );
}
else
{
putc (ch,fp2);
}
fclose (fp2);
}
fclose (fp1);
return count;
}
int main()
{
int sum=exam();
printf ( "Sum=%d\n" ,sum);
return 0;
}
|

方法二: #
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | # include <stdio.h>
# include <stdlib.h>
void main()
{
FILE *fp1,*fp2;
int count=0, row=0;
char c;
fp1= fopen ( "D:/file1.txt" , "r" );
fp2= fopen ( "D:/file2.txt" , "w" );
if (fp1== NULL)
{
printf ( "文件不能打开!\n" );
exit (0);
}
if (fp2== NULL)
{
printf ( "文件不能打开!\n" );
exit (0);
}
while ( (c= fgetc (fp1)) != EOF )
{
if (c!= ' ' )
count++;
if (c== '\n' )
{
row++;
fprintf (fp2, "行号:%d" ,row);
}
fputc (c,fp2);
}
fclose (fp1);
fclose (fp2);
printf ( "count=%d,row=%d\n" ,count,row);
}
|

方法三:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | #include<stdio.h>
void main()
{
FILE *fin,*fout;
int count=0,line=0;
if (!(fin= fopen ( "D:/file1.txt" , "r" )))
printf ( "error" );
if (!(fout= fopen ( "D:/file2.txt" , "w" )))
printf ( "error" );
fprintf (fout, "行号%d " ,line++);
while (! feof (fin))
{
char ch= fgetc (fin);
putchar (ch);
if ((ch!= ' ' )&& (ch!= '\n' ))
count++;
fputc (ch,fout);
if (ch== '\n' )
fprintf (fout, "行号%d " ,line++);
}
fprintf (fout, "总个数%d " ,count--);
printf ( "总个数%d " ,count--);
fclose (fin);
fclose (fout);
}
|
3.4 编写一个完整的程序,使之能完成以下功能:从键盘中输入若干个整数,用链表储存这些输入的数,并要求存储的顺序与输入的顺序相反。(10分) 【见2016年笔试题4.4】#
方法一:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | # include <stdio.h>
# include <malloc.h>
struct node
{
int val;
struct node *next;
};
struct node* createList()
{
struct node *head,*p;
int i,len,val;
head = ( struct node*) malloc ( sizeof ( struct node) );
if (head==NULL)
{
printf ( "空间申请失败!\n" );
return NULL;
}
head->val=NULL;
head->next=NULL;
printf ( "请输入结点数目:\n" );
scanf ( "%d" ,&len);
printf ( "\n\n" );
for (i=0; i<len; i++)
{
p = ( struct node*) malloc ( sizeof ( struct node) );
if (p==NULL)
{
printf ( "空间申请失败!\n" );
return NULL;
}
printf ( "请输入第%d个结点的值:\n" , i+1);
scanf ( "%d" ,&val);
p->val=val;
p->next = head->next;
head->next=p;
}
return head;
}
void traverse( struct node *head)
{
struct node *p=head->next;
if (p == NULL)
{
printf ( "链表为空!\n" );
return ;
}
while (p!=NULL)
{
printf ( "%d " ,p->val);
p=p->next;
}
printf ( "\n" );
}
void main()
{
struct node *head;
head = createList();
printf ( "\n链表创建完成并遍历输出:\n\n" );
traverse(head);
}
|

方法二:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #include "stdio.h"
#include "stdlib.h"
struct sList
{
int data;
struct sList *next;
};
struct sList *head=NULL;
struct sList *creat()
{
int n;
struct sList *p,*q;
head=( struct sList *) malloc ( sizeof ( struct sList));
head->next=NULL;
p=head;
while (1)
{
printf ( "(0结束)n=" );
scanf ( "%d" ,&n);
if (n<=0) break ;
else
{
q=( struct sList *) malloc ( sizeof ( struct sList));
q->data=n;
p->next=q;
q->next=NULL;
p=q;
}
}
return head;
}
void ShowList( struct sList *head)
{
struct sList *p=head->next;
while (p!=NULL)
{
printf ( "%d\t" ,p->data);
p=p->next;
}
}
int main()
{
struct sList *p,*newhead,*p1;
ShowList(creat());
printf ( "\n" );
p=head->next;
newhead=head;
newhead->next=NULL;
while (p!=NULL)
{
p1=p;
p=p->next;
p1->next=newhead->next;
newhead->next=p1;
}
head=newhead;
printf ( "\n" );
ShowList(head);
return 0;
}
|

方法三:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | #include<stdio.h>
#include<malloc.h>
struct Node
{
struct Node*next;
int data;
};
void main()
{
int i=0,n;
struct Node *head,*p;
printf ( "格式:第一行数据的个数,第二行,输入数据空格隔开\n" );
scanf ( "%d" ,&n);
head= malloc ( sizeof ( struct Node));
head->next=NULL;
for (; i<n; i++)
{
int temp;
scanf ( "%d" ,&temp);
p= malloc ( sizeof ( struct Node));
p->data=temp;
p->next=head->next;
head->next=p;
}
p=head->next;
while (p)
{
printf ( "%d\n" ,p->data);
p=p->next;
}
}
|

3.5 【文件+堆栈】编写一个完整的程序,使之能完成以下功能:一段名为file.c的程序,该程序中含有括号,现要检查程序中的括号是否配对,提示:利用堆栈实现。(15分) 【见2015年笔试题【保】6】#
代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #include <stdio.h>
#define Stack char
#define Max 999
int judge( char p[])
{
int len= strlen (p);
int top=-1,i;
Stack s[Max];
for (i=0; i<len; i++)
{
switch (p[i])
{
case '(' :
case '[' :
case '{' :
s[++top]=p[i];
break ;
case ')' :
if (s[top]== '(' )
top--;
else return 0;
break ;
case ']' :
if (s[top]== '[' )
top--;
else return 0;
break ;
case '}' :
if (s[top]== '{' )
top--;
else return 0;
break ;
}
}
if (top==-1)
return 1;
else
return 0;
}
void main()
{
char p[Max];
FILE * fin;
if (!(fin= fopen ( "D:/file1.txt" , "r" )))
printf ( "failed" );
while (! feof (fin))
{
fscanf (fin, "%s" ,p);
}
printf ( "%d\n" ,judge(p));
fclose (fin);
}<br>
|
方法一:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | #include <stdio.h>
#include <math.h>
int a[100];
int isPrime( int n)
{
int i;
for (i=2; i<= sqrt (n); i++)
if (n%i==0)
return 0;
return 1;
}
int main()
{
int i,j,in;
while ( scanf ( "%d" ,&in)&&in>-1)
{
if (in==0||in==1)
printf ( "没有质因数" );
else
{
j=0;
printf ( "%d=" ,in);
while (in>3)
{
for (i=2; i<=in; i++)
{
if (isPrime(i)&&in%i==0)
break ;
}
in=in/i;
a[j++]=i;
}
if (in>1)
a[j++]=in;
for (i=0; i<j; i++)
{
printf ( "%d" ,a[i]);
if (i<j-1)
printf ( "*" );
}
printf ( "\n" );
}
}
return 0;
}
|

方法二:#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # include <stdio.h>
void main()
{
int i,j,k,val;
printf ( "请输入一个正整数:" );
scanf ( "%d" ,&val);
printf ( "将该正整数分解质因数输出为: " );
printf ( "%d=" ,val);
for (i=2 ; i<=val; i++)
{
while (val!=i)
{
if (val%i == 0)
{
printf ( "%d*" ,i);
val=val/i;
}
else
break ;
}
}
printf ( "%d" ,val);
printf ( "\n" );
}
|

3.7 
方法一:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | # include <stdio.h>
void main()
{
float sum=1, sum1=1, sum2=1, sum3, x;
int i, n, flag=-1;
printf ( "请输入x的值:\n" );
scanf ( "%f" , &x);
printf ( "请输入n的值:\n" );
scanf ( "%d" , &n);
for (i=1; i<=n; i++)
{
sum1 = sum1*x;
sum2 = sum2*i;
sum3 = sum1/sum2 * flag;
flag = -flag;
sum = sum + sum3;
}
printf ( "Sum=%f\n" , sum );
}
|

方法二:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | #include "stdio.h"
#include "math.h"
int nJie( int a);
int main()
{
int i,n,sign;
float ex=1,temp,x,fenmu,fenzi;
printf ( "n=" );
scanf ( "%d" ,&n);
printf ( "x=" );
scanf ( "%f" ,&x);
for (i=1; i<=n; i++)
{
fenzi= pow (x,i);
fenmu=nJie(i);
temp=fenzi/fenmu;
if (i%2==0)
sign=1;
else
sign=-1;
ex+=sign*temp;
}
printf ( "ex=%6.2f" ,ex);
return 0;
}
int nJie( int a)
{
int i,ex=1;
for (i=1; i<=a; i++)
ex*=i;
return ex;
}
|

作者:Hang Shao
出处:https://www.cnblogs.com/pam-sh/p/12609679.html
版权:本作品采用「知识共享」许可协议进行许可。
声明:欢迎交流! 原文链接 ,如有问题,可邮件(mir_soh@163.com)咨询.
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· .NET10 - 预览版1新功能体验(一)