| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1577 人关注过本帖
标题:指针问题,我是新手,帮帮忙~谢谢
只看楼主 加入收藏
剑启郁兰
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-4-6
结帖率:71.43%
收藏
已结贴  问题点数:5 回复次数:8 
指针问题,我是新手,帮帮忙~谢谢
程序代码:
#include "stdio.h"
main()
{
    int c[10],*p,min,max,m,n,t;
    printf("请输入十个整数:\n");
    for(p=c;p-c<10;p++)
        scanf("%d",p);
    p=c;
    for(max=min=*p,p=p+1;p-c<10;p++)
    {
        if(max<*p) {max=*p;m=p-c;}
        if(min>*p) {min=*p;n=p-c;}
    }
    p=c;t=*p;*p=*(p+n);*(p+n)=t;
    t=*(p+9);*(p+9)=*(p+m);*(p+m)=t;
    printf("对换后的十个数为:\n");
    for(p=c;p-c<10;p++)
        printf("%d ",*p);
    printf("\n");
    return 0;
}
运行发现,这一部分是由问题的{ p=c;t=*p;*p=*(p+n);*(p+n)=t;
    t=*(p+9);*(p+9)=*(p+m);*(p+m)=t;}但我找不到哪里出错。刚学指针。求帮帮忙~(题目:输入十个整数,将最小的数与第一个数对换,最大的与最后一个数对换,并输出对换后的十个数)
搜索更多相关主题的帖子: color 
2016-05-25 11:32
alice_usnet
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:18
帖 子:370
专家分:2020
注 册:2016-3-7
收藏
得分:2 
不知道你说的是什么问题,反正我没发现问题。

未佩好剑,转身便已是江湖
2016-05-25 13:22
小白小白一只
Rank: 2
来 自:吉林大学
等 级:论坛游民
帖 子:16
专家分:41
注 册:2016-3-30
收藏
得分:2 
在t=*(p +9)之前加一个p =c 试试????
2016-05-25 14:06
剑启郁兰
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-4-6
收藏
得分:0 
回复 2楼 alice_usnet
知道错误在哪儿了。因为n m 没有初始化,使得如果没有使if执行则会出现程序错误。不过还是谢谢你啦

颜色不一样的烟火
2016-05-25 14:16
TK4Moma
Rank: 2
等 级:论坛游民
帖 子:9
专家分:19
注 册:2016-4-26
收藏
得分:2 
#pragma region TraceMe
////#include <stdio.h>
////#include<string.h>
////#include<windows.h>
////unsigned char Table[8] = {0xC,0xA,0x13,0x9,0xC,0xB,0xA,0x8};
////int main()
////{
////    printf(" input Name\n");
////    char *s;
////    char st[10];
////    scanf_s("%st",&st);
////    s = st;
////    int len;
////    len = strlen("st");
////    int i,j;
////    unsigned long code=0;
////
////    for(i=3,j=0;i<len;i++,j++)
////         {if(j>7) j=0;
////           code+=(s[i])*Table[j];                                                                                                      
////          }
////
////    wsprintf((TCHAR*)s,TEXT("%ld"),code);
////    printf("%ld",*s);
////    getchar();
////    return 0;
////}
#pragma endregion

#pragma region
//
//#include<stdio.h>
//#include<iostream>
//#include<string>
//#include <iomanip>
//using namespace std;
//class StuData
//{
//private:
//    string name;
//    string sex;
//    int id;
//    int result[4];
//public:
//    StuData();
//    void get();
//    string getname();/*得到名字这样的函数叫常成员函数。常成员函数可以理解为是一个“只读”函数,
//                                           它既不能更改数据成员的值,也不能调用那些能引起数据成员值变化的
//                                           成员函数,只能调用const成员函数。*/
//    string getsex();//得到性别
//    int getid();//得到学号
//    int getmath();//得到数学成绩
//    int getchinese();//得到语文成绩
//    int getenglish();//得到英语的成绩
//    int getofc();//得到c++成绩
//    ~StuData();
//};
//class Stulist:public StuData
//{   
//    private:
//        string grade;//班级
//        string department;//系别
//    public:
//    Stulist *next;
//    Stulist();//构造函数,对成员进行初始化
//    void setgrade();//设置班级
//    void add();//添加
//    void deletion();//删除
//    void change();//修改
//    void find();//查找
//    void put();//输出
//    void subjectsort();//单科成绩排名
//    int sum(Stulist *s);//计算总分
//    void sumsort();//按总分排序
//    int menu();//菜单
//    void ADmain();//主控函数
//    ~Stulist();//对数据进行相应的处理---链表的销毁自动执行
//};
////----------------------StuData------------------------
//StuData::StuData()
//{
//    name="";
//    sex="";
//    id=0;
//    result[0]=0;
//    result[1]=0;
//    result[2]=0;
//    result[3]=0;
//}
//void StuData::get()//输入
//{
//    int n,d;
//    cout<<"请输入姓名:";
//    cin>>name;
//    cout<<"请选择性别(1.男 2.女):";
//    cin>>n;
//    for(;n!=1&&n!=2;)
//    {
//        cout<<"选择错误,请重新选择(1.男 2.女):";
//        cin>>n;
//    }
//    if(n==1)
//    {
//        sex="男";
//    }
//        else
//    {
//        sex="女";
//    }
//    cout<<"请输入八位学号:";
//    cin>>id;
//    for(;n!=8;)
//    {
//        for(d=id,n=1;d/10!=0;n++)
//        {
//            d=d/10;
//        }
//        if(n!=8)
//        {
//            cout<<"学号必须为八位数字,请重新输入:";
//            cin>>id;
//        }
//    }
//    cout<<"请输入数学成绩:";
//    cin>>result[0];
//    cout<<"请输入语文成绩:";
//    cin>>result[1];
//    cout<<"请输入英语成绩:";
//    cin>>result[2];
//    cout<<"请输入c++成绩:";
//    cin>>result[3];
//}
//string StuData::getname()
//{
//    return name;
//}
//string StuData::getsex()
//{
//    return sex;
//}
//int StuData::getid()
//{
//    return id;
//}
//int StuData::getmath()
//{
//    return result[0];
//}
//int StuData::getchinese()
//{
//    return result[1];
//}
//int StuData::getenglish()
//{
//    return result[2];
//}
//int StuData::getofc()
//{
//    return result[3];
//}
//StuData::~StuData()
//{
//}
////---------------------Stulist------------------------
//Stulist::Stulist()
//{
//    grade="";
//    department="软件学院";
//    next=NULL;
//}
//void Stulist:: setgrade()//设置班级
//{
//    int n;
//    cout<<"1.网安1 2.网工 3.软件设计 4.数媒 5.云计算"<<endl
//        <<"请选择班级:";
//    cin>>n;
//    for(;n<1||n>5;)
//    {
//        cout<<" 选择错误,请重新选择(1-5):";
//        cin>>n;
//    }
//    switch(n)
//    {
//    case 1:grade="网安";break;
//    case 2:grade="网工";break;
//    case 3:grade="软件设计";break;
//    case 4:grade="数媒";break;
//    case 5:grade="云计算";break;
//    }
//}
//void Stulist::add()//添加学生信息
//{
//    Stulist *p=NULL,*pr=NULL,*P=NULL;
//    char ch='y';
//    pr=this;
//    for(;ch=='y';)
//    {
//        system("cls");
//        p=new Stulist;
//        p->setgrade();
//        p->get();
//        P=pr->next;
//        pr->next=p;
//        p->next=P;
//        cout<<"添加成功!"<<endl;
//        cout<<"是否继续添加(y/n):";
//        cin>>ch;
//        ch=tolower(ch);//将字母变为小写
//    }
//}
//void Stulist::deletion()//删除
//{
//    Stulist *p=NULL,*pr=NULL;
//    string str;
//    int id,d,n=0;
//    char ch='y';
//    p=this->next;
//    if(p==NULL)
//    {
//        cout<<"无任何信息!"<<endl;
//        system("pause");
//    }
//    else
//    {
//        for(;ch=='y';)
//        {
//            pr=this;
//            p=pr->next;
//            system("cls");
//            cout<<"请输入要删除学生的学号:";
//            cin>>id;
//            for(;n!=8;)
//            {
//                for(d=id,n=1;d/10!=0;n++)
//                {
//                    d=d/10;
//                }
//                if(n!=8)
//                {
//                    cout<<"学号必须为八位数字,请重新输入:";
//                    cin>>id;
//                }
//            }
//            n=0;
//            for(;pr->next!=NULL;)
//            {
//                if(id==p->getid())
//                {
//                    pr->next=p->next;
//                    delete(p);
//                    cout<<"删除成功!"<<endl;
//                    n=1;
//                    break;
//                }
//                else
//                {
//                    pr=pr->next;
//                    p=pr->next;
//                }
//            }
//            if(n==0)
//            {
//                cout<<"查无此人!"<<endl;
//            }
//            getchar();
//            cout<<"是否继续删除(y/n):";
//            cin>>ch;
//            ch=tolower(ch);
//        }
//    }
//}
//void Stulist::change()//修改
//{
//    Stulist *p=NULL,*pr=NULL;
//    string str;
//    int id,d,n=0;
//    char ch='y';
//    p=this->next;
//    if(p==NULL)
//    {
//        cout<<"无任何信息!"<<endl;
//        system("pause");
//    }
//    else
//    {
//        for(;ch=='y';)
//        {
//            pr=this;
//            p=pr->next;
//            system("cls");
//            cout<<"请输入要修改学生的学号:";
//            cin>>id;
//            for(;n!=8;)
//            {
//                for(d=id,n=1;d/10!=0;n++)
//                {
//                    d=d/10;
//                }
//                if(n!=8)
//                {
//                    cout<<"学号必须为八位数字,请重新输入:";
//                    cin>>id;
//                }
//            }
//            n=0;
//            for(;pr->next!=NULL;)
//            {
//                if(id==p->getid())
//                {
//                    system("cls");
//                    cout<<"开始修改";
//                    p->setgrade();
//                    p->get();
//                    cout<<"修改成功!"<<endl;
//                    n=1;
//                    break;
//                }
//                else
//                {
//                    pr=pr->next;
//                    p=pr->next;
//                }
//            }
//            if(n==0)
//            {
//                cout<<"查无此人!"<<endl;
//            }
//            getchar();
//            cout<<"是否继续修改(y/n):";
//            cin>>ch;
//            ch=tolower(ch);
//        }
//    }
//}
//void Stulist::find()//查找
//{
//    Stulist *p=NULL,*pr=NULL;
//    string str;
//    int id,d,n=0;
//    char ch='y';
//    p=this->next;
//    if(p==NULL)
//    {
//        cout<<"无任何信息!"<<endl;
//        system("pause");
//    }
//    else
//    {
//        for(;ch=='y';)
//        {
//            pr=this;
//            p=pr->next;
//            system("cls");
//            cout<<"请输入要查找学生的学号:";
//            cin>>id;
//            for(;n!=8;)
//            {
//                for(d=id,n=1;d/10!=0;n++)
//                {
//                    d=d/10;
//                }
//                if(n!=8)
//                {
//                    cout<<"学号必须为八位数字,请重新输入:";
//                    cin>>id;
//                }
//            }
//            n=0;
//            for(;pr->next!=NULL;)
//            {
//                if(id==p->getid())
//                {
//                    system("cls");
//                    str=p->getname();
//                    cout<<str<<"  ";
//                    str=p->getsex();
//                    cout<<str<<"  ";
//                    n=p->getid();
//                    cout<<n<<"  ";
//                    str=p->grade;
//                    cout<<str<<"  ";
//                    n=p->getmath();
//                    cout<<n<<"  ";
//                    n=p->getchinese();
//                    cout<<n<<"  ";
//                    n=p->getenglish();
//                    cout<<n<<"  ";
//                    n=p->getofc();
//                    cout<<n<<endl;
//                    n=1;
//                    break;
//                }
//                else
//                {
//                    pr=pr->next;
//                    p=pr->next;
//                }
//            }
//            if(n==0)
//            {
//                cout<<"查无此人!"<<endl;
//            }
//            getchar();
//            cout<<"是否继续查询(y/n):";
//            cin>>ch;
//            ch=tolower(ch);
//        }
//    }
//   
//        
//}
//void Stulist::put()//输出学生信息
//{
//    Stulist *p=NULL,*pr=NULL;
//    string str;
//    int n;
//    pr=this;
//    p=this->next;
//    if(p!=NULL)
//    {
//        cout<<setw(10)<<"姓名"<<setw(6)<<"性别"<<setw(10)<<"学号"<<setw(10)<<"院系"<<setw(10)<<"班级"
//            <<setw(6)<<"数学"<<setw(6)<<"语文"<<setw(6)<<"英语"<<setw(6)<<"c++"<<endl;
//        for(;pr->next!=NULL;)
//        {
//            str=p->getname();
//            cout<<setw(10)<<str;
//
//            str=p->getsex();
//            cout<<setw(6)<<str;
//
//            n=p->getid();
//            cout<<setw(10)<<n;
//
//            str=p->department;
//            cout<<setw(10)<<str;
//
//            str=p->grade;
//            cout<<setw(10)<<str;
//
//            n=p->getmath();
//            cout<<setw(6)<<n;
//
//            n=p->getchinese();
//            cout<<setw(6)<<n;
//
//            n=p->getenglish();
//            cout<<setw(6)<<n;
//
//            n=p->getofc();
//            cout<<setw(6)<<n<<endl;
//            pr=pr->next;
//            p=pr->next;
//        }
//    }
//    else
//        cout<<"无任何信息!"<<endl;
//    system("pause");
//}
//void Stulist::subjectsort()//单科排名
//{
//    int n;
//    Stulist *p=NULL,*pr=NULL,*tp=NULL,*np=NULL,*wp=NULL;
//    p=this->next;
//    if(p==NULL)
//    {
//        cout<<"无任何信息!"<<endl;
//        system("pause");
//    }
//    else
//    {
//        cout<<"1.数学2.语文 3.英语 4.c++"<<endl
//                <<"请选择科目:";
//        cin>>n;
//        for(;n<1||n>4;)
//        {
//            cout<<" 选择错误,请重新选择(1-4):";
//            cin>>n;
//        }
//        switch(n)
//        {
//        case 1:
//                for(pr=this;pr->next!=NULL;pr=pr->next)
//                {
//                    for(tp=this,p=this->next;p->next!=NULL;tp=tp->next,p=p->next)
//                    {
//                        if(p->getmath()<p->next->getmath())
//                        {
//                            np=p->next;
//                            wp=np->next;
//                            p->next=wp;
//                            np->next=p;
//                            tp->next=np;
//                            p=np;
//                        }
//                    }
//                }
//                break;
//        case 2:
//            for(pr=this;pr->next!=NULL;pr=pr->next)
//                {
//                    for(tp=this,p=this->next;p->next!=NULL;tp=tp->next,p=p->next)
//                    {
//                        if(p->getchinese()<p->next->getchinese())
//                        {
//                            np=p->next;
//                            wp=np->next;
//                            p->next=wp;
//                            np->next=p;
//                            tp->next=np;
//                            p=np;
//                        }
//                    }
//                }
//            break;
//        case 3:
//            for(pr=this;pr->next!=NULL;pr=pr->next)
//                {
//                    for(tp=this,p=this->next;p->next!=NULL;tp=tp->next,p=p->next)
//                    {
//                        if(p->getenglish()<p->next->getenglish())
//                        {
//                            np=p->next;
//                            wp=np->next;
//                            p->next=wp;
//                            np->next=p;
//                            tp->next=np;
//                            p=np;
//                        }
//                    }
//                }
//            break;
//        case 4:
//            for(pr=this;pr->next!=NULL;pr=pr->next)
//                {
//                    for(tp=this,p=this->next;p->next!=NULL;tp=tp->next,p=p->next)
//                    {
//                        if(p->getofc()<p->next->getofc())
//                        {
//                            np=p->next;
//                            wp=np->next;
//                            p->next=wp;
//                            np->next=p;
//                            tp->next=np;
//                            p=np;
//                        }
//                    }
//                }
//            break;
//        }
//        put();
//    }
//}
//int Stulist::sum(Stulist *s)//计算总分
//{
//    return s->getmath() + s->getchinese() + s->getenglish() + s->getofc();
//}
//void Stulist::sumsort()//总分排名
//{
//    Stulist *p=NULL,*pr=NULL,*tp=NULL,*np=NULL,*wp=NULL;
//    for(pr=this;pr->next!=NULL;pr=pr->next)
//    {
//        for(tp=this,p=this->next;p->next!=NULL;tp=tp->next,p=p->next)
//        {
//            if(p->sum(p)<p->sum(p->next))
//            {
//                np=p->next;
//                wp=np->next;
//                p->next=wp;
//                np->next=p;
//                tp->next=np;
//                p=np;
//            }
//        }
//    }
//    put();
//}
//int Stulist::menu()//菜单
//{
//    int n;
//    cout<<"\t\t\t\t学生信息管理系统"<<endl
//        <<"\t\t\t\t    1.添加"<<endl
//        <<"\t\t\t\t    2.删除"<<endl
//        <<"\t\t\t\t    3.修改"<<endl
//        <<"\t\t\t\t    4.查找"<<endl
//        <<"\t\t\t\t    5.输出"<<endl
//        <<"\t\t\t\t6.单科成绩排名"<<endl
//        <<"\t\t\t\t7.班内总分排名"<<endl
//        <<"\t\t\t\t    0.退出"<<endl<<endl<<endl<<endl
//        <<"\t\t请选择:";
//    cin>>n;
//    return n;
//}
//void Stulist::ADmain()//主控
//{
//    int n;
//    for(;;)
//    {
//        n=menu();
//        switch(n)
//        {
//        case 1:add();system("cls");break;
//        case 2:deletion();system("cls");break;
//        case 3:change();system("cls");break;
//        case 4:find();system("cls");break;
//        case 5:put();system("cls");break;
//        case 6:subjectsort();system("cls");break;
//        case 7:sumsort();system("cls");break;
//        case 0:exit(0);break;
//        }
//    }
//}
//Stulist::~Stulist()
//{   
//}
//void main()
//{
//    Stulist a;
//    a.ADmain();
//}
#pragma endregion

#pragma region
//#include<iostream>
//using namespace std;
//char fun(char *s)
//{
//
//}
//int main()
//{
//    char *a = "abcdef";
//    printf("%d\n",fun(a));
//    /*int a;
//    scanf("%d",&a);
//    printf("%s",(a%2! = 0)? "No":"Yes");*/
//    return 0;
//}
#pragma endregion

#pragma region
//#include<stdio.h>
//int main (void)
//{
//    int year, month, day, sum=0,leap;
//    scanf("%d %d %d\n",&year,&month,&day);
//    switch(month)
//    {
//case1:
//        sum=0;
//        break;
//case2:
//        sum=31;
//        break;
//case3:
//        sum=59;
//        break;
//case4:
//        sum=90;
//        break;
//case5:
//        sum=120;
//        break;
//case6:
//        sum=151;
//        break;
//case7:
//        sum=181;
//        break;
//case8:
//        sum=212;
//        break;
//case9:
//        sum=243;
//        break;
//case10:
//        sum=273;
//        break;
//case11:
//        sum=304;
//        break;
//case12:
//        sum=334;
//        break;
//    }
//    sum=sum+day;
//    if(year%4==0)
//        leap=1;
//    else
//        leap=0;
//    if(leap==1&&month>2)
//        sum=sum++;
//    printf("%d\n",sum);
//    return 0;
//}
#pragma endregion



#include "stdio.h"
int main()
{
    int c[10],*p,min,max,m,n,t;
    printf("请输入十个整数:\n");
    for(p=c;p-c<10;p++)
        scanf("%d",p);
    p=c;
    max=min=*p;  //改动一下就可以啦,,本来是逗号表达式  ,,,具体细节自己调试吧,,其实多算一个数
    for(p=p+1;p-c<10;p++)
    {
        if(max<*p) {max=*p;m=p-c;}
        if(min>*p) {min=*p;n=p-c;}
    }
    p=c;t=*p;*p=*(p+n);*(p+n)=t;
    t=*(p+9);*(p+9)=*(p+m);*(p+m)=t;
    printf("对换后的十个数为:\n");
    for(p=c;p-c<10;p++)
        printf("%d ",*p);
    printf("\n");
    getchar();
    return 0;
}
编译环境   vs2010  debug 模式
2016-05-25 14:17
剑启郁兰
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-4-6
收藏
得分:0 
回复 3楼 小白小白一只
p=c已经有了,p就是指向c[0]位置的。我已经解决啦。还是谢谢你啦

颜色不一样的烟火
2016-05-25 14:18
linlulu001
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:20
帖 子:944
专家分:4047
注 册:2016-4-13
收藏
得分:2 
你确定m,n有值????
如果没有值,那你之后的代码在语法上是没错,但是真的能计算吗。
2016-05-25 14:18
剑启郁兰
Rank: 1
等 级:新手上路
帖 子:16
专家分:0
注 册:2016-4-6
收藏
得分:0 
回复 7楼 linlulu001
其实在if语句执行后,m n是被赋值的。只是有漏洞,如果输入的十个数比较特殊,使得if语句不执行,那么m n 就是任意数,系统出错。所以还是初始化的问题

颜色不一样的烟火
2016-05-25 14:25
吹水佬
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:451
帖 子:10548
专家分:42983
注 册:2014-5-20
收藏
得分:0 
调换时m,n位置会变的,简单点就分开两次调换。
如:先找min的位置,调换min后,再找max的位置调换max。

程序代码:
#include "stdio.h"
main()
{
    printf("请输入十个整数:\n");
    int c[10], *p=c, *min, *max, t;
    for(int i=0; i<10; i++,p++) scanf("%d",p);
    p=min=c; p++;
    for(int i=1; i<10; i++,p++) if(*min>*p) min=p;
    p=c; t=*p; *p=*min; *min=t;
    p=max=c; p++;
    for(int i=1; i<10; i++,p++) if(*max<*p) max=p;
    p=c+9; t=*p; *p=*max; *max=t;
    printf("对换后的十个数为:\n");
    for(int *p=c,i=0; i<10; i++,p++)
        printf("%d ",*p);
    printf("\n");
    return 0;
}


[此贴子已经被作者于2016-5-25 14:54编辑过]

2016-05-25 14:51
快速回复:指针问题,我是新手,帮帮忙~谢谢
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017663 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved