下面是修改后的程序,可以正确的执行。循环变量的时候有问题。
#include "stdioh"
#include "mathh"
int s(int n)
{
int i,s=0;
for(i=1;i<=n;i++)
s+=i;
return s;
}
int q(int n,int x)
{ int i,s=1; //这儿
for(i=0;i<n;i++) //<-这儿
s = x; //这儿
return s;
}
void main()
{
int n,x,i;
double sum=1;
printf("input n and x\n");
scanf("%d%d",&n,&x);
for(i=1;i<=n;i++)
sum+= (q(i,x)10)/s(i); //这儿有问题
printf("sum=%lf\n",sum);
}
#include<iostream>
#define
PI
31415926;
using
namespace
std;
class
Shape
//抽象类的
定义
{
public:
virtual
double
GetArea()
=
0;
//纯虚函数
virtual
double
GetPerim()
=
0;
//纯虚函数
};
class
Rectangle
:
public
Shape
//矩形类,公有继承
{
public:
Rectangle(double
aa,
double
bb)
//带参数的
构造函数
{
a=aa;
b=bb;
cout<<"长"<<a<<"宽"<<b<<endl;
}
virtual
double
GetArea()
{
return
a
b;
}
virtual
double
GetPerim()
{
return
2(
a
+
b
);
}
private:
double
a;
double
b;
};
class
Circle
:
public
Shape
//圆类,公有继承
{
public:
Circle(double
rr)
//带参数的
构造函数
{
r=rr;
cout<<"半径"<<r<<endl;
}
virtual
double
GetArea()
{
return
r
r
PI;
}
virtual
double
GetPerim()
{
return
2
r
PI;
}
private:
double
r;
};
void
main()
{
double
length,
width;
cout
<<
"输入长和宽:
";
cin
>>
length
>>
width;
Rectangle
rect(length,
width);
cout
<<
"面积是:"<<
rectGetArea()
<<
endl<<"周长是:"<<rectGetPerim()<<endl;
double
rr;
cout
<<
"输入半径:
";
cin
>>
rr;
Circle
cir(rr);
cout
<<
"面积是:"<<cirGetArea()
<<
endl<<"周长是:"<<cirGetPerim()<<endl;
}
(a++,b++,a+b)为逗号表达式,从左到右执行,其值为最后一个表达式的值,执行完a++,b++后a=2,b=3,所以此逗号表达式的值为5。c++的值为3,所以fun函数两实参分别为5和3,最后sum得8,然后输出sum的值8。
第一个
#include<stdioh>
int main()
{
int x,f(x);
printf("please scanf x\n");
scanf("%d",&x);
if(x>0)
f(x)==1;
else if(x==0)
f(x)==0;
else f(x)==-1;
printf("%d\n",f(x));
return 0;
}
#include<stdioh>
typedef double(P)(double,double);
double add(double a,double b){
return a+b;
}
double sub(double a,double b){
return a-b;
}
double mul(double a,double b){
return ab;
}
double div(double a,double b){
return a/b;
}
double fun(double a,double b,P p){
return p(a,b);
}
int main()
{
double a,b;
printf("请输入a和b的值:\n");
scanf("%lf%lf",&a,&b);
printf("加法运算结果为:%2lf\n", fun(a,b,add));
printf("减法运算结果为:%2lf\n" ,fun(a,b,sub));
printf("乘法运算结果为:%2lf\n" ,fun(a,b,mul));
printf("除法运算结果为:%2lf\n" ,fun(a,b,div));
return 0;
}
实用计算器之程序设计
[摘 要]多用计算器的构思及设计代码
[关键词]多用计算器;设计
数值计算可以说是日常最频繁的工作了,WIN98提供了“计算器”软件供用户使用,该软件可以处理一般的一步四则运算,例如:3+2、5/3等等,但在日常中用户经常遇到多步四则运算问题,例如:3+45-4/2,4534/2+187等等,那么该个计算器就无法胜任了,作者制作了一个实用的计算器,该计算器新增不少功能:(程序界面如图)
1.可以实现连续的四则运算
2.可以实现输入式子的显示
3.可以方便计算个人所得税
4.鼠标、键盘均可输入数据
5. *** 作界面友好
6.击键可发声
构建该个计算器所需研究及解决的核心问题有如下几个:1、连乘求值2、字符显示 3、键盘输入4、击键发声5、个人所得税法规,为了使大家对程序有更一步认识,现将代码提供给读者参考:
定义数组及窗体变量
Dim number2(0 To 50) As Double
Dim number(0 To 50) As Double
Dim z As Integer
Dim k As Integer, r As Integer
Dim j As Integer
Dim str As String
调用名为“playsound”的API函数
Private Declare Function PlaySound Lib "winmmdll" Alias "PlaySoundA" (ByVal lpszName As String, ByVal hModule As Long, ByVal dwFlags As Long) As Long
Private Const SND_FILENAME = &H20000
Private Const SND_ASYNC = &H1
Private Const SND_SYNC = &H0
判断通用过程
Sub pianduan(p As String)
r = 0
Dim i As Integer, l As Integer, h As Integer
h = 0
i = 1
If InStr(Trim$(p), "") <> 0 Then
k = k + 1
End If
If InStr(Trim$(p), "/") <> 0 Then
r = r + 1
End If
End Sub
连乘通用过程(略)
各按钮事件过程
Private sub Command1_Click(Index As Integer)
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text1Text = Text1Text + Command1(Index)Caption
Text2Text = Text2Text + Command1(Index)Caption
Text1SetFocus
End Sub
rivate sub Command10_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
str = Text3Text
End Sub
Private sub Command11_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text3Text = str
End Sub
rivate sub Command2_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "+"
z = z + 1
Text1SetFocus
End Sub
rivate sub Command3_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "-"
Text1Text = Text1Text & "-"
z = z + 1
Text1SetFocus
End Sub
Private sub Command4_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text2Text = Text2Text + ""
Text1Text = Text1Text + ""
Text1SetFocus
End Sub
rivate sub Command5_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text2Text = Text2 + "/"
Text1Text = Text1 + "/"
Text1SetFocus
End Sub
Private sub Command6_Click()
PlaySound AppPath & "\soundwav", 0, SND_SYNC
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
z = z + 1
Dim dengyu As Double
Dim v As Integer
For v = 0 To 50
dengyu = dengyu + number2(v)
Next v
If dengyu < 0 Then
Text3ForeColor = &HFF&
Else
Text3ForeColor = &HFF0000
End If
Text3Text = dengyu
Text1SetFocus
If Len(Text3Text) >= 14 Then
calcresultShow
End If
End Sub
rivate sub Command7_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
z = 0: k = 0: r = 0: j = 0
Dim i As Integer
For i = 0 To 50
number(i) = 0
number2(i) = 0
Next i
Text1Text = ""
Text2Text = ""
Text3Text = ""
Text1SetFocus
End Sub
rivate sub Command8_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
If Val(Text3Text) = 0 Then
MsgBox "除数不能为0!"
Exit Sub
End If
Text3Text = 1 / Val(Text3Text)
End Sub
Private sub Command9_Click()
PlaySound AppPath & "\startwav", 0, SND_SYNC
Text3ForeColor = &HFF0000
Text3Text = Val(Text3Text) Val(Text3Text)
End Sub
rivate sub muninternet_Click()
Dim i
i = Shell("C:\Program Files\InternetExplorer\iexploreexe", vbMaximizedFocus)
End Sub
rivate sub munmp3_Click()
Dim i
i = Shell("C:\Program Files\Windows Media Player\mplayer2", vbNormalNoFocus)
End Sub
Private sub munsm_Click()
DialogShow
End Sub
rivate sub muntax_Click()
taxShow
End Sub
rivate sub munver_Click()
verShow
End Sub
rivate sub notepad_Click()
Dim i
i = Shell("c:\windows\notepad", vbNormalFocus)
End Sub
Private sub Text1_KeyPress(KeyAscii As Integer)
PlaySound AppPath & "\startwav", 0, SND_SYNC
Dim num As Integer
num = Val(KeyAscii)
If num > 47 And num < 58 Then
Text1Text = Text1Text + CStr(num - 48)
Text2Text = Text2Text + CStr(num - 48)
End If
If num = 46 Then
Text1Text = Text1Text + ""
Text2Text = Text2Text + ""
End If
If KeyAscii = 43 Then
Dim totle As Double
Dim n As Integer
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "+"
z = z + 1
End If
If KeyAscii = 45 Then
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
Text2Text = Text2 + "-"
Text1Text = Text1Text & "-"
z = z + 1
End If
If KeyAscii = 42 Then
Text2Text = Text2Text + ""
Text1Text = Text1Text + ""
End If
If KeyAscii = 47 Then
Text2Text = Text2Text + "/"
Text1Text = Text1Text + "/"
End If
If KeyAscii = vbKeyReturn Then
PlaySound AppPath & "\soundwav", 0, SND_SYNC
Call pianduan(Text1Text)
If k >= 1 Or r >= 1 Then
Call liancheng(totle)
number2(z) = totle
If Mid$(Trim$(Text1Text), 1, 1) = "-" Then
number2(z) = -totle
End If
k = 0: r = 0
Else
number2(z) = Val(Text1Text)
End If
Text1Text = ""
z = z + 1
Dim dengyu As Double
Dim v As Integer
For v = 0 To 50
dengyu = dengyu + number2(v)
Next v
If dengyu < 0 Then
Text3ForeColor = &HFF&
Else
Text3ForeColor = &HFF0000
End If
Text3Text = dengyu
End If
If KeyAscii = vbKeyEscape Then
z = 0: k = 0: r = 0: j = 0
Dim i As Integer
For i = 0 To 50
number(i) = 0
number2(i) = 0
Next i
Text1Text = ""
Text2Text = ""
Text3Text = ""
Text1SetFocus
End If
If Len(Text3Text) >= 14 Then
calcresultShow
End If
End Sub
rivate sub Text3_Change()
tax2Text1 = Text3Text
End Sub
以上就是关于C语言 求编程序,用函数调用的方法计算如下公式全部的内容,包括:C语言 求编程序,用函数调用的方法计算如下公式、C++:程序函数、C程序函数问题,谢谢!等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)