C基本概念(一)

来源:互联网 发布:自己开淘宝店怎么代理 编辑:程序博客网 时间:2024/05/16 08:50

 

1、编写第一个程序
#include  <stdio.h>main(){printf("hello, world/n");}     
2、编写一个用于打印摄氏与华氏对照表的程序(两种方法)
#include  <stdio.h>main(){float fahr, celsius;int lower, upper , step;lower = 0;upper = 300;step = 20;fahr = lower;while(fahr <= 300){celsius = (5.0 / 9.0) * (fahr - 32);printf("%3.0f%6.1f/n", fahr, celsius);fahr = fahr + 20;} }
#include  <stdio.h> main(){    int fahr;    for(fahr = 0; fahr <= 300; fahr = fahr + 20){    printf("%3d/t%6.1f/n", fahr, (5.0 / 9.0) * (fahr - 32));    }}
这个程序引入了一些概念,包括:变量、算术表达式、循环以及格式输出

 

3、编写一个用于统计空格、制表符与换行符个数的程序
#include  <stdio.h>main(){   int c, ck, ct, cl;   ck = 0;   ct = 0;   cl = 0;      while( (c = getchar()) != EOF){    if(c == ' ') ++ck;    if(c == '/t') ++ct;    if(c == '/n') ++cl;   }   printf("%d/n%d/n%d/n", ck, ct, cl);}

 

4、编写一个程序,把它的输入复制到输出,并在此过程中将相连的多个空格用一个空格代替
#include  <stdio.h>int main(){   char str[128];    int i = 0;   int flag = 1;      gets(str);   while(i++ < strlen(str)){    if(str[i] == ' '){      if(flag)        flag = 0;              else                continue;    }    else      flag = 1;            putc(str[i], stdout);   }      return 0; }
5、统计输出的行数、单词数和字符数
#include  <stdio.h>#define IN 1#define OUT 0main(){   int c, nl, nw, nc, state;   state = OUT;   nl = nw = nc = 0;      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/n", nl, nw, nc);}
6、编写一个程序以每行一个单词的形式打印输出
#include <stdio.h>void main(){   char ch;   char str[100];   int i = 0 ;   int hw = 0;      while( (ch = getchar()) != '/n' ){    if(ch != ' '){       str[i++] = ch;       if(hw == 0){    hw = 1;            }    }    else{           str[i] = '/0';           printf("%s/n", str);           hw = 0;           i = 0;            }   }      if(hw == 1){    str[i] = '/0';    printf("%s/n", str);   }}


原创粉丝点击