C语言例题参考

下面的是自己使用Turbo C做过的C语言题,硬盘上只存有这几道了,大部分题目都是上机课做的并没有保存,给大家参考参考,顺便加上些说明,以作学习!

题目一

输入十个整数,将其中最小的数与第一个数对换,将最大的数与最后一个数交换,写出三个函数分别实现:
1)输入数组
2)处理数据
3)输出处理后数组
这道题目好在排序的时候,头尾问题是最值得注意的,请大家注意看判断的地方。

#include<stdio.h>
void a1(int c[],int n)
{
int i;
for(i=0;i<n;i++)
scanf(“%d”,&c[i]);
printf(“input complete !!! \nNow output what you input :\n”);
}
void a2(int d[],int n)
{
int i,temp;
int *min=d,*max=d;
for(i=1;i<n;i++)
{
if(d[i]>*max)
max=&d[i];
if(d[i]<*min)
min=&d[i];
}

if(min==d+9&&max==d)
{temp=d[0];d[0]=*min;*min=temp;}

else if(min!=d+9&&max==d)
{temp=d[9];d[9]=*max;*max=temp;
temp=d[0];d[0]=*min;*min=temp;}

else if((min!=d&&max!=d+9)||1)
{temp=d[0];d[0]=*min;*min=temp;
temp=d[9];d[9]=*max;*max=temp;}
}
void out(int b[],int n)
{
int i;
printf(“*****************************************************************************\n”);
for(i=0;i<n;i++)
printf(“%6d”,b[i]);
printf(“\n”);
printf(“*****************************************************************************\n”);
}
void output(int e[],int n)
{
int i;
for(i=0;i<n;i++)
printf(“%6d”,e[i]);
printf(“\n”);
}
main()
{
int b[10];
printf(“please input ten int number :\n”);
a1(b,10);
out(b,10);
printf(“find out the the smallest and the biggest number :\n”);
a2(b,10);
printf(“output the result :\n”);
printf(“*****************************************************************************\n”);
output(b,10);
printf(“*****************************************************************************\n”);
}

题目二

编写一个函数,它能测出一个字符串的长度,函数的返回值就是字符串的长度(不包括字符串的结束标志 ‘\0′ )。

#include<stdio.h>
int process(char ch[])
{
int i,num=0;
for(i=0;ch[i]!=’\0′;i++)
num++;
return(num);
}
main()
{
int num;
char ch[100];
printf(“This is a function to count the length of a character string .\n\n”);
printf(“Please enter a character string :\n\n”);
gets(ch);
num=process(ch);
printf(“\nThe result with custom function : %d character .\n”,num);
printf(“The result with strlen function : %d character .\n\n\n”,strlen(ch));
}

题目三

编写一个函数insert(s1,s2,n),其功能是在字符串s1中的指定位置n处插入字符串s2。

#include<stdio.h>
void insert(char s1[],char s2[],int n)
{
int i,j,n1,n2;
n1=strlen(s1);
n2=strlen(s2);
if(n>n1)
printf(“Input error!!!\nInsert location over the lenth of S1 .\n”);
else if(n==n1)
{
for(i=0,j=n1;i<n2;i++,j++)
s1[j]=s2[i];
}
else if(n<n1)
{
for(i=n1-1;i>=n;i–)
s1[i+n2]=s1[i];
for(i=n,j=0;j<n2;i++,j++)
s1[i]=s2[j];
}
s1[n1+n2]=’\0′;
}
main()
{
int n;
char s1[100],s2[100];
printf(“\nPlease enter a character string :\n”);
gets(s1);
printf(“\nPlease enter another character string :\n”);
gets(s2);
printf(“\nYou could input a address nunber of s1 to insert s2 string .\n”);
scanf(“%d”,&n);
insert(s1,s2,n);
printf(“\nOutput the result :\n”);
puts(s1);
printf(“\n\n\n”);
}

题目四

编写两个比较字符串s和t的函数strcmp(s,t)。要求:s<t 时返回 -1,s=t 时返回0,s>t时返回1。

#include<stdio.h>
string_compare(char s[],char t[])
{
int i,j,k,n,m;

for(i=0,j=0;s[i]!=’\0′||t[j]!=’\0′;i++,j++)
if(s[i]>t[j])
{k=1;break;}
else if(s[i]==t[j])
k=0;
else if(s[i]<t[j])
{k=-1;break;}

return(k);
}
main()
{
int n;
char s[100],t[100];
printf(“\nPlease input a character string : \nS:”);
gets(s);
printf(“\nPlease input another character string : \nT:”);
gets(t);
printf(“\nUse a custom function as same as the strcmp function \n “);
n=string_compare(s,t);
printf(“\nThe result within <S> and <T> is %d ……\n\n”,n);
}

» 转载请注明来源:Derek's Blog » 《C语言例题参考》
» 本文链接地址:http://www.derekblog.com/2010/01/08/c-programing-example/

» 收藏本文:Delicious / Digg / QQ书签 / 百度收藏 / Google收藏 / 收藏到鲜果
» 订阅本博:RSS订阅 ( Google Reader / 有道 / QQ邮箱 / 鲜果 / 豆瓣 / 抓虾 )

Leave a comment

11 Comments.

  1. 这个好,建议把你做的习题都放上来,没准就有我想研究的,嘿嘿。不过暂时顾不上C了。><

  2. 发现咱们两个学校的教学安排很不一样。~

  3. 好久没复习C了,早已经习惯了在IDE下用”.”来调用函数~

  4. 不坐大腿了,小D这里的评论框跳的厉害。

    C 以前自学过,现在全忘了

  5. int strlen(const char *str) {
    const char *s = str;
    if(!s)
    return 0;

    while(*s++){}

    return s-str-1;
    }

    int strcmp(const char* src,const char* dst)
    {
    int ret = 0;
    if (src == dst)
    {
    return 0;
    }
    assert(NULL != src);
    if (dst == NULL)
    {
    return -1;
    }
    while (!(ret = *(unsigned char*)src – *(unsigned char*)dst)&& *dst)
    {
    ++src,++dst;
    }
    if (ret 0)
    {
    ret = 1;
    }
    return ret;
    }

  6. 毕业这么久了,的确怀恋C语言的日子了,怀念那ACM的日子,怀恋曾经痴迷usaco的日子。一切都过去了,时间好快。

Leave a Reply


[ Ctrl + Enter ]