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...
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...
Write a C++ function that lets the user enter alphabet letters into a static char array...
Write a C++ function that lets the user enter alphabet letters into a static char array until either the user enters a non-alphabet letter or, it has reached the MAXSIZE. You can use the isalpha([Char]) function to check if the input is an alphabet letter or not. void fillArray (char ar[], size_t& size){ // this is the function prototype }
IN C++ Write a program that prompts the user to enter the number of students and...
IN C++ Write a program that prompts the user to enter the number of students and each student’s name and score, and finally displays the student with the highest score (display the student’s name and score). Also calculate the average score and indicate by how much the highest score differs from the average. Use a while loop. Sample Output Please enter the number of students: 4 Enter the student name: Ben Simmons Enter the score: 70 Enter the student name:...
In Python: Design a program that lets the user enter the total rainfall for each of...
In Python: Design a program that lets the user enter the total rainfall for each of the 12 months into a list. The program should calculate and display the total rainfall for the year, the average monthly rainfall, the months with the highest and lowest amounts. However, to start, do not ask the user for input. Instead, hard code the monthly rainfalls as a list in your program, and then use the list to determine how to conduct the processing...
Checker for integer string Forms often allow a user to enter an integer. Write a program...
Checker for integer string Forms often allow a user to enter an integer. Write a program that takes in a string representing an integer as input, and outputs yes if every character is a digit 0-9. Ex: If the input is: 1995 the output is: yes Ex: If the input is: 42,000 or 1995! the output is: no Hint: Use a loop and the Character.isDigit() function. import java.util.Scanner; public class LabProgram { public static void main(String[] args) { Scanner scnr...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT