본문 바로가기
학부공부/C_Basic

9. Struct[C Basic]

by sonpang 2021. 11. 7.
반응형
* 2
char text[MAX][80];
char *temp = NULL;
gets(&text[i][0]);
for (i = 0;i < MAX;i++) {
	strcpy(std[i].name, strtok(&strtok(&text[i][], "/")));
	atoi()이용하여 stdnum에 저장
}
scanf("%s / %d", &korea_university[i].name, &korea_university[i].stdnum);
scanf("%s \ %d",,);
while (scanf("%c", &korea_university[i].name[j++] != '/'));

void bubble(struct *std) {

}

				temp_int = korea_university[j].stdnum;
				korea_university[j].stdnum = korea_university[j+1].stdnum;
				korea_university[j+1].stdnum = temp_int;
				strcpy(temp_char,korea_university[j].name);
				strcpy(korea_university[j].name,korea_university[j+1].name);
				strcpy(korea_university[j+1].name,temp_char);

* 4

for(i=0;i<3;i++){
		gets(text[i]);
		j=0;
		for(j=0;text[i][j];j++){
			for(k='a';k<='z';k++){
				if(k==text[i][j])
					count[i].lowercount[k-'a']++;
				if(k-32==text[i][j])
					count[i].uppercount[k-'a']++;
			}
		}
	}

9.1.

Write a bubblesort alrgorithm program (increasing order)

#include<stdio.h>
#define MAX 10

void bubbleSort(int *arr);

int main()
{
	int arr[MAX] = { 10,5,1,6,3,9,4,2,8,7 };
	int i;
	bubbleSort(arr);

	for (i = 0;i<MAX;i++) {
		printf("%d  ", arr[i]);
	}

	return 0;
}

void bubbleSort(int *arr) {
	int i, j, temp;
	for (i = 0;i<MAX;i++) {
		for (j = 0;j<MAX - 1 - i;j++) {
			if (arr[j]>arr[j + 1]) {
				temp = arr[j];
				arr[j] = arr[j + 1];
				arr[j + 1] = temp;
			}
		}
	}
}

 

9.2.

Write a program that reads student name and ID number prints the list of student in numerical order

Use bubblesort alrgorithm (increasing order)

Use Student struct

Use “/” as a delimiter

typedef Struct{
     char name[80];
     int stdnum;
}student;
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#define MAX 5

typedef struct {
	char name[80];
	int stdnum;
}student;
student korea_university[MAX];

void bubbleSort();

int main()
{
	int i;
	char text[MAX][30], *temp;
	
	for (i = 0;i < MAX;i++) {
		printf("<%d> Enter student name / student number: ",i+1);
		gets(text[i]);
		temp = strtok(text[i], "/");
		strcpy(korea_university[i].name,temp);
		temp = strtok(NULL, " ");
		korea_university[i].stdnum=atoi(temp);
	}
	
	bubbleSort();
	
	printf("\nID\t   Name\n");
	for(i=0;i<MAX;i++)
		printf("%-10d  %s\n",korea_university[i].stdnum,korea_university[i].name);
	
	return 0;
}

void bubbleSort() {
	int i, j;
	student temp;
	
	for (i = 0;i<MAX;i++) {
		for (j = 0;j<MAX - 1 - i;j++) {
			if (korea_university[j].stdnum>korea_university[j+1].stdnum) {
				temp=korea_university[j];
				korea_university[j]=korea_university[j+1];
				korea_university[j+1]=temp;
			}
		}
	}
}

 

9.3.

Wirte a program that read 3 lines of text and prints a table indicating the number of occurrences of each different word in the text.

Use wordutility struct

Typedef Struct{
     char word[20]; // Store the unique word
     int count;         // Store the number of occurrences
     int isWritten;    // Indicate whether the word is 
	            // written or not
}wordutility;
#include<stdio.h>
#include<string.h>

typedef struct {
	char word[20];
	int count;
	int isWritten;
}wordutility;

wordutility text[20];

int check(char *temp);

int main()
{
	char input_text[3][80], *temp;
	int i, j=0,index;
	
	printf("Enter three lines of text :\n");
	for (i = 0;i < 3;i++) {
		gets(input_text[i]);
		temp = strtok(input_text[i], " ");
		while (temp) {
			index = check(temp);
			if (index)
				text[index-1].count++;
			else{
				text[j].count++;
				text[j].isWritten++;
				strcpy(text[j++].word,temp);
			}
			temp = strtok(NULL, " .\n");
		}
	}
	
	for(i=0;text[i].isWritten;i++)
		printf("\n\"%s\" appeared %d %s",text[i].word,text[i].count,text[i].count==1?"time":"times");

	return 0;
}

int check(char *temp) {
	int i;
	for (i = 0;text[i].count;i++) {
		if (!strcmp(temp, text[i].word))
			return i+1;
	}
	return 0;
}

 

9.4.

Write a program that inputs 3 lines of text and uses function “strchr” to determine the total occurences of each letter of the alphabet in the lines of text.

Uppercase and lowercase letters should be counted another.

Use chcount struct

Typedef Struct{
     int lowercount[26]; // Store the lowercase alphabet
     int uppercount[26]; // Store the uppercase alphabet
}chcount;
#include<stdio.h>
#include<string.h>

typedef struct {
	int lowercount[26];
	int uppercount[26];
}chcount;

chcount count[3];
int main()
{
	int i, j;
	char text[3][80], *temp;
	
	printf("Enter three lines of text : \n");
	for(i=0;i<3;i++)
		gets(text[i]);
	
	for(i='a';i<='z';i++){
		for(j=0;j<3;j++){
			temp=text[j];
			while(temp=strchr(temp,i)){
				count[j].lowercount[i-'a']++;
				temp++;
			}
		}	
	}
	
	for(i='A';i<='Z';i++){
		for(j=0;j<3;j++){
			temp=text[j];
			while(temp=strchr(temp,i)){
				count[j].uppercount[i-'A']++;
				temp++;
			}
		}	
	}
	
	printf("\nPrint Line\n\n");
	printf("%-17d%-17d%-17d\n",0,1,2);
	for(i='a';i<='z';i++){
		for(j=0;j<3;j++){
			printf("%c :%2d   %c :%2d    ",i,count[j].lowercount[i-'a'],i-32,count[j].uppercount[i-'a']);
		}
		printf("\n");
	}
	
	return 0;
}
반응형

'학부공부 > C_Basic' 카테고리의 다른 글

1. Quiz[C Basic]  (0) 2021.11.07
10. File[C Basic]  (0) 2021.11.07
8. Bit operator[C Basic]  (0) 2021.11.07
7. String[C Basic]  (0) 2021.11.07
6. Pointer[C Basic]  (0) 2021.11.07

댓글