getchar(),gets()
最重要的区别是gets读一行,getchar()()读一个字符。
先看原型:
char() * gets(char() * ptr);
int getchar(void);
作用是:
gets()用于从标准输入流stdin读入一个整行(以'\n'或EOF)结束,写入ptr指向的字符数组,并返回这个指针;出错或遇到文件结束时则返回NULL。行末的'\n'从流中取出,但不写入数组。gets()不检查被写入的数组大小。
getchar()用于从标准输入流stdin读入一个字符,并返回这个字符。如果读到文件结尾,则返回EOF。注意到EOF不能用char类型表示,所以getchar()函数返回的是一个int型的数。使用时也应该注意这一点。
#include<stdio.h>
#include<stdlib.h>
int main()
{
system("color f5");
printf(" *\n **\n ***\n");
system("pause");
return 0;
}
#include<iostream>
#include<math.h>
using namespace std;
class Shape
{
public:
virtual double Area() const = 0;
virtual double Perim() const = 0;
virtual void Show()
{
cout << "Area is:" << Area() << endl;
cout << "Perim is:" << Perim() << endl;
}
};
class Rectangle:public Shape
{
private:
double Length;
double Width;
public:
Rectangle(double Length = 0,double Width = 0)
{
this->Length = Length;
this->Width = Width;
}
~Rectangle()
{
}
double Area() const
{
return Length*Width;
}
double Perim() const
{
return 2*(Length+Width);
}
/*void Show( )
{
cout << "Area is:" << Area() << endl;
cout << "Perim is:" << Perim() << endl;
}*/
};
const double PI = 3.14159;
class Circle:public Shape
{
private:
double Radius;
public:
Circle(double Radius = 0)
{
this->Radius = Radius;
}
~Circle()
{
}
double Area()const
{
return PI*Radius*Radius;
}
double Perim() const
{
return 2*PI*Radius;
}
/*void show()
{
cout << "Area is:" << Area() << endl;
cout << "Perim is:" << Perim() << endl;
}*/
};
class Triangle:public Shape
{
private:
double A,B,C;
public:
Triangle(double A = 0,double B = 0,double C = 0)
{
this->A = A;
this->B = B;
this->C = C;
}
~Triangle()
{
}
double Area() const
{
double P;
P = (A +B + C)/2;
return sqrt(P*(P-A)*(P-B)*(P-C));
}
double Perim() const
{
return(A+B+C);
}
/*void show()
{
cout << "Area is:" << Area() << endl;
cout << "Perim is:" << Perim() << endl;
}*/
};
void main()
{
double Length,Width,Radius,A,B,C;
cout << "Rectangle :" << endl;
loopa:
cout << "请输入矩形的长河宽(Length,Width):" << endl;
cin >> Length >> Width;
if(Length <= 0||Width <= 0)
{
cout << "输入的长度不能构成一个矩形!请重新输入!" <<endl;
goto loopa;
}
else
{
Rectangle Rect(Length,Width);
Rect.Show();
cout << "Circle: "<<endl;
}
loopb:
cout << "请输入圆形的半径(Radius): " << endl;
cin >> Radius;
if(Radius <= 0)
{
cout << "输入的半径不能构成一个圆!请重新输入!" << endl;
goto loopb;
}
else
{
Circle Cir(Radius);
Cir.Show();
cout << "Triangle:" << endl;
}
loopc:
cout << "请输入三角形的三边(A,B,C): "<< endl;
cin >> A >> B >> C;
if(A <= 0||B <= 0||C <= 0 ||(A+B) <= C || (A + C) <= B||(B + C) <= A)
{
cout << "输入的三边值不能构成一个三角形!请重新输入!" << endl;
goto loopc;
}
else
{
Triangle Tri(A,B,C);
Tri.Show();
}
}