我来自C背景,现在我正在使用C++学习OOP
以下是计算阶乘的程序.
#include <iostream>
using namespace std;
void main ()
{
char dummy;
_int16 numb;
cout << "Enter a number: ";
cin >> numb;
double facto(_int16);
cout << "factorial = " <<facto(numb);
cin >> dummy;
}
double facto( _int16 n )
{
if ( n>1 )
return ( n*facto(n-1) );
else
return 1;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码工作正常.
但是如果我替换return语句
return ( n*facto(n-1) );
Run Code Online (Sandbox Code Playgroud)
有了这个
return ( n*facto(n--) );
Run Code Online (Sandbox Code Playgroud)
那它不起作用.该N--将不会递减ñ 1.为什么呢?
我正在使用Visual Studio 2012
编辑:知道了!谢谢:)
*也,我想添加到下面的答案:使用--n将导致在n执行语句之前减少.因此,由于预先递减,表达式将成为(n-1)*facto(n-1).这就是为什么在这种情况下最好不要使用预减量*
单击一个按钮后,会将短信发送到NumTxt文本框中输入的号码,然后发送在SMSTxt文本框中输入的文本。在texbox ComPort中输入的端口名称这是按钮单击事件的事件处理程序。
using System.IO.Ports;
private void button1_Click(object sender, EventArgs e)
{
try
{
int mSpeed = 1;
serialport.PortName = ComPort.Text;
serialport.BaudRate = 96000;
serialport.Parity = Parity.None;
serialport.DataBits = 8;
serialport.StopBits = StopBits.One;
serialport.Handshake = Handshake.XOnXOff;
serialport.DtrEnable = true;
serialport.RtsEnable = true;
serialport.NewLine = Environment.NewLine;
Console.WriteLine("1a");
try
{
serialport.Open();
}
catch (Exception)
{
MessageBox.Show("Try another Port." +
Environment.NewLine + "Phone not detected or The requested resource is in
use.", "CONNECTION ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
Console.WriteLine("2a");
serialport.WriteLine("AT+CMGF=1" + Environment.NewLine);
System.Threading.Thread.Sleep(200);
serialport.WriteLine("AT+CSCS=GSM" …Run Code Online (Sandbox Code Playgroud) 我正在使用Robert Lafore的书(OOP with C++)学习C++.在本书中,我遇到了这个例子:
#include <iostream>
#include <conio.h>
using namespace std;
void main ()
{
int numb;
for ( numb = 1 ; numb <= 10 ; numb++ )
{
cout << setw(4) << numb;
int cube = numb*numb*numb;
cout << setw(6) << cube << endl;
}
getch();
}
Run Code Online (Sandbox Code Playgroud)
变量'cube'已在循环体内声明为int'.
int cube = numb*numb*numb;
Run Code Online (Sandbox Code Playgroud)
由于循环迭代10次,变量'cube'也将被声明10次.无论迭代次数如何,'cube'都可以在循环体内访问.所以,当我们进入第二次迭代的循环体时,'cube'已经知道了(因为它已经在第一次迭代中声明了),而'cube'的另一个声明应该给出"重新定义"的错误.但相反,它成功构建并调试没有问题.为什么?
在C#中,我们了解到当多个函数具有相同的标识符但签名不同时会发生函数重载.
虽然函数重载的概念是面向对象语言的,但在以下观察的基础上它是否也适用于C语言?
printf("%d", 3);
printf("%d + %d = %d", 1 , 2 , 3 );
Run Code Online (Sandbox Code Playgroud)
第一个printf只传递两个参数.第二个printf传递了四个参数.
这是否意味着printf超载?