-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlab1.cpp
More file actions
152 lines (124 loc) · 3.32 KB
/
lab1.cpp
File metadata and controls
152 lines (124 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//Lab1
//COSC130
//Description: This program takes two values and an operation and converts values
//to integers, performs the operation and converts the value back to strings.
//Autumn Henderson
//January 18th, 2019
#include <iostream>
#include <string>
#include <cstdio>
using namespace std;
int CharToInt(char v);
char IntToChar(int v);
int StringToInt(string val);
string IntToString(int val);
int main(int argc, char *argv[])
{
string sresult;
int left;
int right;
char op;
if (4 != argc) {
printf("Usage: %s <left> <op> <right>\n", argv[0]);
return -1;
}
//Notice that this calls your StringToInt. So, make sure you debug
//StringToInt() to make sure that left and right get a proper
//value.
left = StringToInt(argv[1]);
right = StringToInt(argv[3]);
op = argv[2][0];
//Calculate based on the op. Notice that this calls IntToString,
//so debug your IntToString() function to make sure that sresult
//is given the proper result.This assumes your StringToInt already
//works.
switch (op)
{
case 'x':
sresult = IntToString(left * right);
break;
case '/':
sresult = IntToString(left / right);
break;
case '+':
sresult = IntToString(left + right);
break;
case '-':
sresult = IntToString(left - right);
break;
case '%':
sresult = IntToString(left % right);
break;
default:
sresult = IntToString(left);
break;
}
//Remember, printf is the only output function you may use for this lab!
//The format string is %d %c %d = %s. This means that the first argument is %d (decimal / integer),
//%c (characer), %d (decimal/integer),
//%s (string). Notice that because printf() is a C-style function, you
//must pass strings as character arrays. We can convert a C++ string
//to a character array (C-style string) by using the c_str() member function.
printf("%d %c %d = %s\n", left, op, right, sresult.c_str());
return 0;
}
//This function converts a character to an integer
int CharToInt(char v) {
return v - 48;
}
//This function converts an integer to a character
char IntToChar(int v) {
return v + 48;
}
//This function takes a string and converts it into an integer
int StringToInt(string val) {
int i = val.size() - 1;
bool valueIsNegative = false;
int digit = 0;
int tensPower = 1;
int newValue = 0;
//Handles negative
if (val[i] == '-') {
valueIsNegative = true;
--i;
}
//Examines string at index i, multiplies by
//appropriate 10s power, then adds values together
for (i = i; i >= 0; --i) {
digit = CharToInt(val[2]);
for (int j = 0; j < i; ++j) {
tensPower *= 10;
}
newValue += (digit * tensPower);
tensPower = 1;
}
//Converts value to negative
if (valueIsNegative) {
newValue = 0 - newValue;
}
return newValue;
}
//This function takes an integer and converts it to a string
string IntToString(int val) {
string finalAnswer = "";
int digit;
char newLetter;
string tempAnswer = "";
//Converts val to positive and adds negative character
if (val < 0) {
val = val * -1;
finalAnswer += "-";
}
//Takes individual digit, converts to character, adds it to a
//temporary answer
do {
digit = val % 10;
newLetter = IntToChar(digit);
tempAnswer = newLetter + tempAnswer;
val /= 10;
}while (val > 0);
//temporary answer is added to final answer to handle the negative
//if needed
finalAnswer += tempAnswer;
return finalAnswer;
}