请计算y=1+1/(1+1/(1+1/5))(用C语言),要求:答案使用浮点输出一行,不要输出多余的零,行尾回车。

#include<stdio.h>#include<math.h>intmain(){inta;floaty;a=1;y=1.2;while(a<3){y=1+1/y;a... #include <stdio.h>
#include <math.h>
int main()
{
int a;
float y;
a=1;
y=1.2;
while(a<3){
y=1+1/y;
a=a+1;}
printf("%f\t",y);
return 0;
}
我这哪里有错
求y=1+1/(1+1/(1+1/5))
这是作业 程序本身没问题 叫上去说wrong answer
展开
 我来答
250431615
2011-09-21 · TA获得超过635个赞
知道小有建树答主
回答量:384
采纳率:0%
帮助的人:267万
展开全部
#include <stdio.h>
#include <math.h>
int main()
{
int a;
float y;
a=1;//改为0 不然下面只做了俩次循环就退出了
y=1.2;
while(a<3){
y=1+1/y;
a=a+1;}
printf("%f\t",y);//printf("%0.1f\t\n",y);
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
手机用户30750
2011-09-22 · TA获得超过439个赞
知道小有建树答主
回答量:677
采纳率:0%
帮助的人:460万
展开全部
拿以前做的稍微修改了下,主要使用string类来实现算式的运算的,支持的运算符有+-*/%^(乘方)(本打算扩充,但一直无时间),你可以参考下:

#include <iostream>
#include <string>
#include <sstream>
#include <cstddef>
#include <cmath>
#include <cctype>
#include <cstdlib>
#include <iomanip>
using namespace std;

template<class T>
class calculator
{
public:
calculator();
calculator(const calculator& cal);
calculator(const string& str);
calculator(const char* c_str);

template<class U>
calculator(const U& u);

string GetFormula();
T Result();

calculator operator = (const calculator& cal);

template<class X, class U>
X lexical_cast(U u);
private:
T dummyCal(const string& str);
T analyse();
T ops(T lhs, T rhs, char op);
string formula;
T value;
};

template<class T>
calculator<T>::calculator() : formula(""), value(T())
{
}

template<class T>
calculator<T>::calculator(const calculator& cal) : formula(cal.formula), value(cal.value)
{
}

template<class T>
calculator<T>::calculator(const string& str) : formula(str)
{
value = analyse();
}

template<class T>
calculator<T>::calculator(const char* c_str) : formula(string(c_str))
{
value = analyse();
}

template<class T>
template<class U>
calculator<T>::calculator(const U& u)
{
formula = u.formula;
value = analyse();
}

template<class T>
inline string calculator<T>::GetFormula()
{
return formula;
}

template<class T>
T inline calculator<T>::Result()
{
return value;
}

template<class T>
calculator<T> calculator<T>::operator = (const calculator& cal)
{
formula = cal.formula;
value = cal.value;
return *this;
}

template<class T>
T calculator<T>::ops(T lhs, T rhs, char op)
{
switch(op)
{
case '+':
return lhs + rhs;
case '-':
return lhs - rhs;
case '*':
return lhs * rhs;
case '/':
return lhs / rhs;
case '%':
return static_cast<T>(static_cast<int>(lhs) % static_cast<int>(rhs));
case '^':
return static_cast<T>(pow(static_cast<double>(lhs), static_cast<double>(rhs)));
default:
return 0;
}
}

template<class T>
T calculator<T>::dummyCal(const string& str)
{
string temp(str);
temp.erase(0, temp.find_first_not_of(' '));
size_t opPos;
while((opPos = temp.find_first_of("*/%^")) != string::npos
|| (opPos = temp.find_first_of("+-",1)) != string::npos)
{
size_t lhsValPos = temp.find_last_not_of("0123456789.", opPos - 1) == string::npos ?
0 : temp.find_last_not_of("0123456789.", opPos - 1) + 1;
if(temp[0] == '-' && temp.find_last_of("+-*/%^") != 0)
--lhsValPos;
size_t rhsValPos = temp.find_first_not_of("0123456789.", opPos + 2) == string::npos ?
temp.size() - 1 : temp.find_first_not_of("0123456789.", opPos + 2) - 1;
string LopR(temp, lhsValPos, rhsValPos - lhsValPos + 1);

istringstream isstrm(LopR);
T lhsVal, rhsVal;
char op;
isstrm >> lhsVal >> op >> rhsVal;
T result = ops(lhsVal, rhsVal, op);

temp.erase(lhsValPos, rhsValPos - lhsValPos + 1);
temp.insert(lhsValPos, lexical_cast<string>(result));

if(temp[0] == '-' && temp.find_last_of("+-*/%^") == 0)
break;
if(temp.find('e') != string::npos)
{
size_t ePos = temp.find('e');
if(temp.find_first_of("+-*/%^", ePos) == string::npos
&& (temp.find_last_of('-', ePos) == 0
|| temp.find_last_of("+-*/%^", ePos) == string::npos))
break;
}
}

return lexical_cast<T>(temp);
}

template<class T>
T calculator<T>::analyse()
{
string temp1 = formula;
while(temp1.find_first_of("()") != string::npos)
{
size_t rhsBracket = temp1.find(')');
size_t lhsBracket = temp1.rfind('(', rhsBracket);
string sResult(temp1, lhsBracket + 1, rhsBracket - lhsBracket - 1);

T partResult = dummyCal(sResult);

temp1.erase(lhsBracket, rhsBracket - lhsBracket + 1);
temp1.insert(lhsBracket, lexical_cast<string>(partResult));
}

istringstream sstrm(temp1);
T test;
while(sstrm >> test);
if(temp1 != lexical_cast<string>(test))
return dummyCal(temp1);

return lexical_cast<T>(temp1);
}

template<class T>
template<class X, class U>
X calculator<T>::lexical_cast(U u)
{
stringstream sstrm;
sstrm << u;
X x;
sstrm >> x;
return x;
}

void title()
{
cout.fill('=');
cout << setw(24) << '=' << "计算器" << setw(24) << '=' << '\n' << endl;
cout.fill(' ');
cout << setw(18) << ' ' << "C清屏, X退出, N继续" << setw(12) << ' '<< endl;
cout << "\n操作: 加 +\t减 -\t乘 *\t除 /\t模 %\t乘方 ^\n";
cout << "\n请输入任意计算式:\n";
}

int main()
{
title();
string formula;
int count = 1;
while(true)
{
cout << "\n式子" << count << " : ";
string input;
cin >> input;

if(input.find_first_not_of("CNXcnx0123456789.+-*/%^()") != string::npos)
{
cout << "输入中有错误, 请重新输入!\n";
cin.clear();
continue;
}

if(input.find_first_of("Cc") != string::npos)
{
system("cls");
formula.clear();
count = 1;
title();

}else if(input.find_first_of("Xx") != string::npos)
{
break;

}else
{
formula += input;
calculator<double> cal(formula);
++count;
cout << "结果" << count << " : " << formula << " = " << cal.Result() << endl;
formula = cal.lexical_cast<string>(cal.Result());
}
}
cout << "程序结束..." << endl;

//calculator<int> cal1("1+2+3+4");
//calculator<long> cal2("4*3*2*1");
//calculator<float> cal3("((1*3/2)+(4/(1+1)))*3");
//calculator<double> cal4("((1+3)*(20+4/6+(3-9)))^-2");
//cout << cal1.GetFormula() << " = " << cal1.Result() << '\n';
//cout << cal2.GetFormula() << " = " << cal2.Result() << '\n';
//cout << cal3.GetFormula() << " = " << cal3.Result() << '\n';
//cout << cal4.GetFormula() << " = " << cal4.Result() << endl;
}

打了注释那部分是我以前对算式的测试,你可以运行下看看。

以下是我测试的结果:

计算器
C清屏, X退出, N继续
(3/2+2)^3+(9*(2-5)-3)*2
(3/2+2)^3+(9*(2-5)-3)*2 = -17.125
请选择(C/X/N): n
+1
-17.125+1 = -16.125
请选择(C/X/N): n
/2
-16.125/2 = -8.0625
请选择(C/X/N): x
程序结束...
追问
我靠你吓人啊
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
百度网友1d70db5
2011-09-21 · 超过13用户采纳过TA的回答
知道答主
回答量:57
采纳率:0%
帮助的人:32.4万
展开全部
//程序没有错
#include <stdio.h>
//#include <math.h> //在该程序中不需要
int main()
{
int a;
float y;
a=1;
y=1.2;
while(a<3){
y=1+1/y;
a=a+1;}
printf("%f\n",y); //\n才是回车符
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
350276892
推荐于2016-12-02
知道答主
回答量:13
采纳率:0%
帮助的人:13.6万
展开全部
#include<stdio.h>
void main(){

float n=5.0;
int m=3;
while(m){
n=1+1/n;
m--;}
printf("%g\n",n);
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
惠企百科
2022-12-01 · 百度认证:北京惠企网络技术有限公司官方账号
惠企百科
惠企百科网是一家科普类综合网站,关注热门中文知识,集聚互联网精华中文知识,本着自由开放、分享价值的基本原则,向广大网友提供专业的中文知识平台。
向TA提问
展开全部

y=1+1/(1+1/(1+1/5)):

#include <iostream>

using namespace std;

int main()

{

printf("%.4f",1.0+1.0/(1.0+1.0/(1.0+1.0/5)));

return 0;

}

扩展资料

1-1/3+1/5-??的前n项之和需要变量来表示符号位(正+或-),分子不变可直接用常量1,分母需要变量表示,每下一项+2。循环相加,可用for循环。

本题需要五个变量:分母m:从1开始,到n结束,每次+2;项f:由分子和分母构成,因为有小数,用1.0/i;符号flag:正负相间隔,一次正+,下一次为负-;和s。循环变量i:表示循环的次数。输入:分子:常量1,j。处理:循环相加。输出:s。

#include <stdio.h>

int main(void){

int i,n;

double f,s;

int flag=1,m=1;

printf("请输入分母的终值:");

scanf("%d",&n);

for(i=1,s=0;i<=n;i++)

{

f=flag*1.0/m;

s+=f;

flag=-flag;

m=m+2;

}

printf("1+1/3-1/5+...+1/n=%.2f\n",s);

system("pause");

return 0;

}

参考资料来源:百度百科—c语言

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式