mirror of
https://github.com/Comp211-SP24/lab-02-Rushilwiz.git
synced 2025-04-09 14:50:19 -04:00
finished negate
This commit is contained in:
parent
f25699e3a6
commit
7ef18644e8
3
.gitignore
vendored
3
.gitignore
vendored
|
@ -5,9 +5,10 @@ signed
|
||||||
twos
|
twos
|
||||||
negate2s
|
negate2s
|
||||||
f32
|
f32
|
||||||
|
.vscode
|
||||||
|
|
||||||
# macOS
|
# macOS
|
||||||
.DS_Store
|
.DS_Store
|
||||||
|
|
||||||
# IDE
|
# IDEif (i >= 0)
|
||||||
.idea
|
.idea
|
||||||
|
|
34
negate2s.c
34
negate2s.c
|
@ -2,7 +2,37 @@
|
||||||
// I pledge the COMP 211 honor code.
|
// I pledge the COMP 211 honor code.
|
||||||
|
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
int main() {
|
int main(int argc, char *argv[]) {
|
||||||
|
char input[33];
|
||||||
|
char output[33] = {'\0'};
|
||||||
|
|
||||||
|
strncpy(input, argv[1], 32);
|
||||||
|
input[32] = '\0';
|
||||||
|
|
||||||
|
for (int i = 0; i < 32; i++)
|
||||||
|
output[i] = (input[i] == '0') ? '1' : '0';
|
||||||
|
|
||||||
|
output[32] = '\0';
|
||||||
|
|
||||||
|
if (input[0] == '1') {
|
||||||
|
int carry = 1;
|
||||||
|
for (int i = 31; i >= 0 && carry; i--) {
|
||||||
|
output[i] = (input[i] == '0') ? '1' : '0';
|
||||||
|
if (input[i] == '0') carry = 0;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
int i = 31;
|
||||||
|
while (input[i] == '0' && i >= 0) {
|
||||||
|
output[i] = '0';
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
|
||||||
|
output[i] = '1';
|
||||||
|
}
|
||||||
|
|
||||||
|
printf("%s\n", output);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user