웹 프로그래밍 및 실습 (Web Programming & Practice) PHP 개요 및 설치 최 미정 강원대학교 IT 대학 컴퓨터과학전공
Programming Practice - Seoul National...
Transcript of Programming Practice - Seoul National...
Programming Practice2019-09-18
Week 3
NoticeSubmission Deadline, Servers, Accounts, etc.
Submission Deadline- Until the following Lecture day 15:30 (not lab session)- ex) This week’s assignment - until 23rd September 15:30- We do not accept late submission, so try to finish it during the lab session
Connecting to server via SSHFor CS department students (주전공/복수전공/부전공): martini.snucse.org
Other students: pp1.snucse.org
You should have a working account for one of these servers.
If you still don’t have an account or cannot connect to server, contact TA as soon as possible.
Connecting to server via SSH (타과생 전용)If you got your account credentials via email, then make sure to change your password
Enter server via SSH and execute the following command in the shell:
$ passwd
Enter a good password (at least 10 characters)
Practice LectureData Type, ASCII Code, I/O Redirection, EOF, ...
Data Type● C language has some primitive types such as
● Character: char, signed char, unsigned char● Integer: (unsigned or signed) short, int, long, and long long● Floating-point number: float, double, long double
Data Type - Character or Integer
Data Type - Floating-point
Data Type - Type Conversion● For binary operations with operands of different types,
the “lower” type is promoted to the “higher” type before operation proceeds.● For assignment operations, the value of the right side is converted to the type
of the left, which is the type of the result● Type Casting: Explicit Conversion
○ int a=3;printf(“%f\n”, (float) a/2);
ASCII Code● A character encoding-scheme● Each character constant has its corresponding integer value● No particular relationship between the value of the character constant
representing a digit and the digit’s intrinsic integer value● ‘2’ != 2● ‘2’ == 50
ASCII Code
I/O Redirection● I/O = Input / Output● To redirect standard input/output/error to a file● You can use file content as the input for a program
or save the output of a program as file content● You can reuse input and output
I/O Redirection● Usage example
○ $ ./program < input○ $ ./program > output○ $ ./program < input > output
* ‘$’ sign means terminal (do not type ‘$’)
I/O Redirection
I/O Redirection
I/O Redirection
I/O Redirection
I/O Redirection Practice
$ gcc adder.c -o adder
$ ./adder
1 3
4
#include <stdio.h>
int main() {
int a, b;
scanf(“%d %d”, &a, &b);
printf(“%d\n”, a+b);
return 0;
}
adder.cshell (terminal)
adder
1 3 4
I/O Redirection Practice
$ gcc adder.c -o adder
$ ./adder < input.txt
4
#include <stdio.h>
int main(){
??
}
adder.cshell (terminal)
adder
1 3 4
intput.txt
1 3
I/O Redirection Practice
$ gcc adder.c -o adder
$ ./adder > output.txt
1 3
#include <stdio.h>
int main(){
??
}
adder.cshell (terminal)
adder
1 3 4
output.txt
4
End of File (EOF)● a symbolic constant that stands for End of file● a condition where no more data can be read from a data source
○ file or stream● In terminal, we can <control +d > as EOF
End of File (EOF)● a symbolic constant that stands for End Of File● a condition where no more data can be read from a data source
○ file or stream● In terminal, we can <control +d > as EOF
● In C, we can check whether EOF comes○ while ( scanf(“%d”, &a) != EOF)
or
○ while ( (c= getchar()) != EOF)
EOF Practice
$ gcc adder.c -o adder
$ ./adder
3 10 2 16 8
[ctrl + d]
39
$
#include <stdio.h>
int main(){
int a, s=0;
while(scanf(“%d”, &a) != EOF){
s+=a;
}
printf(“%d\n”, s);
return 0;
}
adder.cshell (terminal)
EOF Practice
$ gcc adder.c -o adder
$ ./adder
3 10 2 16 8
[ctrl + d]
39
$
#include <stdio.h>
int main(){
int a, s=0;
while(scanf(“%d”, &a) != EOF){
s+=a;
}
printf(“%d\n”, s);
return 0;
}
adder.cshell (terminal)
EOF Practice
$ gcc adder.c -o adder
$ ./adder < input.txt
39
$
#include <stdio.h>
int main(){
int a, s=0;
while(scanf(“%d”, &a) != EOF){
s+=a;
}
printf(“%d\n”, s);
return 0;
}
adder.cshell (terminal)
3 10 2 16 8input.txt
EOF Practice
$ gcc adder.c -o adder
$ ./adder < input.txt > output.txt
$
#include <stdio.h>
int main(){
int a, s=0;
while(scanf(“%d”, &a) != EOF){
s+=a;
}
printf(“%d\n”, s);
return 0;
}
adder.cshell (terminal)
3 10 2 16 8input.txt
39output.txt
Now,It’s your turn
Problem.
Description
Input
Output
Sample
Data-types Practice1
Let’s see how data types work in the C language! Copy and submit the following exact program that prints results of some arithmetic operations. Use the following exact code. Please make sure you understand why the code prints this output.
Exactly same as the sample output
2
4
2.333333 4
2.333333
8
2.333333 8
141006540 1000000000
8
130 130 4 1
A a
2 98 50
None.
Problem.
Description
Input
Output
Sample
Data-types Practice1
Let’s see how data types work in the C language! Copy and submit the following exact program that prints results of some arithmetic operations. Use the following exact code. Please make sure you understand why the code prints this output.
Exactly same as the sample output
2
4
2.333333 4
2.333333
8
2.333333 8
141006540 1000000000
8
130 130 4 1
A a
2 98 50
None.
Problem. No.2
Write a program that calculates the average of given integers.
Any number of integers maybe given as input_you must continuously get input until EOF.
It is Guaranteed that at most 1000 integers will be given, and the absolute value of each given integer will be less than or equal to 100000.
[Input]1 2 3 4
[Output]
2.500000
[Input]
4 2 1
[Output]
2.333333Print the average of given integers. Absolute error is allowed up to 10^-6
A single line with any number of integers
Description
Input
Output
Sample
Average
Problem. No.3
Write a program that gets any number of characters as input, and convert any occurrence of upper-case letters to lowercase letters and any occurrence of lower-case letters to upper-case letters.
Don’t do anything for non-alphabet characters.
For instance, there may be white spaces included in the input – just leave them as whitespaces.
Print the same line as input, except convert all upper-case letters to lower-case and all lower-case letters to upper-case.
[input]
abcd123 ABCDEF
[output]
ABCD123 abcdef
A single line with any number of characters. It is guaranteed that every character is of ASCII table
Description
Input
Output
Sample
Character Conversion
Caesar CipherProblem. No.4
The Caesar cipher is the one of simplest ciphers. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet. For example, with a right shift of 4, A would be replaced by E, H would become L, and so on.
It is guaranteed that there is only a right of shift with a positive number. And string to be encrypted consists of the capital characters without any white spaces.
The encrypted string (only consists of the capital letter)
[Input]
4HAPPY
[Output]
LETTC
At the first line, there is the number N for shifting to right (0 <= N <= 25). At the next line, there is the string to be encrypted (only consists of the capital letter)
Description
Input
Output
Sample
The negative input (-25 <= N <= 25) is your optional self challenge!Input: -3 ABOutput: XY
Notice: You should filter input characters by using if statement which examine that input characters are the capital characters because of input buffer problem.
FAQ
Q1. I worked on my martini (or other server). Can I copy the files to my computer?
A1. Yes, you can use scp (shell copy)
scp {id@server:path} {localpath}example: scp [email protected]:~/week3/pa1.c ~/week3/pa1.c
You can also copy local file from your computer to other server vice versascp {localpath} {id@server:path} example: scp ~/week3/pa1.c [email protected]:~/week3/pa1.c
Q2. My vim screen suddenly freezed. What happened?
A2. One possible cause: “Ctrl+s” freezes vim. To unfreeze type “Ctrl+q”