[ create a new paste ] login | about

Link: http://codepad.org/4QHzOBw8    [ raw code | output | fork ]

C, pasted on Jul 17:
#include <stdio.h> //i/o
#include <string.h>//strpbrk
#include <stdlib.h>//malloc free

// ñëó÷àå åñëè â ñòðîêå åñòü äâóçíà÷íûå ÷èñëà
//âîçâðàùàåìîå çíà÷åíèå 1 èíà÷å 0
int isStrHas2DigitValues(char * str);

int main()
{
	int ncount  = 0;//Áóäåò ñîäåðæàòü ÷èñëî ñòðîê áåç äâóçíà÷í ÷èñåë
	int length  = 0;//Áóäåò ñîäåðæàòü äëèííó ôàéëà
	char delim[]= "\n";//Ñèìâîë ïåðåâîäà ñòðîêè äëÿ strtok
	char * text = NULL;//Áóôôåð äëÿ òåêñòà â ôàéëå
	char * line = NULL;//Óêàçàòåëü íà ñòðîêó
	FILE * file = NULL;//Óêàçàòåëü íà ôàéëîâûé ïîòîê
	file = fopen("input.txt","rb");//Ïðîáóåì îòêðûòü ôàéë äëÿ ÷òåíèÿ
	if(file == NULL)
		printf("Error open input.txt\n");
	else
	{
		//Óçíà¸ì äëèííó ôàéëà
		fseek(file,0,SEEK_END);
		length = ftell(file);
		fseek(file,0,SEEK_SET);

		//Âûäåëÿåì ïàìÿòü ïîä ñîæåðæèìîå ôàéëà
		text = (char *)malloc((1 + length)*sizeof(char));
		if(text == NULL)
			printf("Allocation memory error\n");
		else
		{
			if(!fread(text,1,length,file))
				printf("Error read input.txt\n");
			else
				text[length] = '\0';//Óáèðàåì ìóñîð âêîíöå ñòðêîè
			//Âûäåëÿåì èç òåêñòà ôàéëà ñòðîêè
			for
			(
				line = strtok(text,delim); 
				line != NULL; 
				line = strtok(NULL,delim)
			)
			{
				//Ïðîâåðêà íå ñîäåðæèò ëè ñòðîêà 2-íûõ
				if(!isStrHas2DigitValues(line))
				{
					printf("%s\n", line);//Ïå÷àòàåì ñòðîêó
					ncount = ncount + 1;//Óâåëè÷èâàåì ñ÷¸ò÷èê ñòðîê
				}
			}
			printf("Text contain %d lines with absent 2dig values\n",ncount);
		}
		fclose(file);
	}
	printf("Press any key to continue\n");
	getchar();
	return 0;
}

int isStrHas2DigitValues(char * str)
{
	int flag     = 0;
	int value    = 0;//Áóäåò ñîåðàæòü ÷èñëî
	int length   = 0;//Áóäåò ñîäåðæàòü äëèííó str
	char delim[] = "0123456789";
	char * pch   = NULL;//Áóäåò ñîäåðæàòü ÷èñëî
	char * buf   = NULL;//áóôôåð äëÿ str
	if(str != NULL)
	{
		length = strlen(str);
		//âûäåëÿåì ïàìÿòü äëÿ áóôôåðà ñòðîêè
		buf = (char *)malloc((1 + length)*sizeof(char));
		if(buf == NULL)
			printf("Allocation memory error\n");
		else
			strcpy(buf,str);//Êîïèðóåì str â buf
		if(buf != NULL)
		{
			for
			(
				pch = strpbrk(buf,delim); 
				pch != NULL && flag == 0; 
				pch = strpbrk(pch + 1,delim)
			)
			{
				//Ïîëó÷àåì ÷èñëî èç ñòðêîè
				sscanf(pch,"%d",&value);
				//Ïðîâåðêà äâóçíà÷íîå ëè ÷èñëî
				if(9 < value && value < 100)
					flag = 1;
			}
		}
	}
	return flag;
}


Output:
1
2
Error open input.txt
Press any key to continue


Create a new paste based on this one


Comments: