佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

搜索
查看: 1986|回复: 22

c++ programming高手请进..(有新问题!!)

[复制链接]
发表于 25-9-2009 02:18 AM | 显示全部楼层 |阅读模式
有问题想请教你们..
可能我的很乱..
但是我有点开心因为有50%的成功,哈哈..
如果code乱的话请帮忙指点哦..
那里需要改善..

问题是:


但是我的output不是那样哦..


my code :

#include<iostream>
#include<conio.h>
using namespace std;

void main(){
    int userInput,x,y,z;
    cout << "Enter a value: ";
    cin >> userInput;

    for(x=1;x<userInput;x++){
        cout << "#";
    }
        for(y=2;y<=userInput;y++){
            cout <<"#";
            cout << "\n";
            for(int w=2;w<=userInput;w++){
                cout << " ";
                }
        }
        for(z=0;z<userInput;z++){
            cout << "#";
        }   
        cout << endl;   
        _getch();
}
那里出问题了?
谢谢...

[ 本帖最后由 emotion 于 12-11-2009 11:44 PM 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 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 | 显示全部楼层
  1. #include <iostream>

  2. using namespace std;

  3. //print # at given position
  4. void printHashAt(int n)
  5. {
  6.         for(int i = 0 ; i < n ; i++)
  7.                 cout << " ";
  8.        
  9.         cout << "#";
  10. }

  11. //print 1st n last line
  12. void printBaseLine(int n)
  13. {
  14.         for(int i = 0 ; i < n ; i++)
  15.                 cout << "#";
  16.                
  17.         cout << endl;
  18. }

  19. //print middle part
  20. void printMiddleLine(int n)
  21. {
  22.         for(int i = n - 1 ; i >= 2; i--)
  23.         {
  24.                 printHashAt(i-1);
  25.                 cout << endl;
  26.         }
  27. }



  28.        
  29. int main()
  30. {
  31.         int input;
  32.        
  33.         cout << "Enter a value: ";
  34.         cin >> input;
  35.        
  36.         printBaseLine(input);
  37.         printMiddleLine(input);
  38.         printBaseLine(input);
  39.        
  40.         return 0;
  41. }
  42.        
复制代码
回复

使用道具 举报

 楼主| 发表于 28-9-2009 11:22 AM | 显示全部楼层
另外一个问题~~..
Volume calculator
Write a program that allows usersto compute the volume of cubes, cones, cylinders and spheres. The usershould be able to choose the type of object from a menu (use switchcase for menus) and then enter the required parameters for calculatingthe volume. Use functions named as cubeVolume, coneVolume,cylinderVolume and sphereVolume to perform the calculations for eachtype of object.

我的code,
我本身觉得没问题..
但我老师说我的code有点错..
但不懂那里错..
#include<iostream>
#include<conio.h>
#define PI 3.142
using namespace std;

void cubeVolume(void);
void coneVolume(void);
void cylinderVolume(void);
void sphereVolume(void);

void main(){
    int choice=5;
    bool stop=false;
    while (stop==false){
        while(choice>=4){

    cout << "=================" << endl;
    cout << "Volume Calculator" << endl;
    cout << "=================" << endl;
    cout << endl;
    cout << "lease enter your choice" << endl;
    cout << "1.Cube" << endl;
    cout << "2.Cone" << endl;
    cout << "3.Cylinder" << endl;
    cout << "4.Sphere" << endl;
    cout << "Your choice is: ";
    cin >> choice;


    switch(choice){
        case 1:
            system("cls";
            cubeVolume();
            stop=false;
            break;
        case 2:
            system("cls";
            coneVolume();
            stop=false;
            break;
        case 3:
            system("cls";
            cylinderVolume();
            stop=false;
            break;
        case 4:
            system("cls";
            sphereVolume();
            stop=false;
            break;
        default:
            stop=true;
            cout << "Unknown Choice" << endl;
            system("cls";
            break;
    }
        }
    }
}
        


void cubeVolume(){
    double result;
    double length;
    cout << "----------------" << endl;
    cout <<  "  Cube Volume    " << endl;
    cout << "----------------" << endl;
    cout << "Enter length: ";
    cin >> length;
    result = length * length * length;
    cout << "Result = " << result << endl;
    _getch();
    system("cls";
    main();

}

void coneVolume(){
    double result;
    double radius,height;
    cout << "--------------" << endl;
    cout << " Cone Volume  " << endl;
    cout << "--------------" << endl;
    cout << endl;
    cout << "Enter radius: ";
    cin >> radius;
    cout << "Enter height: ";
    cin >> height;
    result = PI * radius * radius * height / 3;
    cout << "Result = " << result << endl;
    _getch();
    system("cls";
    main();
}

void cylinderVolume(){
    double result;
    double radius,height;
    cout << "------------------" << endl;
    cout << " Cylinder Volume  " << endl;
    cout << "------------------" << endl;
    cout << endl;
    cout << "Enter radius: ";
    cin >> radius;
    cout << "Enter height: ";
    cin >> height;
    result = PI * radius * radius * height;
    cout << "Result = " << result << endl;
    _getch();
    system("cls";
    main();
}

void sphereVolume(){
    double radius;
    double result;
    cout << "---------------" << endl;
    cout << " Sphere Volume " << endl;
    cout << "---------------" << endl;
    cout << endl;
    cout << "Enter radius: ";
    cin >> radius;
    result = 4 * PI * radius * radius * radius / 3;
    cout << "Result = " << result << endl;
    _getch();
    system("cls";
    main();
}
回复

使用道具 举报

发表于 28-9-2009 11:58 AM | 显示全部楼层
system("cls");
   >> main();

似乎不太需要这个
回复

使用道具 举报

Follow Us
 楼主| 发表于 28-9-2009 01:57 PM | 显示全部楼层
原帖由 24hours 于 28-9-2009 11:58 AM 发表
system("cls");
   >> main();

似乎不太需要这个


多余的?
回复

使用道具 举报

发表于 28-9-2009 10:16 PM | 显示全部楼层
是。。。。。。。。。。。。
回复

使用道具 举报


ADVERTISEMENT

发表于 29-9-2009 10:27 AM | 显示全部楼层
then enter the required parameters for calculating the volume.


你连题目都没看好
回复

使用道具 举报

 楼主| 发表于 1-10-2009 10:47 PM | 显示全部楼层
原帖由 onlylonly 于 29-9-2009 10:27 AM 发表


你连题目都没看好


么意思?
我有给我老师看他没讲关于这..
只是讲我的有问题//
回复

使用道具 举报

发表于 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 | 显示全部楼层
原帖由 chan1314 于 2-10-2009 10:16 AM 发表
main call function, 然后function 又call main.
用return.


谢谢答复.
我老师有讲有问题..哈哈...
回复

使用道具 举报

 楼主| 发表于 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 编辑 ]
回复

使用道具 举报


ADVERTISEMENT

发表于 13-11-2009 09:28 PM | 显示全部楼层
con.family.name[0]>con.family[i+1].name[0]

你是用BUBBLE SORT?
  1. void sortFamily()
  2. {
  3.     int i;
  4.     bool sorted = true;
  5.     while(sorted){
  6.         sorted = false;
  7.         for(i=0;i<3;i++){
  8.           if(con.family[i].name[0]>con.family[i+1].name[0])
  9.           {
  10.              //swap con.family[i] with (con.family[i+1]
  11.              sorted = true;
  12.           }
  13.         }
  14.     }
  15. }
复制代码
大概是酱子......
回复

使用道具 举报

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

回复 16# emotion 的帖子

#11 有人讲你了还不听。。。
我不明白到底为什么要用whilte...loop 加 for...loop
其实可以酱写吧

  1. for(int iFamily=0; iFamily<20; iFamily++)
  2. {
  3.         for(int iCompare=iFamily; iCompare<20; iCompare++)
  4.         {
  5.                 // If condition match
  6.                 // swap entries
  7.         }
  8. }
复制代码



在每个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....
对大家都有好处....
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2026 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 11-5-2026 05:29 AM , Processed in 0.083562 second(s), 12 queries , Gzip On, Redis On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表