C语言编程入门经典18个小程序(2)

电脑杂谈  发布时间:2020-02-15 11:01:29  来源:网络整理

{ printf("cannot open the file\n");

exit(0); }

printf("Enter a string:\n");

gets(str);

printf("Enter a&&b:\n");

scanf("%c,%c",&a,&b);

printf("%s\n",str);fprintf(fp,"%s\n",str);

replace(str,a,b);

printf("The new string is----%s\n",str);

fprintf(fp,"The new string is----%s\n",str);

fclose(fp);

}

13 、 /* 在一个字串 s1 中查找一子串 s2c 语言入门经典,若存在则返回子串在主串中的起始位置

,不存在则返回 -1 。 */

main()

{char s1[6]="thisis";char s2[5]="is";

printf("%d\n",search(s1,s2));

system("pause");

}

int search(char s1[],char s2[])

{int i=0,j,len=strlen(s2);

while(s1[i]){

for(j=0;j

if(s1[i+j]!=s2[j]) break;

if(j>=len)return i;

else i++;

}

return -1;

}

14 、 /* 用指针变量输出结构体变量元素。 */

struct student

{

int num;

char *name;

char sex;

int age;

}stu[5]={{1001,"lihua",'F',18},{1002,"liuxing",'M',19},{1003,"huangke",'F',19},{1004,"fengshou",'F',19},{1005,"Wangming",'M',

18}};

main()

{int i;

struct student *ps;

printf("Num \tName\t\t\tSex\tAge\t\n");

/* 用指针变量输出结构体变量元素。 */

for(ps=stu;ps

printf("%d\t%-10s\t\t%c\t%d\t\n",ps->num,ps->name,ps->sex,ps->age);

/* 用函数下标法输出结构体数组元素学号和年龄。 */

for(i=0;i<5;i++)

printf("%d\t%d\t\n",stu[i].num,stu[i].age);

}

15 、 /* 建立一个有三个结点的简单数组: */#define NULL 0

struct student

{

int num;

char *name;

int age ;

struct student *next;

};

void main()

{

struct student a,b,c,*head,*p;

a.num=1001; a.name="lihua"; a.age=18; /* 对节点成员进行赋值 */

b.num=1002; b.name="liuxing"; b.age=19;

c.num=1003; c.name="huangke"; c.age=18;

head=&a; /* 建立链表, a 为头结点 */

a.next=&b;

b.next=&c;

c.next=NULL;

p=head; /* 输出链表 */

do{

printf("%5d,%s,%3d\n",p->num,p->name,p->age);

p=p->next;

}while(p!=NULL);

}

16 、 /* 输入一个字符串,判断其能否为回文。回文字符串是指从左至右读跟从右到左读完全相同的字符串。 */

#include

c语言入门经典程序_c语言入门经典 怎么样_c 语言入门经典

#include

#include

main()

{ char s[100];

int i,j,n;

printf(" 输入字符串: \n");

gets(s);

n=strlen(s);

for(i=0,j=n-1;i

if(s[i]!=s[j]) break;

if(i>=j) printf(" 是回文串 \n");

else printf(" 不是回文串 \n");

}

17 、 /* 冒泡排序,从小到大,排序后结果输出至屏幕及文件 myf2 .out*/

#include

void fun(int a[],int n)

{int i,j,t;

for(i= 0 ;i<=n-1;i++)for(j=0;j

if(a[j]>a[j+1]) {t=a[j];a[j]=a[j+1];a[j+1]=t;}

}

main()

{int a[10]={12,45,7,8,96,4,10,48,2,46},n=10,i;

FILE *f;

if((f=fopen("myf2 .out ","w"))==NULL)

printf("open file myf2.out failed!\n");

fun(a,10);

for(i=0;i<10;i++)

{printf("%4d",a[i]);

fprintf(f,"%4d",a[i]);

}

fclose(f);

}

小编推荐一个学C语言/C++的学习裙【 二六三,六八八,二七六 】c 语言入门经典,无论你是大牛还是小白,是想转行还是想入行都可以来认识一起进步一起学习!裙内有研发工具,很多干货和技术资料分享!

18 、编写函数 countpi ,利用公式

+ × × × + × × + × + + ≈

9

4

7

3

5

2

3

1

7

3

5

2

3

1

5

2

3

1

3

1

1

2

π

计算 π 的近似值 , 当某一项的值大于 10 -5

时 , 认为超过效率规定 , 请加强函数 。 将结果显示在屏幕上并输出至文件 p 7 _ 3 . out 中 。

#include

double countpi(double eps) /*eps 为允许误差 */

{

int m=1;

double temp=1.0,s=0;

while(temp>=eps)

{ s+=temp;

temp=temp*m/(2*m+1);

m++;

}

return(2*s);

}

main()

{FILE *fp;

double eps=1e-5,pi;

if((fp=fopen("p7_3.out","w"))==NULL)

{ printf("cannot open the file\n");

exit(0);

}

pi= countpi(eps);

printf("pi=%lf\n",pi);

fprintf(fp,"pi=%lf\n",pi);

fclose(fp);

}


本文来自电脑杂谈,转载请注明本文网址:
https://www.pc-fly.com/a/jisuanjixue/article-140846-2.html

相关阅读
    发表评论  请自觉遵守互联网相关的政策法规,严禁发布、暴力、反动的言论

    热点图片
    拼命载入中...