我的C程序设计语言学习日记#01

这几个程序笼统的涵盖了C中宏定义、循环语句、标准的输入输出语句(包括了数值、字符的输入输出)、字符变量和字符串常量等知识点,主要是为了适应C语言编程环境而设立的。



1-1 打印温度对照表

#include <stdio.h>

/*定义常数常量*/
//表的下限
#define LOWER 0
//表的上限
#define UPPER 300
//步长
#define STEP 20

/*打印华氏温度-摄氏温度对照表*/
main()
{
    int fahr;

    /*for循环语句*/
    for(fahr = LOWER;fahr <= UPPER;fahr += STEP)
        printf("%3d %6.1f\n",fahr,(5.0/9.0)*)(fahr-32));
}

注意点:

  • 利用了公式:摄氏度 =(5/9)(华氏度-32)
  • 用#define进行符号常量的预编译,格式:#define 名字 替换文本
  • for循环语句格式:for(起始定义; 循环条件; 循环附加操作){语句执行内容}

1-2 输入字符计数程序

#include <stdio.h>

/*统计输入字符数:版本1*/
int main()
{
    long nc;
    nc = 0;
    while (getchar() != '\n')
        ++nc;
    printf("%ld\n",nc);
    return 0;
}
#include <stdio.h>

/*字符计数版本2*/
int main()
{
    double nc;
    for(nc = 0;getchar() != '\n';++nc)
        ;
    printf("%.0f\n",nc);
    return 0;
}

注意点:

  • 两个程序的差别有2:
    • 使用了不同类型的循环语句
    • 使用不同的变量储存字符数
  • 在编程中使用getchar()函数体会其作用

1-3 各类字符统计程序

#include <stdio.h>

int main()
{
    int c,i,nwhite,nother;
    int ndigit[10];

    nwhite = nother = 0;
    for(i=0;i < 10;++i)
        ndigit[i] = 0;

    while((c = getchar()) != EOF)
        if(c >= '0' && c <= '9')
            ++ndigit[c-'0'];
        else if(c == ' '|| c == '\n' || c == '\t')
            ++nwhite;
        else
            ++nother;
    printf("各个数字出现次数:\n");
    for(i=0;i<10;++i)
        printf("'%d' - %d次\n",i,ndigit[i]);
    printf("空白字符出现次数:%d次\n其他字符出现次数:%d次\n",nwhite,nother);
    return 0;
}

注意点:

  • 字符 ‘1’~ ‘9’的值为0到9
  • 用 c-‘0’来表示 c 字符或字符串对应的数值

1-4 行数统计程序

#include <stdio.h>

/*行数输入统计*/
int main()
{
    int c,n;
    n = 0;
    while((c = getchar()) != EOF)
        if(c == '\n')
            ++n;
    printf("%d\n",n);    
    return 0;
}

注意点:

  • EOF(end of file)为文件结束符,值为-1
  • Windows的DOS界面中使用‘Enter,Ctrl+Z,Enter’来输入EOF符结束输入
  • Linux的终端界面中使用‘Enter,Ctrl+D’来输入EOF符结束输入

1-5 单词打印程序

#include <stdio.h>

/*以一个单词一行的形式打印输入*/
int main()
{
    printf("输入一个句子,本程序将以一个单词一行的形式打印显示\n请输入:");
    char c;
    while((c = getchar()) != '\n')
    {
        if(c == ' ')
            printf("\n");
        else
            putchar(c);
    }
    printf("\n");
    return 0;
}

1-6 打印输入最长行

#include <stdio.h>
#define MAXLINE 1000    /*允许输入的最大长度*/

int getline(char line[],int maxline);
void copy(char to[],char from[]);

//打印最长的输入行
main()
{
    int len;    //当前行长度
    int max;    //目前为止发现的最大行长度
    char line[MAXLINE]; //当前输入行
    char longest[MAXLINE];  //用于保存最长的行

    max = 0;
    while((len = getline(line,MAXLINE)) > 0)
        if(len > max){
            max = len;
            copy(longest,line);
        }
    if(max > 0) //存在这样的行?
        printf("%s",longest);
    return 0;
}

//getline函数:将一行读入到s中并返回其长度
int getline(char s[],int lim)
{
    int c,i;

    for(i=0;i<lim-1 && (c = getchar()) != EOF && c != '\n';++i)
        s[i] = c;
    if(c == '\n')
    {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    return i;
}


//copy函数:将from复制到to;这里假定to足够大
void copy(char to[],char from[])
{
    int i;

    i = 0;
    while((to[i] = from[i]) != '\0')
        ++i;
}

注意点:

  • 本程序使用多个函数并在其中进行传值调用
  • 这里的字符数组发挥了很大作用,难度也较大,需好好体会其运用原理

1-7 行倒放输出

#include <stdio.h>

int main()
{
    char c,s[1000];
    int i = 0;
    while((c = getchar()) != '\n'){
        s[i] = c;   
        i++;
    }
    if(c == '\n')
        i--;
    int k;
    char l[i];
    for(k=0;k<=i;k++)
        l[k]=s[i-k];
    printf("%s\n",l);
    return 0;
}

参考学习资料

《C程序设计语言》

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值