きまぐれ日記

きまぐれで趣味のこといろいろ書きます(*´∀`*)

カテゴリ:プログラミング > C言語

/*	program		: array.c
	last updated	: 2018/07/14		*/

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdbool.h"
#include "ctype.h"

#define	or		||
#define	and		&&

#define	BOLD	printf("\x1b[1m")
#define	RESET	printf("\x1b[0m")

#define	NEW_LINE	printf("\n")

#define	MNOC	128		// 英単語の最大文字数 maximum number of character

const char EOF_STR[] = "***END***";
char	g_STR[MNOC];
int		g_CNT = 0;
bool	g_FLAG = false;

struct _Array{
	char word[MNOC];		// 英単語
	int count;	// 頻出頻度
} Array[10000];

/**************************************************************************************
 *	SpecialRemove
 *				--	英単語の最後の文字がアルファベット,数字以外であればそれを除く
 *	パラメータ
 *		str		-- 英単語
 *		len		-- 英単語の文字列長
 *
 *	補足
 *		isalpha	-- 文字がアルファベットか判定		ヘッダファイル:ctype.h
 *		isdigit	-- 文字が数字か判定				ヘッダファイル:ctype.h
 **************************************************************************************/

void SpecialRemove(char *str){
	int len = strlen(str);
	if (strcmp(str,EOF_STR) == 0) return;
	if (isalpha(str[len-1]) or isdigit(str[len-1])) return;
	else str[len-1] = '\0';
}

// データ入力関数
void Input(){
	scanf("%s",g_STR);
	SpecialRemove(g_STR);
}

// データ出力関数
void Output(){
	int i = 0;
	BOLD;
	while ( i < g_CNT ) {
		printf("%3d\t%s\n", Array[i].count, Array[i].word);
		i++;
	}
	RESET;
}

// 入力した単語がすでに既存しているか判定
int Compare(){
	int i = 0;
	while ( i < g_CNT ) {
		if (strcmp(g_STR, Array[i].word) ==0) {
			g_FLAG = true;
			return i;
		}
	}
	return false;
}

// メインループ
void MainLoop(){
	int num;
	while (strcmp(g_STR,EOF_STR) != 0) {
		num = Compare();
		if (g_FLAG == true) {
			Array[num].count++;
			g_FLAG = false;
		} else {
			strcpy(Array[g_CNT].word, g_STR);
			Array[g_CNT].count = 1;
			g_CNT++;
		}
		Input();
	}
}

// メイン関数
int main(void){
	Input();
	if (strcmp(g_STR,EOF_STR) == 0) exit(0);
	MainLoop();
	
	NEW_LINE;
	Output();
	
	exit(EXIT_SUCCESS);
}


入力データ ( data.txt として保存 ) 終了判定文字列は「 ***END*** 」


Until about a decade ago, American newspapers had a clear idea what the "news" was. It was what the prime ministers and presidents of leading countries announced at press conferences. It was politics and diplomacy and cabinet shuffles. It was economic statistics and business mergers and Wall Street. But over the last dozen years, there has been a growing movement in American newspapers away from that conception of news and toward something looser, more feature-oriented, and more trend-driven. Max Frankel, who ruled as executive editor of the New York Times from 1986 through 1994 and thus helped set the journalistic agenda, pioneered the concept of soft features on the front page. Frankel wanted newspapers to be read, and he argued that articles about country music and miniskirts were not only engaging but also reflected important trends: Many readers probably cared more about hemlines than about arms-control talks. Under Frankel, the Times also began to search for new and compelling ways to cover foreign affairs in the post-Cold War world. Editors felt that politics mattered less than before, and that correspondents should write increasingly about the people of foreign countries rather than just the governments. Correspondents were pushed to spend less time in the national capitals talking to cabinet ministers and more time in rural areas talking to farmers and homemakers. There was more attention to trends; indeed, Frankel emphasized that "sometimes the most important news doesn't happen on any one day." Other newspapers, particularly the Washington Post and Los Angeles Times, pursued a similar approach. They all beefed up their science and medical reporting, and soon nobody blinked when stories about health or fitness―Does vitamin C prevent colds? Does weightlifting help shed pounds as well as running does?―were promoted to the front page. The Wall Street Journal led the way in pioneering skeptical reporting about corporations, and soon all major newspapers were assigning investigative reporters to look at corporate executives as well as politicians. Yet critics complained that while newspapers were becoming perhaps more readable, they were also becoming somewhat dispensable. An article about vitamin C might be engaging, but it did not matter if you missed it. Front pages were becoming clogged with so many articles about fascinating people that it was difficult to tell what, if anything, had happened that was truly of any consequence. Moreover, as the Internet joined television in providing the public with an alternative source of information, many young people simply spurned newspapers altogether. The debate remains unresolved. Newspaper executives do worry that their relevancy may decrease as they reduce their coverage of hard news, but they say that in a world of 24-hour news on the Internet and cable television, they simply cannot compete effectively at being the first to inform the public of the latest events. They must take a different approach. ***END***




実行結果


user$ gcc array.c
user$ ./a.out < data.txt

1 Until
9 about
6 a
1 decade
1 ago
2 American
7 newspapers
2 had
1 clear
1 idea
3 what
22 the
1 "news
7 was
3 It
1 prime
2 ministers
19 and
1 presidents
10 of
1 leading
2 countries
1 announced
3 at
1 press
1 conferences
2 politics
1 diplomacy
2 cabinet
1 shuffles
1 economic
1 statistics
1 business
1 mergers
2 Wall
2 Street
1 But
1 over
1 last
1 dozen
1 years
1 there
1 has
1 been
1 growing
1 movement
7 in
1 away
2 from
10 that
1 conception
4 news
1 toward
1 something
1 looser
6 more
1 feature-oriented
1 trend-driven
1 Max
4 Frankel
1 who
1 ruled
7 as
1 executive
1 editor
1 New
1 York
3 Times
1 1986
1 through
1 1994
1 thus
1 helped
1 set
1 journalistic
1 agenda
1 pioneered
1 concept
1 soft
1 features
3 on
2 front
2 page
1 wanted
11 to
2 be
1 read
1 he
1 argued
2 articles
1 country
1 music
1 miniskirts
6 were
2 not
1 only
2 engaging
3 but
3 also
1 reflected
2 important
2 trends
1 Many
1 readers
1 probably
1 cared
1 hemlines
3 than
1 arms-control
1 talks
1 Under
1 began
1 search
1 for
1 new
1 compelling
1 ways
1 cover
2 foreign
1 affairs
1 post-Cold
1 War
2 world
1 Editors
1 felt
1 mattered
2 less
1 before
1 correspondents
1 should
1 write
1 increasingly
3 people
1 rather
1 just
1 governments
1 Correspondents
1 pushed
1 spend
2 time
1 national
1 capitals
2 talking
1 rural
1 areas
1 farmers
1 homemakers
1 There
1 attention
1 indeed
1 emphasized
1 "sometimes
1 most
1 doesn't
1 happen
2 any
1 one
1 day.
1 Other
1 particularly
1 Washington
1 Post
1 Los
1 Angeles
1 pursued
1 similar
2 approach
2 They
2 all
1 beefed
1 up
3 their
1 science
1 medical
2 reporting
2 soon
1 nobody
1 blinked
1 when
1 stories
1 health
1 or
1 fitness―Does
2 vitamin
2 C
1 prevent
1 colds
1 Does
1 weightlifting
1 help
1 shed
1 pounds
2 well
1 running
1 does?―were
1 promoted
2 The
1 Journal
1 led
1 way
1 pioneering
1 skeptical
1 corporations
1 major
1 assigning
1 investigative
1 reporters
1 look
1 corporate
2 executives
1 politicians
1 Yet
1 critics
1 complained
1 while
3 becoming
1 perhaps
1 readable
4 they
1 somewhat
1 dispensable
1 An
1 article
1 might
3 it
1 did
1 matter
2 if
1 you
1 missed
1 Front
1 pages
1 clogged
2 with
1 so
2 many
1 fascinating
1 difficult
1 tell
1 anything
1 happened
1 truly
1 consequence
1 Moreover
2 Internet
1 joined
2 television
1 providing
2 public
1 an
1 alternative
1 source
1 information
1 young
2 simply
1 spurned
1 altogether
1 debate
1 remains
1 unresolved
1 Newspaper
1 do
1 worry
1 relevancy
1 may
1 decrease
1 reduce
1 coverage
1 hard
1 say
1 24-hour
1 cable
1 cannot
1 compete
1 effectively
1 being
1 first
1 inform
1 latest
1 events
1 must
1 take
1 different



/* program      : liner_list.c
   cording by   : utf-8
   last updated : 2018/07/09  */

#include "stdio.h"
#include "string.h"
#include "stdlib.h"
#include "stdbool.h"
#include "ctype.h"

#define NEW_LINE printf("\n")
#define N 32

const char EOF_STR[] = "***END***";		// 終了判定文字列
char gSTR[N];		// メイン関数以外で操作する為、グローバル変数にしている


typedef struct _cell {
	char array[N];			// この構造体の英単語
	int count;				// この構造体の英単語の頻出頻度
	struct _cell *next;	    // nextは_cellの子
} cell;


/*	liner	: データ格納用
	ptr		: データ比較用
	root	: 親			*/

cell *liner, *ptr, *root;

/**********************************************************************************
 *	Special_remove	-- 英単語の最後の文字が特殊文字であれば除く
 *
 *	パラメータ
 *			str			-- 英単語
 *			len			-- 英単語の文字数
 *
 *	補足
 *		isalpha		-- 文字がアルファベットかを判別する関数
 *				真 : アルファベット
 *				偽 : アルファベットでない
 **********************************************************************************/

void Special_remove(char *str){
	int len = strlen(str);
	
	if (strcmp(str,EOF_STR) != 0)
	if (!isalpha(str[len-1])) str[len-1] = '\0';
}

// 線形リストを標準出力する
void LinerPrint(){
	liner = root;		// linerを初期化
	while (liner != NULL) {
		printf("count >%3d\tword > %s\n", liner -> count, liner -> array);
		liner = liner -> next;
	}
}

// 入力、特殊文字を除去
void InputWord(){
	scanf("%s",gSTR);
	Special_remove(gSTR);
}

// メイン関数
int main(void) {
	bool flag = true;
	
	InputWord();
	
	liner = malloc(sizeof(cell));
	root = liner;
	ptr = liner;
	
	liner -> count = 1;
	strcpy(liner -> array, gSTR);
	liner -> next = NULL;
	
	InputWord();
	
	// 終了判定文字列と一致した場合ブレーク
	while (strcmp(gSTR,EOF_STR) != 0) {
		while (ptr != NULL) {
			if (strcmp(gSTR, ptr -> array) == 0) {
				ptr -> count++;
				flag = false;
				break;
			}
			ptr = ptr -> next;
		}
		
		if (flag == true) {
			liner -> next = malloc(sizeof(cell));
			liner = liner -> next;
			liner -> count = 1;
			strcpy(liner -> array, gSTR);
		}
		
		ptr = root;		// ptrを初期化
		flag = true;
		
		InputWord();
	}
	
	NEW_LINE;
	LinerPrint();
	
	return EXIT_SUCCESS;
}


入力データ ( data.txt として保存 ) 終了判定文字列は「 ***END*** 」


Until about a decade ago, American newspapers had a clear idea what the "news" was. It was what the prime ministers and presidents of leading countries announced at press conferences. It was politics and diplomacy and cabinet shuffles. It was economic statistics and business mergers and Wall Street. But over the last dozen years, there has been a growing movement in American newspapers away from that conception of news and toward something looser, more feature-oriented, and more trend-driven.
Max Frankel, who ruled as executive editor of the New York Times from 1986 through 1994 and thus helped set the journalistic agenda, pioneered the concept of soft features on the front page. Frankel wanted newspapers to be read, and he argued that articles about country music and miniskirts were not only engaging but also reflected important trends: Many readers probably cared more about hemlines than about arms-control talks. 
Under Frankel, the Times also began to search for new and compelling ways to cover foreign affairs in the post-Cold War world. Editors felt that politics mattered less than before, and that correspondents should write increasingly about the people of foreign countries rather than just the governments. Correspondents were pushed to spend less time in the national capitals talking to cabinet ministers and more time in rural areas talking to farmers and homemakers. There was more attention to trends; indeed, Frankel emphasized that "sometimes the most important news doesn't happen on any one day."
Other newspapers, particularly the Washington Post and Los Angeles Times, pursued a similar approach. They all beefed up their science and medical reporting, and soon nobody blinked when stories about health or fitness―Does vitamin C prevent colds? Does weightlifting help shed pounds as well as running does?―were promoted to the front page. The Wall Street Journal led the way in pioneering skeptical reporting about corporations, and soon all major newspapers were assigning investigative reporters to look at corporate executives as well as politicians. 
Yet critics complained that while newspapers were becoming perhaps more readable, they were also becoming somewhat dispensable. An article about vitamin C might be engaging, but it did not matter if you missed it. Front pages were becoming clogged with so many articles about fascinating people that it was difficult to tell what, if anything, had happened that was truly of any consequence. Moreover, as the Internet joined television in providing the public with an alternative source of information, many young people simply spurned newspapers altogether. 
The debate remains unresolved. Newspaper executives do worry that their relevancy may decrease as they reduce their coverage of hard news, but they say that in a world of 24-hour news on the Internet and cable television, they simply cannot compete effectively at being the first to inform the public of the latest events. They must take a different approach. ***END***




実行結果


user$ gcc liner_list.c
user$ ./a.out < data.txt

count >  1 word > Until
count >  9 word > about
count >  6 word > a
count >  1 word > decade
count >  1 word > ago
count >  2 word > American
count >  7 word > newspapers
count >  2 word > had
count >  1 word > clear
count >  1 word > idea
count >  3 word > what
count > 22 word > the
count >  1 word > "news
count >  7 word > was
count >  3 word > It
count >  1 word > prime
count >  2 word > ministers
count > 19 word > and
count >  1 word > presidents
count > 10 word > of
count >  1 word > leading
count >  2 word > countries
count >  1 word > announced
count >  3 word > at
count >  1 word > press
count >  1 word > conferences
count >  2 word > politics
count >  1 word > diplomacy
count >  2 word > cabinet
count >  1 word > shuffles
count >  1 word > economic
count >  1 word > statistics
count >  1 word > business
count >  1 word > mergers
count >  2 word > Wall
count >  2 word > Street
count >  1 word > But
count >  1 word > over
count >  1 word > last
count >  1 word > dozen
count >  1 word > years
count >  1 word > there
count >  1 word > has
count >  1 word > been
count >  1 word > growing
count >  1 word > movement
count >  7 word > in
count >  1 word > away
count >  2 word > from
count > 10 word > that
count >  1 word > conception
count >  4 word > news
count >  1 word > toward
count >  1 word > something
count >  1 word > looser
count >  6 word > more
count >  1 word > feature-oriented
count >  1 word > trend-driven
count >  1 word > Max
count >  4 word > Frankel
count >  1 word > who
count >  1 word > ruled
count >  7 word > as
count >  1 word > executive
count >  1 word > editor
count >  1 word > New
count >  1 word > York
count >  3 word > Times
count >  1 word > 198
count >  1 word > through
count >  1 word > 199
count >  1 word > thus
count >  1 word > helped
count >  1 word > set
count >  1 word > journalistic
count >  1 word > agenda
count >  1 word > pioneered
count >  1 word > concept
count >  1 word > soft
count >  1 word > features
count >  3 word > on
count >  2 word > front
count >  2 word > page
count >  1 word > wanted
count > 11 word > to
count >  2 word > be
count >  1 word > read
count >  1 word > he
count >  1 word > argued
count >  2 word > articles
count >  1 word > country
count >  1 word > music
count >  1 word > miniskirts
count >  6 word > were
count >  2 word > not
count >  1 word > only
count >  2 word > engaging
count >  3 word > but
count >  3 word > also
count >  1 word > reflected
count >  2 word > important
count >  2 word > trends
count >  1 word > Many
count >  1 word > readers
count >  1 word > probably
count >  1 word > cared
count >  1 word > hemlines
count >  3 word > than
count >  1 word > arms-control
count >  1 word > talks
count >  1 word > Under
count >  1 word > began
count >  1 word > search
count >  1 word > for
count >  1 word > new
count >  1 word > compelling
count >  1 word > ways
count >  1 word > cover
count >  2 word > foreign
count >  1 word > affairs
count >  1 word > post-Cold
count >  1 word > War
count >  2 word > world
count >  1 word > Editors
count >  1 word > felt
count >  1 word > mattered
count >  2 word > less
count >  1 word > before
count >  1 word > correspondents
count >  1 word > should
count >  1 word > write
count >  1 word > increasingly
count >  3 word > people
count >  1 word > rather
count >  1 word > just
count >  1 word > governments
count >  1 word > Correspondents
count >  1 word > pushed
count >  1 word > spend
count >  2 word > time
count >  1 word > national
count >  1 word > capitals
count >  2 word > talking
count >  1 word > rural
count >  1 word > areas
count >  1 word > farmers
count >  1 word > homemakers
count >  1 word > There
count >  1 word > attention
count >  1 word > indeed
count >  1 word > emphasized
count >  1 word > "sometimes
count >  1 word > most
count >  1 word > doesn't
count >  1 word > happen
count >  2 word > any
count >  1 word > one
count >  1 word > day.
count >  1 word > Other
count >  1 word > particularly
count >  1 word > Washington
count >  1 word > Post
count >  1 word > Los
count >  1 word > Angeles
count >  1 word > pursued
count >  1 word > similar
count >  2 word > approach
count >  2 word > They
count >  2 word > all
count >  1 word > beefed
count >  1 word > up
count >  3 word > their
count >  1 word > science
count >  1 word > medical
count >  2 word > reporting
count >  2 word > soon
count >  1 word > nobody
count >  1 word > blinked
count >  1 word > when
count >  1 word > stories
count >  1 word > health
count >  1 word > or
count >  1 word > fitness―Does
count >  2 word > vitamin
count >  2 word > C
count >  1 word > prevent
count >  1 word > colds
count >  1 word > Does
count >  1 word > weightlifting
count >  1 word > help
count >  1 word > shed
count >  1 word > pounds
count >  2 word > well
count >  1 word > running
count >  1 word > does?―were
count >  1 word > promoted
count >  2 word > The
count >  1 word > Journal
count >  1 word > led
count >  1 word > way
count >  1 word > pioneering
count >  1 word > skeptical
count >  1 word > corporations
count >  1 word > major
count >  1 word > assigning
count >  1 word > investigative
count >  1 word > reporters
count >  1 word > look
count >  1 word > corporate
count >  2 word > executives
count >  1 word > politicians
count >  1 word > Yet
count >  1 word > critics
count >  1 word > complained
count >  1 word > while
count >  3 word > becoming
count >  1 word > perhaps
count >  1 word > readable
count >  4 word > they
count >  1 word > somewhat
count >  1 word > dispensable
count >  1 word > An
count >  1 word > article
count >  1 word > might
count >  3 word > it
count >  1 word > did
count >  1 word > matter
count >  2 word > if
count >  1 word > you
count >  1 word > missed
count >  1 word > Front
count >  1 word > pages
count >  1 word > clogged
count >  2 word > with
count >  1 word > so
count >  2 word > many
count >  1 word > fascinating
count >  1 word > difficult
count >  1 word > tell
count >  1 word > anything
count >  1 word > happened
count >  1 word > truly
count >  1 word > consequence
count >  1 word > Moreover
count >  2 word > Internet
count >  1 word > joined
count >  2 word > television
count >  1 word > providing
count >  2 word > public
count >  1 word > an
count >  1 word > alternative
count >  1 word > source
count >  1 word > information
count >  1 word > young
count >  2 word > simply
count >  1 word > spurned
count >  1 word > altogether
count >  1 word > debate
count >  1 word > remains
count >  1 word > unresolved
count >  1 word > Newspaper
count >  1 word > do
count >  1 word > worry
count >  1 word > relevancy
count >  1 word > may
count >  1 word > decrease
count >  1 word > reduce
count >  1 word > coverage
count >  1 word > hard
count >  1 word > say
count >  1 word > 24-hour
count >  1 word > cable
count >  1 word > cannot
count >  1 word > compete
count >  1 word > effectively
count >  1 word > being
count >  1 word > first
count >  1 word > inform
count >  1 word > latest
count >  1 word > events
count >  1 word > must
count >  1 word > take
count >  1 word > different

↑このページのトップヘ