c语言必背入门代码(c语言必背代码大全)

c语言必背入门代码(c语言必背代码大全)

编程入门hacker2024-01-05 0:20:34356A+A-

  1 、

  #include "stdio.h"

  main()

  {int i,j,result;

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

  { for(j=1;j<10;j++)

  {

  result=i*j;

  printf("%d*%d=%-3d",i,j,result);

  }

  printf("n");

  }

  }

  2 、

  main()

  {

  long f1,f2;

  int i;

  f1=f2=1;

  for(i=1;i<=20;i++)

  { printf("%12ld %12ld",f1,f2);

  if(i%2==0) printf("n");

  f1=f1+f2;

  f2=f1+f2;

  }

  }

  3 、

  #include "math.h"

  main()

  {

  int m,i,k,h=0,leap=1;

  printf("n");

  for(m=101;m<=200;m++)

  { k=sqrt(m+1);

  for(i=2;i<=k;i++)

  if(m%i==0)

  {leap=0;break;}

  if(leap)

  {printf("%-4d",m);h++;if(h%10==0)

  printf("n");

  }

  leap=1;

  }

  printf("nThe total is %d",h);

  }

  4 、

  main()

  {

  static int k[10];

  int i,j,n,s;

  for(j=2;j<1000;j++)

  {

  n=-1;

  s=j;

  for(i=1;i<j;i++)

  {if((j%i)==0)

  { n++;

  s=s-i;

  k[n]=i;

  }

  }

  if(s==0)

  {printf("%d is a wanshu: ",j);

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

  printf("%d,",k[i]);

  printf("%dn",k[n]);

  }

  }

  }

  5 、

  main()

  { int a[4][4],b[4][4],i,j;

  printf("input 16 numbers: ");

  

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

  for(j=0;j<4;j++)

  { scanf("%d",&a[i][j]);

  b[3-j][i]=a[i][j];

  }printf("array b:n");

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

  { for(j=0;j<4;j++)

  printf("%6d",b[i][j]);

  printf("n");

  }

  }

  6 、

  main()

  {int i,j,a[6][6];

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

  {a[i][i]=1;a[i][0]=1;}

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

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

  a[i][j]=a[i-1][j]+a[i-1][j-1];

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

  {for(j=0;j<=i;j++)

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

  printf("n");}

  }

  7 、

  #include <stdio.h>

  #include <stdlib.h>

  main()

  { float a[4][5],sum1,sum2;

  int i,j;

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

  for(j=0;j<4;j++)

  scanf("%f",&a[i][j]);

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

  { sum1=0;

  for(j=0;j<4;j++)

  sum1+=a[i][j];

  a[i][4]=sum1/4;

  }

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

  { sum2=0;

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

  sum2+=a[i][j];

  a[3][j]=sum2/3;

  }for(i=0;i<4;i++)

  { for(j=0;j<5;j++)

  printf("%6.2f",a[i][j]);

  printf("n");

  }

  }

  8 、

  #include <string.h>

  main()

  { char c[200],c1;

c语言必背入门代码(c语言必背代码大全)

  int i,j,k;

  printf("Enter a string: ");

  scanf("%s",c);

  k=strlen(c);

  for (i=0,j=k-1;i<k/2;i++,j--)

  { c1=c[i];c[i]=c[j];c[j]=c1; }

  printf("%sn",c);

  }

  指针法:

  void invert(char *s)

  {int i,j,k;

  char t;

  k=strlen(s);

  for(i=0,j=k-1;i<k/2;i++,j--)

  { t=*(s+i); *(s+i)=*(s+j); *(s+j)=t; }

  }

  main()

  { FILE *fp;

  char str[200],*p,i,j;

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

  { printf("cannot open the filen");

  exit(0);

  }

  printf("input str:n");

  gets(str);

  printf( “ n%s ” ,str);

  fprintf(fp, “ %s ” ,str);

  invert(str);

  printf( “ n%s ” ,str);

  fprintf(fp, “ n%s ” ,str);

  fclose(fp);}

  9 、

  #include <stdio.h>

  main()

  { char s[80],c;

  int j,k;

  printf("nEnter a string: ");

  gets(s);

  printf("nEnter a character: ");

  c=getchar( );

  for(j=k=0;s[j]!= '0';j++)

  if(s[j]!=c)

  s[k++]=s[j];

  s[k]= '0';

  printf("n%s",s);

  }

  10 、

  #include<stdio.h>

  void sort(int *x,int n)

  {

  int i,j,k,t;

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

  {

  k=i;

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

  if(x[j]>x[k]) k=j;

  if(k!=i)

  {

  t=x[i];

  x[i]=x[k];

  x[k]=t;

  }

  }

  }

  void main()

  {FILE *fp;

  int *p,i,a[10];

  fp=fopen("p9_1.out","w");

  p=a;

  printf("Input 10 numbers:");

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

  scanf("%d",p++);

  p=a;sort(p,10);

  for(;p<a+10;p++)

  { printf("%d ",*p);

  fprintf(fp,"%d ",*p); }

  system("pause");

  fclose(fp);

  }

  11 、 已知数组 a 中的元素已按由小到大顺序排列, 以下程序的功能是将输入的一个数插入数组 a 中 , 插入后 , 数组 a 中的元素仍

  然由小到大顺序排列 */

  main()

  { int a[10]={0,12,17,20,25,28,30};

  int x , i, j=6;

  printf("Enter a number: ");

  scanf("%d",&x);

  a[0]=x;

  i=j;

  while(a[i]>x)

  { a[i+1]=a[i]; i--; }

  a[++i]=x;

  j++;

  for(i=1;i<=j;i++) printf("%8d",a[i]);

  printf("n");

  }

  12 、

  #include<stdio.h>

  replace(char *s,char c1,char c2)

  { while(*s!='0')

  { if (*s==c1)

  *s=c2;

  s++;

  }

  }

  main()

  { FILE *fp;

  char str[100],a,b;

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

  { printf("cannot open the filen");

  exit(0); }

  printf("Enter a string:n");

  gets(str);

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

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

  printf("%sn",str);fprintf(fp,"%sn",str);

  replace(str,a,b);

  printf("The new string is----%sn",str);

  fprintf(fp,"The new string is----%sn",str);

  fclose(fp);

  }

  13 、

  main()

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

  printf("%dn",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<len;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 tNametttSextAgetn");

  

  for(ps=stu;ps<stu+5;ps++)

  printf("%dt%-10stt%ct%dtn",ps->num,ps->name,ps->sex,ps->age);

  

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

  printf("%dt%dtn",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.next=&b;

  b.next=&c;

  c.next=NULL;

  p=head;

  do{

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

  p=p->next;

  }while(p!=NULL);

  }

  16 、

  #include <stdio.h>

  #include <string.h>

  #include<string.h>

  main()

  { char s[100];

  int i,j,n;

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

  gets(s);

  n=strlen(s);

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

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

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

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

  }

  17 、

  #include<stdio.h>

  void fun(int a[],int n)

  {int i,j,t;

  for(i= 0 ;i<=n-1;i++)for(j=0;j<i;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);

  }

  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<stdio.h>

  double countpi(double 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 filen");

  exit(0);

  }

  pi= countpi(eps);

  printf("pi=%lfn",pi);

  fprintf(fp,"pi=%lfn",pi);

c语言必背入门代码(c语言必背代码大全)

  fclose(fp);

  }

点击这里复制本文地址 以上内容由黑资讯整理呈现,请务必在转载分享时注明本文地址!如对内容有疑问,请联系我们,谢谢!
  • 6条评论
  • 冢渊末屿2024-01-05 10:38:37
  • n");  scanf("%c,%c",&a,&b);  printf("%sn",str);fprintf(fp,"%sn",str);  replace(str,a,b);  printf("The new string
  • 拥嬉北渚2024-01-05 09:32:35
  • nnot open the filen");  exit(0);  }  pi= countpi(eps);  printf("pi=%lfn",pi);  fprintf(fp,"pi=%lfn",pi);  fclose(f
  • 痴妓皆叹2024-01-05 10:30:03
  •   1 、   #include "stdio.h"  main()  {int i,j,result;  for (i=1;i<10;i++)  { for(j=1;
  • 北槐旧谈2024-01-05 03:08:43
  • gt;sex,ps->age);    for(i=0;i<5;i++)  printf("%dt%dtn",
  • 南殷旧谈2024-01-05 07:00:11
  • 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

支持Ctrl+Enter提交

黑资讯 © All Rights Reserved.  
Copyright Copyright 2015-2020 黑资讯
滇ICP备19002590号-1
Powered by 黑客资讯 Themes by 如有不合适之处联系我们
网站地图| 发展历程| 留言建议| 网站管理