我有些C++程序设计的题不会做,请大仙帮我指点,我将不胜感激,跪谢!!

我有些C++程序设计的题不会做,请大仙帮我指点,我将不胜感激,跪谢!!,第1张

#include <iostream>

#include______ //看见下面的fstream没 所以这里是#include <QStream>

using namespace std;

void main( )

{

fstream fin,fout;

fout.open("my.txt",ios::out); //打开文件

if(! fout.is_open( ))

return;

for(int i=0;i<3;i=i+1) //输出line的号

fout<<"This is line"<<i+1<<endl;

fout.close( );

fin.open("my.txt",ios::in); //打开文件

if(! fin.is_open( ))

return;

char str[100];

while(______) /*把文件东西读入到str.知道文件结束 所以 判断文件结束的一个东东.忘记是什么函数了,哥又不是百度,哥可以查msdn....至此,后面不看了,出这道题的人傻逼,肯定不是一个公司程序员!*/

{

fin.getline(str,100);

cout<<str<<endl;

}

fin.close( );

}

50.求两个浮点数之差的cha函数的原型声明、调用方法。

#include <iostream>

using namespace std;

void main( )

{

float a,b;

______∥函数cha的原型声明 float cha( float x, float y )

a=12.5;

b=6.5;

float c=__________;∥调用函数cha cha( a, b )

cout<<c<<endl;

}

float cha(float x,float y)

{

float w;

w=x-y;

return w;

}

后面的不看了,上面两道题第一道题够二,第二道题你自己看书就能解决

/*

设计一个程序,读入三个整数,代表三角形边长

程序显示三角形是不等腰三角形,等腰三角形或等边三角形

实现方式1:控制台程序tri_consloe,后跟三个参数,显示判断结果。

实现方式2:控制台程序tri_loop,启动后显示一个提示符tri:

若输入的是三个数,显示判断结果,再次显示提示符

若输入exit则退出程序。

*/

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

/*

切换实现的方式的常量,可选的值为:1,2

当 STYLE =1 时,程序保存名为:tri_consloe.c

当 STYLE =2 时,程序保存名为:tri_loop.c

*/

#define STYLE 2

/* 枚举三角形类型,分别是:

不能组成三角形

普通三角形

等腰三角形

等边三角形

直角三角形

*/

enum TriangleType

{

NOT_TRIANGLE,

COMMON_TRIANGLE,

ISOCELES_TRIANGLE,

EQUILATERAL_TRIANGLE,

RIGHT_ANGLE_TRIANGLE

}

// 输出实现方式1的帮助文档

void PrintHelpText1()

{

printf("使用方法:程序名 三角形边长1 三角形边长2 三角形边长3\n")

}

// 输出实现方式2的帮助文档

void PrintHelpText2()

{

printf("使用方法:\n")

printf(" 1.判断三角形类型命令:三角形边长1 三角形边长2 三角形边长3\n")

printf(" 2.结束程序命令:exit\n")

}

/*

判断三角形类型

参数:

a,b,c 三角形的三边长

返回:

三角形类型

*/

int JudegTriangle(int a,int b,int c)

{

if((a+b)<=c || (a+c)<=b || (b+c)<=a)

{

printf("边长:%3d,%3d,%3d 不能组成三角形\n",a,b,c)

return NOT_TRIANGLE

}

else if((a==b &&b!=c) || (a==c &&a!=b) || (b==c &&b!=a))

{

printf("边长:%3d,%3d,%3d 组成等腰三角形\n",a,b,c)

return ISOCELES_TRIANGLE

}

else if(a==b &&b==c)

{

printf("边长:%3d,%3d,%3d 组成等边三角形\n",a,b,c)

return EQUILATERAL_TRIANGLE

}

else if((a*a+b*b)==c*c || (b*b+c*c)==a*a || (a*a+c*b)==b*b)

{

printf("边长:%3d,%3d,%3d 组成直角三角形\n",a,b,c)

return RIGHT_ANGLE_TRIANGLE

}

else

{

printf("边长:%3d,%3d,%3d 组成普通三角形\n",a,b,c)

return COMMON_TRIANGLE

}

}

int main(int argc, char *argv[])

{

int a,b,c

char edges[3][10]

if(STYLE==1)

{

if(argc!=4)

{

PrintHelpText1()

}

else

{

a=atoi(argv[1])

b=atoi(argv[2])

c=atoi(argv[3])

JudegTriangle(a,b,c)

}

}

else if(STYLE==2)

{

printf("tri:")

while(scanf("%s",edges[0]) &&strcmp(edges[0],"exit")!=0)

{

scanf("%s%s",edges[1],edges[2])

a=atoi(edges[0])

b=atoi(edges[1])

c=atoi(edges[2])

JudegTriangle(a,b,c)

printf("tri:")

}

}

return 0

}

CTest1

#include <iostream>

#include <cmath>

using namespace std

int main()

{

while (true)

{

cout << "1.圆形" << endl

cout << "2.长方形" << endl

cout << "3.直角三角形" << endl

cout << "4.退出" << endl

int choice

cin >> choice

system("cls")

if (choice == 4)

break

switch (choice)

{

case 1:{

double r = 0

cout << "请输入圆形的半径:" cin >> r

cout << "圆形的面积:" << 3.14 * r * r << endl

<< "周长:" << 3.14 * 2 * r

}break

case 2:{

double l = 0.0, w = 0.0

cout << "请输入长方形的长和宽"<<endl

cout << "长:" cin >> l

cout << "宽:" cin >> w

cout << "长方形的面积:" << l * w << endl

<< "周长:" << 2 * (l + w)

}break

case 3:{

double b = 0.0, h = 0.0

cout << "请输入直角三角形的长和宽" << endl

cout << "底:" cin >> b

cout << "高:" cin >> h

cout << "长方形的面积:" << 0.5 * b * h << endl

<< "周长:" << (b + h + sqrt(b*b + h*h))

}break

default:break

}

getchar() getchar()

system("cls")

}

//getchar()getchar()

system("pause")

return 0

}

执行结果:

CTest2

#include <iostream>

#include <cmath>

using namespace std

class Circle

{

public:

Circle(double r) : radius(r){}

double area(){ return 3.14*radius*radius }

double girth(){ return 3.14 * 2 * radius }

private:

double radius

}

class Rect

{

public:

Rect(double l, double w) : length(l), width(w){}

double area(){ return length * width}

double girth(){ return 2 * (length + width) }

private:

double length

double width

}

class Tri

{

public:

Tri(double b, double h) : bottom(b), height(h){}

double area(){ return 0.5*bottom*height }

double girth(){ return (bottom + height + sqrt(bottom*bottom + height*height)) }

private:

double bottom

double height

}

int main()

{

while (true)

{

cout << "1.圆形" << endl

cout << "2.长方形" << endl

cout << "3.直角三角形" << endl

cout << "4.退出" << endl

int choice

cin >> choice

system("cls")

if (choice == 4)

break

switch (choice)

{

case 1:{

double r = 0

cout << "请输入圆形的半径:" cin >> r

Circle circle(r)

cout << "圆形的面积:" << circle.area() << endl

<< "周长:" << circle.girth()

}break

case 2:{

double l = 0.0, w = 0.0

cout << "请输入长方形的长和宽"<<endl

cout << "长:" cin >> l

cout << "宽:" cin >> w

Rect rect(l, w)

cout << "长方形的面积:" << rect.area() << endl

<< "周长:" << rect.girth()

}break

case 3:{

double b = 0.0, h = 0.0

cout << "请输入直角三角形的长和宽" << endl

cout << "底:" cin >> b

cout << "高:" cin >> h

Tri tri(b, h)

cout << "长方形的面积:" << tri.area() << endl

<< "周长:" << tri.girth()

}break

default:break

}

getchar() getchar()

system("cls")

}

//getchar()getchar()

system("pause")

return 0

}

执行结果:


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/7844992.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-04-10
下一篇2023-04-10

发表评论

登录后才能评论

评论列表(0条)

    保存