
getchar和putchar是C语言中的输入输出函数,而cin和cout是C++中的输入输出流。如果你想在C++中使用cin和cout,只需要在程序开头加上`#include<iostream>`,并在代码中使用`std::cin`和`std::cout`即可。²³
很好用的,功能多··你理解的我帮你解释include <stdioh>
#include <mathh>void SubMain_f(); /下级菜单,四则运算功能/
void SolveForX_f(); / 求一元一次方程解/
void MmToM_f(); / 单位转换:毫米转米/
void InchesToFeet_f(); / 单位转换: 英尺转尺 /
void CelsiusToFahrenheit_f(); / 单位转换:摄氏转绝对温度/
void Quadformula_f(); / 求一元二次方程解/
void SqRoot_f(); / 平方根运算/
void CalcAdd_f(); / 加法运算/
void CalcSub_f(); / 减法运算/
void CalcDiv_f(); / 除法运算/
void CalcMult_f(); / 乘法运算/
void Menu_f(); / 主菜单 /int nNumber1, nNumber2, nResult;int main(void)
{
int nMenuChoice;
Menu_f();
scanf("%d", &nMenuChoice); /now you input the number you want /
switch(nMenuChoice) / this is the long if statment used for the menu effect /
{
case 1:
SolveForX_f(); / Solve for x section/
break;
case 2:
SubMain_f(); /solve for diamond problems /
break;
case 3:
MmToM_f(); / convert mm to m /
break;
case 4:
InchesToFeet_f(); / convert inches to feet /
break;
case 5:
CelsiusToFahrenheit_f(); / code from C++ book that converts cel to farh /
break;
case 6:
SqRoot_f();
break;
case 7:
Quadformula_f();
break;
case 8:
return 0;
default:
printf("Please choose from one of the choices above\n");
break;
}
return 0;
}void Menu_f()
{
printf("\n\t\t Math Homework Helper Version 10 ");
printf("\n 1 Solve For The X Value ");
printf("\n 2 Simple Calculation ");
printf("\n 3 Covert Millimeter To Meter");
printf("\n 4 Covert Feet To Inches ");
printf("\n 5 Covert Celsius To fahrenhiet ");
printf("\n 6 Find the square root of a number");
printf("\n 7 QUADRATIC FORMULA");
printf("\n 8 Exit");
printf("\n Choice:" );
}
void SubMain_f()
{
char chChoice;
system("cls"); / 执行系统命令:cls-清屏 /
printf( "==================MENU===============\n");
printf( "1: Add two numbers\n");
printf( "2: Subtract two numbers\n");
printf( "3: Divide two numbers\n");
printf("4: Multiply two numbers\n");
printf("5: Back MainMenu\n");
printf( "Choice: ");
scanf(" %c", &chChoice);
printf("=====================================\n");
switch (chChoice)
{
case '1':
CalcAdd_f();
break;
case '2':
CalcSub_f();
break;
case '3':
CalcDiv_f();
break;
case '4':
CalcMult_f();
break;
case '5':
return;
default:
printf("Please choose from one of the choices above\n");
break;
}
}
void CalcAdd_f()
{
printf("The Add Function\n");
printf( "First number: ");
scanf("%d", &nNumber1);
printf("Second number: ");
scanf("%d", &nNumber2);
nResult=nNumber1+nNumber2;
printf("%d +%d=%d\n", nNumber1, nNumber2, nResult);
printf( "Press any key to continue\n");
getchar();
getchar();
SubMain_f();
}
void CalcSub_f()
{
printf( "The Subtract Function\n");
printf( "First number: ");
scanf("%d", &nNumber1);
printf( "Second number: ");
scanf("%d", &nNumber2);
nResult=nNumber1-nNumber2;
printf("%d + %d = %d\n", nNumber1,nNumber2 , nResult);
printf("Press any key to continue\n");
getchar();
getchar();
SubMain_f();
}
void CalcDiv_f()
{
printf( "The Divide Function\n");
printf( "First number: ");
scanf("%d", &nNumber1);
printf("Second number: ");
scanf("%d", &nNumber2);
nResult= nNumber1/nNumber2;
printf("%d / %d = %d\n ", nNumber1, nNumber2 , nResult);
printf( "Press any key to continue\n");
getchar();
getchar();
SubMain_f();
}
void CalcMult_f()
{
printf("The Multiply Function\n");
printf( "First number: ");
scanf("%d", &nNumber1);
printf( "Second number: ");
scanf("%d", &nNumber2);
nResult=nNumber1nNumber2;
printf("%d %d= %d\n", nNumber1, nNumber2 , nResult);
printf( "Press any key to continue\n");
getchar();
getchar();
SubMain_f();
}void SqRoot_f()
{
printf("The Square Root Function\n");
printf( "First number: ");
scanf("%d", &nNumber1);
nResult=sqrt(nNumber1);
printf( "The square root of %d is %d\n", nNumber1, nResult);
printf( "Press any key to continue\n");
getchar ();
Menu_f();
}void SolveForX_f()
{
int nCoef, nWholeNumber, nEqual, nX;
printf("\nThis will run you through solving for x in the since that the");
printf("\nProgram reads like y = 2x + 3");
printf("\nOnly put in one number at a time then push enter");
printf("\nMore instructions will come after you put that number in and push enter\n\n");
printf("y = ");
scanf("%d", &nCoef);
printf("%dx + ", nCoef);
scanf("%d", &nWholeNumber);
printf("%dx + %d=", nCoef,nWholeNumber);
scanf("%d", &nEqual);
for(;;)
{
nX = rand()%100;
if(((nCoef nX) + nWholeNumber) == nEqual)
{
printf("\nWe found out that x is equal to %d\n\n", nX);
system("PAUSE");
return ;
}
}
}void MmToM_f()
{
double dlMeter;
double dlMilimeter;
printf("Enter the value in 'Millimeters': ");
scanf("%lf", &dlMilimeter);
if(dlMilimeter < 1000)
{
printf("Way to low buddy Number MUST be over 1000!!!\n");
MmToM_f();
}
dlMeter = (dlMilimeter / 1000);
printf("\nThe answer to your question is: %lf meters\n\n", dlMeter);
return ;
}void InchesToFeet_f()
{
float flFeet;
float flInch;
printf("Enter the number of feet to get the length in inches: ");
scanf("%f", &flInch);
flFeet = 12 flInch;
printf("\n\nOh that is %f feet\n\n", flFeet);
system("PAUSE");
return ;
}void CelsiusToFahrenheit_f()
{
int nFahrenheit;
int nFactor;
int nCelsius;
printf( "Enter the temperature in Celsius:");
scanf("%d", &nCelsius);
nFactor = 212 - 32; nFahrenheit = nFactor nCelsius/100 + 32;
printf( "Fahrenheit value is:%d\n", nFahrenheit);
}void Quadformula_f()
{
int nCoefa, nCoefb, nCoefc;
float flHalfDone;
float flTopA;
float flTopB;
float flWholeA;
float flWholeB;
printf("\nWhat is the value of a: ");
scanf("%d", &nCoefa);
printf("\nWhat is the value of b: ");
scanf("%d", &nCoefb);
printf("\nWhat is the value of c: ");
scanf("%d", &nCoefc);
if (nCoefb < 0)
{
nCoefb +=(nCoefb nCoefb);
}
else
{
nCoefb -=(nCoefbnCoefb);
}
flHalfDone = sqrt( ( (nCoefb nCoefb) - (4 nCoefa nCoefc ) ) );
flTopA = nCoefb + flHalfDone;
flTopB = nCoefb - flHalfDone;
flWholeA = flTopA / (2 nCoefa);
flWholeB = flTopB / (2 nCoefa);
printf("X's Are %f , %f\n", flWholeA , flWholeB);
return ;
}
getchar()是c语言中的一个函数,可以用它来赋一个字符的值。
当程序调用getchar时,程序就等待用户按键并将输入的字符被存放在键盘缓冲区中。getchar函数的返回值是用户输入的第一个字符的ASCII码,如出错返回-1,且将用户输入的字符回显到屏幕。在此之前,如果用户在按回车之前输入了一个以上的字符,那么其他字符会保留在键盘缓存区中,等待后续getchar调用读取。也就是说,后续的getchar调用不会等待用户按键,而直接读取缓冲区中的字符,直到缓冲区中的字符读完为后,才等待用户按键。
getch与getchar基本功能相同,差别是getch直接从键盘获取键值,不等待用户按回车,只要用户按一个键,getch就立刻返回, getch返回值是用户输入的ASCII码,出错返回-1。
以前上课的时候不想做,现在很想补下,很后悔没好好学习呢~希望楼主好好努力
第4道1):
#include<stdioh>
void main(void)
{
int i,j,a[80],max,max_i;
printf("please input 10 num!\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("the 10 num is:\n");
for(i=0;i<10;i++)
printf("%d\t",a[i]);
for(max=a[0],i=0;i<10;i++)
{
if(max<a[i])
{
max=a[i];
max_i=i;
}
}
printf("the max num is:a[%d]=%d\n",max_i+1,max);
getch();
}
2)道:
#include<stdioh>
void main(void)
{
int a[3][2],i,j,sum[3];
for(i=0;i<3;i++)
{
printf("please input the score of the student %d\n",i+1);
scanf("%d%d",&a[i][0],&a[i][1]);
sum[i]=a[i][0]+a[i][1];
}
for(i=0;i<3;i++)
{
printf("the score of the student %d is:%d\t%d\n",i+1,a[i][0],a[i][1]);
printf("the sum score of the student %d is:%d\n",i+1,sum[i]);
}
getch();
}
3)道:
#include<stdioh>
#include<stringh>
main()
{
int i;
char string1[80],string2[80];
printf("please input the string 1\n");
scanf("%s",string1);
printf("please input the string 2\n");
scanf("%s",string2);
if(strcmp(string1,string2)==0)
printf("the 2 string is the same\n");
else
printf("the 2 string is different\n");
getch();
}
第5道:
#include<stdioh>
main()
{
float c,f;
printf("please input the F\n");
scanf("%f",&f);
c=(f-32)5/9;
printf("the convert result is:%2f\n",c);
getch();
}
小问:
float convert(float a)
{
float b;
b=(a-32)5/9;
return b;
}
main()
{
float c,f;
printf("please input the F\n");
scanf("%f",&f);
c=convert(f);
printf("the convert result is %2f\n",c);
getch();
}
第6道:
#include<stdioh>
main()
{
int x,y,p1,p2;
printf("please input the 2 nums;\n");
scanf("%d%d",&x,&y);
swap1(x,y);
printf("the swap1 result is:%d\t%d\n",x,y);
p1=&x;
p2=&y;
swap2(p1,p2);
printf("the swap2 result is:%d\t%d\n",p1,p2);
getch();
}
swap1(int x,int y)
{
int temp;
temp=x;
x=y;
y=temp;
}
swap2(int p1,int p2)
{
int temp;
temp=p1;
p1=p2;
p2=temp;
}
62)
#include <stdioh>
void main()
{
char s[80];/定义一个数组/
char c;
printf("which style you want to:\n");/是\不是|/
printf("capital ( c ) or uncapital(a):");
c=getchar();
if(c=='c')/是==不是=/
strcpy(s,"COMPUTER");
else
strcpy(s,"computer");
puts(s);
getch();
}
第七道
struct student
{
char num[8];
char name[80];
float score;
}stu[4];
main()
{
int i,max,max_i;
for(i=0;i<4;i++)
{
printf("input the num of student %d\n",i+1);
scanf("%s",&stu[i]num);
printf("input the name of student %d\n",i+1);
scanf("%s",&stu[i]name);
printf("input the score of student %d\n",i+1);
scanf("%f",&stu[i]score);
}
for(max=stu[0]score,i=0;i<4;i++)
{
if(max<stu[i]score)
{
max=stu[i]score;
max_i=i;
}
}
printf("the highest score student is student%d\n",max_i+1);
printf("%s\t%s\t%2f\n",stu[max_i]num,stu[max_i]name,stu[max_i]score);
getch();
}
第八道:
Java的行不,C++的不记得了。
public class Circle
{
private static double PI=314
private double radius;
public Circle(double radius)
{
thisradius=radius;
}
public double Area()
{
return PIradiusradius;
}
public double Perimeter()
{
return 2PIradius;
}
}
以上就是关于如何将getchar,putchar函数改为cin,cout函数全部的内容,包括:如何将getchar,putchar函数改为cin,cout函数、怎么用C语言写一个计算器、getchar()是什么意思等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)