Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!

# CS50 Hacker Problem Set 1

By: bubblesort on Jan 16th, 2014  |  syntax: C  |  size: 6.74 KB  |  views: 885  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. /*This is my source code for the hacker edition of pset1 in Harvard's CS50.  I'm getting a few errors off of check50 on my mario program, but as far as I can tell it meets the specifications set forth in the assignment, and since it can't be turned in anyway I'm just going to let the problems go.
2.
3. ===================================================================
4. credit.c source code:
5. -------------------------------------------------------------------*/
6. #include <cs50.h>
7. #include <stdio.h>
8. #include <math.h>
9.
10. //used to get the length of the credit card number entered
11. int CCLength(long long int ccNum)
12. {
13.     if(ccNum < 999999999999)
14.         return 0;
15.
16.     else if(ccNum <= 9999999999999)
17.         return 13;
18.
19.     else if(ccNum <= 99999999999999)
20.         return 14;
21.
22.     else if(ccNum <= 999999999999999)
23.         return 15;
24.
25.     else if(ccNum <= 9999999999999999)
26.         return 16;
27.
28.     else
29.         return 0;
30. }
31.
32. int main(void)
33. {
34.     printf("Please enter a credit card number.\n");
35.     long long int cNumber = GetDouble();
36.     //printf("You entered %lli\n", cNumber);
37.
38.     //get the length of the credit card number
39.     int cLength = CCLength(cNumber);
40.
41.     //if the number is too short or too long then it is invalid
42.     if(cLength == 0)
43.     {
44.         //printf("This number is not the right length to be a credit card number.\n");
45.         printf("INVALID\n");
46.         return 0;
47.     }
48.
49.     int total1 = 0;
50.     int total2 = 0;
51.     int ccType = 0;
52.
53.     for(int count=0; count < cLength; count++)
54.     {
55.         //seperate the digit we are going to be working with
56.         long long int mod = pow(10, count+1);
57.         long long int divide = pow(10, count);
58.         int digit = (cNumber % mod) / divide;
59.
60.         //if this is an odd digit then add it to the total
61.         if(count%2 == 0)
62.             total1 += digit;
63.
64.         //if this is an even digit then multiply by two and add each digit of the result to the total
65.         else
66.         {
67.             int oddDigits = (digit * 2);
68.             //if the result has 1 digit then add it to the total
69.             if(oddDigits < 10)
70.                 total2 += oddDigits;
71.             //if the result has 2 digits then add each digit to the total
72.             else
73.             {
74.                 oddDigits = (oddDigits % 10) + 1;
75.                 total2 += oddDigits;
76.             }
77.         }
78.         //if we are on the last 2 digits then put them in ccType to identify the kind of card this is.
79.         if(cLength - count == 2)
80.             ccType += digit;
81.         else if(cLength - count == 1)
82.             ccType += (digit * 10);
83.
84.         //printf("Mod = %lli, Divide = %lli, Result = %lli, Total1 = %i, Total2 = %i\n", mod, divide, ((cNumber % mod) / divide), total1, total2 );
85.     }
86.     int finalTotal = total1 + total2;
87.     //printf("The total is %i.\n", finalTotal);
88.     if((finalTotal % 10) == 0)
89.     {
90.         //printf("This is a valid credit card number.\n");
91.
92.         if((ccType == 34) || (ccType == 37))
93.             printf("AMEX\n");
94.         else if((ccType == 51) || (ccType == 52) || (ccType == 53) || (ccType == 54) || (ccType == 55))
95.             printf("MASTERCARD\n");
96.         else if(ccType / 10 == 4) //because we only need the first digit to ID a visa card
97.             printf("VISA\n");
98.         else
99.             printf("INVALID\n");
100.     }
101.     else
102.         printf("INVALID\n");
103. }
104. /*===================================================================
105. Running credit:
106. -------------------------------------------------------------------
107. jharvard@appliance (~/Dropbox/CS50/hacker1/credit): ./credit
108. Please enter a credit card number.
109. 4012888888881881
110. VISA
111. ===================================================================
112. Checking credit:
113. -------------------------------------------------------------------
114. jharvard@appliance (~/Dropbox/CS50/hacker1/credit): check50 2014/x/hacker1/credit credit.c
115. :) credit.c exists
116. :) credit.c compiles
117. :) identifies 378282246310005 as AMEX
118. :) identifies 371449635398431 as AMEX
119. :) identifies 5555555555554444 as MASTERCARD
120. :) identifies 5105105105105100 as MASTERCARD
121. :) identifies 4111111111111111 as VISA
122. :) identifies 4012888888881881 as VISA
123. :) identifies 1234567890 as INVALID
124. :) rejects a non-numeric input of "foo"
125. :) rejects a non-numeric input of ""
126. https://sandbox.cs50.net/checks/0dd1e381e3254b7c8dc9060f359a401b
127.
128.
129. ===================================================================
130. mario.c source code:
131. -------------------------------------------------------------------*/
132. #include <cs50.h>
133. #include <stdio.h>
134.
135. int main(void)
136. {
137.     printf("Please enter a number between 1 and 23:  ");
138.     int height = GetInt();
139.
140.     if((height > 23) || (height < 1))
141.     {
142.         //printf("Invalid input\n");
143.         return 0;
144.     }
145.
146.     int x = 0;
147.     int y = 0;
148.
149.     for(int count = 0; count < height; count++)
150.     {
151.         //makes the left side of the pyramid
152.         while(y < height)
153.         {
154.             int number = height - (count + 1);
155.             if(y >= number)
156.                 printf("#");
157.             else
158.                 printf(" ");
159.             y++;
160.         }
161.         y = 0;
162.
163.         //makes the spaces in the middle
164.         printf("  ");
165.
166.         //makes the right side of the pyramid
167.         while(x < height)
168.         {
169.             if(x > count)
170.                 printf(" ");
171.             else
172.                 printf("#");
173.
174.             x++;
175.         }
176.         x = 0;
177.         printf("\n");
178.     }
179. }
180. /*===================================================================
181. Running it:
182. -------------------------------------------------------------------
183. jharvard@appliance (~/Dropbox/CS50/hacker1/mario): ./mario
184. Please enter a number between 1 and 23:  5
185.     #  #
186.    ##  ##
187.   ###  ###
188.  ####  ####
189. #####  #####
190. ===================================================================
191. Checking it:
192. -------------------------------------------------------------------
193. jharvard@appliance (~/Dropbox/CS50/hacker1/mario): check50 2014/x/hacker1/mario mario.c
194. :) mario.c exists
195. :) mario.c compiles
196. :( rejects a height of -1
197.    \ expected output, not an exit code of 0
198. :) handles a height of 0 correctly
199. :) handles a height of 1 correctly
200. :( handles a height of 2 correctly
201.    \ expected output, but not " #  # \n##  ##\n"
202. :( handles a height of 23 correctly
203.    \ expected output, but not "                      #  #             ..."
204. :( rejects a height of 24
205.    \ expected output, not an exit code of 0
206. :) rejects a non-numeric height of "foo"
207. :) rejects a non-numeric height of ""
208. https://sandbox.cs50.net/checks/d34c1b1bba124ebcb47d5406850cedc3
clone this paste RAW Paste Data
Top