Instructions
Objective
Write a program to implement collaborating plotterin C-language.
Requirements and Specifications
Lab 04: To demonstrate command of I/O
Problem Statement: Collaborating Plotter
You are asked to write a C homework coPlot.c that will cooperate with the end-user, accept keyboard input during execution, and output the results based on the user's commands. More precisely, your program will support the following "commands" typed-in by the end-user during runtime:
1> Bit operations:
Logic Gate | Operands | Manipulation |
bit_or | bitwise OR of the two specified integers | |
bit_and | bitwise AND of the two specified integers | |
bit_xor | bitwise XOR of the two specified integers | |
bit_shift_left | - | shift the bits of the integer s k bits to the left |
bit_shift_right | - | shift the bits of the integer s k bits to the right |
2> String Operations:
reverse - reverse of the specified string
3> Termination:
exit - stop the execution of your program
When your program is started, it should ask the user for a command to run. Once the user types-in the command, your program should do the processing and print the results to the screen. This shall continue until the user types the exit command, at which point your program terminates.
Here is an example run:
• :~> ./coPlot
>> Please enter a command to run.
bit_or 15 25
31
>> Please enter a command to run.
bit_and 15 25
9
>> Please enter a command to run.
bit_xor 15 25
22
>> Please enter a command to run.
reverse hello
olleh
>> Please enter a command to run. reverse CST_8234_Intro_to_C_Prog gorP_C_ot_ortnI_4328_TSC >> Please enter a command to run. exit
Good Bye!
Useful Tips:
- Use the function atoi, defined in stdlib.h, to convert string input from the user to an integer for the bitwise operations.
- chars have numerical values in ASCII (for example 'a' has the numerical value 97, 'A' has value 65, and '0' has value 48 in ASCII). Additionally, sequential characters have sequential numerical values (for example, 'b' has value 98 and 'c' has value 99). Thus, to get the character forward from char c, you can just do c+1.
Submission Instructions
- You must submit your source code (.c file) and makefile in the assigned submission folder on BRS section 010 (activities Assignments). We don’t need your entire project folder.
- Your programs will be compiled with GCC and tested on WSL. Keep in mind that your assignment testing will be on fixed environment and therefore your applications is expected to run on that.
Marking Scheme
Item | Penalty |
Coding correctness (functions’ structure) is substandard | 5 marks |
Inappropriate and Inefficient Logic | 1 marks |
Other specification violations | 2 marks |
Defilements in regards to distribution of well-contained functions between files and makefile | 2 marks |
Missed demonstration | 2 marks |
Input validation is inacceptable | 5 marks |
Total | _Out of 20. |
Source Code
#include
#include
#include
#define TRUE 1
#define STR_LEN 255
/* Entry point of the program */
int main(int argc, char *argv[])
{
int int1, int2;
char command[STR_LEN];
char line[STR_LEN];
while (TRUE)
{
/* Get command */
printf(">> Please enter a command to run.\n");
fgets(line, STR_LEN, stdin);
line[strlen(line) - 1] = '\0';
/* Break the command into tokens */
if (strcmp("exit", line) == 0)
{
/* Exit command executed */
break;
}
else if (sscanf(line, "%s %d %d", command, &int1, &int2) == 3)
{
/* If there are 3 tokens then it might be integer operations */
if (strcmp(command, "bit_or") == 0)
printf("%d\n", int1 | int2);
else if (strcmp(command, "bit_and") == 0)
printf("%d\n", int1 & int2);
else if (strcmp(command, "bit_xor") == 0)
printf("%d\n", int1 ^ int2);
else if (strcmp(command, "bit_shift_left") == 0)
printf("%d\n", int1 << int2);
else if (strcmp(command, "bit_shift_right") == 0)
printf("%d\n", int1 >> int2);
}
else if (sscanf(line, "%s", command) == 1 && strcmp(command, "reverse") == 0)
{
/* Perform reverse, we start at the end of the line and stop
before the word 'reverse'. The reason why we stop at index 7
is because 'reverse' has a length of 6 and add 1 to the space */
for (int1 = strlen(line) - 1; int1 >= 7; int1--)
printf("%c", line[int1]);
printf("\n");
}
}
printf("Good Bye!\n");
return 0;
}
Similar Samples
Explore our C assignment sample to see how we tackle complex programming challenges with precision. Our expert solutions demonstrate clear, structured code and thorough explanations, ensuring you understand each concept. Whether you're grappling with loops, functions, or data structures, our sample will guide you through effective problem-solving techniques and best practices.
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C
C