Question

C++ Write a rudimentary spreadsheet program(include main). Display a grid of cells with columns A through...

C++

Write a rudimentary spreadsheet program(include main). Display a grid of cells with columns A through H and rows 1 through 20. Accept input in the first row of the screen. The commands are of the form column row entry, where entry is a number, a cell address preceded by a plus (e.g., +A5), a string, or a function preceded by an at sign, @. The functions are: max, min, avg, and sum. During execution of your program, build and modify the graph reflecting the situation in the spreadsheet. Show the proper values in proper cells. If a value in a cell is updated, then the values in all cells depending on it should also be modified. For example, after the following sequence of entries:

A1 10
B1 20
A2 30
D1 +A1
C1 @sum(A1..B2) D1 +C1

both cells C1 and D1 should display the number 60.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#C++ Program For a rudimentary spreadsheet:

#Code:

#include <iostream>
#include <cctype>
#include <cstdlib>
#include <vector>
#include <list>
#include <algorithm>

using namespace std;

class Variable
{
public:
char id;
int exp;
Variable()
{
}
Variable(char c, int i)
{
id = c;
exp = i;
}
bool operator== (const Variable& v) const
{
return id == v.id && exp == v.exp;
}
};
class Term
{
public:
Term()
{
coeff = 0;
}
int coeff;
vector<Variable> vars;
bool operator== (const Term&) const;
bool operator!= (const Term& term) const
{
return !(*this == term);
}
bool operator< (const Term&) const;
bool operator> (const Term& term) const
{
return *this != term && (*this < term);
}
int min(int n, int m) const
{
return (n < m) ? n : m;
}
};
class Polynomial
{
public:
Polynomial()
{ }
Polynomial operator+ (const Polynomial&) const;void error(char *s)
{
cerr << s << endl;
exit(1);
}
private:
list<Term> terms;
friend istream& operator>> (istream&, Polynomial&);
friend ostream& operator<< (ostream&, const Polynomial&);
};
// IMPLICATION .cpp file
bool Term::operator== (const Term& term) const
{
for (int i = 0; i < min(vars.size(), term.vars.size()) &&
vars[i] == term.vars[i]; i++)
{
return i == vars.size() && vars.size() == term.vars.size();
}
}
bool Term::operator< (const Term& term2) const
{
if (vars.size() == 0)
return false;
else
if (term2.vars.size() == 0)
return true;
for (int i = 0; i < min(vars.size(), term2.vars.size()); i++)
if (vars[i].id < term2.vars[i].id)
return true;
else
if (term2.vars[i].id < vars[i].id)
return false;
else
if (vars[i].exp < term2.vars[i].exp)
return true;
else
if (term2.vars[i].exp < vars[i].exp)
return false;
return ((int)vars.size() - (int)term2.vars.size() < 0) ? true : false;
}
Polynomial Polynomial::operator+ (const Polynomial& polyn2) const
{
Polynomial result;
list<Term>::iterator p1, p2;
bool erased;
for (p1 = terms.begin(); p1 != terms.end(); p1++)
result.terms.push_back(*p1);
for (p1 = polyn2.terms.begin(); p1 != polyn2.terms.end();)
{
for (p2 = p1, p2++, erased = false; p2 != result.terms.end(); p2++)
if (*p1 == *p2)
{
p1->coeff += p2->coeff;
result.terms.erase(p2);
if (p1->coeff == 0)
result.terms.erase(p1);
erased = true;
}
if (erased)
p1 = result.terms.begin();
else
p1++;
}
result.terms.sort();
return result;
}
istream& operator>> (istream& in, Polynomial& polyn)
{
char ch, sign, coeffUsed, id;
int exp;
Term term;
in >> ch;while (true)
{
coeffUsed = 0;
if (!isalnum(ch) && ch != ';' && ch != '-' && ch != '+')
polyn.error("Wrong character entered2 ");
sign = 1;
while (ch == '-' || ch == '+')
{
if (ch == '-')
sign *= -1;
ch = in.get();
if (isspace(ch))
in >> ch;
}
if (isdigit(ch))
{
in.putback(ch);
in >> term.coeff;
ch = in.get();
term.coeff *= sign;
coeffUsed = 1;
}
else
term.coeff = sign;
for (int i = 0; isalnum(ch); i++)
{
id = ch;
ch = in.get();
if (isdigit(ch))
{
in.putback(ch);
in >> exp >> ch;
}
else
exp = 1;
term.vars.push_back(Variable(id, exp));
// removed }
polyn.terms.push_back(term);
term.vars.resize(0);
if (isspace(ch))
in >> ch;
if (ch == ';')
if (coeffUsed || i > 0)
break;
else
polyn.error("Term is missing");
else
if (ch != '-' && ch != '+')
polyn.error("wrong character entered");
}
} // added }
for (list<Term>::iterator i = polyn.terms.begin(); i != polyn.terms.end(); i++)
if (i->vars.size() > 1)
sort(i->vars.begin(), i->vars.end());
return in;
}
ostream& operator<< (ostream& out, const Polynomial& polyn)
{
int afterFirstTerm = 0, i;
for (list<Term>::iterator pol = polyn.terms.begin();
pol != polyn.terms.end(); pol++)
{
out.put(' ');
if (pol->coeff < 0)
out.put('-');
else
if (afterFirstTerm)
out.put('+');
afterFirstTerm++;
if (abs(pol->coeff) != 1)
out << ' ' << abs(pol->coeff);
else
if (pol->vars.size() == 0)
out << " 1";
else
out.put(' ');
for (i = 1; i <= pol->vars.size(); i++)
{
out << pol->vars[i - 1].id;
if (pol->vars[i - 1].exp != 1)
out << pol->vars[i - 1].exp;
}
}
out << endl;
return out;
}

// MAIN FILE
int main()
{
Polynomial polyn1, polyn2;cout << "Enter two polynomials, each ended with a semicolon: \n";
cin >> polyn1 >> polyn2;
cout << "The result is: \n" << polyn1 + polyn2;system("pause");
return 0;
}

#Note: Hope This Code Is Usefull For You. For Any Doubts And Queries Related To Code You May Comment Down Below. Happy Coading. Thank u.

Add a comment
Know the answer?
Add Answer to:
C++ Write a rudimentary spreadsheet program(include main). Display a grid of cells with columns A through...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a C++ program that uses a two dimensional array to display a table of probabilities...

    Write a C++ program that uses a two dimensional array to display a table of probabilities for a pair of rolling dice. Your custom assigned range of values of each die are: 5 up to and including 10. Section 1 of Program - Specifications: The top row of the table, left to right, and the left column of the array, top to bottom, must contain the assigned range of values displayed on each of the die in ascending order populated...

  • Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’,...

    Write a JAVA program to solve a sudoku! Given a partially filled 9×9 2D array ‘grid[9][9]’, the goal is to assign digits (from 1 to 9) to the empty cells so that every row, column, and subgrid of size 3×3 contains exactly one instance of the digits from 1 to 9. I have posted 3 input files: sudoku1.txt, sudoku2.txt and sudoku3.txt Problem Analysis & Design - Think about how you will need to solve this problem. You should do an...

  • I am using C++ Write a program to determine if a gird follows the rules to...

    I am using C++ Write a program to determine if a gird follows the rules to be classified as a Lo Shu Magic Square. The Lo Shu Magic Square is a grid with 3 rows and 3 columns shown in figure below. The Lo Shu Magic Square has the following properties: The grid contains the numbers 1 through 9 exactly. The sum of each row, each column, and each diagonal all add up to the same number. This is shown...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT