Order numbers in c++ and recognize the repeated ones - c++

I was trying to make a program in c++ that should order 3 random numbers (the user will write them) and then print which is the higher and the lower, but if two numbers or more are equal should print it.
New code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all numbers are equal";
}
else if (num1 == num2){
cout << "num1 and num2 are equal";
}
else if (num2 == num3){
cout << "num2 and num3 are equal";
}
else if(num3 == num1){
cout << "num1 and num3 are equal";
}
else{
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "higher is num1";
}
else if(num2 > num1 && num2 > num3){
cout << "higher is num2";
}
else if(num3 > num1 && num3 > num2){
cout << "higher is num3";
}
}
}
return 0;
}
New problem:
The programm needs to know which is the lowest too, so how can I do that?
Old code:
#include <iostream>
using namespace std;
int main()
{
int num1;
int num2;
int num3;
cout << "num1" << endl;
cin >> num1;
cout << "num2" << endl;
cin >> num2;
cout << "num3" << endl;
cin >> num3;
if(num1 == num2 && num3==num2 && num1==num3){
cout << "all your numbers are equal";
}
if (num1 != num2 && num2 != num3 && num3 != num1){
if (num1 > num2 && num1 > num3){
cout << "num1";
}
else if(num2 > num1 && num2 > num3){
cout << "num2";
}
else /*(num3 > num1 && num3 > num2)*/{//Here I tried to use and else if
cout << "num3";
}
}
return 0;
}
Old problem
This code is all wrong, but I don't know what I'm doing wrong, please help me.
And I have a last question, do I have a limit of if's into a if sentence? or I just can't write two else if or..? Thanks.

#include <iostream>
#include <set>
int main()
{
std::set<int> numbers;
int input;
for (int i=1; i<=3; ++i) {
std::cout << "Enter number " << i << ": ";
std::cin >> input;
numbers.insert(input);
}
if (numbers.size() < 3) {
std::cout << "You entered the same number more than once, silly!" << std::endl;
std::cout << "Nevertheless, ";
}
std::cout << "the maximum number is " << *numbers.rbegin() << std::endl;
}
The important feature of this code is that it takes advantage of the properties of std::set which are:
It keeps all its elements in sorted order
It does not allow duplicate elements
std::set::rbegin is used to get the last element, which is the largest number (because the numbers are automatically sorted).
This code does not display which numbers are the largest and smallest, but this can easily be added. For example, std::set::insert returns information that can allow you to determine which insertion failed in the set. An std::set can contain only single copies of its contained objects. Therefore, if the user enters a number more than once the calls to insert will fail.

The following piece of code solves your purpose. It has three functions findLargest, findSmallest and checkEqual. This is a very basic program and can be modified to your needs on how to display and what to return etc etc.
#include <iostream>
using namespace std;
void findLargest(int n1,int n2,int n3)
{
if(n1>n2 && n1>n3)
{
cout<<"Largest number is :"<<n1;
cout<<"\n";
}
else if((n2>n1) && (n2>n3))
{
cout<<"Largest number is :"<<n2;
cout<<"\n";
}
else
{
cout<<"Largest number is :"<<n3;
cout<<"\n";
}
}
void findSmallest(int n1,int n2,int n3)
{
if(n1<=n2 && n1<=n3)
{
cout<<"Smallest number is :"<<n1;
cout<<"\n";
}
else if((n2<=n1) && (n2<=n3))
{
cout<<"Smallest number is :"<<n2;
cout<<"\n";
}
else
{
cout<<"Smallest number is :"<<n3;
cout<<"\n";
}
}
int checkEqual(int n1,int n2,int n3)
{
if(n1==n2 && n2==n3 && n3==n1)
{
cout<<"All three are equal";
cout<<"\n";
}
else if(n1==n2||n2==n3||n3==n1)
{
cout<<"Two numbers are equal";
cout<<"\n";
}
else
{
cout<<"None are equal.. Finding Largest and Smallest....!!";
cout<<"\n";
}
}
int main() {
int num1,num2,num3;
cout<<"Enter the numbers";
cin>>num1>>num2>>num3;
cout<<"\n";
checkEqual(num1,num2,num3);
findSmallest(num1,num2,num3);
findLargest(num1,num2,num3);
return 0;
}
Ideone link: http://ideone.com/hz4keQ
Hope it helps. :)

Related

Program to calculate test scores [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 12 months ago.
Improve this question
I am writing a program to calculate the grade of 3 test scores. The lowest of the first 2 scores is dropped and added to the third test score to make the final grade. The 3 test scores cannot be higer than 50, lower than 0 and cannot be a character or string. So far, I have satisified all those requirment but I need to implement decimal grades to the program like for instance 45.5. Also to round the final grade up or down. For example if final grade is 89.5 round up to an A.
#include <iostream>
#include <algorithm>
#include <cstdlib>
using namespace std;
char getGrade(int num) {
if (num < 60)
return 'F';
if (num < 69)
return 'D';
if (num < 79)
return 'C';
if (num < 89)
return 'B';
return 'A';
}
bool isnumeric(string temp) {
for (char &chr : temp) {
if ((chr >= '0' and chr <= '9') or chr == '-')
continue;
else
return false;
}
return true;
}
int main(int argc, char const *argv[]) {
cout << "Welcome to the grade calculator.You will input three test "
"scores.\nThe highest of the first two grades and the third grade "
"will be\nadded together to determine the numeric grade average for "
"the\ncourse.Each test score has a maximum of 50 points.\n";
int arr[3];
int ctr = 0;
string temp;
int num;
while (ctr < 3) {
cout << "\nPlease enter test score " << (ctr + 1) << ": ";
label1:
cin >> temp;
if (isnumeric(temp)) {
num = atoi(temp.c_str());
if (num > 50) {
cout << "\nTest scores cannot be higher than 50, try again: ";
goto label1;
} else if (num < 0) {
cout << "\nTest scores cannot be negative, try again: ";
goto label1;
} else {
arr[ctr++] = num;
}
} else {
cout << "\nInvalid test score entered, try again: ";
goto label1;
}
}
int average = 0;
average = max(arr[0], arr[1]);
average = average + arr[2];
cout << "\nThe average for the course = " << average << "\n";
cout << "The letter grade = " << getGrade(average);
cout << "\n\n\nThank you for using this program\n";
return 0;
}
Just changed a couple of things to make it work with decimals:
1. Added chr == '.' to the isNumeric() function:
bool isnumeric(string temp) {
for (char& chr : temp) {
if ((chr >= '0' and chr <= '9') or chr == '-' or chr == '.')
continue;
else return false;
}
return true;
}
2. Changed variable types:
double arr[3]{};
int ctr = 0;
std::string temp;
double num;
3. Removed goto: (You can just use continue)
while (ctr < 3) {
std::cout << "\nPlease enter test score " << (ctr + 1) << ": ";
std::cin >> temp;
if (isnumeric(temp)) {
num = atof(temp.c_str());
if (num > 50) {
std::cout << "\nTest scores cannot be higher than 50, try again: ";
continue;
}
else if (num < 0) {
std::cout << "\nTest scores cannot be negative, try again: ";
continue;
}
else {
arr[ctr++] = num;
}
}
else {
std::cout << "\nInvalid test score entered, try again: ";
continue;
}
}
4. For rounding off, you can use std::round() as such:
double average = 0;
average = std::max(arr[0], arr[1]);
average = std::round(average + arr[2]);
You can also change your cout statements:
std::cout << "\nThe average for the course = " << average;
if (std::round(average) != average) std::cout << ", rounded off to = " << std::round(average);
std::cout << ".\nThe letter grade = " << getGrade(average);
std::cout << "\n\n\nThank you for using this program\n";
Just make all these changes and your program will successfully work with decimals.
Also, consider not using the following in your code:
using namespace std;
..as it's considered as a bad practice. For more info on why, look up to Why is using namespace std considered as a bad practice.
Edit: To accomplish your requirement, you can just change the while loop as such:
while (ctr < 3) {
if (temp.size() == 0)
{
std::cout << "\nPlease enter test score " << (ctr + 1) << ": ";
std::cin >> temp;
}
if (isnumeric(temp)) {
num = atof(temp.c_str());
if (num > 50) {
std::cout << "\nTest scores cannot be higher than 50, try again: ";
std::cin >> temp;
continue;
}
else if (num < 0) {
std::cout << "\nTest scores cannot be negative, try again: ";
std::cin >> temp;
continue;
}
else {
arr[ctr++] = num;
temp.clear();
}
}
else {
std::cout << "\nInvalid test score entered, try again: ";
std::cin >> temp;
continue;
}
}
The above code works as you said.

My code is not showing an output when I am trying to find the largest number among three numbers in C++

This Is My Code
I Am A Beginner
Whenever I run my code it takes the input for the three numbers but sometimes mostly when the given numbers are small is doesn't give back an output.
#include <iostream>
using namespace std;
int main()
{
int num1, num2, num3;
cout << "Enter First Number" << endl;
cin >> num1;
cout << "Enter Second Number" << endl;
cin >> num2;
cout << "Enter Third Number" << endl;
cin >> num3;
if (num1 > num2)
{
if (num1 > num3)
{
cout << "First Number Is The Largest";
}
}
else if (num2 > num1)
{
if (num2 > num3)
{
cout << "Second Number Is The Largest";
}
}
else
{
cout << "Third Number Is The Largest";
}
return 0;
}
My thought is you're trying to get more complicated then necessary:
if (num1 >= num2 && num1 >= num3)
{
cout << "First Number Is The Largest";
}
else if (num2 >= num1 && num2 >= num3)
{
cout << "Second Number Is The Largest";
}
else
{
cout << "Third Number Is The Largest";
}
In this case, you are not testing equal numbers, e.g: num1 >= num2. Also:
if (num1 > num2)
{
if (num1 > num3)
{
cout << "First Number Is The Largest";
}
}
You check if num1 > num2, but if num3 == num 1 or num 3 > num1 it will not appear a message. Try to put a message in the first if to show if it doesn't enter in second if, and then you can edit all the conditions.
I don't know if it's a requirement that you say "First", "Second", or "Third", but if it's not, your code can be simplified down to something like this (this assumes you haven't learned about loops yet):
#include <iostream>
int main() {
int biggest;
int input;
std::cout << "First number: ";
std::cin >> biggest;
std::cout << "Second number: ";
std::cin >> input;
if (input > biggest) biggest = input;
std::cout << "Third number: ";
std::cin >> input;
if (input > biggest) biggest = input;
std::cout << "Largest number was " << biggest << '\n';
}
Output:
~/tmp
❯ ./a.out
First number: 25
Second number: 64
Third number: 45
Largest number was 64
The first number is de-facto the biggest when it's read because it's the only number we know. After that, we read into a different variable, input so that we can compare against biggest.
There's some repetition though, and this method falls apart pretty quickly as the number of numbers to check grows.
If you know about loops and arrays, your code can be made smaller:
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
const std::vector<std::string> ordinals{"First", "Second", "Third"};
int biggest = std::numeric_limits<int>::min();
for (const auto& i : ordinals) {
int input;
std::cout << i << " number: ";
std::cin >> input;
if (input > biggest) biggest = input;
}
std::cout << "Largest number was " << biggest << '\n';
}
We now have to initialize biggest, so we give the lowest possible value an int can hold. Our loop reads into input and compares against biggest. We have the same effect, however, in that biggest is always replaced by the first value we enter.
If you are required to actually say "First", etc., it's now trivial to do as well.
#include <iostream>
#include <limits>
#include <string>
#include <vector>
int main() {
const std::vector<std::string> ordinals{"First", "Second", "Third"};
std::string ordinalWord;
int biggest = std::numeric_limits<int>::min();
for (const auto& i : ordinals) {
int input;
std::cout << i << " number: ";
std::cin >> input;
if (input > biggest) {
biggest = input;
ordinalWord = i;
}
}
std::cout << ordinalWord << " number was largest. Value: " << biggest << '\n';
}
Output:
~/tmp
❯ ./a.out
First number: 25
Second number: 64
Third number: 45
Second number was largest. Value: 64
Again, this would fall apart if we had much more than three numbers to compare. So instead of words like "First" and so on, we could modify the program to print ordinal numbers.
#include <iostream>
#include <limits>
#include <random> // For simulating larger sets
#include <string>
#include <vector>
std::string ordinal_suffix(int num) {
num %= 10;
if (num == 1) return "st";
if (num == 2) return "nd";
if (num == 3) return "rd";
return "th";
}
int main() {
// *** Automation setup and execution
constexpr int setSize = 10'000;
std::mt19937 prng(std::random_device{}());
std::uniform_int_distribution<int> range(1, 100'000);
std::vector<int> inputs;
for (int i = 0; i < setSize; ++i) {
inputs.push_back(range(prng));
}
// *** End of automation setup and execution
int biggest = std::numeric_limits<int>::min();
int location = -1;
for (std::size_t i = 0; i < inputs.size(); ++i) {
if (inputs[i] > biggest) {
biggest = inputs[i];
location = i;
}
}
std::cout << location + 1 << ordinal_suffix(location + 1)
<< " number was largest. Value: " << biggest << '\n';
}
Outputs:
~/tmp
❯ ./a.out
8725th number was largest. Value: 99944
~/tmp
❯ ./a.out
433rd number was largest. Value: 100000
~/tmp
❯ ./a.out
7674th number was largest. Value: 99987
~/tmp
❯ ./a.out
891st number was largest. Value: 99995

Find whether a random number is Prime or not and Store in a character value

I am writing a game, where it should do below
Randomly generate a number. Check whether it is a Prime or not. Store "y" if its a Prime else "n"
I will ask the user whether it is prime or not. If he answers correctly I will proceed. Otherwise I will say you lost.
Code sample I tried is as follows:
cout<<"\n \n a no. of prime no.s will be displaed to you ,you will have to decide if it is prime or not within the given time\n \n ";
char ch1,ch2;
for(int i=1;i<=20;i++)
{
int a= rand( ) % 20;
cout<< "The number is:";
cout<<a;
for(int l=2;l<=a-1;l++)
{
if(a%l==0)
ch2='n';
else
ch2='y';
}
cout<<ch2;
cout<<"\n\n Enter ""y"" If the number is Prime Else Please enter ""n""\n";
cin>>ch1;
if(ch1 == ' ')
{
cout<<"\n \n u lost";
break;
}
else if(ch1==ch2)
continue;
else if(ch1!=ch2)
{
cout<<"\n \n u lost";
break;
}
}
My code is not determining whether it is Yes or No. It is everytime storing "Y" only
You made a couple of mistakes
#include <math.h>
#include <iostream>
using namespace std;
int main() {
char ch1, ch2;
for (int i=i; i<=20; i++) {
int a = rand () % 20;
cout << "The number is: " << a << endl;
ch2 = 'y';
//You only need to loop until its square root; after that is repetition
for (int l=2; l<=sqrt(a); l++) {
//You need to break if a divisor is found, else it is always going to say it's prime
if (a%l==0){
ch2 = 'n';
break;
}
}
cout << ch2 << endl;
cout << "Enter 'y' if number is prime else 'n'" << endl;
cin >> ch1;
if (ch1 != ch2) {
cout << "You lost" << endl;
break;
}
}
}
I also did some cleanup of your code
May be this one helps
#include <iostream>
#include <ctype.h>
#include <string.h>
using namespace std;
int main()
{
int n, i;
char res='y';
char ans;
n =rand( ) % 20;
cout << "The Number is:" << n << endl;
for(i = 2; i <= n / 2; ++i)
{
if(n % i == 0)
{
res='n';
break;
}
}
cout << "Enter 'y' if it is a prime number and 'n' if it is a non-prime number"<< endl;
cin >> ans;
ans = tolower(ans);
if (ans==res)
{cout << "You win";}
else
{cout << "You Lost";}
return 0;
}

C++ Do-while loop stopping

I got an assignment where we make a cmd prompt show up and display a flashcard game for multiplication. After inputting a correct answer a prompt shows up and asks the user to go "Again? Y/N." after the second input answer the prompt to ask the user doesn't show up and it's stuck on a "congratulations" message. This happens when I write in code to randomly generate two numbers for the game twice. one outside the while loop, and one inside while loop. If I leave one out the 2nd code for the random numbers it will run fine but will only display the same numbers over again. what I'm asking is how do I fix it so that it won't get stuck after the second answer input?
sample code below:
#include <iostream>
using namespace std;
int main()
{
int num1, num2, ans, guess, count = 0;
char choice;
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
//first number generator.
ans = num1 * num2;
do
{
{
cout << num1 << " X " << num2 << " = ";
cin >> guess;
cout << "Wow~! Congratulations~! ";
count++;
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
//second number generator.
} while (guess != ans);
cout << "\nAgain? Y/N: ";
cin >> choice;
} while ((choice == 'y') || (choice == 'Y'));
//after two turns the loop stops. Can't make a choice.
cout << " Thanks for playing! Number of tries:" << count << endl;
return 0;
}
I'd guess the problem is because your loops aren't quite what you think they are.
do
{
The code above has started a do loop.
{
I suspect you intended to start another (nested) do loop here--but you left off the do, so it's just a block that gets entered, executed, and exited. Useless and pointless in this case.
cout << num1 << " X " << num2 << " = ";
cin >> guess;
cout << "Wow~! Congratulations~! ";
count++;
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
//second number generator.
} while (guess != ans);
You've formatted this as if the while were closing the nested do loop--but since you didn't actually create a nested do loop, this is just a while loop with an empty body. Its meaning would be more apparent with a little re-formatting:
// second number generator
}
while (guess != ans)
/* do nothing */
;
The problem can be found here:
do
{
{
cout << num1 << " X " << num2 << " = ";
cin >> guess;
As you can see, the second scope has no do statement. As a result it is only a codeblock.
You can solve it by writing a do statement for the second code block.
Because the do is not present in the second bracket ({), the while is interpreted as a while loop:
while (guess != ans);
or
while (guess != ans) {
}
this thus keeps looping until guess is not equal to ans. But since in the loop does not modify any of the two variables, the loop will keep iterating.
Other errors: note that the program is still incorrect, since it will claim you have answered the question, regardless of the answer. You can fix it by implementing this as follows:
int main()
{
int num1, num2, ans, guess, count = 0;
char choice;
do {
num1 = rand() % 12 + 1;
num2 = rand() % 12 + 1;
ans = num1 * num2;
do {
cout << num1 << " X " << num2 << " = ";
cin >> guess;
if(guess == ans) {
cout << "Wow~! Congratulations~! ";
} else {
cout << "No, wrong!\n";
}
count++;
} while (guess != ans);
cout << "\nAgain? Y/N: ";
cin >> choice;
} while ((choice == 'y') || (choice == 'Y'));
//after two turns the loop stops. Can't make a choice.
cout << " Thanks for playing! Number of tries:" << count << endl;
return 0;
}

Creating a calculator c++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
My current code is:
#include<iostream>
#include<cmath>
#include<cstdlib>
using namespace std;
int main()
{
char cAgain, type;
int x, y=0;
double addition(double num1, double num2), subtraction(double num1, double num2), division(double num1, double num2),
multiplication(double num1, double num2), exponential(double num1, double num2), num1, num2, total;
do
{
cout << "How many operations would you like to do?" << endl;
cin >> x;
if (x <= 0)
{
cout << "Error: You must have 1 or more operations." << endl;
}
else
{
while (x != 0, x--)
{
y++;
cout << "Enter in your "<<y<< " operation. (First Number (+,-,*,/,^) Second Number)" << endl;
cin >> num1 >> type >> num2;
switch (type)
{
case '+':
total = addition(num1, num2);
cout << num1 << " + " << num2 << "= " << total<< endl;
break;
case'-':
total = subtraction(num1, num2);
cout << num1 << " - " << num2 << "= " << total<< endl;
break;
case'/':
total = division(num1, num2);
cout << num1 << " / " << num2 << "= " << total<< endl;
break;
case'*':
total = multiplication(num1, num2);
cout << num1 << " * " << num2 << "= " << total << endl;
break;
case'^':
total = exponential(num1, num2);
cout << num1 << " ^ " << num2 << "= " << total << endl;
break;
}
}
}
cout << "Would you like to run the program again.(Y/N)" << endl;
cin >> cAgain;
} while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
double addition(double num1, double num2)
{
double total;
total = num1 + num2;
return (total);
}
double subtraction(double num1, double num2)
{
double total;
total = num1 - num2;
return (total);
}
double division(double num1, double num2)
{
double total;
total = num1 / num2;
return (total);
}
double multiplication(double num1, double num2)
{
double total;
total = num1 * num2;
return (total);
}
double exponential(double num1, double num2)
{
double total;
total = pow(num1,num2);
return (total);
}
Currently the code works as it is suppose too.
It starts by asking
How many operations would you like to do?
You enter a number say 3
Then it ask you to enter in your operation
5+5
10
Then it ask for second
10^2
100
Then it ask for third
100-10
90
Then it ask if you want to start over and run the program again.
What I am trying to change it too is to 5+5^2-10= and it give me 20.
Essentially I want the user to be able to enter in the entire operation like 5+5^2-10= and it give me the correct answer. Allowing the user to type in a function and use '=' and return to start the calculation.
You can use following technique to solve an expression :
#include<iostream>
#include<cmath>
#include<stdio.h>
#include<cstdlib>
#include<map>
#include<stack>
using namespace std;
int expression_value(string str)
{
map<char,int>priority;
priority['^']=3;
priority['*']=2,priority['/']=2;
priority['+']=1,priority['-']=1;
stack<char>op_stack;
stack<int>val_stack;
int val=0;
for(int i=0;str[i];i++)
{
if(str[i]>='0'&&str[i]<='9')
val=val*10+str[i]-'0';
else
{
if(op_stack.empty()) // first operator
{
val_stack.push(val);
op_stack.push(str[i]);
}
else if(priority[op_stack.top()] < priority[str[i]]) // current operator is more prior then previous operator. so push it to stack.
{
val_stack.push(val);
op_stack.push(str[i]);
}
else // current operator is less prior then previous operator. so calculate previous operators resultant value
{
int num1,num2;
num1=val_stack.top(); val_stack.pop();
num2=val;
if(op_stack.top()=='+')
//val_stack.push(addition(num1, num2));
val_stack.push(num1 + num2);
else if(op_stack.top()=='-')
//val_stack.push(subtraction(num1, num2));
val_stack.push(num1 - num2);
else if(op_stack.top()=='*')
//val_stack.push(multiplication(num1, num2));
val_stack.push(num1 * num2);
else if(op_stack.top()=='/')
//val_stack.push(division(num1, num2));
val_stack.push(num1 / num2);
else
//val_stack.push(exponential(num1, num2));
val_stack.push(pow(num1 , num2));
op_stack.pop(); // as operator's value calculation done, pop it from the stack
op_stack.push(str[i]); // push the new operator
}
val=0;
}
}
val_stack.push(val); // last value
// calculate remaining operators value
while(!op_stack.empty())
{
int num1,num2;
num2=val_stack.top(); val_stack.pop();
num1=val_stack.top(); val_stack.pop();
if(op_stack.top()=='+')
//val_stack.push(addition(num1, num2));
val_stack.push(num1 + num2);
else if(op_stack.top()=='-')
//val_stack.push(subtraction(num1, num2));
val_stack.push(num1 - num2);
else if(op_stack.top()=='*')
//val_stack.push(multiplication(num1, num2));
val_stack.push(num1 * num2);
else if(op_stack.top()=='/')
//val_stack.push(division(num1, num2));
val_stack.push(num1 / num2);
else
//val_stack.push(exponential(num1, num2));
val_stack.push(pow(num1 , num2));
op_stack.pop();
}
return val_stack.top();
}
Here you have to pass the whole expression as string.
Operators will be stored in op_stack and value in val_stack
You should implement a
binary expression tree
I have just modify your program to meet your requirement:
#include <iostream>
#include <cstdio>
using namespace std;
char op;
double multiplication()
{
double product;
cin>>product;
while(1)
{
cin>>op;
if(op == '*')
{
double temp;
cin>>temp;
product *= temp;
}
else if(op == '/')
{
double temp;
cin>>temp;
product /= temp;
}
else break;
}
return product;
}
double addition()
{
double sum = multiplication();
while(op != '=')
{
if(op == '+')
sum += multiplication();
else if(op == '-')
sum -= multiplication();
else
{
cout<<"Operator error"<<endl;
return 0.0;
}
}
return sum;
}
int main()
{
char cAgain;
int x;
do
{
cout << "How many operations would you like to do?" << endl;
cin >> x;
if (x <= 0)
{
cout << "Error: You must have 1 or more operations." << endl;
}
else
{
while (x != 0, x--)
{
double result = addition();
cout<<result<<endl;
}
}
cout << "Would you like to run the program again.(Y/N)" << endl;
cin >> cAgain;
} while (cAgain == 'Y' || cAgain == 'y');
return 0;
}
Calculate some expressions like a+b,a-b,a^b,a*b etc is simple. But if you want to solve expressions like this:5+5^2-10,I think you need to learn something about Reverse Polish notation . It is not very difficult . Good Luck.