最差劲最凌乱的一个小程序,给自己一个教训-Linux下C语言

前两天写了一个对txt里的圣经进行处理,然后最后通过命令输出圣经的字符数,行数,单词书,第一天晚上毫无思路,各种报错,归根结底是因为对二位数组和结构体的返回值以及对结构体分配内存的不熟,各种报错,差点就失去了自信,而且自己并不是什么都会,这不就是一个例子吗,不要再自满了,谦逊的学习,一步一个脚印的学习,总有一天会成功的,加油把,去掉浮躁,去掉不屑,用心专研。


贴上自己的代码,通过这个凌乱的代码 还有点BUG的代码给自己的一个深刻的教训。

#ifndef ANDREW
#define ANDREW


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
// #include <sys/types.h>
// #include <sys/stat.h>
// //#include <fcntl.h>

#define MAX_TXT_LINE  40000
#define MAX_SPACE 70000

typedef struct tagw
{
	char str[32];
	int count;//µ¥´Ê´ÊƵ£¬ÓÃÓÚÅÅÐò
}Txtw,*Ptxtw;
typedef struct tag
{
	int word_n;
	int char_n;
	int line_n;
	char *txt[MAX_TXT_LINE];

}Txt,*Ptxt;

int New_Bible();//¸Ä±äÊ¥¾­£¬·µ»ØÐÐÊý
Ptxtw  Word_num();//¹¹Ôìµ¥´ÊÁ´±í
Ptxt Line_num();//¹¹ÔìÐжþάÁ´±í
void Search_string(Ptxt all,char str[]);
void Delete(char *str);
void Sort_num(Ptxtw p,int top);
int word_comp(const void *left,const void *right);
#endif 

#include "nevergiveup.h"
int New_Bible()
{
	int line=0;
	char ch;
	FILE *fp;
	FILE *nfp;
	fp=fopen("/home/liubin/0623/New Bible/The_Holy_Bible.txt","rb");  
	nfp=fopen("/home/liubin/0623/New Bible/New_Bible.txt","wb");   
	while(fp==NULL)
	{
		printf("Open Error!\n");
		exit(0);
	}
	while(nfp==NULL)
	{
		printf("Write Error!\n");
		exit(0);
	}
	while((ch=fgetc(fp))!=EOF)     
	{
		if(ch>='A'&&ch<='Z')
		{
			ch=ch-'A'+'a';
		}
		else if(ispunct(ch))    
		{
			ch=' ';
		}
		if(ch=='\n')
		{
			line++;
		}
		fputc(ch,nfp);    
	}
	fclose(fp);
	fclose(nfp);
	//printf("the line = %d",line);
	return 0;
}
//¹¹ÔìÐжþάÊý×é
Ptxt Line_num()
{
	int i=0,j;
	char buf[1500];
	Ptxt all;
	FILE *fp;
	all=(Ptxt)calloc(1,sizeof(Txt));
	fp=fopen("/home/liubin/0623/New Bible/New_Bible.txt","rb");
	while(fp==NULL)
	{
		printf("Open Error!\n");
		exit(0);
	}

	while(memset(buf,0,1024),fgets(buf,1024,fp)!=NULL)
	{
		for(j=0;buf[j]!='\n';++j)
		{
			if(buf[j]!=' '&&buf[j+1]==' ')
			{
				all->word_n++;
			}
			if((buf[j]!=' '))
			{
				all->char_n++;
			}
		}
		
		all->txt[i]=(char *)malloc(1024*sizeof(char));
		strcpy(all->txt[i],buf);
		i++;
		all->line_n++;
	}
// 	for(i=0;all->txt[i]!='\0';i++)
// 	{
// 		printf("%s\n",all->txt[i]);
// 	}
//	printf("all.char:%d\n",all->char_n);
//	printf("all.line:%d\n",all->line_n);
//	printf("all.word:%d\n",all->word_n);
	fclose(fp);
	return all;
}
void Search_string(Ptxt all,char str[])
{
	int i=0;
	while(all->txt[i]!='\0')
	{
		if(strstr(all->txt[i],str)!=NULL)
		{
			printf("%s\n",all->txt[i]);
		}
		i++;
	}

}
Ptxtw Word_num()
{
	int i;
	char buf[32];
	Ptxtw p;
	Ptxtw word;
	FILE *fp;
	word=(Ptxtw)malloc(MAX_SPACE*sizeof(Txtw));
	memset(word,0,MAX_SPACE);
	fp=fopen("/home/liubin/0623/New Bible/New_Bible.txt","rb");
	while(fp==NULL)
	{
		printf("Open Error!\n");
		exit(0);
	}
	while(fscanf(fp,"%s",buf)!=EOF)
	{
		//printf("%s\n",buf);
		//printf("%s\n",word[i].str);
		for(i=0;word[i].count!=0;i++)   //ÅжϲåÈë
		{
			if(strcmp(word[i].str,buf)==0)
			{

				word[i].count++;
				break;
			}
		}
		if(word[i].count==0)
		{
			strcpy(word[i].str,buf);
			word[i].count=1;
		}
		
	}
	for(i=0;word[i].count!=0;i++)
	{
		printf("%s    %d    ",word[i].str,word[i].count);
	}
	return word;
}
void Delete(char *str)
{
	int i,j;
	for(i=-1,j=0;j<strlen(str);j++)
	{
		if(str[j]!=' ')
		{
			if(str[j]>='A' && str[j]<='Z')
			{
				str[++i]=str[j]+32;
			}
			else
			{
				str[++i]=str[j];
			}
		}
		else
		{
			if(i!=-1 && str[i]!=' ')
			{
				str[++i]=' ';
			}
		}
	}
	for(;i>=0;i--)
	{
		if(str[i]!=' ')
		{
			break;
		}
	}
	str[++i]='\0';
}
void Sort_num(Ptxtw word,int top)
{
	int i,j,num=0;
	Txtw temp;
	for(i=0;word[i].count!=0;++i)
	{
		num++;
	}

	for(i=0;i<num-1;i++)
	{
		for(j=0;j<num-i-1;j++)
		{
			if(word[i].count>word[j].count)
			{
				temp=word[i];
				word[i]=word[j];
				word[j]=temp;

			}
		}

	}
//	qsort(word[0].count,word[j].count,sizeof(Txtw),word_comp);
//	for(i=0;i<=top;i++)
//	{
//		printf("%s  %d\n ",word[i].str,word[i].count);
//	}
	
}
int word_comp(const void* left,const void* right)
{
	Ptxtw word_left,word_right;
	word_left=(Ptxtw)left;
	word_right=(Ptxtw)right;
	int count_left=word_left->count;
	int count_right=word_right->count;
	if(count_left > count_right)
	{
		return -1;
	}
	else if(count_left == count_right)
	{
		return 0;
	}
	else
	{
		return 1;
	}

}

#include "nevergiveup.h"
int main(int argc,char *argv[])
{
	char com[50];
	char str[1024];
	//Ptxtw first;
	Ptxt all;
	Ptxtw word;
	//New_Bible();
	//first=Word_num();
	all=Line_num();
	//scanf("%s",str);
	//Search_string(all,str);
	word=Word_num();
	printf("input the commod:\n");
	fflush(stdin);
	fgets(com,50,stdin);
	printf("%s",com);
	Delete(com);
	printf("%s",com);
	if(strcmp(com,"wc -l")==0)
	{
		printf("the line is %d",all->line_n);
	}
	else if(strcmp(com,"wc -w")==0)
	{
		printf("the word is %d",all->word_n);
	}
	else if(strcmp(com,"wc -c")==0)
	{
		printf("the char is %d",all->char_n);
	}
	printf("over\n");
	Sort_num(word,10);
	return 0;

}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值