user-0 提问: C语言 字符数组的ASCII 码问题

回复:
user: user-1
content: 在国外学习？学到这个份上了，主要还是要靠自己，我以前这些课程全都是自学的

user: user-0
content: 哎， 很努力的学， 无奈美帝老师讲的。。。哎 还是国内讲这些能把基础打扎实

user: user-2
content: 代码功能归根结底不是别人帮自己看或讲解或注释出来的；而是被自己静下心来花足够长的时间和精力亲自动手单步或设断点或对执行到某步获得的中间结果显示或写到日志文件中一步一步分析出来的。 提醒：再牛×的老师也无法代替学生自己领悟和上厕所！ 单步调试和设断点调试（VS IDE中编译连接通过以后，按F10或F11键单步执行，按Shift+F11退出当前函数；在某行按F9设断点后按F5执行停在该断点处。）是程序员必须掌握的技能之一。

user: user-1
content: while (list[i]!='\0')     {         printf("The ASC code for %c is %d", list[i],list[i]);         i++;     } 这个i没有从0开始的 还有，你输入list[i]之后，没有将最后一个置为'\0'，简单的，就直接输入整个字符串，用”%s“

user: user-0
content: #include <stdio.h> #include <string.h>  int minCharRead ( char list[], int listSize); int numAlphaCharRead ( char list[], int listSize); double avgCharRead ( char list[], int listSize);  int main() {     static char list[100];     int num[100];     int i, a, b,c,d;     printf("please type a character \n");      for (i=0;i<99;i++)     {         list[i] =  getchar();         printf("you just typed %c \n", list[i]);         num[i] = list[i];     if (i>=100)         printf("the max size for array is full");         break;     }      while (list[i]!='\0')     {         printf("The ASC code for %c is %d", list[i],list[i]);         i++;     }      printf("the minascii is \n");     a = minCharRead (list,d );     for (i=0;i<100;i++)         printf (" %d", a);      printf ("the number of  \n");    b = numAlphaCharRead(list, d);    //for (i=0;i<100;i++)         printf (" %d", b);      printf ("the ave is");     c = avgCharRead(list, d);     //for (i=0;i<100;i++)         printf (" %d", c);     return 0; }  int minCharRead(char list[], int listSize ) {     int i,j;     for (i=1; list[i]!= '\0' ;i++)         if ((int)list[0]< (int)list[i])          {              list [0] = list[i];             i++;          }     j= (int)list[0];     return j; }  int numAlphaCharRead(char list[], int listSize) {     int i;     listSize =0;     for (i=0; list[i]!= '\0' ;i++)         if ((list[i]>='A'&&list[i]<='Z')||(list[i]>='a'&&list[i]<='z'))              listSize++;         return listSize; }  double avgCharRead(char list[], int listSize) {     double avg;     int sum,i;     sum=0;     for (i=0;i<100;i++)         sum+=(int)list[i];         avg = sum / i;     return avg; }      结果不太对，比如我输入多个字母进去，只能显示第一个char的

user: user-1
content: a = minCharRead (x,d ); 直接传list就好     a = minCharRead (list,d );

user: user-0
content: #include <stdio.h> #include <string.h>  int minCharRead ( char list[], int listSize); int numAlphaCharRead ( char list[], int listSize); double avgCharRead ( char list[], int listSize);  int main() {     static char list[100];     int num[100];     int i, a, b,c,d;     char x;     printf("please type a character \n");      for (i=0;i<99;i++)     {         list[i] =  getchar();         printf("you just typed %c \n", list[i]);         num[i] = list[i];     if (i>=100)         printf("the max size for array is full");         break;     }      while (list[i]!='\0')     {         printf("The ASC code for %c is %d", list[i],list[i]);         i++;     }      x= list[i];     printf("the minascii is ");     a = minCharRead (x,d );     for (i=0;i<100;i++)         printf (" %d", a);      printf ("the number of  ");     b = numAlphaCharRead(x, d);    for (i=0;i<100;i++)         printf (" %d", b);      printf ("the ave is");     c = avgCharRead(x, d);     for (i=0;i<100;i++)         printf (" %d", c);     return 0; }  int minCharRead(char list[], int listSize ) {     int i,j;     for (i=1; list[i]!= '\0' ;i++)         if ((int)list[0]< (int)list[i])          {              list [0] = list[i];             i++;          }     j= (int)list[0];     return j; }  int numAlphaCharRead(char list[], int listSize) {     int i;     listSize =0;     for (i=0; list[i]!= '\0' ;i++)         if ((list[i]>='A'&&list[i]<='Z')||(list[i]>='a'&&list[i]<='z'))              listSize++;         return listSize; }  double avgCharRead(char list[], int listSize) {     double avg;     int sum,i;     sum=0;     for (i=0;i<100;i++)         sum+=(int)list[i];         avg = sum / i;     return avg; }

user: user-0
content: #include <stdio.h> #include <string.h>  int minCharRead ( char list[], int listSize); int numAlphaCharRead ( char list[], int listSize); double avgCharRead ( char list[], int listSize);  int main() {     static char list[100];     int num[100];     int i, a, b,c,d;     char x;     printf("please type a character \n");      for (i=0;i<99;i++)     {         list[i] =  getchar();         printf("you just typed %c \n", list[i]);         num[i] = list[i];     if (i>=100)         printf("the max size for array is full");         break;     }      while (list[i]!='\0')     {         printf("The ASC code for %c is %d", list[i],list[i]);         i++;     }      x= list[i];     printf("the minascii is ");     a = minCharRead (x,d );     for (i=0;i<100;i++)         printf (" %d", a);      printf ("the number of  ");     b = numAlphaCharRead(x, d);    for (i=0;i<100;i++)         printf (" %d", b);      printf ("the ave is");     c = avgCharRead(x, d);     for (i=0;i<100;i++)         printf (" %d", c);     return 0; }  int minCharRead(char list[], int listSize ) {     int i,j;     for (i=1; list[i]!= '\0' ;i++)         if ((int)list[0]< (int)list[i])          {              list [0] = list[i];             i++;          }     j= (int)list[0];     return j; }  int numAlphaCharRead(char list[], int listSize) {     int i;     listSize =0;     for (i=0; list[i]!= '\0' ;i++)         if ((list[i]>='A'&&list[i]<='Z')||(list[i]>='a'&&list[i]<='z'))              listSize++;         return listSize; }  double avgCharRead(char list[], int listSize) {     double avg;     int sum,i;     sum=0;     for (i=0;i<100;i++)         sum+=(int)list[i];         avg = sum / i;     return avg; }   改了一下，但是还是不对，我需要在哪里修改呢 ？ 谢谢你

user: user-1
content: 如果你要找到所有元素里面最小的，某种类型的东西有多少，都是需要循环的 这在数据库方面叫做聚集函数，即该函数不是针对某个个体的，而是群体的

user: user-0
content: 主函数里往 list【 】写char 进去的时候使用了循环。  下边三个子函数里是否也要用循环呢，我看了好多例子还是比较蒙，刚刚入门，请指教，谢谢

user: user-1
content: 你的函数调用没什么问题啊，就是在编写函数上面发蒙 其实你编写函数，跟main里面差不多的定义变量做法就可以了啊

user: user-0
content: 我不懂函数怎么用，所以在变量使用上完全蒙的

user: user-1
content: avgCharRead貌似还OK 另外2个，承诺的循环哪去了？

user: user-0
content: 看了好多资料，照猫画虎的code，不working。。。  #include <stdio.h> #include <string.h>  int minCharRead ( char list[], int listSize); int numAlphaCharRead ( char list[], int listSize); double avgCharRead ( char list[], int listSize);  int main() {     static char list[50];     int num[50];     int i, a, b,c,d;     char x;     printf("please type a character \n");      for (i=0;i<49;i++)     {         list[i] =  getchar();         //scanf("%c",&list[i]);         printf("you just typed %c \n", list[i]);         num[i] = list[i];     if (i>=50)         printf("the max size for array is full");         break;     }      while (list[i]!='\0')     {         printf("The ASC code for %c is %d", list[i],list[i]);         i++;     }      x= list[i];     printf("the minascii is ");     a = minCharRead (x,d );     //for (i=0;i<50;i++)         printf (" %d", a);      printf ("the number of  ");     b = numAlphaCharRead(x, d);    for (i=0;i<50;i++)         printf (" %d", b);      printf ("the ave is");     c = avgCharRead(x, d);     for (i=0;i<50;i++)         printf (" %d", c);     return 0; }  int minCharRead(char list[], int listSize ) {     int i=1,j;         if ((int)list[0]< (int)list[i])          {              list [0] = list[i];             i++;          }     j= (int)list[0];     return j; }  int numAlphaCharRead(char list[], int listSize) {     int i;     listSize =0;    // for (i=0; list[i]!= '\0' ;i++)     if ((list[i]>='A'&&list[i]<='Z')||(list[i]>='a'&&list[i]<='z'))              listSize++;         return listSize; }  double avgCharRead(char list[], int listSize) {     double avg;     int sum,i;     sum=0;     //int number[i];     for (i=0;i<50;i++)         sum+=(int)list[i];         avg = sum / i;     return avg; }

user: user-1
content: for (i=0;i<100;i++)         scanf("%c",&list[i]); list只有50个字符，你循环却是100，立马越界 输入字符串用%s 既然你都会写循环了，这些函数都是单循环搞定的，还写不出来吗？

user: user-3
content: 加我好友,我也新手!