+ 1
Why is it wrong to answer for some numbers?
#include <conio.h> #include <stdio.h> #include <stdlib.h> int main() { int i,count1=0,count2=0; char s[100],sum=0,sum1=0; printf("enter a statment:\n"); gets(s); for(i=0;s[i];i++) { if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z')) count1++; if(s[i]>='0'&&s[i]<='9') { count2++; sum=sum*10+(s[i]-48); } else { sum1=sum1+sum; sum=0; } } printf("%d\n%d\n%d",count1,count2,sum1); return 0; }
8 Answers
+ 2
عزیزم بازم که برنامه رو بی در و پیکر اتچ کردی!
یه پروزه درست کن اینجا https://code.sololearn.com/#c
+ 2
#include <stdio.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#define MAX 100
int main()
{
int i, j = 0,
alpha_count = 0,
num_count = 0,
sum = 0,
i_table[MAX];
char s[MAX];
bool is_num = false;
printf("enter a statment:\n");
gets(s);
size_t length = strlen(s);
for (i = 0; i < length; i++)
{
if (isalpha(s[i])) {
alpha_count++;
}
else if (isdigit(s[i]))
{
num_count++;
sum += (s[i] - '0');
if (i + 1 < length)
if (isdigit(s[i + 1])) // check the next char
sum *= 10;
else {
i_table[j++] = sum;
sum = 0;
}
}
}
// See if something has remained in sum
if (sum) {
i_table[j++] = sum;
sum = 0;
}
// Add up numbers together
for (i = 0; i < j; i++)
sum += i_table[i];
printf("%d\n%d\n%d", alpha_count, num_count, sum);
return 0;
}
+ 2
برنامه فاقد منطق مناسب برای ذخیره کردن دنباله های عددیه. کلا ی چیز تخیلی نوشته بودی! 8D
+ 2
دیگه عزیزم خودت با دست کدتو تریس کن ببین چی میبینی. همیشه سعی کن قبل از اینکه برنامتو پیاده سازی کنی، ی دور چک کنی ببینی که اصلا طراحیت جوابگوی نیازهای مساله هست یا نه. اصولا عمده برنامه نویسی با کاغذ و قلم سروکار داره تا تایپ کردن اولین کدی که به ذهنت میاد و احساس میکنی که درسته. تو باید قبل از پیاده سازیت حداقل از جانب درستی برنامت مطمئن بشی.
+ 2
Why is it wrong to answer for some numbers?
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i,count1=0,count2=0;
char s[100],
sum=0,sum1=0;
// must be integers
printf("enter a statment:\n");
gets(s);
for(i=0;s[i];i++)
{
if((s[i]>='a'&&s[i]<='z')||(s[i]>='A'&&s[i]<='Z'))
// if it was a letter...
count1++;
if(s[i]>='0'&&s[i]<='9')
// if it was a digit...
{
count2++;
sum=sum*10+(s[i]-48);
// works well for holding consecutive digits
}
else
// what does it mean by else? Again check for letters? Special characters? What happens if the last character isn't a letter to trigger this block?
{
sum1=sum1+sum;
// also works well for adding the previous number to the grand total IFF there's a proper condition was available
sum=0;
}
}
printf("%d\n%d\n%d",count1,count2,sum1);
return 0;
}
0
This program will receive a string and say that we have several letters and several numbers, and then add the numbers together.
For example :
fatemeh124ra10
9
5
134
0
میشه بگی مشکل برنامه ای که خودم نوشتم چیه؟
0
چرا فاقد منطق و یه چیز تخیلی ؟