C语言概述、数据类型、运算表达式、程序结构

C语言概述、数据类型、运算表达式、程序结构

关键字 volatile
float型: 占4字节,7位有效数字
Double型: 占8字节,15~16位有效数字

自动转换的原则:
1、转换方向:char、short->int->unsigned->long->double<-float
2、转换按数据长度增加的方向进行,以保证精度不降低。
有符号数与无符号数运算时,有符号数会转换为无符号数后再运算
3、在赋值运算中,赋值号两边量的数据类型不同时,赋值号右边量的类型将转换为左边量的类型

注意:getchar()只能接收一个字符,而且只有用户按回车后,读入才开始执行

看printf()附加格式说明字符及其功能      

练习1_基础练习:
0.复习今天的知识
a. C语言概述,c程序文件结构
b. 数据类型,运算符,表达式
c. 程序结构if, switch, while等


练习switch
1.    给出任意年月日,计算是一年中的第几天

2.    从键盘输入1~7的数字,分别提示monday、tuesday、wednesday..
  要求可以循环输入检测;
  输入错误(0~7以外的数字)时有提示信息;
  按0结束测试.

练习:if else
3.从键盘输入一行字符串,统计其中的、英文字母、空格、数字等  其它字母的个数
4.对学生成绩进行评定:小于60为“E”,60-69为“D”,70-79为“c”,80-89为“b”,90以上为“A”

练习for循环:
5.采用冒泡法\选择法对十个数进行排序,由键盘输入十个数字,放在一个数组中,对其进行从大到小的排列,并输出其中最大者;

6.从键盘输入一个整数,判断一个是否是质数


7.输出100以内能被7整除的数

8.1元钱一瓶汽水,喝完后两个空瓶换一瓶汽水,问:有20元钱,最多可以喝到几瓶汽水?(使用while的方法)

习题答案
1、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

int main(void)
{
    uint year = 0;
    uchar month = 0;
    uchar day = 0;
    uint totalday = 0;

    fun();
    printf("请输入年、月、日。例:20111025\n");
    scanf("%4d%2d%2d", &year, &month, &day);
    printf("您输入的日期是:\n");
    printf("%d年%d月%d日\n", year , month, day);
    switch(month)
    {
        case 1:
            totalday = day;
            break;
        case 2:
            totalday = day+31;
            break;
        case 3:
            totalday = day+31+28;
            break;
        case 4:
            totalday = day+31+28+31;
            break;
        case 5:
            totalday = day+31+28+31+30;
            break;
        case 6:
            totalday = day+31+28+31+30+31;
            break;
        case 7:
            totalday = day+31+28+31+30+31+30;
            break;
        case 8:
            totalday = day+31+28+31+30+31+30+31;
            break;
        case 9:
            totalday = day+31+28+31+30+31+30+31+31;
            break;
        case 10:
            totalday = day+31+28+31+30+31+30+31+31+30;
            break;
        case 11:
            totalday = day+31+28+31+30+31+30+31+31+30+31;
            break;
        case 12:
            totalday = day+31+28+31+30+31+30+31+31+30+31+30;
            break;
        default:
            printf("输入错误!\n");
            break;
    }
    if(month>2&&((year%4==0)&&(year%100!=0)||(year%400==0)))//闰年
    {
        totalday++;
    }
    printf("这是一年中的第%d天\n",totalday);
    return 0;
}

2、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

int day(void)
{
    uchar day = 0;
    uchar flag = 0;

    printf("请输入星期数:\n");
    scanf("%d", &day);
    printf("您输入的数是:");
    printf("%d\n", day);
    if(day==0)
    {
        flag = 0;
        printf("结束测试!\n");
    }
    else
    {
        flag = 1;
        switch(day)
        {
            case 1:
                printf(" 今天是星期一\n");
                break;
            case 2:
                printf(" 今天是星期二\n");
                break;
            case 3:
                printf(" 今天是星期三\n");
                break;
            case 4:
                printf(" 今天是星期四\n");
                break;
            case 5:
                printf(" 今天是星期五\n");
                break;
            case 6:
                printf(" 今天是星期六\n");
                break;
            case 7:
                printf(" 今天是星期日\n");
                break;
            default:
                printf("输入错误!\n");
                break;
        }
    }
    return flag;
}
int main(void)
{
    while(day());
    return 0;
}

3、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

int main(void)
{
    uchar st = 0;
    uint letter = 0, space = 0, number = 0, other = 0;

    printf("请输入字符串:\n");
    while((st=getchar())!='\n')
    {
        if(st>='A' && st<='z') //英文字母
        {
            letter++;
        }
        else if(st==' ')//空格
        {
            space++;
        }
        else if(st>='0' && st<='9')//数字
        {
            number++;
        }
        else//其它
        {
            other++;
        }

    }
    printf("\n字母个数:%d\n",letter);
    printf("空格个数:%d\n",space);
    printf("数字个数:%d\n",number);
    printf("其它字符数目:%d\n",other);
    return 0;
}

4、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

int main(void)
{
    uchar grade =0;

    printf("请输入考分:\n");
    scanf("%d", &grade);
    printf("您输入的考分:");
    printf("%d\n", grade);
    if(grade<60)
    {
        printf("等级:E\n");
    }
    else if(grade>=60 && grade<=69)
    {
        printf("等级:D\n");
    }
    else if(grade>=70 && grade<=79)
    {
        printf("等级:C\n");
    }
    else if(grade>=80 && grade<=89)
    {
        printf("等级:B\n");
    }
    else if(grade>=90 && grade<=100)
    {
        printf("等级:A\n");
    }
    return 0;
}

5、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

void inorder(void)
{
    uint arry[10];
    uchar i,j;
    uint temp=0;
    printf("从大到小的排列\n");
    for(i=0;i<10;i++)
    {
        printf("请输入第%d个数\n",i+1);
         scanf("%d",&arry[i]);
    }
    printf("您输入的数:");
    for(i=0;i<10;i++)
    {
        printf("%d, ",arry[i]);
    }
    printf("\n排好序的数:");
    for(i=0;i<8;i++)
    {
        for(j=i+1;j<10;j++)
        {
            if(arry[i]<arry[j])
            {
                temp = arry[i];
                arry[i] = arry[j];
                arry[j] = temp;
            }
        }
    }
    for(i=0;i<10;i++)
    {
        printf("%d, ",arry[i]);
    }
    printf("\n");
}

int main(void)
{
    inorder();
    return 0;
}

6、
#include <stdio.h>
#include <math.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

void judgezhishu(void)
{
    uint temp = 0;
    uint tp = 0;
    uchar flag = 0;

    printf("请输入一个数:\n");
    scanf("%d", &temp);
    if(temp != 1)
    {
        for(tp=2;tp<=sqrt(temp);tp++)
        {
            if((temp%tp) == 0)
            {
                flag = 1;
                break;
            }
        }
        if(flag)
        {
            printf("这是和数\n");
        }
        else
        {
            printf("这是质数\n");
        }
    }
    else
    {
        printf("这不是质数也不是和数\n");
    }
    
}

int main(void)
{
    judgezhishu();
    return 0;
}

7、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;

void fun(void)
{
    uchar i = 0;

    printf("100以内能被7整除的数:\n");
    for(i=0;i<=100;i++)
    {
        if((i%7)==0)
        {
            printf("%d ",i);
        }
    }
    printf("\n");
}

int main(void)
{
    fun();
    return 0;
}

8、
#include <stdio.h>
typedef unsigned char uchar ;
typedef unsigned int uint ;


//uint fun(uint price, uint numb, uint money)
void fun(void)
{
    uint a = 20;
    uint sum = 0;
    uint b = 0;

    sum = 20;
    printf("sum= %d \n", sum);
    while(a>0)
    {
        sum+=a/2;
        b=a%2;
        a/=2;
        a+=b;

        printf("sum= %d \n", sum);
        while(getchar()!='\n')
            continue;
    }

//    return total;
}



int main(void)
{
    fun();
    //printf(" ",);
    return 0;
}

posted @ 2012-04-02 08:40  福。oO  阅读(319)  评论(0编辑  收藏  举报