运算代码
❶ 算法代码怎么写
按照你的要求编写的c语言程序如下:
#include
int main(){
int n=0;
while(1){
if(n%1==0 && n%2==1 && n%3==0 && n%4==1 && n%5==1 && n%6==3 && n%7==0 && n%8==1 && n%9==0){
printf("筐里回有答%d个鸡蛋",n);
❷ 用C语言写一个加法运算的代码怎么写
例子如下:
知识扩展:
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言。
尽管C语言提供了许多低级处理的功能,但仍然保持着良好跨平台的特性,以一个标准规格写出的C语言程序可在许多电脑平台上进行编译,甚至包含一些嵌入式处理器(单片机或称MCU)以及超级电脑等作业平台。
C语言属于高级程序语言的一种,它的前身是“ALGOL”。其创始人是布朗·W·卡尼汉和丹尼斯·M·利奇。C语言问世时是带有很大的局限性,因为它只能用于UNIX系统上。然而随着科学技术的进步,计算机工业的发展,C语言逐渐脱离UNIX。1987年美国标准化协会制定了C语言的国际标准,简称“ANSI C”,从此以后它便成为一种广泛使用的程序语言。
❸ c语言函数计算机四则运算代码
4则运算的话。。。。最多有个除0判断,溢出判断吧
❹ 四则运算 源代码
VB原代码:
Private Function EvaluateExpr(ByVal expr As String) As Single
Const PREC_NONE = 11
Const PREC_UNARY = 10 ' Not actually used.
Const PREC_POWER = 9
Const PREC_TIMES = 8
Const PREC_DIV = 7
Const PREC_INT_DIV = 6
Const PREC_MOD = 5
Const PREC_PLUS = 4
Dim is_unary As Boolean
Dim next_unary As Boolean
Dim parens As Integer
Dim pos As Integer
Dim expr_len As Integer
Dim ch As String
Dim lexpr As String
Dim rexpr As String
Dim value As String
Dim status As Long
Dim best_pos As Integer
Dim best_prec As Integer
' Remove leading and trailing blanks.
expr = Trim$(expr)
expr_len = Len(expr)
If expr_len = 0 Then Exit Function
' If we find + or - now, it is a unary operator.
is_unary = True
' So far we have nothing.
best_prec = PREC_NONE
' Find the operator with the lowest precedence.
' Look for places where there are no open
' parentheses.
For pos = 1 To expr_len
' Examine the next character.
ch = Mid$(expr, pos, 1)
' Assume we will not find an operator. In
' that case the next operator will not
' be unary.
next_unary = False
If ch = " " Then
' Just skip spaces.
next_unary = is_unary
ElseIf ch = "(" Then
' Increase the open parentheses count.
parens = parens + 1
' An operator after "(" is unary.
next_unary = True
ElseIf ch = ")" Then
' Decrease the open parentheses count.
parens = parens - 1
' An operator after ")" is unary.
next_unary = True
' If parens < 0, too many ')'s.
If parens < 0 Then
Err.Raise vbObjectError + 1001, _
"EvaluateExpr", _
"Too many )s in '" & _
expr & "'"
End If
ElseIf parens = 0 Then
' See if this is an operator.
If ch = "^" Or ch = "*" Or _
ch = "/" Or ch = "\" Or _
ch = "%" Or ch = "+" Or _
ch = "-" _
Then
' An operator after an operator
' is unary.
next_unary = True
Select Case ch
Case "^"
If best_prec >= PREC_POWER Then
best_prec = PREC_POWER
best_pos = pos
End If
Case "*", "/"
If best_prec >= PREC_TIMES Then
best_prec = PREC_TIMES
best_pos = pos
End If
Case "\"
If best_prec >= PREC_INT_DIV Then
best_prec = PREC_INT_DIV
best_pos = pos
End If
Case "%"
If best_prec >= PREC_MOD Then
best_prec = PREC_MOD
best_pos = pos
End If
Case "+", "-"
' Ignore unary operators
' for now.
If (Not is_unary) And _
best_prec >= PREC_PLUS _
Then
best_prec = PREC_PLUS
best_pos = pos
End If
End Select
End If
End If
is_unary = next_unary
Next pos
' If the parentheses count is not zero,
' there's a ')' missing.
If parens <> 0 Then
Err.Raise vbObjectError + 1002, _
"EvaluateExpr", "Missing ) in '" & _
expr & "'"
End If
' Hopefully we have the operator.
If best_prec < PREC_NONE Then
lexpr = Left$(expr, best_pos - 1)
rexpr = Right$(expr, expr_len - best_pos)
Select Case Mid$(expr, best_pos, 1)
Case "^"
EvaluateExpr = _
EvaluateExpr(lexpr) ^ _
EvaluateExpr(rexpr)
Case "*"
EvaluateExpr = _
EvaluateExpr(lexpr) * _
EvaluateExpr(rexpr)
Case "/"
EvaluateExpr = _
EvaluateExpr(lexpr) / _
EvaluateExpr(rexpr)
Case "\"
EvaluateExpr = _
EvaluateExpr(lexpr) \ _
EvaluateExpr(rexpr)
Case "%"
EvaluateExpr = _
EvaluateExpr(lexpr) Mod _
EvaluateExpr(rexpr)
Case "+"
EvaluateExpr = _
EvaluateExpr(lexpr) + _
EvaluateExpr(rexpr)
Case "-"
EvaluateExpr = _
EvaluateExpr(lexpr) - _
EvaluateExpr(rexpr)
End Select
Exit Function
End If
' If we do not yet have an operator, there
' are several possibilities:
'
' 1. expr is (expr2) for some expr2.
' 2. expr is -expr2 or +expr2 for some expr2.
' 3. expr is Fun(expr2) for a function Fun.
' 4. expr is a primitive.
' 5. It's a literal like "3.14159".
' Look for (expr2).
If Left$(expr, 1) = "(" And Right$(expr, 1) = ")" Then
' Remove the parentheses.
EvaluateExpr = EvaluateExpr(Mid$(expr, 2, expr_len - 2))
Exit Function
End If
' Look for -expr2.
If Left$(expr, 1) = "-" Then
EvaluateExpr = -EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If
' Look for +expr2.
If Left$(expr, 1) = "+" Then
EvaluateExpr = EvaluateExpr( _
Right$(expr, expr_len - 1))
Exit Function
End If
' Look for Fun(expr2).
If expr_len > 5 And Right$(expr, 1) = ")" Then
lexpr = LCase$(Left$(expr, 4))
rexpr = Mid$(expr, 5, expr_len - 5)
Select Case lexpr
Case "sin("
EvaluateExpr = Sin(EvaluateExpr(rexpr))
Exit Function
Case "cos("
EvaluateExpr = Cos(EvaluateExpr(rexpr))
Exit Function
Case "tan("
EvaluateExpr = Tan(EvaluateExpr(rexpr))
Exit Function
Case "sqr("
EvaluateExpr = Sqr(EvaluateExpr(rexpr))
Exit Function
End Select
End If
' See if it's a primitive.
On Error Resume Next
value = Primitives.Item(expr)
status = Err.Number
On Error GoTo 0
If status = 0 Then
EvaluateExpr = CSng(value)
Exit Function
End If
' It must be a literal like "2.71828".
On Error Resume Next
EvaluateExpr = CSng(expr)
status = Err.Number
On Error GoTo 0
If status <> 0 Then
Err.Raise status, _
"EvaluateExpr", _
"Error evaluating '" & expr & _
"' as a constant."
End If
End Function
自己看吧,来自vbworld
这是一个核心函数,本人未修改
❺ 如何用C语言编写幂运算的代码
#include"stdio.h"
#include"math.h"
intmain()
{
floata,b;
printf("请输入整数:");
scanf("%f",&a);
printf("请输入幂:");
scanf("%f",&b);
printf("结果:%g ",pow(a,b));
}
❻ vb加法计算代码
PrivateSub Command1_Click()Dim a As Integer,b As
Integera=Val(Text1.Text)b=Val(Text2.Text)Text3.Text=a+bEnd Sub在这段VB程序中,“a=Val(Text1.Text)b=Val(Text2.Text)”是加法的代码。
代码如下:
PrivateSub Command1_Click()
Label2.Caption = Val(Text1.Text) + Val(Text2.Text)
End Sub
PrivateSub Command2_Click()
Text1.Text = "": Text2.Text = "": Label2.Caption = ""
Text1.SetFocus
End Sub
Private Sub Command3_Click()
End
End Sub
(6)运算代码扩展阅读
OptionExplicit
PrivateSubCombo1_Change()
EndSub
PrivateSubCommand1_Click()
IfIsNumeric(Text1.Text)AndIsNumeric(Text2.Text)Then
SelectCaseCombo1.Text
Case"+"
Text3.Text=CLng(Text1.Text)+CLng(Text2.Text)
Case"-"
Text3.Text=CLng(Text1.Text)-CLng(Text2.Text)
Case"×"
Text3.Text=CLng(Text1.Text)*CLng(Text2.Text)
Case"÷"
IfCLng(Text2.Text)<>0ThenText3.Text=CLng(Text1.Text)/CLng(Text2.Text)ElseMsgBox"出数不能为0",vbOKOnly,"提示"
CaseElse
MsgBox"请选择运算方式",vbOKOnly,"提示"
EndSelect
Else
MsgBox"请输入数字",vbOKOnly,"提示"
EndIf
EndSub
PrivateSubForm_Load()
Combo1.AddItem"+"
Combo1.AddItem"-"
Combo1.AddItem"×"
Combo1.AddItem"÷"
EndSub
❼ c语言代码运算问题
除了最后两个printf,其它的都是%d,对浮点数以整数输出自然是0,在输出前应强制转型,如printf("%d",(int)b);