Hello World

说老实的,我敢说基本上所有的程序员都会看到这句话
相比起看到,自己敲出来还是有种莫名其妙的感觉
几十年前,Dennis Ritchie 在发明C的时候,好像也说过这句话
是不是他说的.....好像忘了
emmmmmm.......
总之就好像跟着时代的步伐,一步一步地向前走

#include<stdio.h>
int main ()
{
printf("Hello world");
}

虽然只是一个非常简单的小程序,但是所有东西都是从简单的开始的额,不是吗

  • // hash sign
  • {} Curly Braces
  • / Forward Slash
  • F11 Compile & Run
  • printf print function

Biggest

到了现在,我们需要比较3个数字的大小
这看起来很简单的3个数字来自用户,简单的来说就是用户输入的
这里涉及到大于号和小于号的使用问题

#include<stdio.h>
void main(){
    int a;
    int b;
    int c;
    printf ("input 3 number and press enter to next\n");
    scanf ("%d",&a);
    scanf ("%d",&b);
    scanf ("%d",&c);
    if(a>b)
    {
        if(a<c)
        {
            printf ("Hey the first is the biggest");
        }
        else
        {
            printf ("Hey the secone is the biggest");
        }
    }
    else
    {
        if (b>c)
        {
            printf ("Hey the secone is the biggest");
        }
        else
        {
            printf ("Hey the third is the biggest");
        }
    }
}

Variable

#include<stdio.h>
void main(){
//set a = 10
//int = integer
    int a = 10;//set a = 10
    //vriable name -> a
    //value -> 10
    //how to input a number from user
    float f=4.4;//(floating point number)
    //read number
    //input a vaule from the user
    float num;//num doesn't have a vaule
    scanf("%f",&num);//save the vaule from user in valiable num
    //Ampersand &-¡· Telling C that save the value from user in the memory in the location of num
    //& -> address 
    printf("%f\n",num);
    printf("welcome\n");//print welcome
    ///n -> print welcome and go to next line 
    printf("%d\n",a);//print a
    //d-> format specifier for ini
    printf("%f",f);//print f
    //f-> fotmat specifier for float
    //tell c what is the types of value
    //interger -> decimal ->  %d (Read : present d)
    //what is the types of value
    //float-> %f

我们遇到了一个很严重的问题!
为什么打出来的数字后面有几个零
数了数,好像都是6个零
为啥啊

因为C只能读懂小数点后面的第六位小数,当我们使用float 的时候
C就会把数字小数化
比如数字是1的话就会变成1.000000
对于int 和 float这两个变量都有最大值
但是现在不需要知道.......

Even&Odd

#include<stdio.h>
void main(){
    int a;
    int remainder;
    scanf("%d",&a);
    remainder = a%2;
    if (remainder ==0)
    {
        printf("Hey Number is Even");
    }
    else
    {
        printf("Hey Number is odd");
    }
}

哦对了
在代码的任意地方打上/ ..... /的话
这两个符号中间的内容会被全自动无视掉
还有一件事,上面这个代码中出现了%
这见鬼的玩意一看就知道不好搞
这玩意叫做余数Remainder
打个比方,3%2 就是1, 4%2 就是0
这样就可以用来检验一个数字是Even偶数 还是 Odd奇数

Biggest in 5 numbers

#include<stdio.h>
void main(){
    int a,b,c,d,e;
    printf ("Enter 5 numbers")
    scanf ("%d%d%d%d%d%d",&a,&b,&c,&d,&e)
    if (a>b&a>c&a>d&a>e)
    {
        max = a;
    }
    else if (b>a&b>c&b>d&b>e)
    {
        max = b;
    }
    else if (c>a&c>b&c>d&c>e)
    {
        max = c;
    }
    else if (d>a&d>b&d>c&d>e)
    {
        max = d;
    }
    else if (e>a&e>b&e>c&e>d)
    {
        max = e
    }
}

俗话说,世上有一种东西叫做暴力美学
上面这行代码简直体现的淋漓尽致
当一个数同时满足大于abcd的时候,它当然就是最大的
然后把所有可能尝试一遍,搞定
所以说,你大爷永远是你大爷

Balabalabalabala

#include<stdio.h>
void main(){
    int i;
    int n = 0;
    for (i=1;i>n;i++)
    {
        printf ("********            *           *                     *         \n");
        printf ("*       *         *   *         *                   *   *       \n");
        printf ("*       *       *       *       *                 *       *     \n");
        printf ("********       ***********      *                ***********    \n");
        printf ("*       *      *         *      *                *         *    \n");
        printf ("*       *      *         *      *                *         *    \n");
        printf ("********       *         *      ***********      *         *    \n");
        printf ("\n\n");
    }    
}

说老实的,我这个只是无聊之作
会一直都在balabalabalabala
最原始的时候只写了bala然后加个循环
后来觉得不够酷炫
弄了个更酷炫的bala
在来个循环
get!

Loop inside loop

#include <stdio.h>
main (){
    int n;
    int j;
    int i;
    printf ("Enter a number of lines");
    scanf("%d",&n);
    //line 1 -> 1 number
    //line 2 -> 2 number
    //line 3 -> 3 number
    //line n -> n number
    for (i=1;i<=n;i++)
    {
        for (j=1;j<i;j++)//run the j loop i times
        printf ("%d\n",i);
    }
    printf ("\n");//a new line in i loop
}

听过碟中谍吗
听过的话loop中loop也差不多
甚至我们还可以做loop 中loop 中loop 中loop 中loop 中loop 中loop 中(笑)

number pyramid

#include<stdio.h>
void main(){
    int a;
    int b;
    int n;
    printf ("enter a number of max line of number");
    scanf ("%d",&n);
    for (a=1;a<=n;a++)
    {
        for (b=n;b>=1;b--)
        printf ("%d",b);
    }
}

这玩意会打出来什么东西呢?

1
12
123
1234
12345
123456
1234567
......

我认为这是个绝妙的方法来认识循环
基本上看过这个之后,循环都能了解的差不多了

Little Star1

嗯,这玩意打印出来就像这个

*       4 space    n-1    i=1
    • 3 space n-2 i=2
      • 2 space n-3 i=3
        • 1 space n-4 i=4
          • 0 space n-5 i=5

      include<stdio.h>

      void main ()
      {

      int n;
      int i;
      int j;
      int k;
      printf ("how much line of * do you want\n");
      scanf ("%d",&n);
      for (i=1;i<=n;i++)//loop for n
      {
          for (k=1;k<=n-i;k++)//loop for space
          {
              printf (" ");
          }
          for (j=1;j<=i;j++)//loop for number of *
          {
              printf ("* ");
          }
          printf ("\n");
      }

      }

Little Star2

看到上面那个正数的吗
就是正过来的
到过来呢?
简单

#include<stdio.h>
void main ()
{
    int n;
    int i;
    int j;
    int k;
    printf ("how much line of * do you want \n");
    scanf ("%d",&n);
    for (i=n;i>=1;i--)//line of loop
    {
        for (k=1;k<=n-i;k++)
        {
            printf (" ");
        }
        for (j=1;j<=i;j++)
        {
            printf ("* ");
        }
        printf ("\n");        
    }
}

Added at every number

这个简单了,我们有了小星星
把小星星换成数字一定超级酷炫
问题是换了之后还得保持一个小星星的基本性质
那就多弄几个循环嘛,简单

write a program to print
1
2 3
4 5 6
7 8 9 10
...
...
the number of lines is

i have 1 loop for row(i loop)
i have 1 loop for columns (j loop)
i 1 to N means->
1 i=1
2 i=2
3 i=3
4 i=4
..... i=n

j from 1 to 10
1 i=1 j=1
2 i=2 j=1 i=2 j=2
3 i=3 j=1 i=3 j=2 i=3 j=3
4 i=4 j=1 i=4 j=2 j=4 i=3 j=4 i=4
..... i=n

#include<stdio.h>
  void main (){
      int i;
      int j;
      int N;
      int A;//declare the cariables
      int number=1;
      printf ("How much line do you want\n");
      scanf ("%d",&A);
      printf ("no no no no , change a number\n");
      scanf ("%d",&N);
      for (i=1;i<=N;i++)
      {
          for (j=1;j<=i;j++)
          {
              printf ("%d\t",number);// \t-> tap space , in c , This is 8 space
              // c doesnt know number
              number++;
          }
          printf ("\n");
      }
  }

While loop

#include<stdio.h>
void main ()
{
    int i;
    int j;
    i = 1;
    
    while (j<10)
    {
        j = 1; //j become 1 again and again
        while (j<=i)
        {
            printf ("%d",i);
            j++;
        }
        printf ("\n");
        i++;
    }
}

这玩意或许可以叫做while in while
while 循环中还有一个while 循环
也就是说只有同时满足外循环和内循环的时候才能走出来

Switch Case

show a memu
1.add +
2.subtract -
3.multiply *
4.divide /
5.modulus %

#include<stdio.h>
void main ()
{
    int variable;
    int variable2;
    int choice;
    int result;
    do
    {
        printf ("input a number : ");
        scanf ("%d",&variable);
        printf ("input another number : ");
        scanf ("%d",&variable2);
        printf ("choooooooooose a number of that box to do\n");
        printf ("***************************************\n");
        printf ("1.add\n");
        printf ("2.subtract\n");
        printf ("3.multiply\n");
        printf ("4.divide\n");
        printf ("5.modulus\n");
        printf ("***************************************\n");
        printf ("***********input 0 to exit*************");
        scanf ("%d",&choice);
        switch (choice)
        {
            case 1:
                {
                    result = variable + variable2;
                    printf ("-------------------\n");
                    printf ("result is :");
                    printf ("%d\n",result);
                    printf ("-------------------\n");
                    break;
                }
            case 2:
                {
                    result = variable - variable2;
                    printf ("-------------------\n");
                    printf ("result is :");
                    printf ("%d\n",result);
                    printf ("-------------------\n");
                    break;
                }
            case 3:
                {
                    result = variable * variable2;
                    printf ("-------------------\n");
                    printf ("result is :");
                    printf ("%d\n",result);
                    printf ("-------------------\n");\
                    break;
                }
            case 4 :
                {
                    result = variable / variable2 ;
                    printf ("-------------------\n");
                    printf ("result is :");
                    printf ("%d\n",result);
                    printf ("-------------------\n");
                    break;
                }
            case 5:
                {
                    result = variable % variable2;
                    printf ("-------------------\n");
                    printf ("result is :");
                    printf ("%d\n",result);
                    printf ("-------------------\n");
                    break;
                }
        }
    }
    while (choice != 0);
    printf ("-------------------\n");
    printf ("result is :");
    printf ("%d\n",result);
    printf ("-------------------\n");
}

Swicth Case 这个玩意每次都让我想起任天堂的Switch游戏机
有一说一,他两还真有点相似
我能够选择其中的一个,拿出来用
其他的就不管不管

哦对了,在后面还可以加上

while (choice != 0);
printf ("show me your id");

来表示输入0就退出

Stupid Day

#include<stdio.h>
void main ()
{
    int a;
    printf ("enter a number of day");
    scanf ("%d",&a);
    if(0<a && a<8) 
    {
        switch (a)
        {
            case 1:
                {
                    printf ("Monday");
                    break;
                }
            case 2:
                {
                    printf ("Tuesday");
                    break;
                }
            case 3:
                {
                    printf("Wednesday");
                    break;
                }
            case 4:
                {
                    printf ("Thurday");
                    break;
                }
            case 5:
                {
                    printf ("friday");
                    break;
                }
            case 6:
                {
                    printf ("Saturday");
                    break;
                }
            case 7:
                {
                    printf ("sunday");
                    break;
                }
//            default: //if the value does not match any case
//                {
//                    printf ("stupid day");
//                    break;
//                }
        }
    }
    else
    {
        printf ("stupid day");
    }
}

我发现我们老师好像蛮喜欢说stupid这个单词的
不过它确实能表达好多意思
好多好多
举个栗子,当用户输入的数字不在列表中的时候,我们可以说stupid input
简单粗暴易懂

Prime Number

#include <stdio.h>
void main()
 {
     int a;
     int b = 2;
     int c = 1;
     printf ("input number n =");
     scanf ("%d",&a);//5
     do 
     {
         c = a%b;//1
         b++;//3
         if (c == 0)
         {    
             printf ("the number is not a prime number");
             break;
         }
     }while (b<a);//3 <81
     //false
     printf("%d\n",c);
    if(c!=0)
     {
         printf ("the number is a prime number");
     }
 }

Biggest in 50 number

#include <stdio.h>
void main()
 {
     int a;
     int b = 2;
     int c = 1;
     printf ("input number n =");
     scanf ("%d",&a);//5
     do 
     {
         c = a%b;//1
         b++;//3
         if (c == 0)
         {    
             printf ("the number is not a prime number");
             break;
         }
     }while (b<a);//3 <81
     //false
     printf("%d\n",c);
    if(c!=0)
     {
         printf ("the number is a prime number");
     }
 }

这个小程序能够通过改变数字N的值来改变来比较多少个数字的值

Functions

#include<stdio.h>
//function declaration  -> tell c that there is a function in the code
//Example : void welcome()
//function definition -> tell what the function will do 
//Example : void welcome(){   }
// function call -> tell c use the function
//Example : welcome()
void main ()
{
    int x=welcome();
    printf("%d",x);
}
int welcome()
{
    return 3.65;    
}
void bye()
{
    
}

这个小程序能够把程序串联起来,并且通过return来进行串联
Return的值能够在程序之间传输

Armstrong Number

听过阿姆斯壮数嘛?什么???没听过?
Alt + F4 谢谢

#include<math.h>
void main()
{
    int a;
    int b;
    int d =0;
    int num;
    printf ("show me your ID = ");
    scanf ("%d",&a);
    b = a;//153
    while (b>0)
    {
        num = b % 10;//1
        b = b/10;//0
        d += pow(num,3);//153
    }
    if (d ==a)
    {
        printf ("the number is the armstrong number");
    }
    else
    {
        printf ("the number is not a armstrong number");
    }
}

这个小程序能够检测一个数字是否是阿姆斯壮数
如果你不知道什么是阿姆斯壮数?下载来试试看就知道了

Two funtion in one program

这个玩意被大量应用在我们组的project中
因为每个人负责的部分不一样,所以用这个方法来区分和分隔开来
讲道理这个方法在大型项目中有着很强的拓展性

//function is a sub-program that contains one or more statements and it performs some task when 
called.
//pre-defined functions : 
//User-defined functions : i make my own function, i make my function to do something.
//
#include<stdio.h>
void add()//created by me
//"add" is the name i give it
//i can change "add" to any name
//i tell the meaning of function 
//call the function 
{
    int a,b,result; //local variable
    printf ("enter number 1 =");//ONLY USED IN FUNCTION "ADD"
    scanf ("%d",&a);
    printf ("enter number 2 =");
    scanf ("%d",&b);
    result = a+b;
    printf ("%d\n",result);
//end of function add
}
//i call function add in function main
void main() //created bu Ritchie
{
    add();//use function of "add"
    printf ("Done!");//print Done! while function is finish
}

简单的来说,就是在main中引一个指针
指针指向function add(这个名字可以完全自定义)
当表述完成后显示Done!
简单的理解

Return value

如果我想要一个数字在不同的function之间跳跃,达到把不同的function串通起来的目的

Last modification:August 18, 2020
如果觉得我的文章对你有用,请随意赞赏