C++高手来答出来200分

用户一个算式,算式允许包括+-*/()和任意个数的十进制数字(整数或者小数),该算式为一次性输入。要求计算出算式的结果或者提示异常算式,并且将正确结果用十进制/五进制两种... 用户一个算式,算式允许包括+-*/()和任意个数的十进制数字(整数或者小数),该算式为一次性输入。要求计算出算式的结果或者提示异常算式,并且将正确结果用十进制/五进制两种方式显示出来。其中,五进制数采取以下的格式显示:
(a1 a2a3a4a5a6a7a8a9a10 a11a12a13a14a15a16)
a1为符号位,0代表正数,1代表负数
a2-a10为整数部分,十进制数的整数部分为:
a2*(5^8)+a3*(5^7)+…+a9*(5^1)+a10*(5^0)
a11-a16为小数部分,十进制数N的小数部分应该约等于(尽量精确):
a11*(5^-1)+a12*(5^-2)+…+a16^(5^-6)
例如,十进制数N=-136.56显示为五进制数(1 000001021 240000)

代码要求:
用Visual C++建立Win32工程完成,不使用MFC,并且不得使用任何额外DLL B完成上述代码。输入/输出界面任意。提供源代码。
展开
 我来答
花式码农
2007-10-09 · TA获得超过1.7万个赞
知道大有可为答主
回答量:4894
采纳率:0%
帮助的人:4748万
展开全部
// EX6_09Extended.CPP
// A program to implement a calculator accepting parentheses

#include <iostream> // For stream input/output
#include <cstdlib> // For the exit() function
#include <cctype> // For the isdigit() function
#include <cstring> // For the strcpy() function
using std::cin;
using std::cout;
using std::endl;

void eatspaces(char* str); // Function to eliminate blanks
double expr(char* str); // Function evaluating an expression
double term(char* str, int& index); // Function analyzing a term
double number(char* str, int& index); // Function to recognize a number
char* extract(char* str, int& index); // Function to extract a substring
const int MAX = 80; // Maximum expression length,
// including '\0'
int main()
{
char buffer[MAX] = {0}; // Input area for expression to be evaluated

cout << endl
<< "Welcome to your friendly calculator."
<< endl
<< "Enter an expression, or an empty line to quit."
<< endl;

for(;;)
{
cin.getline(buffer, sizeof buffer); // Read an input line
eatspaces(buffer); // Remove blanks from input

if(!buffer[0]) // Empty line ends calculator
return 0;

cout << "\t= " << expr(buffer) // Output value of expression
<< endl << endl;
}
}

// Function to eliminate spaces from a string
void eatspaces(char* str)
{
int i = 0; // 'Copy to' index to string
int j = 0; // 'Copy from' index to string

while((*(str + i) = *(str + j++)) != '\0') // Loop while character
// copied is not \0
if(*(str + i) != ' ') // Increment i as long as
i++; // character is not a space
return;
}

// Function to evaluate an arithmetic expression
double expr(char* str)
{
double value = 0.0; // Store result here
int index = 0; // Keeps track of current character position

value = term(str, index); // Get first term

for(;;) // Indefinite loop, all exits inside
{
switch(*(str + index++)) // Choose action based on current character
{
case '\0': // We're at the end of the string
return value; // so return what we have got

case '+': // + found so add in the
value += term(str, index); // next term
break;

case '-': // - found so subtract
value -= term(str, index); // the next term
break;

default: // If we reach here the string
cout << endl // is junk
<< "Arrrgh!*#!! There's an error"
<< endl;
exit(1);
}
}
}

// Function to get the value of a term
double term(char* str, int& index)
{
double value = 0.0; // Somewhere to accumulate
// the result

value = number(str, index); // Get the first number in the term

// Loop as long as we have a good operator
while((*(str + index) == '*') || (*(str + index) == '/'))
{

if(*(str + index) == '*') // If it's multiply,
value *= number(str, ++index); // multiply by next number

if(*(str + index) == '/') // If it's divide,
value /= number(str, ++index); // divide by next number
}
return value; // We've finished, so return what
// we've got
}

// Function to recognize a number in a string
double number(char* str, int& index)
{
double value = 0.0; // Store the resulting value

if(*(str + index) == '(') // Start of parentheses
{
char* psubstr = 0; // Pointer for substring
psubstr = extract(str, ++index); // Extract substring in brackets
value = expr(psubstr); // Get the value of the substring
delete[]psubstr; // Clean up the free store
return value; // Return substring value
}

while(isdigit(*(str + index))) // Loop accumulating leading digits
value = 10*value + (*(str + index++) - '0');

// Not a digit when we get to here
if(*(str + index) != '.') // so check for decimal point
return value; // and if not, return value

double factor = 1.0; // Factor for decimal places
while(isdigit(*(str + (++index)))) // Loop as long as we have digits
{
factor *= 0.1; // Decrease factor by factor of 10
value = value + (*(str + index) - '0')*factor; // Add decimal place
}

return value; // On loop exit we are done
}

// Function to extract a substring between parentheses
// (requires cstring)
char* extract(char* str, int& index)
{
char buffer[MAX]; // Temporary space for substring
char* pstr = 0; // Pointer to new string for return
int numL = 0; // Count of left parentheses found
int bufindex = index; // Save starting value for index

do
{
buffer[index - bufindex] = *(str + index);
switch(buffer[index - bufindex])
{
case ')':
if(numL == 0)
{
size_t size = index - bufindex;
buffer[index - bufindex] = '\0'; // Replace ')' with '\0'
++index;
pstr = new char[index - bufindex];
if(!pstr)
{
cout << "Memory allocation failed,"
<< " program terminated.";
exit(1);
}
strcpy_s(pstr, index-bufindex, buffer); // Copy substring to new memory
return pstr; // Return substring in new memory
}
else
numL--; // Reduce count of '(' to be matched
break;

case '(':
numL++; // Increase count of '(' to be
// matched
break;
}
} while(*(str + index++) != '\0'); // Loop - don't overrun end of string

cout << "Ran off the end of the expression, must be bad input."
<< endl;
exit(1);
return pstr;
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式