问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
你好,欢迎来到懂视!登录注册
当前位置: 首页 - 正文

有没有高手会用C++编写一个功能比较齐全的计算器3

发布网友 发布时间:2023-11-22 09:20

我来回答

2个回答

热心网友 时间:2024-07-23 10:20

// 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--; // Rece 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;
}

热心网友 时间:2024-07-23 10:20

做这个的应该不是高手才会的吧
你自己想吧,遇到问题再问
不要一开始就问,这样的话对你不好
用MFC写!!

热心网友 时间:2024-07-23 10:20

// 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--; // Rece 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;
}

热心网友 时间:2024-07-23 10:20

做这个的应该不是高手才会的吧
你自己想吧,遇到问题再问
不要一开始就问,这样的话对你不好
用MFC写!!
声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。
E-MAIL:11247931@qq.com
收到别单位给的承兑汇票 我单位在给别的单位 这笔业务应该怎么做啊? 荔枝什么生肖 均衡器怎样调成重低音 电脑eq设置低音电脑EQ均衡器怎么设置达到最佳音效 第一排是什么单词 人生中的黑暗乐章——Agust 乐评 Sigur Ros乐队简介 ...在这部影片里面名字叫Agust还是什么的、求名字 请大大们给个英文名 大叶芹叶子可以吃吗 武汉最好的动漫培训学校是哪家啊,我刚大学毕业,可是找不到好一点的工 ... tcp/ ip参考模型有哪四层? 一年改2次的办法 论述题:为什么说科学发展观是我国发展中国特色社会主义必须长期坚持... 什么是爱 什么是关心 什么是虚伪 宫河到西峰的班车 如何不用手机号注册 忘记吃屈螺酮炔雌醇片一天有啥影响 一年几次入团机会 液晶电视中的240HZ到底什么意思9 为什么有些人看似友好却置之不理,有些看似疏远却是亲密无间?1 一年内怎么改第二次 手机号重新注册了一个,原来的咋找回来?? 如果你的亲戚朋友冬季到法国旅游,你会告诉他们那里( )... 核桃峪煤矿人事部电话是多少,谁知道? 定西到正宁多少公里,定西到核桃峪煤矿多少公里 柿子树叶的样子222 我的被盗了,重新注册了,怎么恢复微信,找回以前的微信好... 关于美国队长2彩蛋的几个疑惑!1.彩蛋里面得到loki权杖的人是谁?(九... 跑长途加了乙醇汽油,用不用清洗油路,请欧友解答 微信手机号在24小时内,已绑定两个,已达到限制,不能在绑定其他微信... 北京朔崛科技有限公司怎么样? 三国演义中吴国,蜀国,魏国的领袖各是谁18 三国演义中吴国、蜀国、魏国的领袖各是谁?219 黑龙江省肇东市 光纤价格 100兆 50兆 20兆的都多少一...2 一年内怎么改第二次 上海崇元永泰商务服务有限公司怎么样? 一个手机二个号码可以申请二个吗? 闰年全年一共有多少天,是多少个星期零多少天。3017 2011年是什么年全年有多少天共有几个星期零几天?1 2016年全年一共有多少个星期零多少天3217 我的被盗了,然后又用手机号重新注册了一个微信,还能找回... 山药焖排骨的做法,山药焖排骨怎么做好吃,山药 2个不同手机号注册的能合并在一起吗?8 一个手机怎么申请两个啊? 杮子的样子6 电脑的C盘变红色了怎么办?4 我银行卡是学校办的,学校没有密码也能扣费?毕业后我可以不要那张卡继 ... 2016年卡塔尔乒乓球公开赛男单冠军奖金15 2016年卡塔尔乒乓球公开赛女双决赛谁赢了15
  • 焦点

最新推荐

猜你喜欢

热门推荐