|
查看: 1986|回复: 22
|
c++ programming高手请进..(有新问题!!)
[复制链接]
|
|
|
发表于 25-9-2009 02:41 AM
|
显示全部楼层
for(y=2;y<=userInput;y++){
cout <<"#";
cout << "\n";
for(int w=2;w<=userInput;w++){
cout << " ";
}
}
我只是试试看,好久没做了。。嘻嘻
for(y=2;y<=userInput;y++){
cout <<"#";
for(int w=2;w<=userInput;w++){
cout << " ";
}
cout<<"\n";
} |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 25-9-2009 10:46 AM
|
显示全部楼层
原帖由 笨蛋比比 于 25-9-2009 02:41 AM 发表 
for(y=2;y
谢谢意见..
我等下试试看.. |
|
|
|
|
|
|
|
|
|
|
发表于 25-9-2009 04:24 PM
|
显示全部楼层
- #include <iostream>
- using namespace std;
- //print # at given position
- void printHashAt(int n)
- {
- for(int i = 0 ; i < n ; i++)
- cout << " ";
-
- cout << "#";
- }
- //print 1st n last line
- void printBaseLine(int n)
- {
- for(int i = 0 ; i < n ; i++)
- cout << "#";
-
- cout << endl;
- }
- //print middle part
- void printMiddleLine(int n)
- {
- for(int i = n - 1 ; i >= 2; i--)
- {
- printHashAt(i-1);
- cout << endl;
- }
- }
-
- int main()
- {
- int input;
-
- cout << "Enter a value: ";
- cin >> input;
-
- printBaseLine(input);
- printMiddleLine(input);
- printBaseLine(input);
-
- return 0;
- }
-
复制代码 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 28-9-2009 11:22 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 28-9-2009 11:58 AM
|
显示全部楼层
system("cls");
>> main();
似乎不太需要这个 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 28-9-2009 01:57 PM
|
显示全部楼层
原帖由 24hours 于 28-9-2009 11:58 AM 发表 
system("cls");
>> main();
似乎不太需要这个
多余的? |
|
|
|
|
|
|
|
|
|
|
发表于 28-9-2009 10:16 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 29-9-2009 10:27 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 1-10-2009 10:47 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 2-10-2009 10:16 AM
|
显示全部楼层
main call function, 然后function 又call main.
用return. |
|
|
|
|
|
|
|
|
|
|
发表于 4-11-2009 12:22 PM
|
显示全部楼层
int choice=5;
bool stop=false;
while (stop==false){
while(choice>=4){
.......
如果你要的是让这个program一直loop的话。。好像有问题哦。。当你选1,2,3的时候choice少过4,那么 while(choice>=4) 怎样会loop呢?还有chan1314告诉你了,你的function里不需要call main le..因为他会接着loop的。。 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-11-2009 11:11 PM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-11-2009 11:15 PM
|
显示全部楼层
原帖由 MeoW 于 4-11-2009 12:22 PM 发表 
int choice=5;
bool stop=false;
while (stop==false){
while(choice>=4){
.......
如果你要的是让这个program一直loop的话。。好像有问题哦。。当你选1,2,3的时候choice少过 ...
哈`哈..
老师有讲..
不过没关系啦..
交了..
[ 本帖最后由 emotion 于 12-11-2009 12:06 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 11-11-2009 11:56 PM
|
显示全部楼层
真的要慢慢来做...
[ 本帖最后由 emotion 于 12-11-2009 12:06 AM 编辑 ] |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 12-11-2009 11:39 PM
|
显示全部楼层
有新问题!!
Create a phonebook program using the following structures for the contact and phonebook.
struct contact{
string name;
string hpno;
int age;
};
struct phonebook{
contact family[20];
contact friends[50];
};
Your phonebook program should be able to store phonebooks of at least 5 different people and
Your phonebook program should have the following features:
- Support up to 10 different individual phonebooks
- Allow insertion of new contacts
- Allow deletion of existing contacts
- Allow searching for an existing contact by name
- Sorts alphabetically and displays contacts
问题是在红色字那边
老师要的是Sorts alphabetically and displays contacts
不过sort到有问题哦..
问题在那里 ..
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
void familyInfo(void);
void friendsInfo(void);
void contactSearch(void);
void familyList(void);
void friendsList(void);
void contactList(void);
void sort(void);
struct contact{
string name;
string hpno;
int age;
};
struct phonebook{
contact family[20];
contact friends[50];
}con;
void main(){
int userInput;
cout << "-----------------------------" << endl;
cout << "Phone Book Contact Management" << endl;
cout << "-----------------------------" << endl;
cout << endl;
cout << "Please select" << endl;
cout << "1. Family Contact"<< endl;
cout << "2. Friends Contact"<< endl;
cout << "3. Contact Search" << endl;
cout << "4. Contact List" << endl;
cout << "Your choice is : ";
cin >> userInput;
if(userInput==1){
system("cls");
familyInfo();
}else if(userInput==2){
system("cls");
friendsInfo();
}else if(userInput==3){
system("cls");
contactSearch();
}else if(userInput==4){
system("cls");
contactList();
}else{
cout << "Unknown" << endl;
_getch();
main();
}
}
void familyInfo(){
int x;
for(x=0;x<5;x++){
cout << "Family Contact" << endl;
cout << endl;
cout << x+1 << ".Information" << endl;
cout << "Name: ";
cin >> con.family[x].name;
cout << "Handphone Number: ";
cin >> con.family[x].hpno;
cout << "Age: ";
cin >> con.family[x].age;
_getch();
system("cls");
}
main();
}
void friendsInfo(){
int x;
for(x=0;x<5;x++){
cout << "Friends Contact" << endl;
cout << endl;
cout << x+1 << ".Information" << endl;
cout << "Name: ";
cin >> con.friends[x].name;
cout << "Handphone Number: ";
cin >> con.friends[x].hpno;
cout << "Age: ";
cin >> con.friends[x].age;
_getch();
system("cls");
}
main();
}
void contactSearch(){
string nameSearch;
cout << "Contact Search"<< endl;
cout << "=============="<< endl;
cout << endl;
cout << "Name Search By Name:";
cin >> nameSearch;
for(int y=0;y<5;y++){
if(nameSearch.compare(con.family[y].name)==0){
cout << "Name: " << con.family[y].name << endl;
cout << "Handphone Number: " << con.family[y].hpno << endl;
cout << "Age: " << con.family[y].age << endl;
}else if(nameSearch.compare(con.friends[y].name)==0){
cout << "Name: " << con.friends[y].name << endl;
cout << "Handphone Number: " << con.friends[y].hpno << endl;
cout << "Age: " << con.friends[y].age << endl;
}
}
_getch();
system("cls");
main();
}
void familyList(){
for(int z=0;z<5;z++){
cout << z+1 << ".Family Contact" << endl;
cout << "Name: " << con.family[z].name << endl;
cout << "Handphone Number: " << con.family[z].hpno << endl;
cout << "Age: " << con.family[z].age << endl;
cout << endl;
}
_getch();
}
void friendsList(){
for(int z=0;z<5;z++){
cout << z+1 << ".Friends Contact" << endl;
cout << "Name: " << con.friends[z].name << endl;
cout << "Handphone Number: " << con.friends[z].hpno << endl;
cout << "Age: " << con.friends[z].age << endl;
cout << endl;
}
_getch();
}
void contactList(){
sort();
cout << "===============" << endl;
cout << "Contact Listing" << endl;
cout << "===============" << endl;
cout << endl;
for(int i=0;i<10;i++){
cout << "-->;Please press ENTER again to get the FRIENDS LIST<-- " << endl;
cout << endl;
cout << "FAMILY LIST" << endl;
cout << "___________" << endl;
familyList();
cout << "FRIENDS LIST" << endl;
cout << "____________" << endl;
friendsList();
system("cls");
main();
}
}
void sort(){
int i;
string temp;
string tempNo;
int tempAge;
bool sorted = true;
while(sorted){
sorted = false;
for(i=0;i<3;i++){
if(con.family.name[0]>con.family[i+1].name[0]){
temp = con.family[i+1].name;
tempNo = con.family[i+1].hpno;
tempAge = con.family[i+1].age;
con.family[i+1].name = con.family.name;
con.family[i+1].hpno = con.family.hpno;
con.family[i+1].age = con.family.age;
con.family.name = temp;
con.family.hpno = tempNo;
con.family.age = tempAge;
sorted = true;
}else if(con.friends.name[0]>con.friends[i+1].name[0]){
temp = con.friends[i+1].name;
tempNo = con.friends[i+1].hpno;
tempAge = con.friends[i+1].age;
con.friends[i+1].name = con.friends.name;
con.friends[i+1].hpno = con.friends.hpno;
con.friends[i+1].age = con.friends.age;
con.friends.name = temp;
con.friends.hpno = tempNo;
con.friends.age = tempAge;
sorted = true;
}
}
}
}
[ 本帖最后由 emotion 于 12-11-2009 11:43 PM 编辑 ] |
|
|
|
|
|
|
|
|
|
|
发表于 13-11-2009 09:28 PM
|
显示全部楼层
con.family.name[0]>con.family[i+1].name[0]
你是用BUBBLE SORT?- void sortFamily()
- {
- int i;
- bool sorted = true;
- while(sorted){
- sorted = false;
- for(i=0;i<3;i++){
- if(con.family[i].name[0]>con.family[i+1].name[0])
- {
- //swap con.family[i] with (con.family[i+1]
- sorted = true;
- }
- }
- }
- }
复制代码 大概是酱子...... |
|
|
|
|
|
|
|
|
|
|
发表于 14-11-2009 11:30 PM
|
显示全部楼层
回复 16# emotion 的帖子
#11 有人讲你了还不听。。。
我不明白到底为什么要用whilte...loop 加 for...loop
其实可以酱写吧
- for(int iFamily=0; iFamily<20; iFamily++)
- {
- for(int iCompare=iFamily; iCompare<20; iCompare++)
- {
- // If condition match
- // swap entries
- }
- }
复制代码
在每个function上面写注释那么难吗?是不是要每个人每次都要看完整段代码,从运行规律中猜用途? |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 16-11-2009 10:43 AM
|
显示全部楼层
原帖由 yeenfei 于 14-11-2009 11:30 PM 发表 
#11 有人讲你了还不听。。。
我不明白到底为什么要用whilte...loop 加 for...loop
其实可以酱写吧
for(int iFamily=0; iFamily
谢谢指导.
那里?
我也是学习中,我承认programming没那么厉害.
但是我么都试,因为不是很会.
我把我的code post出来,就是因为我不肯定我的code有没有问题.
希望有人能指点下. |
|
|
|
|
|
|
|
|
|
|
发表于 16-11-2009 11:05 AM
|
显示全部楼层
培养习惯在CODE前放RELATED的COMMENT....
对大家都有好处.... |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|