程序代码如下:
第一个程序
#include <iostream>
using namespace std;
int main ()
{
float a, b, sum;
cout << "请输入加数a:";
cin >> a;
cout << "请输入加数b:";
cin >> b;
cout << "\n请输入结果\na+b=";
cin >> sum;
if (sum == (a+b))
cout << "Right!\n" << endl;
else
cout << "Not correct! Try again!" << endl;
return 0;
}
第二个程序
#include <iostream>
using namespace std;
int main ()
{
float a, b, sum;
bool flag = false;
cout << "请输入加数a:";
cin >> a;
cout << "请输入加数b:";
cin >> b;
while (!flag)
{
cout << "\n请输入结果\na+b=";
cin >> sum;
if (sum == (a+b))
{
flag = true;
cout << "Right!\n" << endl;
}
else
{
cout << "Not correct! Try again!" << endl;
}
}
return 0;
}
第三个程序
#include <iostream>
using namespace std;
int main ()
{
float a, b, sum;
int i = 0;
bool flag = false;
cout << "请输入加数a:";
cin >> a;
cout << "请输入加数b:";
cin >> b;
while (!flag && (i < 3))
{
cout << "\n请输入结果\na+b=";
cin >> sum;
if (sum == (a+b))
{
flag = true;
cout << "Right!\n" << endl;
}
else
{
cout << "Not correct! Try again!" << endl;
}
i++;
} if (i == 3)
{
cout << "Not correct! You have tried three times! Test over!" << endl << endl;
}
return 0;
}
第四个程序
#include <iostream>
#include<stdlib.h>
#include<time.h>
using namespace std;
int main ()
{
float a, b, sum, count = 0, score = 0;
int i = 0;
srand ((unsigned)time (NULL)); //产生随机数种子
while (i < 10)
{
//a+(int)(n*rand()/(RAND_MAX+1.0))产生一个a到n之间的随机数
a = 1 + (int) (10 * rand () / (RAND_MAX + 1)); b = 1 + (int) (10 * rand () / (RAND_MAX + 1)); cout << "\n请输入结果\n" << a << "+" << b << "=";
cin >> sum;
if (sum == (a+b))
{
score += 10;
cout << "Right!\n" << endl;
}
else
{
count++;
cout << "Not correct! Try again!" << endl;
}
i++;
}
cout << "得分:" << score << "\t\t做错了" << count << "道题" << endl;
return 0;
}