xdu程序设计上机作业C++

第一次上机:

要求:
1、比较两个文本文件并打印出它们第一个不相同的行(文件每行字符数不多于80)。

#include <stdio.h>
#include <string.h>
int main(int argc, char const *argv[])
{
//初始化变量 
	FILE *fp1,*fp2;
	int flag=0;
	char line1[200],line2[200];
//打开文件,如果打开失败给出提示  
	if(  ((fp1=fopen("1.txt","r"))==NULL) ||  ((fp2=fopen("2.txt","r"))==NULL)   )
	{
		printf("cannot open file\n");
	return 0;
	}
	fp1=fopen("1.txt","r");
	fp2=fopen("2.txt","r");
	
//读取内容  
	while(!feof(fp1)&&!feof(fp2))
	{
//		按行读取,进行比较 
		fgets(line1,80,fp1);
		fgets(line2,80,fp2);
//		比较利用库函数strcmp 
		if(strcmp(line1,line2)!=0)
		{
			printf("%s\n",line1);
			printf("%s\n",line2);
			flag=1;
			break;
		}
		
	}
//	如果没有读取到不同的内容,则给出提示 
	if(flag==0) printf("The two files are same");
	
//	关闭文件 
	fclose(fp1);
	fclose(fp2);

	return 0;
}

2、将c语言编写的源文件(.c文件)的每行前加上行号并删除其所有注释。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
//	初始化变量  
	FILE*fp1,*fp2;
	char f1[10],f2[10];
	char s1[500],t1[500],t2[500];	
	int i=0,j=0,p=1,n=0,count=0;
//	打开文件 
	if((fp1=fopen("former_test.cpp","r"))==NULL)
	{
		printf("cannot open file1\n");
		return 0;
	}
	
	if((fp2=fopen("now_test.cpp","w"))==NULL)
	{
		printf("cannot open file2\n");
		return 0;
	}
	
// 读取内容 
	while( !feof(fp1) )
	{
//		按行读取 
		count = 1;
		fgets(s1, 199, fp1);
		
		n = strlen(s1);
//		先去注释,再加行号 
		for( i = 1; i < n; i++ )
		{
//			分别考虑单行注释与多行注释 
			if( s1[i] == '/' && s1[i-1] == '/' )
			{
				s1[i-1] = '\n';
				s1[i] = '\0';
//				每次去除注释后,利用fprint加行号 
				fprintf(fp2,"/*""%d""*/",p++);
//				将处理后的字符写入目标文件 
				fputs( s1, fp2 );
				count = 0;
				break;
			}
			if( s1[i] == '*' && s1[i-1] == '/' )
			{
				s1[i-1] = '\n';
				s1[i] = '\0';
				fprintf(fp2,"/*""%d""*/",p++);
				fputs( s1, fp2 );
				count = 0;
				break;
			}
			if( s1[i] == '/' && s1[i-1] == '*' )
			{
				
				for( j = 0; j < n-i; j++ )
					s1[j] = s1[ j + i + 1];
				
				s1[j] = '\0';
				fprintf(fp2,"/*""%d""*/",p++);
				fputs( s1, fp2 );
				count = 0;
				break;
			}
		}
		if(count)
			fprintf(fp2,"/*""%d""*/",p++);
			fputs(s1, fp2);	
	}

//	关闭文件 
	fclose(fp1);
	fclose(fp2);

	printf("ok\n");
	return 0;	 
}


第二次上机:

要求:
1、文本文件num1.txt和num2.txt中各有一组用空格分隔的整数,将num1.txt和num2.txt联合排序,并将结果保存在num3.txt中

#include<stdio.h>

int main()
{
	FILE *fp1=fopen("num1.txt","r");
	FILE *fp2=fopen("num2.txt","r");
	FILE *fp3=fopen("num3.txt","w");
	
	if(fp1==NULL||fp2==NULL||fp3==NULL)
	{
		printf("cannot open file\n");
		return 0;
	}
	
	int num[1000];
	int count=0;
	
	while(!feof(fp1))
	{
		fscanf(fp1,"%d",&num[count]);
		count++;
	}
	while(!feof(fp2))
	{
		fscanf(fp2,"%d",&num[count]);
		count++;
	}
	
	int temp=0;
	for (int i = 0; i < count-1; ++i)
	{
		for (int j = 0; j < count-1-i ; ++j)
		{
			if (num[j]>num[j+1])
			{
				temp=num[j+1];
				num[j+1]=num[j];
				num[j]=temp;
			}
		}
	}

	
	int index=0;
	while(index<count)
	{
		fprintf(fp3,"%d ",num[index]);
		index++;
	}
	
	fclose(fp1);
	fclose(fp2);
	fclose(fp3);
	
	printf("ok\n");

}

2、统计一个英文文本文件中26个英文字母出现次数并按英文字母序输出统计结果,查找并替换此英文文本文件中某字符串。

#include <stdio.h>
#include <string.h>
#include <ctype.h>
void strreplace(char * Src,char * From,char * To);
int main(int argc, char const *argv[])
{
	//统计一个英文文本文件中26个英文字母出现次数并按英文字母序输出统计结果
//	打开文件 
	FILE *f,*tf;
	f=fopen("English.txt","r+");
	tf=fopen("temp.txt","w+");
	if(f==NULL||tf==NULL)
	{
		printf("cannot open file\n");
		return 0;
	}
	
//	利用ascii码进行字母统计 
 
	int character_num[26]={0,};
	char str[1000];
	char temp[80];
	int a=0;
	while(!feof(f)) {
		fgets(temp,80,f);
		strcat(str,temp);
	}

	int n=strlen(str);
	for (int i = 0; i < n; ++i)
	{
		if (isalpha(str[i]))
		{
			tolower(str[i]);
			character_num[(str[i]-97)]++;
		}
	}
//	输出统计结果 
	char character='a';
	printf("the result:\n");
	for (int i = 0; i < 26; ++i)
	{
		printf("%c:%d ",character,character_num[i]);
		character++;
		if ((i+1)%5==0)
		{
			printf("\n");
		}
	}

	fclose(f);
    fclose(tf);

// 查找并替换此英文文本文件中某字符串

	char buf[1024]={0},from[20],to[20];

	printf("Input string you shall find:");
    scanf("%s",from); 
    printf("Input string you shall replace to:");
    scanf("%s",to);

// 把替换的串重新从English.txt读出、写入新文件temp.txt
    
   
    f=fopen("English.txt","r+");
	tf=fopen("temp.txt","w+");
	if(f==NULL||tf==NULL)
	{
		printf("cannot open file\n");
		return 0;
	}

     while(!feof(f))
    {
        fgets(buf,sizeof(buf),f);
        strreplace(buf,from,to);  //每一行都查找、替换目标串
        fputs(buf,tf);
        memset(buf,0,sizeof(buf));
     }

    fclose(f);
    fclose(tf);
    

//     // 把temp.txt内容写回English.txt
//    f=fopen("English.txt","w+");
//	tf=fopen("temp.txt","r+");
//	if(f==NULL||tf==NULL)
//	{
//		printf("cannot open file\n");
//		return 0;
//	}
//
//     while(!feof(tf))
//    {        
//    	fgets(buf,sizeof(buf),tf);
//        fputs(buf,f); 
//        memset(buf,0,sizeof(buf));
//    }    
//
//    fclose(f);
//    fclose(tf);
	printf("ok"); 
	return 0;
}

//自定义的字符串替换函数(所有出现的匹配串都替换为目标串)
void strreplace(char * Src,char * From,char * To){
     int length_to,length_from;
     char *sf,*se,endBuf[1024]={0};
     length_from = strlen(From);
     length_to = strlen(To);
/*
1.C 库函数 char *strstr(const char *haystack, const char *needle) 
在字符串 haystack 中查找第一次出现字符串 needle 的位置,不包含终止符 '\0';
2.C 库函数 char *strcpy(char *dest, const char *src) 把 src 所指向的字符串复制到 dest。
是把第二个字符串覆盖到第一个字符串上
3.C 库函数 void *memset(void *str, int c, size_t n) 
复制字符 c(一个无符号字符)到参数 str 所指向的字符串的前 n 个字符。
4.C 库函数 void *memcpy(void *str1, const void *str2, size_t n) 
从存储区 str2 复制 n 个字节到存储区 str1,覆盖原有部分数据
*/ 
     while(sf = strstr(Src,From)){
//     	 sf获得目标字符串的首地址 
         if(sf == NULL)
             break;
         else
         {
//         	se获得末尾地址 ,也就是目标字符串后面字符的首地址 
             se = sf + length_from;
             strcpy(endBuf,se);
//             重置se 
             memset(se,0,strlen(endBuf));
//             正式替换字符串 
             memcpy(sf,To,length_to);
             memcpy(sf+length_to,endBuf,strlen(endBuf));
//             重置endBuf 
             memset(endBuf,0,strlen(endBuf));
         }
     }
    
 }

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值