fork download
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7. string in;
  8. cin >> in; //Take input
  9.  
  10. string variable, coefficientString; //Variable declerations
  11.  
  12. for(unsigned int i = 0; i < in.length(); ++i)
  13. {
  14. unsigned int currentVal = (int)in.c_str()[i]; //Get the integer value of the current character
  15. if(currentVal > 47 && currentVal < 58) //On an ASCII table all integers are between 47 and 57, in this case a number has been entered
  16. {
  17. coefficientString += in.c_str()[i]; //All numbers are added to a string for the coefficient
  18. }
  19. else
  20. {
  21. variable += in.c_str()[i]; //All non-numerical values are being considered variables, you may want to limit this to alphabetical only
  22. }
  23. }
  24.  
  25. int coefficient = atoi(coefficientString.c_str()); //The string is then converted to an integer
  26. coefficient *= coefficient; //As an example to show this works, coefficient is squared
  27. variable += variable; //Variable is also squared, in this case it is represented by repeating the variables
  28.  
  29. cout << coefficient << variable;
  30. return 0;
  31. }
Success #stdin #stdout 0s 3432KB
stdin
6xy
stdout
36xyxy