I'd like to create a function that checks whether a number input with an exponent such as 1.32e2, 1e3, +1.32e+2 indeed has the e in the correct position, after a number, not before it (such as e1 or .e4)
I'm a beginner so it's just trial and error at this point. This is what I have so far:
bool validate_alpha_e(string op) {
if (count_alpha(op) <= 1) {
if (count_es(op) == 1) {
//return true;
int length = op.length();
for (int i = 0; i < length; i++) {
char ch = op[i-1];
if (ch == 'e' || ch == 'E') {
if (i-1 != isdigit(i)) {
return false;
}
}
}
return true;
}
Worked it out. Thanks for the comments but I wasn't able to figure it out.
So I had to create 3 separate functions (see below)
First function validates for correct input before the e.
Second function validates for correct input after the e.
Third function validates that the first character is not an e.
Put all these together in a bool if statement and if it returns false then the input is not correct.
bool validate_e_after(string op) {
int length = op.length();
for (int i = 1; i < length; i++) {
char ch = op[i-1];
if (ch == 'e' || ch == 'E') {
char ch2 = op[i];
if (isdigit(ch2) || ch2 == '+' || ch2 == '-') {
return true;
}
else {
return false;
}
}
}
}
bool validate_e_before(string op) {
int length = op.length();
for (int i = 1; i < length; i++) {
char ch = op[i+1];
if (ch == 'e' || ch == 'E' && i != 0) {
char ch2 = op[i];
if (isdigit(ch2) || ch2 == '+' || ch2 == '-') {
return true;
}
else {
return false;
}
}
}
}
bool validate_ezero(string op) {
int length = op.length();
for (int i = 0; i < length; i++) {
char ch = op[i];
if (ch == 'e' or ch == 'E') {
if (i == 0) {
return false;
}
}
}
return true;
Related
Basically I have to encode a name into a Soundex Code. The helper functions I implemented do the following:
Discard all non-letter characters from the surname: dashes, spaces, apostrophes, and so on.
Encode each letter as a digit
Coalesce adjacent duplicate digits from the code (e.g. 222025 becomes 2025).
Replace the first digit of the code with the first letter of the original name, converting to uppercase.
Remove all zeros from the code.
Make the code exactly length 4 by padding with zeros or truncating the excess.
Excuse the implementation of the helper functions, I know they could be implemented better. But when I manually pass the output from one function to another I see that the result is what I want. It's only when I combine them all into one function that I see that the output I pass is as if I didn't modify the input I passed at all. I believe my issue might have to do with passing by reference but doing that for all my functions made no difference or gave an incorrect output.
#include <iostream>
#include <string>
string removeNonLetters(string s) {
string result = "";
for (int i = 0; i < s.length(); i++) {
if (isalpha(s[i])) {
result += s[i];
}
}
return result;
}
string encode(string name) {
std::transform(name.begin(), name.end(), name.begin(), ::toupper);
string encoded = "";
for (int i = 0; i < name.size(); ++i) {
if (name[i] == 'A' || name[i] == 'E' || name[i] == 'I' || name[i] == 'O' || name[i] == 'U' || name[i] == 'H' || name[i] == 'W' || name[i] == 'Y')
encoded += '0';
else if (name[i] == 'B' || name[i] == 'F' || name[i] == 'P' || name[i] == 'V')
encoded += '1';
else if (name[i] == 'C' || name[i] == 'G' || name[i] == 'J' || name[i] == 'K' || name[i] == 'Q' || name[i] == 'S' || name[i] == 'X' || name[i] == 'Z')
encoded += '2';
else if (name[i] == 'D' || name[i] == 'T')
encoded += '3';
else if (name[i] == 'L')
encoded += '4';
else if (name[i] == 'M' || name[i] == 'N')
encoded += '5';
else if (name[i] == 'R')
encoded += '6';
}
return encoded;
}
string removeDuplicate(string encoded) {
for (int i = 0; i < encoded.size(); ++i) {
if (encoded[i] == encoded[i+1])
encoded[i] = '\0';
}
return encoded;
}
string removeZeros(string digits) {
for (int i = 0; i < digits.size(); ++i) {
if (digits[i] == '0')
digits[i] = '\0';
}
return digits;
}
string padding(string output) {
int size = output.size();
if (size < 4) {
for (int i = size; i < 4; ++i)
output += '0';
}
else if (size > 4) {
for (int j = size; j > 3; --j)
output[j] = '\0';
}
return output;
}
/* TODO: Replace this comment with a descriptive function
* header comment.
*/
string soundex(string s) {
/* TODO: Fill in this function. */
string copy = s;
removeNonLetters(s);
encode(s);
removeDuplicate(s);
s[0]= copy[0];
removeZeros(s);
padding(s);
return s;
}
int main() {
string s = "Curie";
cout << soundex(s) << '\n';
// Output should be C600 but I keep getting "Curie."
}
Your functions return the adjusted strings, that's good. But your calling code doesn't use the returned values!
Something like this is what you want.
string soundex(string s) {
/* TODO: Fill in this function. */
string copy = s;
s = removeNonLetters(s);
s = encode(s);
s = removeDuplicate(s);
s[0] = copy[0];
s = removeZeros(s);
s = padding(s);
return s;
}
If you want to change the value of a variable you normally use =. I'm sure you know that but for some reason you forgot because functions are involved.
I am newbie and I have to check valid palindrome string.
Here is my code in c++
char to_lower(char ch){
char c = ch - 'A' + 'a';
return c;
}
bool isPalindrome(string s) {
int l = s.length();
string t = "";
for(int i=0;s[i]!='\0';i++){
if(((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z') || (s[i]>='0' && s[i]<='9')))
t.push_back(s[i]);
if(t[i]>='A' && t[i]<='Z')
t[i] = to_lower(t[i]);
}
int i=0;
int j=t.length()-1;
while(i<=j){
if(t[i]!=t[j])
return false;
else
i++;
j--;
}
return true;
}
It is showing stack buffer overflow. I am not understanding this error is showing. Please help me
So there this c++ code for the following question: NFA to accept strings that have at least one character occurring in a multiple of 3.
The problem is it's showing "accepting" as the output for all inputs.
for example: bbaacc output should be "not accepting" but this code is showing excepting
I am not able to figure out where I went wrong. Can somebody help me find where?
#include <bits/stdc++.h>
int nfa = 1;
int flag = 0;
using namespace std;
void state1(char c)
{
if (c == 'a')
nfa = 2;
else if (c == 'b' || c == 'c')
nfa = 1;
else
flag = 1;
}
void state2(char c)
{
if (c == 'a')
nfa = 3;
else if (c == 'b' || c == 'c')
nfa = 2;
else
flag = 1;
}
void state3(char c)
{
if (c == 'a')
nfa = 1;
else if (c == 'b' || c == 'c')
nfa = 3;
else
flag = 1;
}
void state4(char c)
{
if (c == 'b')
nfa = 5;
else if (c == 'a' || c == 'c')
nfa = 4;
else
flag = 1;
}
void state5(char c)
{
if (c == 'b')
nfa = 6;
else if (c == 'a' || c == 'c')
nfa = 5;
else
flag = 1;
}
void state6(char c)
{
if (c == 'b')
nfa = 4;
else if (c == 'a' || c == 'c')
nfa = 6;
else
flag = 1;
}
void state7(char c)
{
if (c == 'c')
nfa = 8;
else if (c == 'b' || c == 'a')
nfa = 7;
else
flag = 1;
}
void state8(char c)
{
if (c == 'c')
nfa = 9;
else if (c == 'b' || c == 'a')
nfa = 8;
else
flag = 1;
}
void state9(char c)
{
if (c == 'c')
nfa = 7;
else if (c == 'b' || c == 'a')
nfa = 9;
else
flag = 1;
}
bool checkA(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 1)
state1(s[i]);
else if (nfa == 2)
state2(s[i]);
else if (nfa == 3)
state3(s[i]);
}
if (nfa == 1) {
return true;
}
else {
nfa = 4;
}
}
bool checkB(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 4)
state4(s[i]);
else if (nfa == 5)
state5(s[i]);
else if (nfa == 6)
state6(s[i]);
}
if (nfa == 4) {
return true;
}
else {
nfa = 7;
}
}
bool checkC(string s, int x)
{
for (int i = 0; i < x; i++) {
if (nfa == 7)
state7(s[i]);
else if (nfa == 8)
state8(s[i]);
else if (nfa == 9)
state9(s[i]);
}
if (nfa == 7) {
return true;
}
}
int main()
{
string s = "bbbca";
int x = 5;
if (checkA(s, x) || checkB(s, x) || checkC(s, x)) {
cout << "ACCEPTED";
}
else {
if (flag == 0) {
cout << "NOT ACCEPTED";
return 0;
}
else {
cout << "INPUT OUT OF DICTIONARY.";
return 0;
}
}
}
I put the following code together to transform infix to postfix. However it only works when the infix is parenthesised, and I have no clue why. I tried a lot of the basic stuff like directly/indirectly assigning the values.
Here's the code
int prec(char c)
{
if(c == '^')
return 3;
else if(c == '*' || c == '/')
return 2;
else if(c == '+' || c == '-')
return 1;
else
return -1;
}
class Expressions
{
public:
std::stack <char> inFix;
std::stack <char> postFix;
std::vector <char> postFixString;
void ptInFix(); // prints the infix and deletes the stack, for testing only, need to tweak later
void ptPostFix();
void inFixtoPostFix();
};
void Expressions::inFixtoPostFix()
{
int size = inFix.size();
for(int i=0;i<size;i++)
{
if((inFix.top() >= 'a' && inFix.top() <= 'z')||(inFix.top() >= 'A' && inFix.top() <= 'Z'))
{
postFixString.push_back(inFix.top());
inFix.pop();
}
else if(inFix.top() == '(')
{
postFix.push(inFix.top());
inFix.pop();
}
else if(inFix.top() == ')')
{
while(postFix.top() != '(')
{
postFixString.push_back(postFix.top());
postFix.pop();
}
if(postFix.top()=='(')
{
postFix.pop();
}
inFix.pop();
}
else if(inFix.top() == '^' || inFix.top() == '*' || inFix.top() == '/' || inFix.top() == '+' || inFix.top() == '-')
{
if(postFix.empty())
{
postFix.push(inFix.top());
inFix.pop();
}
else if(postFix.empty()!=1)
{
while((prec(postFix.top())>=prec(inFix.top())))
{
char c = postFix.top();
postFix.pop();
postFixString.push_back(c);
}
postFix.push(inFix.top());
inFix.pop();
}
}
}
int s = postFix.size();
for(int i=0;i<s;i++)
{
postFixString.push_back(postFix.top());
postFix.pop();
}
}
And this works
Expressions test;
char infix[] = ("(A+B)*(C+D)");
for(int i=(sizeof(infix)-1);i>=0;i--)
{
test.inFix.push(infix[i]);
}
test.inFixtoPostFix();
std::cout<<"\n";
for(int i=0; i<test.postFixString.size(); i++)
{
printf("%c ",test.postFixString[i]);
}
std::cout<<"\n";
But this doesn't, does not output anything at all
Expressions test2;
char infix2[] = ("A+B*C+D");
for(int i=(sizeof(infix2)-1);i>=0;i--)
{
test2.inFix.push(infix2[i]);
}
test2.inFixtoPostFix();
std::cout<<"\n";
int s = test2.postFixString.size();
for(int i=0; i<s; i++)
{
std::cout<<(test2.postFixString[i]) ;
}
std::cout<<"\n";
Since every time I ran the second test case the command window studderes so I suspect there's some kind of memory issue but have not idea where. I had a lot of outputs in the actual function to debug but didn't help so I deleted those for clearity.
I am trying to figure out how to loop through a 2d array horizontally vertically, diagonally and sub diagonally to see if the array is equal to a char.if the array is equal to the char then a bool should return true, but my problem is the bool randomly returns true. the bool only returns true if the char is equal to array at a certain column and row. Here i am hard coding it in but i want to do it with for loops.
Let me know if you need any more info or if something needs to be explained
if (mCard[0][0] == '*' && mCard[0][1] == '*'&& mCard[0][2] == '*'&& mCard[0][3] == '*' && mCard[0][4] == '*') {
CheckBingo = true;
}
if (mCard[1][0] == '*' && mCard[1][1] == '*'&& mCard[1][2] == '*'&& mCard[1][3] == '*' && mCard[1][4] == '*') {
CheckBingo = true;
}
if (mCard[2][0] == '*' && mCard[2][1] == '*'&& mCard[2][2] == '*'&& mCard[2][3] == '*' && mCard[2][4] == '*') {
CheckBingo = true;
}
if (mCard[3][0] == '*' && mCard[3][1] == '*'&& mCard[3][2] == '*'&& mCard[3][3] == '*' && mCard[3][4] == '*') {
CheckBingo = true;
}
if (mCard[4][0] == '*' && mCard[4][1] == '*'&& mCard[4][2] == '*'&& mCard[4][3] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
if (mCard[0][0] == '*' && mCard[1][0] == '*'&& mCard[2][0] == '*'&& mCard[3][0] == '*' && mCard[4][0] == '*') {
CheckBingo = true;
}
if (mCard[0][1] == '*' && mCard[1][1] == '*'&& mCard[2][1] == '*'&& mCard[3][1] == '*' && mCard[4][1] == '*') {
CheckBingo = true;
}
if (mCard[0][2] == '*' && mCard[1][2] == '*'&& mCard[2][2] == '*'&& mCard[3][2] == '*' && mCard[4][2] == '*') {
CheckBingo = true;
}
if (mCard[0][3] == '*' && mCard[1][3] == '*'&& mCard[2][3] == '*'&& mCard[3][3] == '*' && mCard[4][3] == '*') {
CheckBingo = true;
}
if (mCard[0][4] == '*' && mCard[1][4] == '*'&& mCard[2][4] == '*'&& mCard[3][4] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
// checking for daigonal
if (mCard[0][0] == '*' && mCard[1][1] == '*'&& mCard[2][2] == '*'&& mCard[3][3] == '*' && mCard[4][4] == '*') {
CheckBingo = true;
}
// checking for sub diagonal
if (mCard[4][0] == '*' && mCard[3][1] == '*'&& mCard[2][2] == '*'&& mCard[1][3] == '*' && mCard[0][4] == '*') {
CheckBingo = true;
}
here I tried using for loops but it does int always return true if the array is equal to the char.
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 5; col++) {
if(mCard[row][col] == '*') {
CheckBingo = true;
}
else {
CheckBingo = false;
break;
}
}
}
for(int row = 0; row < 5; row++) {
for(int col = 0; col < 5; col++) {
if(mCard[col][row] == '*') {
CheckBingo = true;
}
else {
CheckBingo = false;
break;
}
}
}
mCard[col][row]
...
row and col have to be flipped.
You then start the loop assuming that match is set. If one of the cells is not matched, then match is false. See example below.
Make sure the values are initialized.
int main()
{
char mCard[5][5] = {
'*','x','*','*','*',
'*','*','*','*','*',
'*','*','*','*','*',
'*','*','x','*','*',
'*','*','*','*','*',
};
bool CheckBingo = true;
for(int row = 0; row < 5; row++)
{
CheckBingo = true;
for(int col = 0; col < 5 && CheckBingo; col++)
{
if(mCard[row][col] != '*')
CheckBingo = false;
}
if(CheckBingo)
cout << "bingo on row " << row << "\n";
}
bool match = true;
for(int row = 0; row < 5 && match; row++)
for(int col = 0; col < 5 && match; col++)
if(mCard[row][col] != '*')
match = false;
if(match) cout << "bingo for all lines\n";
match = true;
for(int i = 0; i < 5 && match; i++)
if(mCard[i][i] != '*')
match = false;
if(match) cout << "top-left to bottom-right match\n";
match = true;
for(int i = 0; i < 5 && match; i++)
if(mCard[i][4-i] != '*')
match = false;
if(match) cout << "top-right to bottom-left match\n";
return 0;
}