c语言编程求出加减乘除运算符。具体问题看问题补充。谢谢。

已知一个数,如10,并且还已知用3加减乘除五次等于这个10,其关系表达式为:3*3-3+3+3/3=10,其中加减乘除运算符为* - + + /
另换一个数,如43,它的运算数为7,并且加减乘除一共六次。问:如何用c语言编程求出这些运算符?
注解:那些运算符我都知道(不止一组可能),我主要是要的程序源代码。跪求了!!
运算符中间不可以用括号

visual c++ 2010入门经典 第六章 6.8.1实现计算器 265页
书上从原理到实现讲的清清楚楚
代码我也有
需要我可以发给你
希望能帮到你
满意请采纳
代码:
// Ex6_10Extended.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(true)
{
if(*(str + index) == '*') // If it's multiply,
value *= number(str, ++index); // multiply by next number
else if(*(str + index) == '/') // If it's divide,
value /= number(str, ++index); // divide by next number
else
break;
}
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(nullptr); // 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
}
// There must be at least one digit...
if(!isdigit(*(str + index)))
{ // There's no digits so input is junk...
cout << endl
<< "Arrrgh!*#!! There's an error"
<< endl;
exit(1);
}
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(nullptr); // 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(0 == numL)
{
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);
}追问

抱歉,分暂时还是不能给。我试了一下,还是报错,截图给您看一下:

不知道是不是我平台的问题,你们似乎用的都是正规6.0平台吧?那个东西我也有,可是写好之后怎么运行啊?(本人没找到运行按键)

(多说两句,现在就你和冰火梦幻了,我在等待他的回复,因为他说他的方案是完美性质。如果明天晚上之前还没见他回复或者他的方案没成功,那这200分就归您了)

追答

把所有nullptr改成NULL
nullptr是最新c++标准 NULL是老标准
你分给他吧我累了

追问

呵呵,实在对不起,我把所有的nullptr换成NULL还是不行,幸亏您宽宏大度,不在乎这点分了,但不管怎说,您对我的帮助我还是很感谢,冰火梦幻的方案确实很完美,分我就给他了,见谅。祝您每天开心!

温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-02-22
是否允许用括号?追问

不可以

追答

代码比较长,我发到我的空间去了
经测试,应该是个完美的解决方案。
http://hi.baidu.com/destinyai/item/755c10833e4735874514cfe8

追问

您好,我试着运行了一下,我的c设计学习与试验系统报错提示我说:no such file or directory(没有这个文件或目录,我百度的),我截图您看一下:

请问我应该怎么做?(本人新手,最多入门级别,啥都不懂,请大侠不吝赐教,谢谢!)

追答

奇怪,这个应该是Windows里的标准库。这个无所谓,你把

#include <assert.h>去掉

然后把代码里带有assert(false)的那行去掉就行了。这是我调试用的。

另外,为了在你的那个C/C++程序设计学习与实验系统里运行,我做了点小修改。现在的代码同时适用于你的平台,以及VC6.0。

修改后的代码我已经提交到百度空间了,就是上面那个链接。


PS:VC6.0的编译与运行快捷键是F5。


本回答被提问者采纳
第2个回答  2013-02-22
设多几个变量按顺序排下来然后输出就行了,我不记得单词了,就不献丑了
第3个回答  2013-02-22
你这个有没有要求运算时间,限定运算规模?追问

一切都没要求,只要编程求出运算符即可,要的是编程源代码

追答

留个位置,有点思路,写写看。
另外问一下,是要输出1组可能即可还是要输出所有的?

追问

可以的话,输出所有的。抱歉现在才看到您的回复

相似回答