Lesson 2.
This commit is contained in:
12
02/Exercise01.c
Normal file
12
02/Exercise01.c
Normal file
@@ -0,0 +1,12 @@
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
short a = 10;
|
||||
int b = 100;
|
||||
int short_length = sizeof a;
|
||||
int int_length = sizeof(b);
|
||||
int long_length = sizeof(long);
|
||||
int char_length = sizeof (char) ;
|
||||
printf("short=%d, int=%d, long=%d, char=%d\n", short_length,
|
||||
int_length, long_length, char_length);
|
||||
return 0;
|
||||
}
|
||||
8
02/Exercise02.c
Normal file
8
02/Exercise02.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
int m = 306587;
|
||||
long n = 28166459852;
|
||||
printf("m=%hd, n=%hd\n", m, n);
|
||||
printf("n=%d\n", n);
|
||||
return 0;
|
||||
}
|
||||
9
02/Exercise03.c
Normal file
9
02/Exercise03.c
Normal file
@@ -0,0 +1,9 @@
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
char input, prev, next;
|
||||
input = getchar();
|
||||
prev = input - 1;
|
||||
next = input + 1;
|
||||
printf("ASCII number: %d, Previous char: %c, Next char: %c", input, prev, next);
|
||||
return 0;
|
||||
}
|
||||
19
02/Experiment1-2.c
Normal file
19
02/Experiment1-2.c
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <stdio.h>
|
||||
int main() {
|
||||
int x1, x2; // Define two integers; uses 4 bytes each.
|
||||
unsigned y; // Define an unsigned integer; uses 4 bytes.
|
||||
char c1, c2; // Define two charater; uses 1 bytes each.
|
||||
x1 = 65535;
|
||||
x2 = x1 + 5;
|
||||
printf("Enter y: ");
|
||||
scanf("%u", &y); // Read y from user input
|
||||
c1 = 97;
|
||||
c2 = 'A';
|
||||
c2 = c2 + 32; //Change the value of c2, using c2 itself.
|
||||
printf("x1=%d\n", x1);
|
||||
printf("x2=%d\n",x2);
|
||||
printf("y=%u\n",(y+15)); // Calculate y + 15, and then output it
|
||||
printf("c1=%c\n",c1);
|
||||
printf("c2=%c\n",c2);
|
||||
return 0;
|
||||
}
|
||||
52
02/Optional-2.c
Normal file
52
02/Optional-2.c
Normal file
@@ -0,0 +1,52 @@
|
||||
#include <stdio.h>
|
||||
|
||||
// Code for encrypting
|
||||
void encryptDecrypt(int mode) {
|
||||
char input[100] = {0}; //Maximum length of the string is 100 characters
|
||||
short pass; //Password should be within range 0~25
|
||||
if (mode == 0) {
|
||||
printf("Enter the string to be encrypted (no longer than 100 characters):\n");
|
||||
}
|
||||
else {
|
||||
printf("Enter the string to be decrypted (no longer than 100 characters):\n");
|
||||
}
|
||||
scanf("%s",input);
|
||||
printf("Enter your password (0-25): ");
|
||||
scanf("%hd", &pass);
|
||||
// Go through every character in input
|
||||
for (int i = 0; i < 100; i++){
|
||||
if (input[i] == '\0') {
|
||||
// '\0' means we have reached the string's end.
|
||||
break;
|
||||
}
|
||||
else {
|
||||
input[i] -= 'A'; //Calculate the chacter's place in the alphabet, from 0~25
|
||||
if (mode == 0) {
|
||||
input[i] += pass; //Add the offset of pass
|
||||
}
|
||||
else {
|
||||
input[i] -= pass; //Subtract the offset of pass
|
||||
input[i] += 26; //Avoid negative number
|
||||
}
|
||||
input[i] %= 26; //Prevent the number from overflow
|
||||
input[i] += 'A'; //Turn the number into ASCII again.
|
||||
printf("%c", input[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
char mode;
|
||||
printf("Slect Mode: (1) Encypt (2) Decrypt: ");
|
||||
mode = getchar();
|
||||
if (mode == '1') {
|
||||
encryptDecrypt(0);
|
||||
}
|
||||
else if (mode == '2') {
|
||||
encryptDecrypt(1);
|
||||
}
|
||||
else {
|
||||
printf("Unrecognized command.");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user