今天晚上交C++作业了!要来不及了!奉上3道题目 小弟感激不尽!!(刚学了指针)

如题所述

第一题:

#include <iostream>
using namespace std;

void main()
{
int iTurtle = 0; // Turtle distance
int iRabbit = 0; // Rabbit distance
int iTime = 0; // Input time

// Input the time
cout << "Please input the time: ";
cin >> iTime;
if (iTime == 0)
{
cout << "Time cannot be equal to 0" << endl;
exit(0);
}
cout << endl;

int i = 0;
while (i < iTime)
{
iTurtle += 3;
iRabbit += 9;
if (iRabbit % 10 == 0)
{
iTurtle += 30 * 3;
i += 30;
}
i++;
}

// Output
if (iTurtle>iRabbit)
cout << "@_@" << iTurtle << endl;
else if (iRabbit > iTurtle)
cout << "^_^" << iRabbit << endl;
else
cout << "-__-" << iRabbit << endl;

cin.ignore();

}

第二题

#include <iostream>
using namespace std;

void main()
{
int iInteger = 0;
int i1, i2, i3, i4; // 个十百千
i1 = i2 = i3 = i4 = 0;

cout << "Please input the number: ";
cin >> iInteger;
if (iInteger > 9999 || iInteger < 1000)
{
cout << "ERROR! Invalid input" << endl;
}
cout << endl;

// Get the number of each position
i4 = iInteger / 1000;
i3 = (iInteger - i4 * 1000) / 100;
i2 = (iInteger - i3 * 100 - i4 * 1000) / 10;
i1 = (iInteger - i2 * 10 - i3 * 100 - i4 * 1000);

// Encrypt
i4 = (i4 + 9) % 10;
i3 = (i3 + 9) % 10;
i2 = (i2 + 9) % 10;
i1 = (i1 + 9) % 10;

int temp;
temp = i4;
i4 = i2;
i2 = temp;

temp = i3;
i3 = i1;
i1 = temp;

cout << "The encrypted number is " << i4 * 1000 + i3 * 100 + i2 * 10 + i1 << endl;


}

第三题:

int str_to_int(char *str, int &result)
{
int temp;

if (atoi(str) != 0)
{
// 8进制
if (str[0] == '0')
{
result = strtol(str, NULL, 8);
}
// 16进制
else if (str[0] == '0' && str[1] == 'x')
{
result = strtol(str, NULL, 16);
}
else
{
result = atoi(str);
}
return 1;
}
else
{
return 0;
}
}

请采纳,谢谢。

温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-04

6.3  :

#include <iostream>
using namespace std;
int main()
{
while (true)
{
int t;
int sumA = 0, sumB = 0;
cin >> t;
bool sleep=false;
int  sleepTime = 0;
for (int i = 1; i <= t;i++)
{
sumA += 3;
if (!sleep)
sumB += 9;
else
sleepTime++;
if (i % 10 == 0)
{
if (sumB > sumA)
sleep = true;
}
if (sleepTime == 30)
{
sleep = false;
sleepTime = 0;
}
}
if (sumA > sumB)
cout << "@_@ " << sumA << endl;
else if (sumA = sumB)
cout << "-_- " << sumA << endl;
else
cout << "^- ^" << sumB << endl;
}
system("pause"); //暂停以便查看程序结果 
return 0;
}

 6.2  :

#include <iostream>
using namespace std;
int main()
{
char c[5] = {0};
int  x[4];
cin >> c;
for (int i = 0; i < 4; i++)
{
x[i] = ((c[i] - '0') + 9) % 10;
}
c[0] = '0' + x[2];
c[1] = '0' + x[3];
c[2] = '0' + x[0];
c[3] = '0' + x[1];
cout << "The encryped number is " <<c;
system("pause"); //暂停以便查看程序结果 
return 0;
}

相似回答