8-7-12-01-22.47.c

Source: Internet
Author: User

Simulated operation. Menu editing is relatively simple.

This is a good thing. I have more hands!

The memory segment is incorrect. Be careful when writing the code. Write the code carefully.

/* 8-7-12-01-22.47.c -- Chapter 8, chapter 7 */<br/> # include <stdio. h> <br/> # include <stdlib. h> </P> <p> # define height (0) <br/> # define size (-1) <br/> # define OSS (-65536) </P> <p> typedef int item; <br/> typedef item settype; <br/> typedef settype * disjiontset; </P> <p> void initialize_as_height (disjiontset gather, const int size); <br/> void initialize_as_size (disjiontset gather, const int size); <br/> int set_unio N_as_height (disjiontset gather, settype T1, settype T2); <br/> int set_union_as_size (disjiontset gather, settype T1, settype T2); <br/> settype find (disjiontset gather, const item); <br/> void print_gather (const disjiontset gather, const int size); <br/> int menu (void ); <br/> void eat_line (void); <br/> void get_input (item * const Pi1, item * const Pi2); <br/> void Link (disjiontset gat Her, const item I1, const item I2, const int mode); <br/> int main (void); </P> <p> int main (void) <br/>{< br/> disjiontset gather; <br/> item I1, I2; <br/> int lenth, size, mode; <br/> char choice; </P> <p> lenth = sizeof (settype); <br/> size = 9; <br/> gather = (settype *) malloc (lenth * (size + 1); </P> <p> initialize_as_height (gather, (size + 1); <br/> mode = height; <br/> while (choice = menu () <Br/>{< br/> switch (choice) <br/>{< br/> case 1: get_input (& I1, & I2 ); <br/> Link (gather, i1, I2, mode); <br/> break; <br/> case 2: print_gather (gather, size + 1 ); <br/> break; <br/>}< br/> free (gather); </P> <p> return 0; <br/>}</P> <p> void initialize_as_height (disjiontset gather, const int size) <br/>{< br/> int I; </P> <p> gather [0] = size-1; <br/>/* Be careful! */<Br/> for (I = 1; I <size; I ++) <br/> gather [I] = height; <br/>}</P> <p> void initialize_as_size (disjiontset gather, const int size) <br/>{< br/> int I; </P> <p> gather [0] = size-1; <br/> for (I = 1; I <size; I ++) <br/> gather [I] = size; <br/>}</P> <p> int set_union_as_height (disjiontset gather, settype T1, settype T2) <br/>{< br/> If (gather [T1]> 0 | gather [T2]> 0) <br/> return 0; <br/> else if (Gather [T2] <gather [T1]) <br/> gather [T1] = t2; <br/> else <br/> {<br/> If (gather [T1] = gather [T2]) <br/> gather [T1] --; <br/> gather [T2] = T1; <br/>}</P> <p> return 1; <br/>}</P> <p> int set_union_as_size (disjiontset gather, settype T1, settype T2) <br/> {<br/> If (gather [T1]> 0 | gather [T2]> 0) <br/> return 0; <br/> else if (gather [T2] <gather [T1]) <br/> {<br/> gather [T2] + = gather [T1]; <br/> gat Her [T1] = t2; <br/>}< br/> else <br/>{< br/> gather [T1] + = gather [T2]; <br/> gather [T2] = T1; <br/>}</P> <p> return 1; <br/>}</P> <p> settype find (disjiontset gather, const item) <br/>{< br/> If (item> gather [0]) <br/> return OSS; <br/> else if (gather [item] <= 0) <br/> return item; <br/> else <br/> return gather [item] = find (gather, gather [item]); <br/>}</P> <p> void print_gather (const disjionts Et gather, const int size) <br/>{< br/> int I; </P> <p> for (I = 1; I <size; I ++) <br/> printf ("%-3D", gather [I]); <br/> putchar ('/N '); <br/>}</P> <p> int menu (void) <br/>{< br/> int choice; </P> <p> puts ("<-------- imitation programme -------->"); <br/> puts ("1 ). link "); <br/> puts (" 2 ). show condition "); <br/> puts (" Q ). exit "); <br/> fputs (" ()/B/B ", stdout); </P> <p> scanf (" % d ", & choice ); <br/> eat_l INE (); <br/> switch (choice) <br/>{< br/> case 1: return 1; <br/> case 2: return 2; <br/> default: Return 0; <br/>}</P> <p> void eat_line (void) <br/>{< br/> while (getchar ()! = '/N') <br/> continue; <br/>}</P> <p> void get_input (item * const Pi1, item * const Pi2) <br/> {<br/> puts ("Please input the first number:"); <br/> while (0 = scanf ("% d", Pi1 )) <br/>{< br/> eat_line (); <br/> puts ("Please input the first number :"); <br/>}< br/> eat_line (); <br/> puts ("Please input the second number :"); <br/> while (0 = scanf ("% d", Pi2) <br/>{< br/> eat_line (); <br/> puts ("Please input the second number. "); <br/>}< br/> eat_line (); <br/>}</P> <p> void Link (disjiontset gather, const item I1, const item I2, const int mode) <br/>{< br/> settype T1, T2; </P> <p> T1 = find (gather, i1 ); <br/> T2 = find (gather, I2); <br/> If (OSS = T1 | OSS = t2) <br/> {<br/> If (OSS = T1 & OSS = t2) <br/> puts ("the 1st input and the 2nd input all overflow. "); <br/> else if (OSS = T1) <br/> puts (" the 1st input overflow. "); <br/> else <br/> puts (" the 2nd input overflow. "); <br/> return; <br/>}< br/> If (T1 = t2) <br/> puts (" in the same set. "); <br/> else <br/> {<br/> If (Height = mode) <br/> set_union_as_height (gather, T1, T2 ); <br/> else if (size = mode) <br/> set_union_as_size (gather, T1, T2); <br/> puts ("union operation has happend. "); <br/>}< br/>}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.