检测正数负数和0各自的数量。
•询问用户想输入多少个数字
•循环给定次数
•提示用户每次循环输入一个数字
•显示有多少正数,负数和零数被输入的摘要
汇编代码将在C程序的_asm块中,使用C语言声明变量
语法,如实验室任务中所示。从现有的模板创建一个新的Visual Studio项目。的
下面的屏幕截图显示了程序的预期输出。
#include <iostream>
using namespace std;
int main()
{
int n,tmp;
cout << "How many numbers:";
cin >> n;
int pos = 0, neg = 0, zero = 0;
for (int i = 0; i < n; i++)
{
cout << "Enter a number:";
cin >> tmp;
if (tmp==0)
{
zero++;
}
if (tmp>0)
{
pos++;
}
if (tmp<0)
{
neg++;
}
}
cout << "----------\n";
cout << "Positive:" << pos << "\n";
cout << "Negtive:" << neg << "\n";
cout << "Zero:" << zero << "\n";
}
可以在反汇编里看到汇编代码
00007FF7681222D0 push rbp
00007FF7681222D2 push rdi
00007FF7681222D3 sub rsp,1A8h
00007FF7681222DA lea rbp,[rsp+20h]
00007FF7681222DF lea rdi,[rsp+20h]
00007FF7681222E4 mov ecx,32h
00007FF7681222E9 mov eax,0CCCCCCCCh
00007FF7681222EE rep stos dword ptr [rdi]
00007FF7681222F0 mov rax,qword ptr [__security_cookie (07FF76812E000h)]
00007FF7681222F7 xor rax,rbp
00007FF7681222FA mov qword ptr [rbp+178h],rax
00007FF768122301 lea rcx,[__AD721878_ConsoleApplication1@cpp (07FF768134066h)]
00007FF768122308 call __CheckForDebuggerJustMyCode (07FF7681213D4h)
int n,tmp;
cout << "How many numbers:";
00007FF76812230D lea rdx,[string "How many numbers:" (07FF76812ACE8h)]
00007FF768122314 mov rcx,qword ptr [__imp_std::cout (07FF7681321C8h)]
00007FF76812231B call std::operator<<<std::char_traits<char> > (07FF768121087h)
cin >> n;
00007FF768122320 lea rdx,[n]
00007FF768122324 mov rcx,qword ptr [__imp_std::cin (07FF768132150h)]
00007FF76812232B call qword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (07FF768132158h)]
int pos = 0, neg = 0, zero = 0;
00007FF768122331 mov dword ptr [pos],0
00007FF768122338 mov dword ptr [neg],0
00007FF76812233F mov dword ptr [zero],0
for (int i = 0; i < n; i++)
00007FF768122349 mov dword ptr [rbp+0A4h],0
00007FF768122353 jmp __$EncStackInitStart+84h (07FF768122363h)
00007FF768122355 mov eax,dword ptr [rbp+0A4h]
00007FF76812235B inc eax
00007FF76812235D mov dword ptr [rbp+0A4h],eax
00007FF768122363 mov eax,dword ptr [n]
00007FF768122366 cmp dword ptr [rbp+0A4h],eax
00007FF76812236C jge __$EncStackInitStart+0E5h (07FF7681223C4h)
{
cout << "Enter a number:";
00007FF76812236E lea rdx,[string "Enter a number:" (07FF76812AD00h)]
00007FF768122375 mov rcx,qword ptr [__imp_std::cout (07FF7681321C8h)]
00007FF76812237C call std::operator<<<std::char_traits<char> > (07FF768121087h)
cin >> tmp;
00007FF768122381 lea rdx,[tmp]
00007FF768122385 mov rcx,qword ptr [__imp_std::cin (07FF768132150h)]
00007FF76812238C call qword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (07FF768132158h)]
if (tmp==0)
00007FF768122392 cmp dword ptr [tmp],0
00007FF768122396 jne __$EncStackInitStart+0C7h (07FF7681223A6h)
{
zero++;
00007FF768122398 mov eax,dword ptr [zero]
00007FF76812239E inc eax
00007FF7681223A0 mov dword ptr [zero],eax
}
if (tmp>0)
00007FF7681223A6 cmp dword ptr [tmp],0
00007FF7681223AA jle __$EncStackInitStart+0D5h (07FF7681223B4h)
{
pos++;
00007FF7681223AC mov eax,dword ptr [pos]
00007FF7681223AF inc eax
00007FF7681223B1 mov dword ptr [pos],eax
}
if (tmp<0)
00007FF7681223B4 cmp dword ptr [tmp],0
00007FF7681223B8 jge __$EncStackInitStart+0E3h (07FF7681223C2h)
{
neg++;
00007FF7681223BA mov eax,dword ptr [neg]
00007FF7681223BD inc eax
00007FF7681223BF mov dword ptr [neg],eax
}
}
00007FF7681223C2 jmp __$EncStackInitStart+76h (07FF768122355h)
cout << "----------\n";
00007FF7681223C4 lea rdx,[string "----------\n" (07FF76812AD18h)]
00007FF7681223CB mov rcx,qword ptr [__imp_std::cout (07FF7681321C8h)]
00007FF7681223D2 call std::operator<<<std::char_traits<char> > (07FF768121087h)
cout << "Positive:" << pos << "\n";
00007FF7681223D7 lea rdx,[string "Positive:" (07FF76812AD30h)]
00007FF7681223DE mov rcx,qword ptr [__imp_std::cout (07FF7681321C8h)]
00007FF7681223E5 call std::operator<<<std::char_traits<char> > (07FF768121087h)
00007FF7681223EA mov edx,dword ptr [pos]
00007FF7681223ED mov rcx,rax
00007FF7681223F0 call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF768132168h)]
00007FF7681223F6 lea rdx,[string "\n" (07FF76812AD28h)]
00007FF7681223FD mov rcx,rax
00007FF768122400 call std::operator<<<std::char_traits<char> > (07FF768121087h)
cout << "Negtive:" << neg << "\n";
00007FF768122405 lea rdx,[string "Negtive:" (07FF76812AD40h)]
00007FF76812240C mov rcx,qword ptr [__imp_std::cout (07FF7681321C8h)]
00007FF768122413 call std::operator<<<std::char_traits<char> > (07FF768121087h)
00007FF768122418 mov edx,dword ptr [neg]
00007FF76812241B mov rcx,rax
00007FF76812241E call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF768132168h)]
00007FF768122424 lea rdx,[string "\n" (07FF76812AD28h)]
00007FF76812242B mov rcx,rax
00007FF76812242E call std::operator<<<std::char_traits<char> > (07FF768121087h)
cout << "Zero:" << zero << "\n";
00007FF768122433 lea rdx,[string "Zero:" (07FF76812AD4Ch)]
00007FF76812243A mov rcx,qword ptr [__imp_std::cout (07FF7681321C8h)]
00007FF768122441 call std::operator<<<std::char_traits<char> > (07FF768121087h)
00007FF768122446 mov edx,dword ptr [zero]
00007FF76812244C mov rcx,rax
00007FF76812244F call qword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (07FF768132168h)]
00007FF768122455 lea rdx,[string "\n" (07FF76812AD28h)]
00007FF76812245C mov rcx,rax
00007FF76812245F call std::operator<<<std::char_traits<char> > (07FF768121087h)
}
00007FF768122464 xor eax,eax
00007FF768122466 mov edi,eax
00007FF768122468 lea rcx,[rbp-20h]
00007FF76812246C lea rdx,[__xt_z+1A0h (07FF76812AC40h)]
00007FF768122473 call _RTC_CheckStackVars (07FF768121361h)
00007FF768122478 mov eax,edi
00007FF76812247A mov rcx,qword ptr [rbp+178h]
00007FF768122481 xor rcx,rbp
00007FF768122484 call __security_check_cookie (07FF7681211F9h)
00007FF768122489 lea rsp,[rbp+188h]
00007FF768122490 pop rdi
00007FF768122491 pop rbp
00007FF768122492 ret
#include<stdio.h>
int main()
{
printf("How many numbers: ");
int n,i;
scanf_s("%d",&n);
int Positive=0,Negative=0,Zero=0;
for(i=0; i<n; i++)//循环n次
{
int num;
scanf("%d",&num);
if(num>0)
{
Positive++;
}
else if(num<0)
{
Negative++;
}
else if(num==0)
{
Zero++;
}
}
printf("------------\n");
printf("Positive: %d\n",Positive);
printf("Negative: %d\n",Negative);
printf("Zero: %d\n",Zero);
return 0;
}
```c++
#include <iostream>
using namespace std;
void main()
{
int positiveNum = 0;
int negativeNum = 0;
int count = 0;
double total = 0;
cout<<"输入一个数字"<<endl;
int numb;
while (cin>>numb)
{
if (numb>0)
{
positiveNum++;
total+=positiveNum;
count++;
}
if(numb<0)
{
negativeNum++;
total+=negativeNum;
count++;
}
if(numb==0)break;
cout<<"输入一个数字"<<endl;
}
cout<<"正数的数目是:"<<positiveNum<<endl;
cout<<"负数的数目是:"<<negativeNum<<endl;
cout<<"平均数是:"<<total/count<<endl;
}
```