OlveraeUNSW
返回题目列表
COMP1521

COMP1521 2020T3 Q3 · 进程/系统调用

COMP1521 2020T3 Q3 · 9 marks · C
知识点: 进程/系统调用(PROC) · 次: getenv+字符串转整数 · 难度: 中等
Topic: Processes & syscalls · Source: https://cgi.cse.unsw.edu.au/~cs1521/26T1/resources/practice_exams.html

一句话 / Summary: 环境变量整数相近判断(默认42)


Write a C program, 20t3final_q3.c ,
which takes two names of environment variables as arguments.
20t3final_q3.c should print 1
iff both environment variables
are set to a similar integer value.

20t3final_q3.c should print 0 otherwise.

Two integer values are similar if they differ by, strictly, less than 10 .

So 37 and 42 are similar .

So 52 and 42 are not similar .

An unset environment variable should be assumed to have value 42

The shell command export sets an environment variable to a value;
the shell command unset unsets an environment variable.

In the following example,
export is used to set the environment variables
VAR1 , VAR2 and VAR3 and
unset is used to ensure environment variable VAR4 is not set.

export VAR1=40
export VAR2=45
export VAR3=55
unset VAR4
dcc 20t3final_q3.c -o 20t3final_q3
./20t3final_q3 VAR1 VAR2
1
./20t3final_q3 VAR3 VAR2
0
./20t3final_q3 VAR2 VAR4
1
./20t3final_q3 VAR4 VAR3
0

There is no supplied code for this question.

Your program can assume it is always given 2 arguments.

You program can assume that if an environment variable is set it
contains a valid integer and nothing else.

Your program should always print one line of output to stdout.

The line of output should contain only a single character: 0 or 1, and a newline.

Your solution must be in C only.

You are not permitted to run external programs.

You are not permitted to use system, popen, posix_spawn, fork or exec.


🤖 Adapted: read TWO lines from stdin = the two integer values (<unset> means the variable is unset and is treated as 42). Print 1 iff the two values differ by strictly less than 10, else 0.

Verified on the sandbox AND cross-checked against an independent oracle.

样例

stdin
40
45
期望输出
1
stdin
55
45
期望输出
0
c-dcc
14