C语言经典一百例程序(一)

# include <stdio.h>

 

int main()

{

       /* 定义变量并赋初值 */

      int    a = 5;      

       char   c = 'a';  

       float  f = 5.3;    

       double m = 12.65;

    double result;

      

       /* 同类型数据间进行运算并输出结果 */

       printf("a + c = %d/n", a + c);

       printf("a + c = %c/n", a + c);

       printf("f + m = %f/n", f + m);

 

       /* 不同类型数据间进行运算并输出结果 */

       printf("a + m = %f/n", a + m);

       printf("c + f = %f/n", c + f);

 

       /* 将上述四个变量进行混合运算,并输出结果 */

       result = a + c * (f + m);

       printf("double = %f/n", result);

       getchar();

}

# include <stdio.h>

 

int main()

{

       /* 换行符'/n',用于输出换行 */

       printf("How are you?/n");

       printf("I am fine./n/n");

 

       /* 横向跳格符'/t',使跳到下一个输出区 */

       printf("How are you?/t");

       printf("I am fine./n/n");

 

       /* 退格符'/b',使当前的输出位置退一格,即输出的起始位置左移一位 */

       printf(" How are you?/n");

       printf(" /bI am fine./n/n");

 

       /* 回车符'/r',使当前输出位置回到本行开头 */

       printf("                I am fine.");

       printf("/rHow are you?/n/n");

 

       /* 多个转义字符的混合运用 */

       printf("note:/n  a s/ti/b/bk/rp/n");

       getchar();

}

# include <stdio.h>

 

int main()

{

       /* 定义一个整数类型的变量,用来存放后面算式的值 */

       int logic;   

 

       int a = 1;

       int b = 2;

       int c = 3;

 

       logic = a+b>c&&b<=c;

       printf("logic = %d/n", logic);

 

       logic = a>=b+c||b==c;

       printf("logic = %d/n", logic);

 

       logic = !(a<c)+b!=1&&(a+c)/2;

       printf("logic = %d/n", logic);

getchar();

}

# include <stdio.h>

 

int main()

{

       int i, j, k;

       int m, n, p;

 

       i = 8;

       j = 10;

       k = 12;

   

       /* 自增在操作数之前 */

       m = ++i;

    printf("i = %d/n", i);

       printf("m = %d/n", m);

 

       /* 自减在操作数之后 */

       n = j--;

       printf("j = %d/n", j);     

       printf("n = %d/n", n);

 

       /* 自增、自减的混合运算 */

       p = (++m)*(n++)+(--k);

       printf("k = %d/n", k);   

       printf("p = %d/n", p);   

       getchar();

}

# include <stdio.h>

 

int main()

{

       /* 定义了一个无符号字符型变量,此变量只能用来存储无符号数 */

       unsigned char result;

   

       int a, b, c, d;

       a = 2;

       b = 4;

       c = 6;

       d = 8;

 

       /* 对变量进行“按位与”操作 */

       result = a & c;

       printf("result = %d/n", result);

 

       /* 对变量进行“按位或”操作 */

       result = b | d;

       printf("result = %d/n", result);

 

       /* 对变量进行“按位异或”操作 */

       result = a ^ d;

       printf("result = %d/n", result);

 

       /* 对变量进行“取反”操作 */

       result = ~a;

       printf("result = %d/n", result);

       getchar();

      

}

# include <stdio.h>

 

void main()

{

       unsigned a, b, c, d;

       int n;

 

       a = 64;

       n = 2;

 

       /* 将操作数a右移(6-n) */

       b = a >> (6-n);

       printf("b = %d/n", b);

 

       /* 将操作数a左移n */

       c = a << n;

       printf("c = %d/n", c);

 

       /* 对操作数a进行的混合位运算 */

       d = (a >> (n-1)) | (a << (n+1));

       printf("d = %d/n", d);

}

# include <stdio.h>

 

void main()

{

       /* 定义字符型变量,并给它们付初值 */

       char c1, c2, c3, c4, c5, c6, c7;

       c1 = 'C';

       c2 = 'h';

       c3 = 'i';

       c4 = 'n';

       c5 = 'e';

       c6 = 's';

       c7 = 'e';

 

       /* 输出原码 */

       printf("原码是:%c%c%c%c%c%c%c/n", c1, c2, c3, c4, c5, c6, c7);

 

       /* 对字符进行译码运算 */

       c1 = c1 + 6;

       c2 = c2 + 6;

       c3 = c3 + 6;

       c4 = c4 + 6;

       c5 = c5 + 6;

       c6 = c6 + 6;

       c7 = c7 + 6;

 

       /* 输出译码结果 */

       printf("密码是:%c%c%c%c%c%c%c/n", c1, c2, c3, c4, c5, c6, c7);

}

# include <stdio.h>

 

void main()

{

       /* 定义一个整形指针p */

       int *p;

       int begin, end;

 

       begin = 10;

       /* 给指针p赋初值 */

       p = &begin;

       /* 将指针指向的值传给变量end */

       end = *p;

 

       printf("begin = %d/n", begin);

       printf("end = %d/n", end);

 

       /* 输出指针中的地址值 */

       printf("p = %d/n", p);

       printf("*p = %d/n", *p);

}

# include <stdio.h>

      

void main()

{

              int x, y, z, mid, dec;

              printf("请任意输入三个整数:/n");

              scanf("%d %d %d", &x, &y, &z);

 

              if(x < y)

              {

                     mid = x; x = y; y = mid;

              }

              if(x < z)

              {

                     mid = x; x = z; z = mid;

              }

              if(y < z)

              {

                     mid = y; y = z; z = mid;

              }

 

              printf("请输入一个整数,程序根据其正负判断输出:/n");

              scanf("%d", &dec);

              if(dec >= 0)    printf("最大整数为:%d/n", x);

              else  printf("最小整数为:%d/n", z);

}

# include <stdio.h>

 

void main()

{

       int x, y;

       printf("请输入自变量x");

       scanf("%d", &x);

 

       if(x < 6)

       {

              y = x - 12;

              printf("x = %d, y = %d/n", x, y);

       }

       else if(x < 15)

       {

              y = 3*x - 1;

              printf("x = %d, y = %d/n", x, y);

       }

       else

       {

              y = 5*x + 9;

              printf("x = %d, y = %d/n", x, y);

       }

}

# include <stdio.h>

 

void main()

{

       /* sex代表输血者的性别,weight代表输血者的体重,cubage代表输血量 */

       int sex, weight, cubage;

       printf("请给出输血者的性别和体重:");

       scanf("%d,%d", &sex, &weight);

 

       if(sex >= 0)    /* 若变量sex的数值为非负数,则表示为男性 */

       {

              if(weight >= 120)

              {

            cubage = 200;

                     printf("此人应该输血:%d毫升/n", cubage);

              }

              else

              {

                     cubage = 180;

                     printf("此人应该输血:%d毫升/n", cubage);

              }

       }

       else   /* 否则,表示为女性 */

       {

              if(weight >= 100)

              {

            cubage = 150;

                     printf("此人应该输血:%d毫升/n", cubage);

              }

              else

              {

                     cubage = 120;

                     printf("此人应该输血:%d毫升/n", cubage);

              }

       }

}

# include <stdio.h>

 

void main()

{

       int num;

       /* 下面定义的各变量,分别代表个位,十位,百位,千位,万位,十万位以及位数 */

       int indiv, ten, hundred, thousand;

       int ten_thousand, hundred_thousand, place;

 

       printf("请输入一个整数(0999999)");

       scanf("%d", &num);

 

       /* 判断变量num的位数 */

       if(num > 99999)

              place = 6;

       else if(num > 9999)

              place = 5;

       else if(num > 999)

              place = 4;

       else if(num > 99)

              place = 3;

       else if(num > 9)

              place = 2;

       else

              place = 1;

       printf("place = %d/n", place);

      

       printf("每位数字为:");

 

       /* 求出num在各位上的值 */

       hundred_thousand = num/100000;

       ten_thousand = (num - hundred_thousand*100000)/10000;

       thousand = (num - hundred_thousand*100000 - ten_thousand*10000)/1000;

       hundred = (num - hundred_thousand*100000 - ten_thousand*10000

                    - thousand*1000)/100;

       ten = (num - hundred_thousand*100000 - ten_thousand*10000

                - thousand*1000 - hundred*100)/10;

       indiv = num - hundred_thousand*100000 - ten_thousand*10000

                  - thousand*1000 - hundred*100 - ten*10;

 

       /* 判断变量num的位数,并根据位数做出相应的输出 */

       switch(place)

       {

       case 1: printf("%d", indiv);

                  printf("/n反序数字为:");

                     printf("%d/n", indiv);

                     break;

    case 2: printf("%d, %d", ten, indiv);

                  printf("/n反序数字为:");

                     printf("%d%d/n", indiv, ten);

                     break;

       case 3: printf("%d, %d, %d", hundred, ten, indiv);

                  printf("/n反序数字为:");

                     printf("%d%d%d/n", indiv, ten, hundred);

                     break;

       case 4: printf("%d, %d, %d, %d", thousand, hundred, ten, indiv);

                  printf("/n反序数字为:");

                     printf("%d%d%d%d/n", indiv, ten, hundred, thousand);

                     break;

       case 5: printf("%d, %d, %d, %d, %d", ten_thousand, thousand,

                               hundred, ten, indiv);

                  printf("/n反序数字为:");

                     printf("%d%d%d%d%d/n", indiv, ten, hundred,

                                thousand, ten_thousand);

                     break;

       case 6: printf("%d, %d, %d, %d, %d, %d", hundred_thousand,

                               ten_thousand, thousand, hundred, ten, indiv);

                  printf("/n反序数字为:");

                     printf("%d%d%d%d%d%d/n", indiv, ten, hundred, thousand,

                                ten_thousand, hundred_thousand);

                     break;

       default: printf("Not find./n");

                   break;

       }

}

# include <stdio.h>

 

void main()

{

       int i, j, k;

       /* 变量i04,表示所画菱形图的第一至第五行 */

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

       {

              /* 当行数为i时,空格数是i的函数,为4-i */

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

                     printf(" ");

              /* 星号数也是i的函数,为2i+1 */

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

                     printf("*");

              printf("/n");

       }

       /* 变量i03,表示所画菱形图的第六至第九行 */

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

       {

              /* 当行数为i时,空格数是i的函数,此时为i */

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

                     printf(" ");

              /* 星号数也是i的函数,此时为7-2i */

              for(k = 0; k <= 6-2*i; k++)

                     printf("*");

              printf("/n");

       }

}

# include <stdio.h>

 

void main()

{

       int x, y, num1, num2, temp;

       printf("请输入两个正整数:/n");

       scanf("%d %d", &num1, &num2);

 

       if(num1 < num2)

       {

              temp = num1;

              num1 = num2;

              num2 = temp;

       }

       x = num1;

       y = num2;

       while(y != 0)

       {

              temp = x%y;

              x = y;

              y = temp;

       }

       printf("它们的最大公约数为:%d/n", x);

       printf("它们的最小公倍数为:%d/n", num1*num2/x);

}

# include <math.h>

# include <stdio.h>    /* 数学函数库 */

 

void main()

{

       /* s表示多项式的值,用t表示每一项的值 */

       double s, t, x;

       int n;

       printf("please input x: ");

       scanf("%lf", &x);

    /* 符初值 */

       t = x;

       n = 1;

       s = x;

    /* 进行叠加运算 */

       do

       {

              n = n + 2 ;

              t = t * (-x*x)/((float)(n)-1)/(float)(n);

              s = s + t;

       } while (fabs(t)>=1e-8);

       printf("sin(%f) = %lf/n", x, s);

}

 

经典的程序让一些概念更加精髓的表现出来,阅读并尝试做一些修改,看能否表现出自己的想法~~~~

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值