Question

In: Computer Science

How to write a C++ program that lets the user enter a string and checks if...

How to write a C++ program that lets the user enter a string and checks if it is an accepted polynomial. Accepted polynomials need to have one term per degree, no parentheses, spaces ignored.

Solutions

Expert Solution

#include <iostream>
#include<cctype>
#include<algorithm>
using namespace std;
int main()
{
   string s;
   getline(cin,s);
   s.erase(remove(s.begin(),s.end(),' '),s.end());
   int d;
   int flag=0;
   if(s.length()>=4)
     { d=s[3];
       if(!isdigit(d))
       {
         flag=1;
       }
     }
   for(int i=0;i<s.length();i++)
   {
      if(s[i]=='('|| s[i]==')')
      {
          flag=1;
          break;
      }
      else if(s[i]==' ')
        continue;
     else if(isdigit(s[i]) && (i+4)<s.length())
      {
         if(s[i+1]=='x')
         {  
             if(s[i+2]=='^')
              {
                  if(isdigit(s[i+3]))
                  {   
                      if(d==s[i+3])
                     {
                         d--;
                      if(s[i+4]=='+' || s[i+4]=='-')
                      {
                          i+=4;
                      }
                      else
                      {
                          flag=1;
               
                          break;
                      }
                      
                     }
                     else
                     {
                         flag=1;
                    
                         break;
                     }
                  }
                  else
                  {
                      flag=1;
                     
                      break;
                  }
              }
              else
              {
                  flag=1;
                
                  break;
              }
         }
         else
         {
             flag=1;
           
             break;
         }
      }
      else if(i==s.length()-1)
        {
            if(!isdigit(s[i]))
            {
                flag=1;
               
                break;
            }
        }
      else
      {
          flag=1;
          
          break;
      }
   }
   if(flag==1 || !isdigit(s[s.length()-1]))
    cout<<"Not Accepted";
   else
    cout<<"Accepted";
   return 0;
}

OUTPUT:

NOTE:

STRING SHOULD BE OF FORMAT LIKE ax^3+bx^2+cx^1+d

where a,b,c,d are numeric values.


Related Solutions

C++ Write a program that lets the user enter a two letters which are f and...
C++ Write a program that lets the user enter a two letters which are f and s with a length of 5. And outputs how many times it was occurred and lists the 2 most repeating pattern with 5lengths of f and s. The output display should always start at three-f(s) .Include an option where user can retry the program. Example: Input: sssfsfsfssssfffsfsssssfffsffffsfsfssffffsfsfsfssssfffffsffffffffffffssssssssfffsffffsssfsfsfsfssssfffsssfsfsffffffssssssffffsssfsfsfsss Output: The most repeating 5lengths of pattern is: fffsf and occurred 6times. Output2: The second most repeating...
Write a C++ program that lets the user enter the total rainfall for each of 12...
Write a C++ program that lets the user enter the total rainfall for each of 12 months (starting with January) into an array of doubles. The program should calculate and display (in this order): the total rainfall for the year,     the average monthly rainfall,     and the months with the highest and lowest amounts. Months should be expressed as English names for months in the Gregorian calendar, i.e.: January, February, March, April, May, June, July, August, September, October, November,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol,...
Write a program in C that lets the user enter a message using SMS Language(e.g. lol, omg, omw etc.), then translates it into English (e.g. laughing out loud, oh my god, on my way etc.). Also provide a mechanism to translate text written in English into SMS Language. needs to be able to translate at least 10 sms words
How would I write a program for C++ that checks if an entered string is an...
How would I write a program for C++ that checks if an entered string is an accepted polynomial and if it is, outputs its big-Oh notation? Accepted polynomials can have 3 terms minimum and no decimals in the exponents.
Time Calculator Create a C++ program that lets the user enter a number of seconds and...
Time Calculator Create a C++ program that lets the user enter a number of seconds and produces output according to the following criteria: • There are 60 seconds in a minute. If the number of seconds entered by the user is greater than or equal to 60, the program should display the number of minutes in that many seconds. • There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or...
Write a program that lets the user enter the loan amount, number of years, and interest...
Write a program that lets the user enter the loan amount, number of years, and interest rate and displays the amortization schedule for the loan. Steps: 1) Create scanner 2) Prompt the user to enter loan amount and declare double variable and relate to scanner input 3) Prompt the user to enter number of years and declare integer years and relate to scanner input 4) Prompt the user to enter annual interest rate and declare double variable and relate to...
Write a C++ Program Write a program that prompts the user to input a string. The...
Write a C++ Program Write a program that prompts the user to input a string. The program then uses the function substr to remove all the vowels from the string. For example, if str=”There”, then after removing all the vowels, str=”Thr”. After removing all the vowels, output the string. Your program must contain a function to remove all the vowels and a function to determine whether a character is a vowel. You must insert the following comments at the beginning...
Write a java program that lets the user to enter any two integers and weave them...
Write a java program that lets the user to enter any two integers and weave them digit by digit and print the result of weaving their digits together to form a single number. Two numbers x and y are weaved together as follows. The last pair of digits in the result should be the last digit of x followed by the last digit of y. The second-to-the-last pair of digits in the result should be the second-to- the-last digit of...
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string....
In a file called FourCapitalizations.java, write a program that: Asks the user to enter a string. Prints out four different versions of that string: The original version of the string. The upper-case version of the string. The lower-case version of the string. A version where the character at position 0 capitalized, and all other characters in lower case. For example: if the user enters string "gaNDalF12 !! AB3w", your program output should look EXACTLY like this: Please enter a string:...
IN C This assignment is to write a program that will prompt the user to enter...
IN C This assignment is to write a program that will prompt the user to enter a character, e.g., a percent sign (%), and then the number of percent signs (%) they want on a line. Your program should first read a character from the keyboard, excluding whitespaces; and then print a message indicating that the number must be in the range 1 to 79 (including both ends) if the user enters a number outside of that range. Your program...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT