您的位置:首页 > 编程语言 > C语言/C++

个人C语言笔记

2016-11-20 22:07 218 查看
1 .给出三角形的三边长,求三角形面积(假设三边已符合规律)

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

main()

{

    double a, b, c, s, area;

    a = 3.67;

    b = 5.43;

    c = 6.21;

    s = (a + b + c) / 2;                                             
关键算法!

    area = sqrt(s*(s - a)*(s - b)*(s - c));

         printf("a=%f\tb=%f\tc=%f\n", a, b, c);

    printf("area=%f\n", area);

    system("pause");

}

注:海伦公式:area=

                 s=(a+b+c)/2(半周长)

“\t”意思是“使输出位置调到下一个tab位置”(相当于制表时按一下tab键),使输出的数据整齐,美观,清晰。

 

 

2 .求ax2+bx+c=0方程的根。a,b,c由键盘输入,b2-4ac>0。

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

main()

{

    double a, b, c, disc, x1, x2, p, q;

    printf("请输入方程系数a,b,c之间加以空格:");

    scanf_s("%lf%lf%lf", &a, &b, &c, 50, 50, 50);

    disc = b*b - 4 * a*c;

    p = -b / (2.0*a);

    q = sqrt(disc) / (2.0*a);

    x1 = p + q; x2 = p - q;

    printf("x1=%7.2f\nx2=%7.2f\n", x1, x2);

    system("pause");

}       

注:输入a,b,c之间要加空格!!(因为是双精度数,系统会把三个整数转换为1.0 2.0 3.0,然后赋值给a,b,c)

%lf为双精度实型数据.7.2意思是指定数据占7列,其中小数占2列

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

main()

{

    double a, b, c, disc, x1, x2, p, q;

    printf("请输入方程系数a,b,c之间加以空格:");

    scanf_s("%lf%lf%lf", &a, &b, &c, 50, 50, 50);

    disc = b*b - 4 * a*c;       开始即定义!

    if (disc < 0)

         printf("所求方程无实根\n");

    else

    {

         p = -b / (2.0*a);

         q = sqrt(disc) / (2.0*a);

         x1 = p + q; x2 = p - q;

         printf("x1=%7.2f\nx2=%7.2f\n", x1, x2);

    }

    system("pause");

}

(加入实根判断方程)

 

 

3 .假如现在我国国民生产总值年增长率为10%,计算10年后我国国民生产总值与现在相比增长多少百分比。计算公式为:p=(1+r)n

#include <stdio.h>

#include<stdlib.h>

#include<math.h>

main()

{

    float p, r, n;

    r = 0.1;

    n = 10;

    p = pow(1 + r, n);     pow函数

    printf("增长百分比为%f\n", p);

    system("pause");

}

注,调用数学标准库函数pow(次方函数,注意引用方式!!)

 

4 .运算符优先级

!(非)>算术运算符>关系运算符>&&和||>条件运算符>赋值运算符

 

5 .输入一个人字符,判断其是否为大写字母,如果是,转换为小写;如果不是,则不转换。然后输出最后得到的字符。

#include <stdio.h>

#include<stdlib.h>

main()

{

    char ch;

    scanf_s("%c", &ch, 12);

    ch = (ch >= 'A'&&ch <=
'Z') ? (ch + 32) : ch;     最关键算法!

    printf("%c\n", ch);

    system("pause");

}

注:判断方法用的是条件运算符,相当于一个不带if的if语句。

 

6 .编程思想:用switch语句处理菜单命令。在许多程序中,用菜单对流程进行控制,例如从键盘输入一个“A”或“a”字符,就会执行A操作,输入一个“B”或“b”字符,就会执行B操作。

#include <stdio.h>

#include<stdlib.h>

main()

{

    void action1(int,
int), action2(int,
int);

    char ch;

    int a = 15, b = 23;

    ch = getchar();

    switch (ch)

    {

    case 'a':

    case 'A':action1(a, b);
break;

    case 'b':

    case 'B':action2(a, b);
break;

    default:putchar('\n');

 

    }

    return 0;

    system("pause");

}

void action1(int
x, int
y)

{

    printf("x+y=%d\n",
x + y);  /*执行加法函数*/

    system("pause");

}

void action2(int
x, int
y);    /*执行减法函数*/

{

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

    system("pause");

}

注:void action1(int,
int), action2(int,
int);相当于自定义函数。

(关键注意这种函数调用思想!!)

另:getchar()和putchar()只能输入输出字符,不能输入输出文字,而且括号里用的是单引号!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: