Advertisement
Guest User

手伝ってくれ

a guest
Dec 24th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.14 KB | None | 0 0
  1. FIRST PROGRAM:
  2. #include <stdio.h>
  3.  
  4. int sum (int n);
  5.  
  6. //**************************************************************
  7. // NAME:      main
  8. // PURPOSE:   get n's value and call sum function, print result
  9. // PASSED:    none
  10. // RETURNED:  none
  11. //**************************************************************
  12. int main () {
  13.     int n, add;
  14.     printf("Enter a number: ");
  15.     scanf("%d", &n);
  16.     add=sum(n);
  17.     printf("The result is %d.", add);
  18. }
  19.  
  20. //*************************************************
  21. // NAME:      sum
  22. // PURPOSE:   calculate the sum of n/n+1 until n=0
  23. // PASSED:    int n
  24. // RETURNED:  none
  25. //*************************************************
  26. int sum (int n) {
  27.     int i, total=0;
  28.     for (i=0; i<n; i++) {
  29.         total+=n/(n+1);
  30.         n--;
  31.     }
  32. }
  33.  
  34. __________________________________________________________________________________
  35. SECOND PROGRAM:
  36.  
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "simpio.h"
  41. #include "strlib.h"
  42.  
  43. char C;
  44. //char array1[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  45. //char array2[26] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
  46.  
  47. bool array1[26] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
  48. bool array2[26] = {false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false,false};
  49. bool union_array[26];
  50. bool intersect_array[26];
  51.  
  52. char A[26];
  53. char B[26];
  54.  
  55. bool unions (bool array1[26], bool array2[26]);
  56. bool intersection (bool array1[26], bool array2[26]);
  57. void contain(char A[], char B[]);
  58.  
  59. int main () {
  60.     int x=0;
  61.     printf("\nEnter the values of A. Enter a \".\" when done:\n");
  62.     do {
  63.         scanf(" %c", &A[x]);
  64.         if (A[x]=='.') {
  65.             A[x]='\0';
  66.             break;
  67.         }
  68.         x++;
  69.     } while (A[x]!='.' && x!=26);
  70.     x=0;
  71.     printf("\nEnter the values of B. Enter a \".\" when done:\n");
  72.     do {
  73.         scanf(" %c", &B[x]);
  74.         if (B[x]=='.') {
  75.             B[x]='\0';
  76.             break;
  77.         }
  78.         x++;
  79.     } while (B[x]!='.' && x!=26);
  80.    
  81.    
  82.     for (x=0;x<26;x++) {
  83.         if (A[x]=='a') {array1[0]=true;}
  84.         if (A[x]=='b') {array1[1]=true;}
  85.         if (A[x]=='c') {array1[2]=true;}
  86.         if (A[x]=='d') {array1[3]=true;}
  87.         if (A[x]=='e') {array1[4]=true;}
  88.         if (A[x]=='f') {array1[5]=true;}
  89.         if (A[x]=='g') {array1[6]=true;}
  90.         if (A[x]=='h') {array1[7]=true;}
  91.         if (A[x]=='i') {array1[8]=true;}
  92.         if (A[x]=='j') {array1[9]=true;}
  93.         if (A[x]=='k') {array1[10]=true;}
  94.         if (A[x]=='l') {array1[11]=true;}
  95.         if (A[x]=='m') {array1[12]=true;}
  96.         if (A[x]=='n') {array1[13]=true;}
  97.         if (A[x]=='o') {array1[14]=true;}
  98.         if (A[x]=='p') {array1[15]=true;}
  99.         if (A[x]=='q') {array1[16]=true;}
  100.         if (A[x]=='r') {array1[17]=true;}
  101.         if (A[x]=='s') {array1[18]=true;}
  102.         if (A[x]=='t') {array1[19]=true;}
  103.         if (A[x]=='u') {array1[20]=true;}
  104.         if (A[x]=='v') {array1[21]=true;}
  105.         if (A[x]=='w') {array1[22]=true;}
  106.         if (A[x]=='x') {array1[23]=true;}
  107.         if (A[x]=='y') {array1[24]=true;}
  108.         if (A[x]=='z') {array1[25]=true;}
  109.     }
  110.    
  111.     for (x=0;x<26;x++) {
  112.         if (B[x]=='a') {array2[0]=true;}
  113.         if (B[x]=='b') {array2[1]=true;}
  114.         if (B[x]=='c') {array2[2]=true;}
  115.         if (B[x]=='d') {array2[3]=true;}
  116.         if (B[x]=='e') {array2[4]=true;}
  117.         if (B[x]=='f') {array2[5]=true;}
  118.         if (B[x]=='g') {array2[6]=true;}
  119.         if (B[x]=='h') {array2[7]=true;}
  120.         if (B[x]=='i') {array2[8]=true;}
  121.         if (B[x]=='j') {array2[9]=true;}
  122.         if (B[x]=='k') {array2[10]=true;}
  123.         if (B[x]=='l') {array2[11]=true;}
  124.         if (B[x]=='m') {array2[12]=true;}
  125.         if (B[x]=='n') {array2[13]=true;}
  126.         if (B[x]=='o') {array2[14]=true;}
  127.         if (B[x]=='p') {array2[15]=true;}
  128.         if (B[x]=='q') {array2[16]=true;}
  129.         if (B[x]=='r') {array2[17]=true;}
  130.         if (B[x]=='s') {array2[18]=true;}
  131.         if (B[x]=='t') {array2[19]=true;}
  132.         if (B[x]=='u') {array2[20]=true;}
  133.         if (B[x]=='v') {array2[21]=true;}
  134.         if (B[x]=='w') {array2[22]=true;}
  135.         if (B[x]=='x') {array2[23]=true;}
  136.         if (B[x]=='y') {array2[24]=true;}
  137.         if (B[x]=='z') {array2[25]=true;}
  138.     }
  139.  
  140.    
  141.     printf("\nFor the sets: \n");
  142.    
  143.     printf("A = {");
  144.     for (x=0; x<sizeof(A); x++) {
  145.         if (A[x]=='\0') {break;}
  146.         printf("%c, ", A[x]);
  147.     }
  148.     printf("\b\b}\n");
  149.    
  150.     printf("B = {");
  151.     for (x=0; x<sizeof(B); x++) {
  152.         if (B[x]=='\0') {break;}
  153.         printf("%c, ", B[x]);
  154.     }
  155.     printf("\b\b}\n");
  156.    
  157.     unions(array1, array2);
  158.     intersection(array1, array2);
  159. }
  160.  
  161. bool unions (bool array1[26], bool array2[26]) {
  162.     int x=0;
  163.     for (x=0;x<26;x++) {
  164.         if (array1[x]==true || array2[x]==true) {union_array[x]=true;}
  165.     }
  166.     printf("A Union B = {");
  167.     for (x=0; x<26; x++) {
  168.         if (union_array[x]==true) {printf("T, ");}
  169.         if (union_array[x]==false) {printf("F, ");}
  170.     }
  171.     printf("\b\b}\n\n");
  172. }
  173.  
  174. bool intersection (bool array1[26], bool array2[26]) {
  175.     int x=0;
  176.     for (x=0;x<26;x++) {
  177.         if (array1[x]==true && array2[x]==true) {intersect_array[x]=true;}
  178.     }
  179.     printf("A Intersect B = {");
  180.     for (x=0; x<26; x++) {
  181.         if (intersect_array[x]==true) {printf("T, ");}
  182.         if (intersect_array[x]==false) {printf("F, ");}
  183.     }
  184.     printf("\b\b}\n");
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement