C语言基础整理

数据的输入输出
1.不带修饰符:

e.g.
#include"stdio.h"
main()
{
        int a=5,b,c;
 float x=67.8564,y;
 char d='a',e;
        printf("%d",a);
        scanf("%d",&b);
        printf("%f",x);
        scanf("%f",&y);
 printf("%c",d);
        scanf("%c",&e);
}
2.带修饰符:
//设计数据输入输出的格式
比如:输出一个如下的学员的表格,需要在格式上做一些设置
学号     姓名      性别      平时成绩       期末成绩           
1        王明     男         80  85     
e.g.
#include <stdio.h>
main()
{
 int a=5,b=7,c=-1;
 float x=67.8564,y=-789.124;
 char d='a';
 char s[]="computer";
 printf("%3d%-3d%03d",a,b,c);
        printf("%10f,%.10f",x,y);
 printf("%8.2f,%8.2f,%.4f,%5f,%-5f\n",x,y,x,y,x,y);
        printf("%-e,%10.2e\n",x,y);
 printf("%c,%d,%o,%x\n",d,d,d,d);
 printf("%5.3s,%-5.3s\n","computer");

==============================================================
条件语句
1.if语句  判断一个数为偶数(所有整数不是奇数(又称单数),就是偶数(又称双数)。)
#include <stdio.h>
void main()
{
 int num , res ;
 printf("Enter a number :");
 scanf("%d",&num);
 res = num % 2;
 if (res == 0)
  printf("Then number is Even");
}
输入三个数,按大小顺序输出
#include <stdio.h>
void main()
{
 int a,b,c,t;
 scanf("%d%d%d",&a,&b,&c);
 if(a>b)
          {t=a;a=b;b=t;}
        if(a>c)
          {t=a;a=c;c=t;}
 if (b>c)
          {t=b;b=c;c=t;}
  printf("%d,%d,%d",a,b,c);
}
2.if-else 把不是偶数的一类数归为一类
#include <stdio.h>
void main()
{
 int num , res ;
 printf("Enter a number :");
 scanf("%d",&num);
 res = num % 2;
 if (res == 0)
  printf("Then number is Even");
 else
  printf("The number is Odd ");
}
3.if-else-if 闰年判断
判断闰年的条件:year%400==0或year%100!=0&&year%4==0
#include<stdio.h>
main()
{
 int year,leap;
 scanf("%d",&year);
 if(year%4==0)
 {
  if(year%100!=0)
  {
   if(year%400==0)
    leap=1;
   else
    leap=0;
  }
  else
   leap=1;
 }
 else
  leap=0;
 if(leap)
  printf("%d is leapyear\n",year);
 else
  printf("%d is not leapyear\n",year);
}
4.嵌套if语句
#include <stdio.h>
 void main ()
  {
  int x, y;
  x = y = 0;
  clrscr ();
  printf ("Enter Choice (1 - 3) :");
  scanf ("%d", &x);
  if (x == 1)
  {
   printf("\nEnter value for y (1 - 5) :");
   scanf ("%d", &y);
   if (y <= 5)
    printf("\nThe value for y is : %d", y);
   else
    printf("\nThe value of y exceeds 5");
  }
  else
   printf ("\nChoice entered was not 1");
 }

switch语句  根据输入的成绩判断等级
#include <stdio.h>
void main()
{
 float score1,score2,score3,score;
 char  grade;
 printf("input stduent score:");
 scanf("%f%f%f",&score1,&score2,&score3);
 while (score1>100||score1<0)
 {printf("\n输入有误,请重输.");
 scanf("%f%f%f",&score1,&score2,&score3);
 }
 score=(score1+score2+score3)/3;
 switch((int)(score/10))
 {
 case 10:
 case 9:grade='A';break;
    case 8:grade='B';break;
    case 7:grade='C';break;
    case 6:grade='D';break;
    case 5:
    case 4:
    case 3:
    case 2:
    case 1:
    case 0:grade='E';break;
 
 }
 printf("chengjishi%5.1f,相应的等级是%c.\n",score,grade);
}
================================================================
循环语句
//理解循环的概念,为什么要用到循环!
//提高编程效率
1.while //重复的输入"This is iteration"10遍
#include <stdio.h>
main()
{
 int count = 1;
 while( count <= 10)
 {
  printf("\n This is iteration %d\n ",count);
  count++;
 }
   
 printf("\n The loop is completed. \n ");
}
#include"stdio.h"
void main()
{
 int i=0,j;
 //while(i<5)
 {
            printf("请输入第一个数:");
            //printf("请输入第%d个数:",i+1);
    // scanf("%d",&j);
     //i++;
 }

}
2.do-while
#include <stdio.h>
   main ()
   {
 int num1, num2;
             num2 = 0;
 do
 {
      printf("\nEnter a number : ");
      scanf("%d",&num1);
      printf(" No. is %d ",num1);
      num2++;
 } while (num1 ! = 0);
 printf ("\nThe total numbers entered were %d ",--num2);
 }
3.for
#include <stdio.h>   
main()
{
 int count;
 printf("\tThis is a \n");
  for(count = 1; count <=6 ; count++)
   printf("\n\t\t nice");
    printf("\n\t\t world.\n");

//求100内的自然数之和
#include"stdio.h"
void main()
{
 int i,sum=0;
 for(i=1;i<=100;i++)
  sum+=i;
 printf("%d",sum);
}
//逗号运算符
#include <stdio.h>
main()
{
    int i, j , max;
    printf("Please enter the maximum value \n");
    printf("for which a table can be printed: ");
    scanf("%d", &max);   
    for(i = 0 , j = max ; i <=max ; i++, j--)
    printf("\n%d  +  %d  =  %d",i, j, i + j);
}
//生产Fibonacci系列(1,1,2,3,5,8,13,21...)
#include "stdio.h"
#define num 10  //执行的次数
void main()
{ double num1,num2,temp;
 int i;
 num1=num2=1;
 printf("%.f",num1);
 for(i=0;i<num;i++)
 { printf(",%.f",num2);
  temp=num1;
  num1=num2;
  num2=temp+num1;
 }
}

//打印星花图形
#include <stdio.h>
 main()
 {
  int i, j, k;
  i = 0;
  printf("Enter no. of rows:");
  scanf("%d", &i);
  printf("\n");
  for (j = 0; j < i ; j++)
  {
   printf("\n");
   for (k = 0; k <= j; k++)
        printf("*");
  }
 }
//打印钻石图形
#include"stdio.h"
void main()
{
 int i,k,j;
 for(i=0;i<=3;i++)//先打印出上面部分的图形循环上机练习作业的图形2
 {
   for(j=0;j<=2-i;j++)
     printf(" ");
    for(k=0;k<=2*i;k++)
       printf("*");
        printf("\n"); 
  }
 for(i=0;i<=2;i++)//再打印出下部分图形
 {
  for(j=0;j<=i;j++)
   printf(" ");
  for(k=0;k<=4-2*i;k++)
   printf("*");
  printf("\n");
 }
        for(i=0;i<7;i++)//循环上机练习作业的图形1
 {
  for(j=0;j<7-i;j++)
   printf(" ");
  for(k=0;k<=i;k++)
   printf("*");
        printf("\n");
 }
}
continue语句:continue 语句让封闭循环进行下一次迭代
//求不能被9整除的数
#include <stdio.h>
void main ()
 {
  int num;
  for(num = 1; num <=100; num++)
  {
   if(num % 9 == 0) 
    continue;
   printf("%d\t",num);
  }
 }
break语句 :当循环中遇到 break 语句时,循环立即终止,控制权传递给循环之后的语句
#include <stdio.h>
 main ()
 {
  int count1, count2;
  for(count1 = 1, count2 = 0;count1 <=100; count1++)
  {
   printf(" Enter %d count2 : ",count1);
   scanf("%d", &count2);
   if(count2==100) break;
  }

================================================================================
数组
结合前面所学的条件和循环知识,加上数组知识做综合例题.
1.
#include <stdio.h>
void main() {
    int ary[10]; //数组的定义
    int i, total, high;
    for(i=0; i<10; i++) { //赋值

        scanf("%d",&ary[i]);
    }  
    high = ary[0];
    for(i=1; i<10; i++) { 
        if(ary[i] > high)  //比较
    high = ary[i];
    }  
    for(i=0,total=0; i<10; i++)
        total = total + ary[i];
}
//数组的初始化
#include <stdio.h>
void main() { 
    char alpha[26];
    int i, j;
    for(i=65,j=0; i<91; i++,j++) {
        alpha[j] = i;
       printf("The character is %c \n", alpha[j]);
    }
}

//求一维数组倒置
#include<stdio.h>
main()
{
 int i,a[4];
 for(i=0;i<=3;i++)
 scanf("%d",&a[i]);
 for(i=3;i>=0;i--)
  printf("%d",a[i]);
}
//字符统计
#include<stdio.h>
main()
{
 char string[30];
 int i,num=0,word=0;
 char c;
 gets(string);
 for(i=0;(c=string[i])!='\0';i++)
  if(c==' ') word=0;
  else if(word==0)
  {
   word=1;
   num++;
  }
 printf("there are %d words in the line\n",num);
}
//max字符串
#include"stdio.h"
#include"string.h"
main()
{
 char string[30];
 char str[3][30];
 int i;
 for(i=0;i<3;i++)
  gets(str[i]);
 if(strcmp(str[0],str[1])>0)
    strcpy(string,str[0]);
 else
    strcpy(string,str[1]);
 if(strcmp(str[2],string)>0)
           strcpy(string,str[2]);
 printf("\n the largest string:\n%s\n",string);
}
#include<stdio.h>
//冒泡排序:先计算出循环的轮数,将相邻比较,将小的调到前头
void main()
{
 int i,j,temp,a[5]={2,-45,12,76,-1};
 //int a[5];
    //for(i=0;i<5;i++)
 // printf("%d",a[i]);
 // scanf("%d",&a[i]);
    for(j=1;j<=4;j++)//轮数
   for(i=0;i<5-j;i++)//每轮的次数
     if(a[i]>a[i+1])
  {
    temp=a[i];
    a[i]=a[i+1];
    a[i+1]=temp;
  }
 for(i=0;i<5;i++)
  printf("%d\t",a[i]);
}
//针对一维的数字排序
//采用选择排序
#include<stdio.h>
void main()
{
 int x[5]={2,3,1,7,4};
    int i,j,t;
 for(i=0;i<4;i++)
 {
    for(j=i+1;j<5;j++)
              {
   if(x[i]>x[j])
                        {
      t=x[i];
                           x[i]=x[j];
                           x[j]=t;
                 }
              }
        } 
        for(i=0;i<5;i++)
 {
  printf("%d\t",x[i]);
  
 }
}

 

//前矩阵中的max
#include"stdio.h"
main()
{
 int i,j,row=0,colum=0,max;
 int a[3][4]={{0,2,4,-1},{5,1,3,-9},{7,8,9,11}};
 max=a[0][0];
 for(i=0;i<=2;i++)
  for(j=0;j<=3;j++)
   if(a[i][j]>max)
   {
    max=a[i][j];
    row=i;
    colum=j;
   }
 printf("max=%d,row=%d,colum=%d\n",max,row,colum);
}
//杨辉三角
#define N 11
#include <stdio.h>
main()
{
 int i,j,a[10][10];
 for(i=1;i<10;i++)
 {
  a[i][i]=1;
  a[i][1]=1;

 }
 for(i=3;i<10;i++)
   for(j=2;j<=i-1;j++)
        a[i][j]=a[i-1][j-1]+a[i-1][j];
   for(i=1;i<10;i++)
   {
    for(j=1;j<=i;j++)
     printf("%6d",a[i][j]);
    printf("\n");
   }
   printf("\n");
}
5.字符串排序
#include"stdio.h"
#include"string.h"
//采用选择排序
void sort(char str[][20],int n)//降序
{
 char temp[20];
 int i,j;
 for(i=0;i<n-1;i++)
 {
  
  for(j=i+1;j<n;j++)
   if(strcmp(str[i],str[j])<0)
  
  {
   strcpy(temp,str[i]);
   strcpy(str[i],str[j]);
   strcpy(str[j],temp);
  }
 }
}
void main()
{
 char ch[3][20]={"china","tom","jim"};
 int i;
    sort(ch,3);//调用排序函数
 for(i=0;i<3;i++)
 printf("%s\n",ch[i]);
 printf("\n");
}
===========================================================================
指针
//*p++
#include"stdio.h"
main()
{
 int a[5]={0,2,4,65,1};
 int *p;
 p=a+4;
 int i;
 for(i=0;i<5;i++)
   //printf("%d\t",*p++);
 
    printf("%d\t",*p++);
}
//指针与一维数组
#include <stdio.h>
void main()
{
  static int ary[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int i;
  for (i = 0; i < 10; i ++)
  {
    printf("\ni=%d,ary[i]=%d,*(ary+i)=%d",i,ary[i],*(ary + i));
    printf("&ary[i]= %X,ary+i=%X",&ary[i],ary+i);
  }
}
//指针与字符串
#include <stdio.h>
#include <string.h>
void main ()
{
 char a, str[81], *ptr;
 printf("\nEnter a sentence: ");
 gets(str);
 printf("\nEnter character to search for:");
 a = getchar();
 ptr = strchr(str,a);
 /* return pointer to char */
 printf("\nString starts at address: %u",str);
 printf("\nFirst occurrence of the character is at: %u",ptr);
 printf(“\nPosition of first occurrence is: %d”,ptr-str);
}
//字符串的复制
#include<stdio.h>
#include<string.h>
void main()
{
 char a[20],b[20];
 int i,j;
 printf("please enter you word:");
 scanf("%s",a);
 for(i=0;*(a+i)!='\0';i++)
 *(b+i)=*(a+i);
 //*(b+i)='\0';//如果不用这句会出现的问题
 printf("string a is:%s\n",a);
 printf("string b is:%s",b);
}
//用指针结合函数来进行排序
#include "stdio.h"
#include "malloc.h"
#include "string.h"
#include "stdlib.h"
int accept(char * str[])
{
 int i,n;
 
 printf("Please input the number of string: ");
 scanf("%d",&n);   
 
 if(n>5||n<0)
 {
  printf("Error!\n");
  exit(1);
 }
 
 for(i=0;i<n;i++) {
  str[i]=(char*)malloc(20*sizeof(char));//动态分配空间
  printf("\n Please input %d string: ",i+1);
  scanf("%s",str[i]);
 }
 return n;
}
void sort(char *name[],int n)
{
     int i,j;
     char *temp=(char*)malloc(20*sizeof(char));//动态分配空间
     for(j=0;j<n-1;j++)
  {
  for(i=0;i<n-j-1;i++)
   if(strcmp(name[i],name[i+1])>0)
   {
    strcpy(temp,name[i]);
    strcpy(name[i],name[i+1]);
    strcpy(name[i+1],temp);
   }
 }
}
void print(char *name[],int n)
{
 int i;
 printf("The result is:\n");
        for(i=0;i<n;++i)
    printf("%s\n",name[i]);
}
void main()
{
 
 char *str[5];
 int n;
 n=accept(str);
        sort(str,n);
    print(str,n);

}
补充
//指向指针的指针
main()
{
     static char *name[]={"Follow me","BASIC","Great Wall","FORTRAN","Computer design"};
     char **p;
     int i;
     for(i=0;i<5;i++)
       {
         p=name+i;
         printf("%s\n",*p);
       }
}


=========================================================================
函数
//在C语言中规定,凡不加说明,一律按整型处理.
//在函数中进行实型运算,希望返回为整型,可以让系统自动完成.
//尽量做到类型一致

#include "stdio.h"
max(float x,float y)
{
 float z;
 z=x>y?x:y;
 return(z);
}
void main()
{
 float a=3.5,b=4.6;
 int c;
 //float c;
 c=max(a,b);
        //printf("%d",c);
 //printf("%f",c);
}

#include<stdio.h>
//指针变量做为函数参数
//函数的参数不仅可以是整型\实型\字符型等,还可以是指针类型.
//它的作用是将一个变量的地址传送到另一个函数中
//函数的调用可以得到一个返回值,而运用指针变量作参数,可以得到多个变化的值
/*swap(int *p1,int *p2)
{int p;        //不用*p,在p中并无确定地址,用p中并无确定地址,*p可能会造成破坏系统的正常工作状态
 p=*p1;
 *p1=*p2;
 *p2=p;
}
main()
{
 int a,b;
 int *pointer_1,*pointer_2;
 scanf("%d%d",&a,&b);
 pointer_1=&a;
 pointer_2=&b;

 swap(pointer_1,pointer_2);
 printf("%d,%d",a,b);
}*/
//C语言中实参变量和形参之间的数据是单向的"值传递"
/*swap(int x,int y)
{
 int z;
 z=x;
 x=y;
 y=z;
}
main()
{
 int a,b;
 scanf("%d%d",&a,&b);
 swap(a,b);
 printf("%d,%d",a,b);
}*/
//不能企图通过改变指针形参的值而使指针实参的值,指针变量做为函数参数单向的"值传递"
/*swap(int *p1,int *p2)
{int *p;
 p=p1;
 p1=p2;
 p2=p;
}
main()
{
 int a,b;
 int *pointer_1,*pointer_2;
 scanf("%d%d",&a,&b);
 pointer_1=&a;
 pointer_2=&b;
 swap(pointer_1,pointer_2);
 printf("%d,%d",*pointer_1,*pointer_2);
}*/
//求阶乘
#include"stdio.h"
power(int n)
{
 int x;
 if(n<0)
  printf("error!");
 else if(n==0||n==1)
  x=1;
 else x=power(n-1)*n;
        return (x);
}
main()
{
 int n,y;
 scanf("%d",&n);
 y=power(n);
 printf("%d!=%d",n,y);
 
}
//用递归法求n!
#include"stdio.h"
int fac(int n)
{
 int f;
 if(n<0) printf("n<0,data error!");
 else if(n==0||n==1) f=1;
 else f=fac(n-1)*n;
 return (f);
}
void main()
{
 int n,y;
 printf("Input a number:");
 scanf("%d",&n);
 y=fac(n);
 printf("%d!=%d",n,y);
}
//汉诺塔(用递归法)
#include"stdio.h"
move(char getone,char putone)
{
 printf("%c-->%c\n",getone,putone);
}
hanoi(int n,char one,char two,char three)
{
 if(n==1) move (one,three);
 else
 {
  hanoi(n-1,one,three,two);
  move(one,three);
  hanoi(n-1,two,one,three);
 }
}
main()
{
 int m;
 scanf("%d",&m);
 printf("the step to moving %d disks:\n",m);
 hanoi(m,'A','B','C');//调用函数
}

#include"stdio.h"
//应用打印阶乘
/*fac(int n)
{
 static int f=1;//static保存上一次的值
 f=f*n;
 return(f);
}*/
/*
{
    register int i,f=1;//寄存器变量,提高执行效率
 for(i=1li<=n;i++)
     f=f*i;
  return(f);
}
*/
f(int a)
{
 auto int b=0;
 static int c=3;
 b=1+b;
 c=c+1;
 return(a+b+c);
}
void main()
{
 int a=2,i;
 for(i=0;i<3;i++)
  printf("%d",f(a));

}
====================================================
结构体
//指向结构体变量的指针
/*#include"stdio.h"
#include"string.h"
struct stu
{
 int no;
 char name[20];
}stu1={346, "Abraham"};

void main()
{
 struct stu *p;
 p=&stu1;
 printf("%d\t%s\n",stu1.no,stu1.name);
    printf("%d\t%s\n",(*p).no,(*p).name);
    printf("%d\t%s\n",p->no,p->name);
}*/
/*//指向结构体数组的指针
#include"stdio.h"
#include"string.h"
struct stu
{
 int no;
 char name[20];
}stu1[2]={{346, "Abraham"},{347, "Brower"}};

void main()
{
 struct stu *p;
 printf("  No.     Name\n");
 for(p=stu1;p<stu1+2;p++)
    printf("%5d\t%s\n",p->no,p->name);
}*/
//用指向结构体的指针作函数参数
//1.值传递:用结构体变量成员做参数。如:stu1[1].name做函数形参
//2.地址传递:用指向结构体变量的指针作实参,将结构体变量的地址传给形参
//求分数最高的学员信息
#include"stdio.h"
#include"string.h"
struct stu
{
 int no;
 char name[20];
 int score;
}stu1[3];

void main()
{
 struct stu *p;
 int i,temp=0;
 int max;
 for(i=0;i<3;i++)
  scanf("%d%s%d",&stu1[i].no,stu1[i].name ,&stu1[i].score );
 for(max=stu1[0].score,i=1;i<3;i++)
  if(stu1[i].score >max)
  {
   max=stu1[i].score ;
   temp=i;
  }
 p=stu1+temp;
    printf("\nThe max score:\n");
    printf("No.%5d\tName%sScore%d\n",p->no,p->name,p->score );
}
//结构体数组记票
#include"stdio.h"
#include"string.h"
struct person
{
 char name[20];
 int count;
}leader[3]={"Li",0,"Zhang",0,"Fang",0};
void main()
{
 int i,j;
 char leader_name[20];
 for(i=1;i<=10;i++)
 {
  scanf("%s",leader_name);
  for(j=0;j<3;j++)
   if(strcmp(leader_name,leader[j].name)==0)  leader[j].count++;
 }
 printf("\n");
 for(i=0;i<3;i++)
  printf("%5s:%d\n",leader[i].name,leader[i].count);
}
//求一组学员的成绩,考卷上的内容。
#include"stdio.h"
#include"string.h"
struct grade
{
 char name[20];
 int termlyexam;
 int midterm;
 int endterm;
 float finalgrade;
}stu[10];

void main()
{
 int i=0,max;
 char ch;
 do
 {
  printf("请输入第%d学生成绩信息:\n",i+1);
  printf("姓名:");
  scanf("%s",stu[i].name);
  printf("平时成绩:");
  scanf("%d",&stu[i].termlyexam );
        printf("期中成绩:");
  scanf("%d",&stu[i].midterm );
  printf("期末成绩:");
  scanf("%d",&stu[i].endterm);
        printf("继续输入吗(y/n):\n");
  scanf(" %c",&ch);
  i++;
 }while(ch=='y'||ch=='Y');
 printf("以下是成绩清单\n");
 printf("姓    名   平时成绩   期中成绩    期末成绩   最终成绩\n");
    printf("========   ========   ========    ========   ========\n");
 for(i=0;i<3;i++)
 {
        stu[i].finalgrade =0.1*(stu[i].termlyexam)+0.2*(stu[i].midterm)+0.7*(stu[i].endterm);
     printf("%s%10d%12d%12d%15.2f\n",stu[i].name,stu[i].termlyexam,stu[i].midterm,stu[i].endterm,stu[i].finalgrade);
 }
 for(i=0;i<3;i++)
 {
  max=0;
  if(stu[max].finalgrade <stu[i].finalgrade )
           max=i;
 }
    printf("成绩最好的是:%s,最终成绩是:%f",stu[max].name ,stu[max].finalgrade );
 
}

//求公约数
#include"stdio.h"
void main()
{
 int a,b,k;
 char ch;
 do
 {
 printf("请输入要求最大公约数的数值:");
 scanf("%d,%d",&a,&b);
   int c=a,d=b;
 do
 {
  k=c%d;
  c=d;
  d=k;
 }while(k!=0);
 printf("最大公约数(%d,%d)=%d\n",a,b,c);
    printf("继续输入吗(y/n):\n");
 scanf(" %c",&ch);
 }while(ch=='y'||ch=='Y');
}

#include"stdio.h"    //求最大公倍数
void main()
{
 int a,b,k;
 char ch;
 do
 {
 printf("请输入要求最大公约数的数值:");
 scanf("%d,%d",&a,&b);
   int c=a,d=b;
 do
 {
  k=c%d;
  c=d;
  d=k;
 }while(k!=0);
 printf("最大公倍数(%d,%d)=%d\n",a,b,a*b/c);
    printf("继续输入吗(y/n):\n");
 scanf(" %c",&ch);
 }while(ch=='y'||ch=='Y');
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值