程序设计17
- 问题17_1
 - 代码17_1
 - 结果17_1
 
- 问题17_2
 - 代码17_2
 - 结果17_2
 
- 问题17_3
 - 代码17_3
 - 结果17_3
 
问题17_1
下列给定程序的功能是 :调用函数 f u n fun fun 将指定源文件中的内容复制到指定的目标文件中,复制成功时函数返回 1 1 1 ,失败时返回 0 0 0 。在复制的过程中,把复制的内容输出到屏幕。主函数中源文件名放在变量 s f n a m e sfname sfname 中,目标文件名放在变量 t f n a m e tfname tfname 中。
代码17_1
#include<stdio.h>
#include<stdlib.h>
int fun(char *source, char *target){
	FILE *fs, *ft;
	char ch;
	if((fs=fopen(source, "r"))==NULL)
		return 0;
	if((ft=fopen(target, "w"))==NULL)
		return 0;		
	printf("\nThe data in file:\n");
	ch = fgetc(fs);
	while(!feof(fs)){
		putchar(ch);
		fputc(ch, ft);
		ch = fgetc(fs);
	}
	fclose(fs);
	fclose(ft);
	return 1;
}
void main(void){
	char sfname[20] = "myfile1", tfname[20] = "myfile2";
	FILE *myf;
	int i;
	char c;
	myf = fopen(sfname, "w");
	printf("\nThe original data:\n");
	for(i=1; i<30; i++){
		c = 'A'+rand()%25;
		fprintf(myf, "%c", c);
		printf("%c", c);
	}
	fclose(myf);
	printf("\n\n");
	if(fun(sfname, tfname))
		printf("\nSucceed!");
	else printf("Fail!");
}
 
结果17_1

问题17_2
        函数 
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun的功能是:将长整型数中各位上偶数的数依次取出,构成一个数放在  
     
      
       
       
         t 
        
       
      
        t 
       
      
    t 中。高位仍在高位,低位仍在低位。
         例如,当  
     
      
       
       
         s 
        
       
      
        s 
       
      
    s 中的数为  
     
      
       
       
         87653142 
        
       
      
        87653142 
       
      
    87653142 时,  
     
      
       
       
         t 
        
       
      
        t 
       
      
    t 中的数为  
     
      
       
       
         8642 
        
       
      
        8642 
       
      
    8642 .
代码17_2
#include<conio.h>
#include<stdio.h>
void fun(long s, long *t){
	int d;
	long s1=1;
	*t = 0;
	while(s>0){
		d = s%10;
		if(d%2==0){
			*t = d*s1 + *t;
			s1 *= 10;
		}
		s /= 10;
	}
}
void main(void){
	long s, t;
	printf("\nPlease enter s:");
	scanf("%ld", &s);
	fun(s, &t);
	printf("The result is: %ld\n", t);
}
 
结果17_2

问题17_3
         函数  
     
      
       
       
         f 
        
       
         u 
        
       
         n 
        
       
      
        fun 
       
      
    fun 的功能是:将  
     
      
       
       
         s 
        
       
      
        s 
       
      
    s 所指字符串中除下标为偶数同时  
     
      
       
       
         A 
        
       
         S 
        
       
         C 
        
       
         I 
        
       
         I 
        
       
      
        ASCII 
       
      
    ASCII 码值也为偶数的字符外,其余的全部删除:字符串中剩余字符所形成的新串放在  
     
      
       
       
         t 
        
       
      
        t 
       
      
    t 所指的数组中。
         例如,若  
     
      
       
       
         s 
        
       
      
        s 
       
      
    s 所指字符串中的内容为  
     
      
       
       
         " 
        
       
         A 
        
       
         B 
        
       
         C 
        
       
         D 
        
       
         E 
        
       
         F 
        
       
         G 
        
       
         123456 
        
       
         " 
        
       
      
        "ABCDEFG123456" 
       
      
    "ABCDEFG123456" ,其中字符  
     
      
       
       
         A 
        
       
      
        A 
       
      
    A 码值为奇数,因此应当删除;字符  
     
      
       
       
         B 
        
       
      
        B 
       
      
    B 的  
     
      
       
       
         A 
        
       
         S 
        
       
         C 
        
       
         I 
        
       
         I 
        
       
      
        ASCII 
       
      
    ASCII 码值为偶数,但在数组中的下标为奇数,因此也应当删除;字符  
     
      
       
       
         2 
        
       
      
        2 
       
      
    2 的  
     
      
       
       
         A 
        
       
         S 
        
       
         C 
        
       
         I 
        
       
         I 
        
       
      
        ASCII 
       
      
    ASCII 码值为偶数,在数组中的下标也为偶数,因此不应当删除,其他以此类推。最后  
     
      
       
       
         t 
        
       
      
        t 
       
      
    t 所指的数组中的内容应是  
     
      
       
       
         " 
        
       
         246 
        
       
         " 
        
       
      
        "246" 
       
      
    "246" 。
代码17_3
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
void fun(char *s, char t[]){
	int i, j=0;
	for(i=0; s[i]!='\0'; i++){
		if(i%2==0&&s[i]%2==0)
			t[j++] = s[i];
	}
	t[j] = '\0';  // 在字符串最后加上结束标识 
}
void main(void){
	char s[100], t[100];
	system("CLS");
	printf("\nPlease enter string S:");
	scanf("%s", s);
	fun(s, t);
	printf("\nThe result is: %s\n", t); 
}
 
结果17_3




















