初学者请不要嘲笑!:)

vanishli 2003-08-15 08:41:09
#include <stdio.h>
#include <math.h>

void main()
{
float a,b,c,s,area;
printf("please input 3 sides of one triangle:\n");
scanf("%f,%f,%f",&a,&b,&c);

s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));

printf("a=%7.2f,b=%7.2f,c=%7.2f\n",a,b,c);
printf("area of triangle is %10.5f",area);
}

用cout和cin代替printf()和scanf()函数,改写程序(注意格式)!
谢谢!
...全文
65 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
vanishli 2003-08-15
  • 打赏
  • 举报
回复
谢谢大家的帮助!

特别谢谢icbm()。
icbm 2003-08-15
  • 打赏
  • 举报
回复
下面是一段解释说明为什么用<iostream.h>代替<stdio.h>的说明,中文是我翻译的,希望对楼主有帮助。为了保证语言流畅,部分采取了意译。
来源:http://users.utu.fi/~sisasa/oasis/cppfaq/input-output.html#[15.1]

[15.1] Why should I use <iostream.h> instead of the traditional <stdio.h>?
Increase type safety, reduce errors, improve performance, allow extensibility, and provide subclassability.



printf() is arguably not broken, and scanf() is perhaps livable despite being error prone, however both are limited with respect to what C++ I/O can do. C++ I/O (using << and >>) is, relative to C (using printf() and scanf()):

Better type safety: With <iostream.h>, the type of object being I/O'd is known statically by the compiler. In contrast, <stdio.h> uses "%" fields to figure out the types dynamically.

Less error prone: With <iostream.h>, there are no redundant "%" tokens that have to be consistent with the actual objects being I/O'd. Removing redundancy removes a class of errors.

Extensible: The C++ <iostream.h> mechanism allows new user-defined types to be I/O'd without breaking existing code. Imagine the chaos if everyone was simultaneously adding new incompatible "%" fields to printf() and scanf()?!).

Subclassable: The C++ <iostream.h> mechanism is built from real classes such as ostream and istream. Unlike <stdio.h>'s FILE*, these are real classes and hence subclassable. This means you can have other user-defined things that look and act like streams, yet that do whatever strange and wonderful things you want. You automatically get to use the zillions of lines of I/O code written by users you don't even know, and they don't need to know about your "extended stream" class.


为什么我应该用<iostrea.h>代替以前的<stdio.h>?

增强的类型安全性,减少的产生错误的可能性,性能上的提高,可扩展性以及提供的子类化能力。

printf()可以说是比较稳定的,而scanf()可能在某些情况下因误用而产生错误。它们能做的,C++的I/O都能做到。C++的I/O(用<<和>>实现)和C的I/O(用printf()和scanf()实现)相比,有如下特点:

更好的类型安全性:在<iostream.h>中,I/O使用的对象的类型是在编译过程中,由编译器静态确定的。相反,<stdio.h>中用"%"字段域在执行过程中动态确定对象及变量的类型。

减少的错误发生的可能性:在<iostream.h>中,去除了必须要和I/O对象类型相匹配的冗余的“%”标记。去除冗余的部分减少了一类错误。

可扩展性:C++中<iostream.h>的I/O机制允许添加新的用户自定义类型,而不必修改现有代码。如果每个人同时在printf()和scanf()中加入新的不兼容的“%”字段域,那是一种怎样混乱的情形?!

子类化能力:C++中<iostream.h>的I/O机制是建立在真正的类的基础上的,这些类如:ostream和istream。和<stdio.h>中的FILE*不同,这些是真正的类,因此具有子类化能力。这意味着你可以拥有其它和“流”有着类似外形和行为的用户自定义类型,它们可以完成那些你看起不可思议和令你叹为观止的事情。你可以获得无数由其他人编写的I/O代码,而编写这些代码的人也不需要知道你是如何来扩展这些“流”类的。

(译者注:文中的I/O指的是输入/输出。C++中的I/O是通过过象来实现的。)



zhang,qiuping 2003-08-15
  • 打赏
  • 举报
回复
include <iostream>
#include <math.h>
using namespace std;

void main()
{
float a,b,c,s,area;
cout<<"please input 3 sides of one triangle:"<<endl;
cin>>a>>b>>c;

s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));

cout<<"a="<<a<<", b="<<b<<" ,c="<<c<<endl;
cout<<"area of triangle is :"<<area;
}

cout<<"引号里打印再屏幕上a为变量"<<a;
lyhwei 2003-08-15
  • 打赏
  • 举报
回复
?!
vanishli 2003-08-15
  • 打赏
  • 举报
回复
to icbm()

再请问用cout和cin代替printf()和scanf()函数有什么固定格式吧?

请详细讲一下好吗!谢谢!
kweio 2003-08-15
  • 打赏
  • 举报
回复
?!
sam1111 2003-08-15
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>
#include <iostream>
using namespace std;

void main()
{
float a,b,c,s,area;
cout << "please input 3 sides of one triangle:" << endl;
cin >> a;
cin >> b;
cin >> c;

s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));

char buffer[40] = {0};
sprintf(buffer, "a=%7.2f,b=%7.2f,c=%7.2f\n",a,b,c);
cout << buffer;
memset(buffer, 0, 40);
sprintf(buffer, "area of triangle is %10.5f", area);
cout << buffer;
}
icbm 2003-08-15
  • 打赏
  • 举报
回复
不赞成jack_wq的做法。

printf、scanf和cout、cin不要混合起来使用。因为它们的实现机制是不一样的,混合使用有可能导致输入、输出不正确。

一个程序中要么全用printf、scanf;要么全用cout、cin。
Bandry 2003-08-15
  • 打赏
  • 举报
回复
setw()控制域宽
icbm 2003-08-15
  • 打赏
  • 举报
回复
#include <iostream.h>
#include <math.h>

void main()
{
float a,b,c,s,area;
cout<<"please input 3 sides of one triangle:"<<endl;
cin>>a>>b>>c;

s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));

cout<<"a="<<a<<",b="<<b<<",c="<<c<<endl;
cout<<"area of triangle is "<<area<<ends;
}

注意:要更换头文件。另外,格式化数字我现在想不起来了,你可以查一下资料。
jack_wq 2003-08-15
  • 打赏
  • 举报
回复
#include <stdio.h>
#include <math.h>

void main()
{
float a,b,c,s,area;
printf("please input 3 sides of one triangle:\n");
cin>>a>>b>>c;

s=(a+b+c)/2;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<a<<b<<c<<endl;
cout<<area<<endl;
}

输出的格式控制记不起来了!
cleanduo 2003-08-15
  • 打赏
  • 举报
回复
ding

69,381

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧