Ⅰ 怎么用C++编译一个简单的计算器
我借鉴了别人的某计算器,因为你没有说多简易...我就找了个差不多的...
/*
程序名称:表达式计算器
编译环境:Microsoft Visual C++ 6.0
作者:吉林大学 计算机科学与技术学院 2006 罗泗勇
时间:200801
*/
/*
说明:
采用树形结构处理表达式,按优先级运算结果,一个加,减,乘,除或数值为一个节点
优先级如下:
函数:4
括号:3
乘除:2
加减:1
*/
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;
const char NUM[]={'0','1','2','3','4','5','6','7','8','9','.'};
const char OPERATION[]={'+','-','*','/'};
const double PI=3.14159265358979;
const double EE=2.71828182818281;
class Fun //处理系统数学函数的类
{
public:
Fun(string o,int t,double l=0.0,double r=0.0):op(o),type(t),lvalue(l),rvalue(r){}
static string FUN[];
double calc();
private:
int type; //666 0 1 sin90 2 3! 3 3C2
string op; //函数类型
double lvalue; //函数左边的值
double rvalue; //函数右边的值
static int FunNum;
};
int Fun::FunNum=8;
string Fun::FUN[]={"!","sin","cos","tan","log","ln","C","A","^"};
/*
函数说明:
1:log是以10为底的工程对数
2:ln 是以e为底的自然对数
3:C 计算组合数 输入规则 如计算 3取2的组合 输入表达式 3C2
4:A 计算排列数 输入规则 如计算 3取2的排列 输入表达式 3A2
5:! 计算阶乘
6:^ x的y次方 输入 x^y
*/
int factorial(int n) //阶乘函数
{
int i,s=1;
for(i=1;i<=n;i++)
s*=i;
return s;
}
int C(int a,int b)
{
return factorial(a)/(factorial(b)*factorial(a-b));
}
int A(int a,int b)
{
return factorial(a)/factorial(b);
}
double Fun::calc() //计算系统函数的值
{
if(type==0)
return lvalue;
else
{
if(op=="!")
return factorial(lvalue);
if(op=="sin")
return sin(rvalue/180*PI);
if(op=="cos")
return cos(rvalue/180*PI);
if(op=="tan")
return tan(rvalue/180*PI);
if(op=="log")
return log10(rvalue);
if(op=="ln")
return log10(rvalue)/log10(EE);
if(op=="C")
return C(lvalue,rvalue);
if(op=="A")
return A(lvalue,rvalue);
if(op=="^")
return pow(lvalue,rvalue);
else
{
string err="暂时没有函数"+op;
MessageBox(NULL,err.c_str(),"错误",MB_OK);
return 0;
}
}
}
struct Unit //双向链表保存运算单元
{
Unit(int p,char o,string c,double v,int t,Unit * pr=NULL,Unit * n=NULL)
:PRI(p),Operation(o),Code(c),value(v),Type(t),Pre(pr),Next(n){}
int PRI; //优先级
char Operation; //操作符
string Code; //原始代码
double value; //数据
int Type; //类型 操作符0 数据1 函数2
Unit * Pre; //构成双向链表
Unit * Next;
};
class Node //表达式树状结构的节点
{
public:
Node(char o,int p,int e=1,double v=0,Node * ph=NULL,Node * pl=NULL,Node * pr=NULL)
:Operation(o),PRI(p),Expression(e),value(v),Head(ph),Left(pl),Right(pr){}
Node * Head; //节点的根,左树枝,右树枝
Node * Left;
Node * Right;
double GetValue();
char GetOperation() const {return Operation;}
int GetPri() const {return PRI;}
int IsExp() const {return Expression;}
private:
char Operation; //操作符
int PRI; //优先级
int Expression; //记录该节点是否是表达式0 1
double value; //该节点的值
};
double Node::GetValue() //运算该节点的值
{
if(IsExp()) //该节点的值还未算出来
{
double lvalue,rvalue;
lvalue=Left->GetValue();
rvalue=Right->GetValue();
Expression=0;
char op=GetOperation();
switch(op)
{
case '+':
return lvalue+rvalue;
case '-':
return lvalue-rvalue;
case '*':
return lvalue*rvalue;
case '/':
return lvalue/rvalue;
default:
return 0;
}
}
else
return value;
}
bool Isnum(char c)
{
for(int i=0;i<sizeof(NUM);i++)
{
if(c==NUM[i])
return true;
}
return false;
}
bool Isoperation(char c)
{
for(int i=0;i<sizeof(OPERATION);i++)
{
if(c==OPERATION[i])
return true;
}
return false;
}
Unit * Analyse(string exp) //分析表达式并生成链表
{
int pri=0; //当前优先级
int stat=-1; //当前的读入状态 括号 0 数据 1 运算符 2
Unit * head=NULL,* p=NULL;
int i=0,explen;
explen=exp.size();
for(i=0;i<explen;i++)
{
char c=exp.at(i);
if(c=='(')
{
pri+=3;
stat=0;
}
else if(c==')')
{
pri-=3;
stat=0;
}
else if(Isoperation(c)) //操作符不会出现在表达式开头
{
Unit * temp=p;
int add_pri; //自身增加的优先级
if(c=='+' || c=='-')
add_pri=1;
else
add_pri=2;
p->Next=new Unit(pri+add_pri,c," ",0,0);
p=p->Next;
p->Pre=temp;
}
else //其他的当做函数处理
{
string function="";
while(i<explen && (c=exp.at(i),! Isoperation(c)) && c!=')')
{
function+=c;
i++;
}
i--;
if(head==NULL)
{
p=new Unit(pri,' ',function,0,2);
head=p;
}
else
{
Unit * temp=p;
p->Next=new Unit(pri,' ',function,0,2);
p=p->Next;
p->Pre=temp;
}
}
}
return head;
}
Unit * Calc(Unit * head) //计算双向链表基本单元的值
{
Unit * p=head;
while(p!=NULL)
{
if(p->Type!=0) //非操作符
{
string temp=p->Code;
string op;
double lvalue=0,rvalue=0;
int l_point=0,r_point=0;
int i=0,type=0;
char ch;
while(i<temp.size() && (ch=temp.at(i),Isnum(ch)))
{
if(ch=='.')
{
l_point++;
i++;
continue;
}
if(! l_point)
lvalue*=10;
lvalue+=(ch-'0')*pow(10,-l_point);
i++;
if(l_point)
l_point++;
}
while(i<temp.size() && (ch=temp.at(i),! Isnum(ch)))
{
op+=ch;
type=1;
i++;
}
while(i<temp.size() && (ch=temp.at(i),Isnum(ch)))
{
if(ch=='.')
{
r_point++;
i++;
continue;
}
if(! r_point)
rvalue*=10;
rvalue+=(ch-'0')*pow(10,-r_point);
i++;
if(r_point)
r_point++;
}
Fun * f=new Fun(op,type,lvalue,rvalue);
p->value=f->calc();
}
p=p->Next;
}
return head;
}
Node * Tree(Unit * head) //生成表达式树
{
Node * root=NULL,* proot=NULL,* pbranch=NULL;
Unit * p=head;
int now_pri; //当前优先级
bool hadop=false;
while(p!=NULL)
{
if(p->Type==0) //如果是一个操作符
{
hadop=true;
if(root==NULL)
{
proot=new Node(p->Operation,p->PRI,1);
root=proot;
pbranch=root;
now_pri=p->PRI;
proot->Left=new Node(' ',0,0,p->Pre->value);
proot->Right=new Node(' ',0,0,p->Next->value);
}
else
{
if(p->PRI<=now_pri) //优先级低于当前优先级,树根方向 //最初写的 if(p->PRI<now_pri),9/3/3=9,错的
{
proot=new Node(p->Operation,p->PRI,1); //新的树根
proot->Left=root; //根的变换
proot->Right=new Node(' ',0,0,p->Next->value);
root=proot;
pbranch=proot; //右树枝的变换
//pbranch->Right=new Node(' ',0,0,p->Pre->value); //树枝右边取值
}
else
{
Node * temp;
temp=new Node(p->Operation,p->PRI,1);
pbranch->Right=temp;
temp->Head=pbranch;
pbranch=pbranch->Right;
pbranch->Left=new Node(' ',0,0,p->Pre->value);
pbranch->Right=new Node(' ',0,0,p->Next->value);
}
now_pri=p->PRI;
}
}
p=p->Next;
}
if(! hadop)
root=new Node(' ',0,0,head->value);
return root;
}
int main()
{
string exp;
//ifstream infile("test.txt",ios::in);
while(! getline(cin,exp).eof())
{
if(exp=="")
continue;
Unit * h=Analyse(exp);
h=Calc(h);
Node * root=Tree(h);
cout<<exp<<"="<<root->GetValue()<<endl;
}
return 0;
}
Ⅱ 运用c++编写一个小程序,手动输入年利率,贷款金额,贷款年限,自动输出月还款额!!!
#include"stdio.h"
double count(int a,int b)
{
double c=a-b-3500;
if(c<=0)
c=0;
else if(c<=1500)
c=c*0.03;
else if(c<=4500)
c=c*0.1-105;
else if(c<=9000)
c=c*0.2-555;
else if(c<=35000)
c=c*0.25-1005;
else if(c<=55000)
c=c*0.3-2755;
else if(c<=80000)
c=c*0.35-5505;
else
c=c*0.45-13505;
return c;
}
void main()
{
int chose;
while(1)
{
printf("\t\t个人所得税计算器\n");
printf("1.计算个人所得税\n");
printf("2.退出\n");
printf("请输入选项(1或2):");
scanf("%d",&chose);
if(chose==2)
break;
else if(chose==1)
{
int pay,baoxian;
printf("\n输入你的月收入:");
scanf("%d",&pay);
printf("\n输入你的三险一金:");
scanf("%d",&baoxian);
printf("你的个人所得税为:%0.2f",count(pay,baoxian));
}
else
{
printf("\n\t\t>>>注意:请输入1或2<<<\n");
}
}
}
分享
本回答专业性由电脑网络分类达人 赵丽丽认证
举报| 2012-12-16 19:32
#“行家成长训练营”,双重奖励等你拿!#
提问者采纳
根据2011年9月1日起调整后的7级超额累进税率设计,调试通过
#include"stdio.h"
double count(int a,int b)
{
double c=a-b-3500;
if(c<=0)
c=0;
else if(c<=1500)
c=c*0.03;
else if(c<=4500)
c=c*0.1-105;
else if(c<=9000)
c=c*0.2-555;
else if(c<=35000)
c=c*0.25-1005;
else if(c<=55000)
c=c*0.3-2755;
else if(c<=80000)
c=c*0.35-5505;
else
c=c*0.45-13505;
return c;
}
void main()
{
int chose;
while(1)
{
printf("\t\t个人所得税计算器\n");
printf("1.计算个人所得税\n");
printf("2.退出\n");
printf("请输入选项(1或2):");
scanf("%d",&chose);
if(chose==2)
break;
else if(chose==1)
{
int pay,baoxian;
printf("\n输入你的月收入:");
scanf("%d",&pay);
printf("\n输入你的三险一金:");
scanf("%d",&baoxian);
printf("你的个人所得税为:%0.2f",count(pay,baoxian));
}
else
{
printf("\n\t\t>>>注意:请输入1或2<<<\n");
}
}
}
Ⅲ 求助,用c语言程序编写下面题目,贷款计算器。
现在改好,看到变化的地方:
#包括中
无效的主要(无效)
{
持股量A,B,面积;/ *应被宣布为float * /
:浮动get_area(浮动,浮动,浮动);/ *正常申报的法律,有正式的和实际参数不能相同的名称* /
printf的(“请输入一个梯形的上底:\ n”);
scanf的(“%f”,&A);
输出(“请输入一个梯形下底:\ n”);
scanf的(“%f”,及b); printf的(“请输入梯形的高
:\ n”);
scanf的(“%f”,&H);/ *变化* /
面积= get_area(A,B,H);
printf(“请梯形面积是%f \ n “,区);
}
的持股量get_area(浮动a_x,b_x浮,浮H_X)
{
回报1/2.0 *(a_x + b_x)* H_X; / * 2更改为2.0,因为1/0,1/2 = 2.0 = 0.5,这是想法?C * /
}
Ⅳ c语言设计一个简单的计算器程序
#include<stdio.h>//计算器
voidmenu()//自定义的菜单界面
{
printf("--------------------\n");
printf("请输入你的选择\n");
printf("1.+\n");
printf("2.-\n");
printf("3.*\n");
printf("4./\n");
printf("--------------------\n");
}
intmain()
{
inti=0;
intj=0;
intnum=0;//计算结果存放在nun
intselect=0;//选择的选项存放在select
do//do-while先执行再判断循环条件,即可实现重复计算功能
{
menu();//打印出菜单界面
scanf("%d",&select);//输入你的选项
printf("请输入计算值:");
scanf("%d%d",&i,&j);//输入要计算的数值
switch(select)
{
case1:
printf("%d+%d=%d\n",i,j,num=i+j);//实现加法功能
break;
case2:
printf("%d-%d=%d\n",i,j,num=i-j);//实现减法功能
break;
case3:
printf("%d*%d=%d\n",i,j,num=i*j);//实现乘法功能
break;
case4:
printf("%d-%d=%d\n",i,j,num=i/j);//实现除法功能
break;
default:
printf("输入有误重新选择");
break;
}
}while(select);
return0;
}
运行结果:
return表示把程序流程从被调函数转向主调函数并把表达式的值带回主调函数,实现函数值的返回,返回时可附带一个返回值,由return后面的参数指定。
return通常是必要的,因为函数调用的时候计算结果通常是通过返回值带出的。如果函数执行不需要返回计算结果,也经常需要返回一个状态码来表示函数执行的顺利与否(-1和0就是最常用的状态码),主调函数可以通过返回值判断被调函数的执行情况。
Ⅳ 用c++编写一个关于房贷的程序要求选择贷款方式(商业贷款或公积金贷款)
#include<iostream>
#include<cmath>
#include<iomanip>
usingnamespacestd;
intmain()
{
doublecapital=0;//本金
doubletotalInterest;//总利息
doubleinterestRate;//利率
doublemonthRepayment;//月还款额
doubletotalRepayment;//还款总额
intn,years=0;//还款期限
intchoice=0;//贷款方式
do
{
cout<<"请输入贷款总额x(x>=1,单位:万元):";
cin>>capital;
}while(capital<1);
capital*=10000;//万元转化为元
do
{
cout<<"请输入还款期限y(1<=y<=30,单位:年):";
cin>>years;
}while(years<1||years>30);
n=years*12;//年转化为月
cout<<"请选择贷款方式(1:商业贷款,2:公积金贷款,其他:退出):";
cin>>choice;
switch(choice)
{
case1://商业贷款年利率(%)
interestRate=years>5?4.9:(years>=1?4.75:4.35);
break;
case2://公积金贷款年利率(%)
interestRate=years>5?3.25:2.75;
break;
default:
exit(0);
break;
}
interestRate/=1200;//将年利率转化为月利率,并将百分点转化为小数点
//计算月还款额
monthRepayment=capital*interestRate*pow(1+interestRate,n)/(pow(1+interestRate,n)-1);
totalRepayment=monthRepayment*n;//还款总额
totalInterest=totalRepayment-capital;//总利息
cout<<"您贷款的详情如下: 本金:"<<std::fixed<<setprecision(2)<<capital<<
"元 还款年限:"<<years<<"年 月还款额:"<<monthRepayment<<
"元 还款总额:"<<totalRepayment<<"元 总利息:"<<totalInterest
<<"元"<<endl;
return0;
}
Ⅵ 有关银行贷款还贷的c语言程序
你的错误实在太多了。看代码王的程序简洁易懂#include<stdio.h>#include<math.h>int main(){ double z,k,x,monthPay,allMoney,temp=0; int n,i; printf("输入借款总额、贷款年限、年利率: "); //贷款总和最好不要用int型的,int的最大值是32767,那你岂不是超了 scanf("%lf%d%lf",&z,&n,&k); //计算n年后要还的总的钱数 pow(x,y)是在头文件math.h中的函数计算x^y allMoney = z*pow((1+k/12),12*n); //式子∑x(1+k/12)^i (i=0,1,2,..,n*12-1)将x提出到前面计算 temp=∑(1+k/12)^i for(i=0; i<12*n; i++) temp += pow((1+k/12),i); //根据等式z(1+k/12)^(12*n) = ∑x(1+k/12)^i (i=0,1,2,..,n*12-1) 得x=allMoney/temp; x = allMoney/temp; printf("每月应还款:%lf", x);}Ⅶ java简易贷款计算机
你也不说计算公式,不知道怎么计算,我去网上找了一个月支付款的计算公式,不知道和你题目的要求是否一样,如果不一样你就改下公式就行。 java代码如下: public class Loan { public static void main(String[] args){ double rate ;//利率 int year ; //年数 double money ; //贷款总额 double monthpay ;//月付款 Scanner sc = new Scanner(System.in); System.out.println("输入月利率:"); rate = sc.nextDouble(); System.out.println("输入年数:"); year = sc.nextInt(); System.out.println("输入贷款总额:"); money = sc.nextDouble(); //计算月付款 monthpay = (money * rate)/Math.abs(1 - (1 / (1 + rate ) * year * 12 )); System.out.println("每月应该还贷款:" + monthpay); }}