查看: 2805|回复: 30
|
所有会C++电脑程式的人帮帮忙。。assignment due要到了。。我不会做。。呜呜
[复制链接]
|
|
1. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits of 3456 as 3 4 5 6; output the individual digits of 8030 as 8 0 3 0; output the individual digits of 2345526 as 2 3 4 5 5 2 6; output the individual digits of 4000 as 4 0 0 0.
2。 Write a program that prompts the user to input an integer and then outputs the number with the digits reversed. For example, if the input is 12345, the output should be 54321. Your program must also output 5000 as 0005 and 980 as 089.
我只是新手。。问lecturer她又不要教。。唉。。 |
|
|
|
|
|
|
|
发表于 29-3-2007 01:56 AM
|
显示全部楼层
好像是基本学吧,把integer转换成char不就解决了吗? |
|
|
|
|
|
|
|
发表于 29-3-2007 09:20 AM
|
显示全部楼层
第一题用 % (mod) 吧。。
第二题就如#2所说的吧。。
[ 本帖最后由 deity01 于 2-4-2007 01:36 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 29-3-2007 01:37 PM
|
显示全部楼层
最簡單的方法是把Integer變換成String,然後再將一個一個Character顯示出來就可以了。
如果用%的話,不知要如何知道user輸入的數字是個位數、十位數、百位數等等呢?要怎麼將1234變成4321呢?以下是我的推算,不知還有沒有更好的邏輯呢?大家討論一下吧!
1234 - (((int)(1234/10))*10) = 4
(int)(1234/10) = 123
123 - (((int)(123/10))*10) = 3
(int)(123/10) = 12
12 - (((int)(12/10))*10) = 2
(int)(12/10) = 1
當中並沒有用到%。
[ 本帖最后由 itplanet 于 29-3-2007 01:58 PM 编辑 ] |
|
|
|
|
|
|
|

楼主 |
发表于 29-3-2007 01:54 PM
|
显示全部楼层
|
|
|
|
|
|
|

楼主 |
发表于 29-3-2007 02:03 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 29-3-2007 02:16 PM
|
显示全部楼层
原帖由 angelqq 于 29 303, 2007 14:03 发表
不明白。。怎么又乘又除呢?
123除10後得到12.3,但我們只要其整數,即12。
12乘10得120,用123減120就得到3。
以此類推... |
|
|
|
|
|
|
|

楼主 |
发表于 29-3-2007 02:31 PM
|
显示全部楼层
第二题。。我大概做到了。。只是当我输入0005,它不会出现5000..有谁可以给我建议吗?
/* 2. Write a program that prompts the user to input an integer and then outputs the number with the digits reversed.
For example, if the input is 12345, the output should be 54321. Your program must also output 5000 as 0005 and 980 as 089.*/
#include<iostream>
using namespace std;
int main ()
{
int num,temp;
cout<<"enter an integer:";
cin>>num;
temp = num;
while(num>0)
{
temp=num%10;
num = num/10;
cout<<temp;
}
cout<<endl;
return 0;
} |
|
|
|
|
|
|
|
发表于 29-3-2007 03:32 PM
|
显示全部楼层
int num,temp;
cout<<"enter an integer:";
cin>>num;
有七、八年沒用C了,我想這num如果是用String可以嗎?因為integer會將0005譯為5。 |
|
|
|
|
|
|
|
发表于 29-3-2007 09:56 PM
|
显示全部楼层
原帖由 angelqq 于 28-3-2007 10:18 PM 发表
1. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output t ...
你学着的是basic programming in C 还是data structure?
如是 data structure 就用stack 来做 (fist in last out)
原帖由 angelqq 于 29-3-2007 02:31 PM 发表
第二题。。我大概做到了。。只是当我输入0005,它不会出现5000..有谁可以给我建议吗?
/* 2. Write a program that prompts the user to input an integer and then outputs the number with the digits reve ...
你的question已写明是input an interger.
so 0005, c++ 就拿5吧了。
不用怕错, lecturer 不给分就跟她吵
[ 本帖最后由 boyboyscout 于 29-3-2007 10:31 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 2-4-2007 01:15 PM
|
显示全部楼层
弟二题。。
char num[] = "";
//char num[255];
int i = strlen(num);
while(i > 0)
{
cout<<num[i-1];
--i;
}
自己尝试check invalid input 吧。。
[ 本帖最后由 deity01 于 2-4-2007 01:38 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 4-4-2007 09:13 PM
|
显示全部楼层
第二题:
#include<iostream>
using namespace std;
void intToChar(int num);
int main()
{
int nNum;
cout<<"Please insert a number: ";
cin >>nNum;
do{
intToChar(nNum%10);
nNum/=10;
}while(nNum!=0);
cout<<endl;
return 0;
}
void intToChar(int num)
{
switch(num)
{
case 0:
cout<<"0";
break;
case 1:
cout<<"1";
break;
case 2:
cout<<"2";
break;
case 3:
cout<<"3";
break;
case 4:
cout<<"4";
break;
case 5:
cout<<"5";
break;
case 6:
cout<<"6";
break;
case 7:
cout<<"7";
break;
case 8:
cout<<"8";
break;
case 9:
cout<<"9";
break;
default:
;
}
}
[ 本帖最后由 songpe 于 4-4-2007 09:14 PM 编辑 ] |
|
|
|
|
|
|
|
发表于 4-4-2007 09:22 PM
|
显示全部楼层
第二题:
这个更方便简单
#include<iostream>
#include<string>
using namespace std;
int main()
{
char cNum[256];
cout<<"Please insert a number: ";
cin >>cNum;
for(int i=strlen(cNum)-1; i>=0; i--)
cout<<cNum;
cout<<endl;
return 0;
} |
|
|
|
|
|
|
|
发表于 4-4-2007 09:33 PM
|
显示全部楼层
第一题:
与12楼差不多:
#include<iostream>
#include<string>
using namespace std;
int main()
{
int nNum=0;
int sum=0;
cout<<"Please insert a number: ";
cin >>nNum;
do{
switch(nNum%10){
case 0:
cout<<"0 ";
break;
case 1:
cout<<"1 ";
break;
case 2:
cout<<"2 ";
break;
case 3:
cout<<"3 ";
break;
case 4:
cout<<"4 ";
break;
case 5:
cout<<"5 ";
break;
case 6:
cout<<"6 ";
break;
case 7:
cout<<"7 ";
break;
case 8:
cout<<"8 ";
break;
case 9:
cout<<"9 ";
break;
default:
;
}
sum += nNum%10;
nNum /= 10;
}while(nNum!=0);
cout<<endl<<"Sum: "<<sum<<endl;
return 0;
} |
|
|
|
|
|
|
|
发表于 5-4-2007 01:08 PM
|
显示全部楼层
原帖由 angelqq 于 28-3-2007 10:18 PM 发表
1. Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output t ...
如果用delphi的話:
procedure TForm1.GetIt(iInput:integer; sDirection:string);
var
i : integer;
iSum : integer;
sDisplay : string;
sInput : string;
begin
iSum := 0;
sDisplay := '';
sInput := inttostr(iInput); // convert integer input into string
if sDirection = 'asc' then // for first Question solution
begin
for i:=1 to length(sInput) do
begin
iSum := iSum + StrToInt(sInput); // to get the sum of each individual digit
sDisplay := sDisplay + sInput + ' '; // prepare for displaying
end;
end
else // for 2nd Question solution
begin
for i:=length(sInput) downto 1 do
begin
iSum := iSum + StrToInt(sInput); // to get the sum of each individual digit
sDisplay := sDisplay + sInput + ' '; // prepare for displaying
end;
end;
showmessage(inttostr(iSum)); // display your iSUM here.
showmessage(sDisplay); // display you sDisplay here.
end;
可以寫一個向上面的function as general input, then call it using
GetIt(integer input here, 'asc'); <<<-------------這是第一題
GetIt(integer input here, 'desc'); <<<-------------這是第二題
希望幫到你  |
|
|
|
|
|
|
|
发表于 9-4-2007 10:50 AM
|
显示全部楼层
暂时还没想到要如何处理任何长度的数目。干时间。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int x, count;
cout << "Enter a number(5-digit): ";
cin >> x;
cout << x/10000%10 << " " << x/1000%10 << " " << x/100%10 << " " << x/10%10 << " " << x%10 << endl;
return 0;
} |
|
|
|
|
|
|
|
发表于 10-4-2007 12:24 AM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 10-4-2007 01:29 PM
|
显示全部楼层
其实不需用和 char 有关的 function。以下是我的解答:
1.
#include <iostream>
using namespace std;
int main()
{
float num;
int count=10, x;
cout << "Enter a number: ";
cin >> num;
for(int y=1000000000; y>1; y/=10)
{
if((num/y)>=1) break;
count--;
}
x = (int)num;
int n=1, c=0;
for(int max=1; max < 1000000000; max*=10)
{
if(c < count)
{ cout << x/max%10; c++; }
}
cout << endl;
return 0;
}
******************************************************
2.
#include <iostream>
using namespace std;
int main()
{
float num;
int count=10, x;
cout << "Enter a number: ";
cin >> num;
for(int y=1000000000; y>1; y/=10)
{
if((num/y)>=1) break;
count--;
}
x = (int)num;
int n=1, c=0;
int m = 1;
while(n < count)
{
m *= 10;
n++;
}
for(int max=m; max > 0; max/=10)
{
if(c < count)
{ cout << x/max%10 << " "; c++; }
}
cout << endl;
return 0;
}
**************************************************** |
|
|
|
|
|
|
|
发表于 10-4-2007 01:30 PM
|
显示全部楼层
|
|
|
|
|
|
|
发表于 16-8-2007 12:30 PM
|
显示全部楼层
|
|
|
|
|
|
| |
本周最热论坛帖子
|