The C Programming Language note

 

 

no.1

#include<stdio.h>
#define  IN  1 // inside a word
#define  OUT 0 // outside a wrod
//count lines,words,and characters in input
void main()
 {int c,nl,nw,nc,state,wordslong;
    nl = nw = nc =0;
    state = OUT;
 while((c=getchar())!=EOF)
    {++nc;
    if(c=='\n')
        ++nl;
    if(c==' '||c=='\n'||c=='\t')
        state = OUT;
    else if(state == OUT)
        {state = IN;
        ++nw;
        }
    }
printf("%d %d %d",nl,nw,nc);
 }

 

 no.2

#include<stdio.h>
//count digits,white space,others
void  main()
{
int c,i,j,nwhite,nother,havei=-1;
int ndigit[10];
float count[100][12];
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 == '\n' || c == '\t'  || c == ' ')
        ++nwhite;
    else
        ++nother;

for(i = 0 ; i < 100 ; i++)
    {for( j= 0 ; j < 12 ; j++)
       count[i][j] =0;
    }
for(j = 0 ; j< 10 ; ++j)
    for(i = 0 ; i < ndigit[j] ; ++i)
        count[i][j]=1;

for(i= 0 ;i < nwhite; ++i)
    count[i][10]=1;


for(i= 0 ;i < nother; ++i)
    count[i][11]=1;


for(i = 99 ; i >=0 ; --i)
    {for( j= 0 ; j < 12 ; j++)
        if(count[i][j] == 1)
            {havei=i;
            break;
            }
    if(havei!=-1)
        break;
    }

printf("\n\n\nThis is the result:\n");

for(i = havei ; i >=0 ; --i)
    {for( j= 0 ; j < 12 ; j++)
        if(count[i][j]==0)
            printf("%-4c", ' ');
        else
            printf("%-4c",3);

    printf("\n");
    }

for(i=0;i<10;++i)
    printf("%-4d",i);

printf("no  nw\n");
}

 

 

 

no.3

 

#include<stdio.h>
#define MAXLINE 1000
int getline(char line[],int maxline);
void copy(char to[],char from[]);
//This program used to print the longest input line
int  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;

}


int getline(char s[],int maxline)
//getline : read a line into s
{
 int  c,i;

 for(i = 0;i<maxline -1 && (c=getchar())!=EOF && c!='\n' ; ++i)
    s[i]  = c;

 if(c == '\n')
    {s[i] = c;
    ++i;
    }
 s[i] = '\0';
 return i;
}


void copy(char to[],char from[])
//copy: copy "from" into  "to" ; assume to is big enough   
{
 int i;
 
 i = 0;
 while((to[i] = from[i]) != '\0')
     ++i;
}

 

 

 no.4  + - * /  reverse polish calculator

 

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>  //for   isdigit()....


#define MAXOP 100    //max input operand or operator
#define NUMBER '0'  //number sign
#define MAXVAL 100
#define BUFSIZE 100
char buf[BUFSIZE];



int bufp = 0;
int sp = 0;
double val[MAXVAL];



int getch(void);
void ungetch(int c);
int getop(char s[]);
void push(double);
double pop(void);



int main()
//reverse polish calculater
{
    int type;
    double op2;
    char s[MAXOP];

    while ( (type = getop(s)) != EOF)
    {
    switch (type)
        {case NUMBER:  
             push(atof(s)); //  atof(): from  <stdlib.h>    ASCII to  float
             break;
         case '+':  
             push(pop()+pop());
             break;
         case '*':
             push(pop()*pop());
             break;
         case '-':
             push(pop()-pop());
             break;
         case '/':
             op2 = pop();
             if (op2 != 0.0)
                 push(pop() / op2);
             else
                 printf("error:unknow command %s\n",s);
             break;
         case '\n':
             printf("\t%.8g",pop());
             break;
         default:
             printf("error:unknow command %s \n",s);
             break;
        }
    }
    return 0;
}

int getop(char s[])
// get next character or numeric operand
{
    int i,c;
  
    while((s[0] = c =getch()) == ' '|| c == '\t')
            ;

    s[1] = '\0';

    if(!isdigit(c) && c !='.')
        return c;
    i = 0;

    if(isdigit(c))
        while(isdigit(s[++i] = c = getch()))
            ;
    if(c == '.')
        while(isdigit(s[++i] = c =getch()))
            ;
    s[i] = '\0';
    if( c != EOF)
        ungetch(c);
    return NUMBER;

}



void push(double f)
//push: push f onto value stack
{
    if ( sp < MAXVAL )
        val[sp++] = f;
    else
        printf("error:stack full,can't push %g\n", f);

}


double pop(void)
//pop:pop and return top value from stack
{
    if( sp > 0 )
        return val[--sp];
    else
        {printf("error:stack empty\n");
        return 0.0;
        }
}

int getch(void)
//getch: get a character(be from stack or buffer)
{
    return (bufp > 0)?buf[--bufp]:getchar();
}


void ungetch(int c)
//push character back on input
{
    if( bufp >=BUFSIZE)
        printf("ungetch:too many characters\n");
    else
        buf[bufp++] = c;
}

 

no.5 shell sort 

#include<stdio.h>
#define MAXLINE 10

void  shellsort(int a[],int n);
void  scanintegers(int a[],int n);
void  printintegers(int a[],int n);

int main()
{
int integers[MAXLINE];
scanintegers(integers,MAXLINE);
shellsort(integers,MAXLINE);
printf("\n");
printintegers(integers,MAXLINE);
return 0;
}

void shellsort(int a[],int n)
{
    int gap,i,j,temp;
    for(gap = n/2; gap > 0 ;gap /=2)
        for(i = gap; i < n ; ++i)
            for(j = i - gap ;j >= 0 && a[j] > a[j+gap] ; j -=gap)
                {temp = a[j];
                a[j] = a[j+gap];
                a[j+gap] = temp;
                }
}

void  scanintegers(int a[],int n)
{
    int i;
    for( i = 0 ; i < n ; ++i)
        scanf("%d",&a[i]);
}

void printintegers(int a[],int n)
{
    int i;
    for( i = 0 ; i< n ; ++i)
        printf("%5d", a[i]);
}

 

转载于:https://www.cnblogs.com/chengtou/p/8963479.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值