最重要的区别是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(); } }
相关推荐
C语言中getchar和 gets的区别。
C++深入了解scanf()getchar()和gets()等函数
对函数scanf()、getchar()、gets()的异同进行深入剖析
getchar和gets和scanf区别 getchar 用于单个字符,gets(不推荐)用于字符串,而 scanf 用于各种不同的数据类型,通常需要使用格式字符串来指定输入的格式。在现代C编程中,推荐使用更安全的函数来代替 gets,如 ...
cin.get()和cin.getline()、getline()和gets()、getchar()的区别
与缓存区相关最常见的操作就是字符的输入与输出操作getchar,getc,getch,getche,gets系列函数。 第一个例子(与getchar有关): 代码如下:#include int main() { int ch; ch=getchar(); ch=getchar(); printf(“%...
c语言中各种输入函数应该注意的地方,c语言中的scanf、getchar,gets、getche、getch。等函数的用法和区别。
《C#程序设计项目教程》,书本94面,统计从键盘所输入的中英文字符个数。 统计从键盘输入的一行字符中,中文、英文、数字字符的字数。
getchar(); memset( a , 0 , sizeof(a) ); for( i = 1 ; i ; i++ ){ gets( s1 ); gets( s2 ); index[ s1 ] = i; name[ i ] = s2; } scanf("%d",&m); getchar(); for( i = 0 ; i ; i++ ...
oid jiami(); //加密函数 void jiemi(); //解密函数 //加密函数 ...getchar(ch); gets(in); ///不足5位补齐5位 a=strlen(in)/6; for(l=strlen(in);l(a+1)*6;l++) { in[l]=' '; } /////加密过程
1、cin 2、cin.get() 3、cin.getline() 4、getline() 5、gets() 6、getchar() 附:cin.ignore(); cin.get()//跳过一个字符,例如不想要的回车,空格等字符 1、cin>> 用法1:最基本,也是最常用的用法,输入一个数字:...
给定getchar和putchar函数,实现其它C输入输出库函数。如:gets,puts,printf,scanf等。并且在原函数名前加my构成新函数名。如:mygets,myputs,myprintf,myscanf等。 对myprintf函数,其功能与printf函数相同。...
1、演示putchar和getchar函数的用法; 2、演示putch和getch函数的用法,讲解与putchar和getchar函数的区别; 3、演示puts和gets函数的用法,讲解与printf函数的关系;
//请输入要计算MD5值的字符串:... gets(encrypt); printf("\n加密结果:"); MD5Digest(encrypt,strlen(encrypt),szDigest); int i; for (i=0;i;i++) printf ("X",(unsigned char)szDigest[i]); getchar(); }
getchar 输入字符, printf 格式输出,gets 输入字符串; § 5.8 字符数据的输入输出 putchar 函数(单字符输出函数);输出控制字符;getchar 函数(单字符输入函数) ;§5.9 格式输入与输出 printf函数(格式输出函数...
getchar(c) C. gets(c) D. scanf( %s ,c) E. for(i=0;i;i++) scanf( %c ,&c[i];c[10])='\0' 正确答案:CE 2、设有定义int a[3][6],*p;则正确的指向数组元素的指针赋值是( ) (分数:2分) A. p=*a B. p=a[0] ...
gets(s); fprintf(fpw,"%s",s); fclose(fpw); FILE *fpr=fopen("data1.txt","r"); if(fpr==NULL) { return 0; } fscanf(fpr,"%s",s); puts(s); getchar(); fclose(fpr); return 0; } 输入: ...
C++中几个输入函数的用法和区别(cin、cin.get()、cin.getline()、getline()、gets()、getchar()))
getchar、putchar gets、puts stdlib.h rand()%a+b:产生0~32767的随机数,a为范围,b为范围起始值,即产生b~b+a-1的随机数。 srand(随机种子):如srand(time(0))。 malloc(8)、calloc(10,sizeof(int))、realloc...