C基本概念(一)

 

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 0

main(){
   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);
   }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值